;-
; $Id: showfits.pro,v 1.36 2026/09/11 14:58:57 nathan Exp $
;pro showfits,dir,h,im, XBIN=xbin, WAITSEC=waitsec, SUFFIX=suffix
; Name:     
;
; Purpose:  Monitor directory for new FITS files and display it when it arrives. 
;   	-or- Display a single FITS file image.
;
; Inputs: Directory to monitor
;   	-or- scalar or array of FITS filenames.
;
; Optional Keyword Inputs:
;   XBIN=   	Factor by which to rebin input for display (single image only)
;   WAITSEC=	Number of seconds to wait between checking for update; default=5
;   SUFFIX= 	Set to desired suffix to search for; default is "fits"
;   YWINSIZE=	Set number of pixels for display window, vertical dimension. (Default is 840 for 2 rows and 960 for 3 rows)
;    /SMALLER	Set to display 24 images instead of 8
;   SMALLER=16	Set to display 16 images
;   USEWIN=  	Set to Window ID to display in. Default is 0.
;   ROWS=   	Display this number of rows per image; number displayed sized accordingly.
;   WINCOLS=	Number of cols of images
;   SAVEPNG=	Filename (root) to output .png of displayed images
;   SAVELOG=	Filename to write image info to for the displayed images.
;
; Optional Outputs: 
;   h	Header structure
;   im	Image from (most recent) FITS file 
;
; Calls:    wnd.pro, mreadfits.pro, hist_equal.pro, break_file.pro
;
; Restrictions: Input directory must exist.
;
; WRitten by N.Rich, NRL
;-
; $Log: showfits.pro,v $
; Revision 1.36  2026/09/11 14:58:57  nathan
; add /DERECTIFY; fix output sizing; adjust color scheme
;
; Revision 1.35  2024/04/04 14:36:08  nathan
; allow header-only input
;
; Revision 1.34  2024/01/11 19:06:26  nathan
; make print msg more robust
;
; Revision 1.33  2023/06/23 01:02:31  nathan
; add WINROWS=, change how window size is computed so works better for subfields
;
; Revision 1.32  2023/03/13 22:51:25  nathan
; fixes
;
; Revision 1.31  2023/03/13 21:18:23  nathan
; use putin_array if subfield
;
; Revision 1.30  2023/03/13 19:39:13  nathan
; use putin_array if subfield
;
; Revision 1.29  2022/03/18 21:30:00  nathan
; typos
;
; Revision 1.28  2022/02/17 22:38:16  nathan
; add SAVELOG=
;
; Revision 1.27  2022/02/17 22:06:20  nathan
; fix SAVEPNG
;
; Revision 1.26  2022/02/17 21:43:21  nathan
; add SAVEPNG option
;
; Revision 1.25  2022/02/17 21:30:24  nathan
; update col/row logic
;
; Revision 1.24  2022/01/07 17:37:11  nathan
; print gainmode for other
;
; Revision 1.23  2021/04/25 16:35:18  nathan
; add naxis2 to default output
;
; Revision 1.22  2021/04/25 16:34:21  nathan
; add WINCOLS=
;
; Revision 1.21  2021/02/26 19:34:13  nathan
; print keywords for other for CCOR
;
; Revision 1.20  2021/02/26 19:19:39  nathan
; print keywords for other for CCOR
;
; Revision 1.19  2020/06/19 20:48:08  nathan
; fix smaller=16
;
; Revision 1.18  2020/06/19 20:23:44  nathan
; add 16 option
;
; Revision 1.17  2020/04/27 16:39:16  nathan
; various tweaks
;
; Revision 1.16  2018/03/13 19:40:59  nathan
; add info on image, update wispr prints
;
; Revision 1.15  2017/03/22 08:31:40  nathan
; add /rows, utilize cgColor.pro for index display
;
; Revision 1.14  2016/11/19 05:04:15  nathan
; fix auto window size for 1 image
;
; Revision 1.13  2016/11/19 02:57:46  nathan
; add save option
;
; Revision 1.12  2016/10/24 20:04:34  nathan
; update solohi keywords
;
; Revision 1.11  2016/08/16 21:21:48  nathan
; change printed header values
;
; Revision 1.10  2016/02/08 19:34:07  nathan
; print wispr particulars
;
; Revision 1.9  2015/07/27 16:21:19  nathan
; remove requirement for input to be solohi
;
; Revision 1.8  2015/07/02 22:08:07  nathan
; add USEWIN option
;
; Revision 1.7  2015/07/02 18:45:01  nathan
; add YWINSIZE and SMALLER options
;
; Revision 1.6  2015/06/19 16:27:52  nathan
; do not display if constant value; xyout color=128; set up 3-d output array
;
; Revision 1.5  2015/05/07 16:00:34  nathan
; display up to 8 images in window
;
; Revision 1.4  2015/04/20 18:29:29  nathan
; add im,h output and print keywords
;
; Revision 1.3  2015/04/16 19:17:35  nathan
; add single image option
;
; Revision 1.2  2015/04/16 17:52:39  nathan
; already changes
;
; Revision 1.1  2015/04/16 17:50:44  nathan
; for displaying FITS files from solohi
;
pro showfitsimage, fname, xsz, ysz, h, im, i, LLC=llc, WWIN=wwin, DERECTIFY=derectify

