; file: gong_solis_boxpx.idl = analyze px determination per box size
; init: Apr  3 2022  Rob Rutten  Deil from nisp_sdo_tilefigs.idl
; last: Apr  4 2022  Rob Rutten  Deil

;; pro dummy

; set box size in arcsec for trimboxim2 coalignment around disk center
;RR if not small then for SOLIS sumwing upset by actual heightdiff > 0?? 
;RR so that non-small-box align gives wrong scale and tiling goes bad??
boxsize=500
boxoffset=[-600,0]
heightdiff=2500

maxmetcalf=8

; output choices
blink=0
checkmetcalf=0
show=0  
verbose=1

; select data    ## must have nisp_sdp.pro output for GONG and/or SOLIS
cd,'/home/rutten/data/NISP/2012-08-15-quiet'

; ----- get best images and parameters

; NB: in tmode=2 these best images are averspan averages with
; derotation for the trimboxcoal center (averspan 6 min = 1 arcsec) so
; they have corresponding near-limb tile shifts during the averspan
; that get time-averaged

; get GONG "best" image
gongim=readfits('gong/temp/nispbest.fits')

; get GONG parameters
readcol,'gong/temp/nisp_coalign.dat',px2,angle,xpx2asym,ypx2asym,$
  xshiftfor,yshiftfor,xshiftrev,yshiftrev,nxfor,nyfor,nxrev,nyrev
px_gong=px2[0]
angle_gong=angle[0]

; get GONG-corresponding "best" AIA 304 image
gongaiaim=readfits('gong/temp/aiabest.fits')

; get SOLIS "best" image
solisim=readfits('solis/hacw/temp/nispbest.fits')

; get SOLIS parameters
readcol,'solis/hacw/temp/nisp_coalign.dat',px2,angle,xpx2asym,ypx2asym,$
  xshiftfor,yshiftfor,xshiftrev,yshiftrev,nxfor,nyfor,nxrev,nyrev
px_solis=px2[0]
angle_solis=angle[0]

; use findalignimages for trimboxcenter (two images must get same px scale)
print,' -----'
print,' ----- start findalignimages trimboxim2center'
print,' -----'

wavpair=['solis','gong'] ; lower first
im1=gongim   
im2=solisim
px1=px_gong
px2=px_solis                        
angle=+(angle_solis-angle_gong)

; suppress memory!
px2asym=[1.,1.]
inshift12=[0,0]

; boxsize above > trimboxim2 in SOLIS px
boxpx=fix(boxsize/px2)
boxoff=boxoffset/px2
szim2=size(im2)
nx_im2=szim2[1]
ny_im2=szim2[2]
trimboxim2=[nx_im2/2-boxpx/2+boxoff[0],ny_im2/2-boxpx/2+boxoff[1],$
            nx_im2/2+boxpx/2+boxoff[0],ny_im2/2+boxpx/2+boxoff[1]]

findalignimages,im1,im2,px1,px2,angle,px2asym,$
  shiftfor,shiftrev,nxfor,nyfor,nxrev,nyrev,$    
  nxmuckfor=nxmuckfor,nymuckfor=nymuckfor,$
  nxmuckrev=nxmuckrev,nymuckrev=nymuckrev,$
  skipmetcalf=skipmetcalf,maxmetcalf=maxmetcalf,minmetqual=minmetqual,$
  applypx2asym=applypx2asym,trimboxim2=trimboxim2,inshift12=inshift12,$
  smearim1=smearim1,smearim2=smearim2,$
  histopim1=histopim1,histopim2=histopim2,$
  muckdarkim1=muckdarkim1,muckdarkim2=muckdarkim2,$
  muckbrightim1=muckbrightim1,muckbrightim2=muckbrightim2,$
  flatten=flatten,flatim1=flatim1,flatim2=flatim2,$
  metqual=metqual,nmet=nmet,finalrinse=finalrinse,$
  checkmetcalf=checkmetcalf,blink=blink,show=0,verbose=verbose

rescale=1.-heightdiff/2./695700. ; heightdiff = limb extension in km 
px2_rescale=px2*rescale

print,' ##### box ='+trimd(boxsize)+'  boxoffset ='+trimd(boxoffset)+$
  '  px2 ='+trimd(px2,5)+'  px2_rescale='+trimd(px2_rescale,5)
;; print,' ----- px_gong ='+trimd(px_gong,5)+' px_solis ='+trimd(px_solis,5)

end

 ;; box = 100  boxoffset = 0 0  px2 = 1.05744  px2_rescale= 1.05554
 ;; box = 150  boxoffset = 0 0  px2 = 1.05432  px2_rescale= 1.05242
 ;; box = 200  boxoffset = 0 0  px2 = 1.05551  px2_rescale= 1.05361
 ;; box = 250  boxoffset = 0 0  px2 = 1.05509  px2_rescale= 1.05319
 ;; box = 300  boxoffset = 0 0  px2 = 1.05509  px2_rescale= 1.05319
 ;; box = 350  boxoffset = 0 0  px2 = 1.05402  px2_rescale= 1.05213
 ;; box = 400  boxoffset = 0 0  px2 = 1.05352  px2_rescale= 1.05163
 ;; box = 450  boxoffset = 0 0  px2 = 1.05316  px2_rescale= 1.05127
 ;; box = 500  boxoffset = 0 0  px2 = 1.05278  px2_rescale= 1.05088
 ;; box = 600  boxoffset = 0 0  px2 = 1.05266  px2_rescale= 1.05076
 ;; box = 700  boxoffset = 0 0  px2 = 1.05253  px2_rescale= 1.05064
 ;; box = 800  boxoffset = 0 0  px2 = 1.05296  px2_rescale= 1.05107
;; box = 1000  boxoffset = 0 0  px2 = 1.05289  px2_rescale= 1.05100
;; box = 1200  boxoffset = 0 0  px2 = 1.05320  px2_rescale= 1.05131

; px2 wordt kleiner ipv groter ???

; excentric
 ;; box = 500  boxoffset = 0 0  px2 = 1.05278  px2_rescale= 1.05088
 ;; box = 500  boxoffset = 300 0  px2 = 1.05176  px2_rescale= 1.04987
 ;; box = 500  boxoffset = -300 0  px2 = 1.05322  px2_rescale= 1.05133
 ;; box = 500  boxoffset = 0 300  px2 = 1.05353  px2_rescale= 1.05164
 ;; box = 500  boxoffset = 0 -300  px2 = 1.05330  px2_rescale= 1.05140 in 2
 ;; box = 500  boxoffset = 0 -600  px2 = 1.05262  px2_rescale= 1.05073
 ;; box = 500  boxoffset = 0 600  px2 = 1.05370  px2_rescale= 1.05181
 ;; box = 500  boxoffset = 600 0  px2 = 1.05274  px2_rescale= 1.05085
 ;; box = 500  boxoffset = -600 0  px2 = 1.05305  px2_rescale= 1.05115
 