common show, pixrows, flun

IF datatype(i) EQ 'UND' THEN i=0

break_file,fname,di,pa,ro,sx
mreadfits,fname,h,im,/silent
IF tag_exist(h,'INSTRUME') THEN BEGIN
    solohi=(h.instrume EQ 'SoloHI')
    wispr =(strmid(h.instrume,0,5) eq 'WISPR')
    secchi=(h.instrume EQ 'SECCHI')
    ccor=(strmid(h.instrume,0,4) eq 'CCOR')
ENDIF

hm=histogram(im)
mdni = median(im)

h1=h
IF tag_exist(h,'PXBEG1') THEN BEGIN
    subfield=0
    IF h.naxis EQ 1 THEN im=intarr(1024,960) ELSE $
    IF ((h.pxend1-h.pxbeg1+1)/h.nbin1 NE 2048 ) or ((h.pxend2-h.pxbeg2+1)/h.nbin2 NE 1920 ) THEN BEGIN
    	IF (wispr) THEN im=shi_putin_array(im,h1,1024,/silent)
    	IF (solohi) THEN im=shi_putin_array(im,h1,1024,/silent)
    	IF (ccor) THEN im=ccor_putin_array(im,h1,1024,/silent)
    ENDIF
ENDIF
IF keyword_set(DERECTIFY) THEN im=rotate(im,h1.rectrota)

sz=size(im)
IF (sz[0] eq 1) THEN im[*]=0
ypix=pixrows<(sz[2])

IF n_elements(hm) GT 4 THEN dim=(hist_equal(im)+1)<255 ELSE dim=im
IF xsz LT 400 THEN charsz=1.5 ELSE charsz=2

IF keyword_set(LLC) THEN BEGIN
    wndfac=((float(ysz)/ypix)<(float(xsz)/sz[1]))<1.
    ; SoloHI DM special case:
    IF (solohi) THEN IF h.ccbincmd eq 61441 and h.naxis1 eq 2048 THEN im=reform(im[*,0:479],1024,960)
    x=llc[0]
    y=llc[1]
    osid=0
    IF (secchi) THEN osid=h.obs_id
    IF (solohi) THEN osid=h.study_id
    IF (wispr) THEN osid=h.ispreg7
    IF (sz[0] eq 2) THEN tv,congrid(dim[*,0:ypix-1],sz[1]*wndfac,ypix*wndfac),x,y
    cgtext,x+5,y+5,trim(i),/device,charsize=charsz,color=2,charthi=2
    cgtext,x+5,y+5,trim(i)+' '+string(osid,'(i4)')+' '+rstrmid(h.filename,0,25),/device,charsize=charsz,color=2


ENDIF ELSE BEGIN
    print,'Displaying '+ro+sx
;help,im
    mini = min(im,max=maxi)
    stdi=stddev(im)

    help,im
    print,'Max,Median,Min,Stdev:',maxi,',',mdni, ',',mini,',',stdi

    wndfac=xsz
    device,get_screen_size=ssz
    while (wndfac*sz[1] gt ssz[0] or wndfac*sz[2] gt ssz[1]) do wndfac=wndfac/2.
    	
    IF keyword_set(WWIN) THEN wid=wwin ELSE wid=0
    
    wnd,wid,dim,wndfac
    
ENDELSE

gain=99
IF tag_exist(h,'GAINMODE') then gain=h.gainmode
IF tag_exist(h,'LGAINMOD') then gain=h.lgainmod

IF tag_exist(h,'FILENAME') THEN fn=h.filename ELSE fn=fname
IF tag_exist(h,'OBS_MODE') THEN obsmode=h.obs_mode ELSE obsmode=''
IF tag_exist(h,'SEB_PROG') THEN obsmode=h.seb_prog 
IF tag_exist(h,'TARGET') THEN obsmode=h.target
IF tag_exist(h,'IP_PROG') THEN ipprog=h.ip_prog ELSE ipprog=''
IF tag_exist(h,'NSUMEXP') THEN nsum=h.nsumexp ELSE nsum=0
IF tag_exist(h,'N_IMAGES') THEN nsum=h.n_images
IF tag_exist(h,'FILE_RAW') THEN fileraw=h.file_raw ELSE fileraw=''
IF tag_exist(h,'FILEORIG') THEN fileraw=h.fileorig

per=strpos(fn,'.')
fichars=strmid(fn,per-4,4)
naxis2=1
IF tag_exist(h,'NAXIS2') THEN naxis2=h.naxis2
IF (solohi) THEN $
msg=string(i,'(i4)')+'  '+rstrmid(fn,9,20)+' STUDY_ID='+trim(osid)+' IMGTYPE='+trim(h.imgtype)+' NAXIS1='+string(h.naxis1,'(i04)')+' NSUM='+trim(h.nsumexp)+' MDN='+trim(mdni) $

ELSE IF (wispr) THEN $
msg=string(i,'(i4)')+'  '+fileraw+'  '+fichars+' '+h.timeline+' '+strmid(h.date_obs,0,19)+ ' '+string(h.naxis1,'(i04)')+'x'+string(h.naxis2,'(i04)')+'  '+string(osid,'(i3)')+' BIN='+trim(fix(h.nbin))+' NSUM='+trim(nsum)+' MDN='+trim(mdni) $

ELSE msg=string(i,'(i4)')+' '+fn+string(nsum,'(i4)')+string(gain,'(i3)')+' '+fileraw+string(naxis2,'(i5)')+string(mdni,'(i6)')+' '+string(obsmode,'(a12)')+' '+ipprog

print,msg
IF (flun GT 0) THEN printf,flun,msg

end
;
;
;
pro showfits,dir,h,im,XBIN=xbin, WAITSEC=waitsec, SUFFIX=suffix, YWINSIZE=ywinsize, SMALLER=smaller, $
    USEWIN=usewin, ROWS=rows, WINCOLS=wincols, SAVEPNG=savepng, SAVELOG=savelog, WINROWS=winrows, $
    DERECTIFY=derectify
common show, pixrows, flun

flun=0

IF keyword_set(SAVELOG) THEN BEGIN
	openw,flun,savelog,/append,/get_lun
	IF (keyword_set(SAVEPNG)) THEN printf,flun,savepng+'.png:'
ENDIF
mreadfits,dir[0],h0,im0
sz=size(im0)
x0=float(sz[1])
y0=float(sz[2])
IF tag_exist(h0,'NBIN1') THEN BEGIN
	x0=2048./h0.nbin1
	y0=1920./h0.nbin2
ENDIF
loadct,0
device,get_screen_size=z
zx=(z[0])<2048
zy=(z[1])<2048
IF keyword_set(SUFFIX) THEN srchstr='*.'+suffix ELSE srchstr='*.fits'
IF keyword_set(WAITSEC) THEN waittime=waitsec ELSE waittime=5
IF keyword_set(XBIN) THEN dispfac=xbin ELSE dispfac=1.
IF keyword_set(ROWS) THEN pixrows=rows ELSE pixrows=1920
IF keyword_set(USEWIN) THEN winid=usewin ELSE winid=0
n=n_elements(dir)
IF n GT 1 THEN BEGIN
; list of filenames, display 512x480 in 4x2 mosaic or 256x240 in 6x4 mosaic
; or if ROWS set, 4x(960/(rows/2)) or 6x(960/(rows/4))
   IF keyword_set(YWINSIZE) THEN ywin=ywinsize ELSE ywin=960
   IF keyword_set(SMALLER) THEN BEGIN
	IF (smaller EQ 16) THEN BEGIN
		xwin=ywin*x0/y0
		IF xwin GT zx THEN BEGIN
		    frac=float(zx)/xwin
		    xwin=zx
		    ywin=xwin
		ENDIF
		nrow=4
		ysz=ywin/nrow
		xsz=xwin/4
		ncol=4
	ENDIF ELSE BEGIN
		xwin=1536	;.*ywin/y0
		IF xwin GT z[0] THEN BEGIN
		    frac=float(z[0])/xwin
		    xwin=z[0]
		    ywin=fix(ywin*frac)
		ENDIF
		nrow=(ywin/(pixrows/4))>4
		ysz=ywin/nrow
		xsz=xwin/6
		ncol=6
    	ENDELSE
    ENDIF ELSE BEGIN    ; not SMALLER
	IF n_elements(dir) LE 4 THEN ncol=2 ELSE ncol=4
	IF keyword_set(WINCOLS) THEN ncol=wincols
	nrow=ceil(float(n)/ncol)

	IF keyword_set(WINROWS) THEN nrow=winrows
	IF ~keyword_set(YWINSIZE) THEN ywin=(nrow*480)<zy
	xwin=ncol*x0*((ywin/nrow)/y0)
help,xwin,ywin

    ysz=ywin/nrow

;	nrow=(ywin/(pixrows/2))>1

	IF xwin GT zx THEN BEGIN
	    frac=0.9*float(z[0])/xwin
	    xwin=0.9*z[0]
	    ywin=fix(ywin*frac)
	ENDIF
	IF ywin GT z[1]-1 THEN BEGIN
	    frac=0.9*float(z[1])/ywin
	    ywin=0.9*z[1]
	    xwin=fix(xwin*frac)
	ENDIF	
    	ysz=ywin/nrow
	;help,xwin
	xsz=xwin/ncol

    ENDELSE ; not SMALLER
    help,nrow,ncol,xwin,ywin,xsz,ysz

    window,winid,xsize=xwin, ysize=ywin
;    textcolor=cgColor("Red",0)
;help,textcolor
tek_color
    FOR i=0,n-1 DO BEGIN
	y= ((i/ncol) mod nrow)*ysz 
	x= (i mod ncol)*xsz
	var=''
	IF i GT 0 and x EQ 0 and y EQ 0 THEN BEGIN
	    read,'Enter to continue, s to save png, q to exit.',var
	    IF strlowcase(var) EQ 'q' or strlowcase(var) eq 'x' THEN return
	    IF strlowcase(var) eq 's' THEN x=ftvread(/png)
	    erase
	ENDIF
	;help,x,y
	showfitsimage,dir[i],xsz,ysz, hi,imi, i, LLC=[x,y], DERECTIFY=derectify
	IF i EQ 0 THEN BEGIN
	    h=hi
	    sz=size(imi)
	    im=uintarr(sz[1],sz[2],n)
	    im[*,*,0]=imi
	ENDIF ELSE BEGIN
	    ;h=[h,hi]
	ENDELSE
	
    ENDFOR
;    return
ENDIF ELSE $

    
IF (file_test(dir,/regular)) THEN BEGIN
    showfitsimage,dir,dispfac,0,h,im
;    return
ENDIF ELSE BEGIN
    
fitslist=file_search(concat_dir(dir,srchstr),count=lastcount)
help,waittime
help,fitslist

while (1) do begin
    
    nlist=file_search(concat_dir(dir,srchstr), count=thiscount)
    FOR i=0,thiscount-1 DO BEGIN
    	w=where(fitslist eq nlist[i],nf)
	IF nf EQ 0 THEN BEGIN
	    showfitsimage,nlist[i],dispfac,0,h,im, WWIN=usewin
	    wait,waittime
	ENDIF
    ENDFOR
    IF lastcount EQ thiscount THEN wait,waittime
    fitslist=nlist
    lastcount=thiscount
ENDWHILE
ENDELSE

loadct,0

IF keyword_set(SAVELOG) THEN BEGIN
	printf,flun,' '
	close,flun
	free_lun,flun
ENDIF

IF (keyword_set(SAVEPNG)) THEN BEGIN
	havefilename=0
	IF (datatype(SAVEPNG) EQ 'STR') THEN havefilename=1
	message,'Saving '+savepng+'.png',/info
	zz=ftvread(/png,filename=savepng,noprompt=havefilename)
ENDIF

 end
