;---------------------------------------------------------------------------
; Document name: hsi_image_test.pro
; Time-stamp: <Thu Oct 23 2008 16:19:04 csillag tournesol2.local>
;---------------------------------------------------------------------------
;+
; PROJECT:
;       RHESSI
;
; NAME:
;       HSI_IMAGE_TEST
;
; PURPOSE:
;       This is the main test procedure for  testing the RHESSI imaging
;       software. Performs a variety of tests.
;
; CATEGORY:
;       Imaging
;
; CALLING SEQUENCE:
;       hsi_image_test
;
; OUTPUTS:
;       None, but lots of things happen (you have been warned!)
;
; HISTORY:
;       2008-08-02 - acs moved from hsi_image_test as it gets bigger 
;       2007-12-31 - acs lots of tests rom problems detected by kim
;                    with visibility files.
;       2007-07-07 - acs, add visibility tests, introduce "image_ag" kwd
;                    (which is not really used yet)
;-

pro hsi_test_vis

Obj = Hsi_image()                                                                         

obj-> set, det_index_mask= [0B, 0B, 0B, 1B, 1B, 1B, 1B, 0B,  0B]                          
obj-> set, im_energy_binning= [10,20,50]     
obj-> set, im_time_interval= ['20-Feb-2002 11:05:00.000', '20-Feb-2002 11:05:40.000']    
obj-> set, image_algorithm= 'njit'                                                       
obj-> set, pixel_size= [1,1]
d=obj->getdata()
obj-> set, det_index_mask= [0B, 0B, 0B, 1B, 1B, 1B, 1B, 0B, 1B] 
d=obj->getdata()



obj = hsi_image()                                                                        
obj-> set, im_energy_binning= [6,12]
obj-> set, im_time_interval= ['21-Apr-2002 01:31:06.000', '21-Apr-2002 01:31:10.000']
obj-> set, image_algorithm= 'MEM NJIT'
d=obj->getdata()
pmm,d
print,obj->get(/vis_edit)
obj->set,vis_edit=0
d=obj->getdata()
pmm,d


o=hsi_image()
o->set,obs_time='21-Apr-2002 ' +['01:20:00.000','01:20:12.000']
o->set, image_alg='vf'
d=o->getdata() ; here it makes visibilities for 12-25 keV

o->set,image_alg='clean'
o->set,im_energy_bin=[3.,6.]
; here calib_eventlist and visibility obj both need update
print,(o->get(/obj,class='hsi_calib_eventlist'))->need_update(), $
      (o->get(/obj,class='hsi_visibility'))->need_update()
d=o->getdata()

o->set,image_alg='vf'
; here calib_eventlist does not need update, but and visibility obj does
print,(o->get(/obj,class='hsi_calib_eventlist'))->need_update(), $
      (o->get(/obj,class='hsi_visibility'))->need_update()
d=o->getdata()  ; WRONG: uses the visibilities from 12-25 it had done earlier.

; problem with vis files setting up

obj = hsi_image()                                                                         
obj-> set, im_time_interval= ['21-Apr-2002 01:31:06.000', '21-Apr-2002 01:31:10.000']     
obj-> set, image_algorithm= 'clean'
;obj->plotman 
obj->plot
obj->set,det_index_mask=[1,1,1,1,1,1,1,1,1]

;obj->plotman
obj->plot
obj->set,im_energy_binning=[25,50]
;obj->plotman
obj->plot


om = hsi_image()                                                                         
om-> set, im_time_interval= ['21-Apr-2002 01:31:06.000', '21-Apr-2002 01:31:10.000']     
om-> set, image_algorithm= 'MEM NJIT'  
om->plot
;om->plotman
om->set,det_index_mask=[1,1,1,1,1,1,1,1,1]
om->plot    
;om->plotman
om->set,im_energy_binning=[25,50]
om->plot                                                                                                  
;om->plotman

;---

;The problem we talked about on the phone is a definite problem.  I think if vis_file was already set, and you switch to a different visibility file, the enableset is never called before reading in the new vis file, so things aren't set properly from the new vis file.
o=hsi_image()
fs = findfile( '*vi*fits' )
o->set, vis_in = fs[0]
o->plot, image_alg = 'njit'
o->set, vis_in = fs[2]
o->plot

;I think this shows you what I mean:

o=hsi_image()
o->set,image_alg='mem_njit'
o->set,im_time_int='14-jan-2003 ' + ['12:30','12:31']
o->set,image_alg='vis_fwdfit'
o->set,im_time_int='14-jan-2003 ' + ['12:30','12:31']
o->set,image_alg='mem_njit'

ptim, o->get(/im_time_int)
;14-Jan-2003 12:30:00.000 14-Jan-2003 12:31:00.000
ptim,(o->get(/obj,class='hsi_visibility')) -> get(/im_time_int)
;23-Jan-2003 12:30:00.000 23-Jan-2003 12:31:00.000
;These should return the same times, no?

;2.  More problem between energy_axis obj and im_energy__binning:
;In HSI_Visibility_File::Process you do
;self.caller->set, _extra = control
;so this sets im_energy_binning, but not the energy_axis object so they don't
;match.  Here's an example:

fs = findfile( '*vi*fits' )
o2 = hsi_image(vis_input=fs[2])
o2->set,image_alg='mem_njit'
vis = o2->getdata(class='hsi_visibility')
print,o2->get(/im_energy_binning)

;This doesn't cause a crash under normal circumstances, and the plot shows the
;right energy band because it's using self->get(/energy_band) which is 30-40 keV.
;But something might be using the energy_axis object and it would have the wrong
;values.

;In fact if I do this, it crashes because hsi_image_raw thinks it has two images
;to process (the vis file just has energy 30-40kev):
o2=hsi_image()
o2->set, im_energy_binning=[12.,25.,50.]
fs = findfile( '*vi*fits' )
o2->set, vis_in=fs[0]
o2->set,image_alg='mem_njit'
help,o2->getdata()

;1.  When you change vis_filename, it doesn't read the new file. 
fs = findfile( '*vi*fits' )
o = hsi_image( vis_in = fs[0], image_alg = 'njit' )
o->plot
o->plot, vis_file  = fs[2]


;goto, gaga

o=hsi_image()
o->set, im_time_int = '20-jan-2005 ' + ['06:45:04', '06:46:04']
o->set, det_index_mask = [0,0,1,1,1,0,0,0,0]
;o->set, image_alg = 'njit'
; bith values must be the same
o->set, im_energy_bin = [30,40]
print, o->get( /im_energy_bin )
print, o->getaxis( /energy, /edges_2 )


; When using a vis file as input, and setting mem_njit alg, I used the  default
; selection for det_index_mask, and did a getdata, the det_index_mask was changed
; to dets 2-9 (I guess that's what the file was made with - I don't know - it's
; Rick's file).  Then I change the det_index_mask to various settings, and the
; getdata doesn't seem to reprocess, but returns the same data for each (in fact,
; now that I check, I see that after changing det_index_mask, need_update() is
; still 0)


; now with this let's check how it behaves when we have 

; shoudl not change the values:
o->set, im_energy = [11,20]
print, o->get( /im_energy )
; should have the same values and not reprocess
o->plot
;o->set, /vis_allow
o->set, im_energy = [11,20]
; now it shoudl be visible
print, o->get( /im_energy )
; now it should reprocess
o->plot
; but actually this does not have the aspect solution necessary to do
; the image.bummer.

obj_destroy, o
visfile = findfile('hsi_visibili_*')
; now do the same test with im time
o = hsi_image()
o->set,vis_in=visfile[0]
o->set,image_algorithm='njit'
print, o->need_update() ; should be 1
o->plot
; origina problem: Right now, if I set a vis_file, and plot the image, 
; then change im_time_int and plot it, I get the same plot 
; with the new time in the label.  
;  And ptim,o->get(/im_time_int) shows the new time.
;
; problem 2
; If I change obs_time_int, o->get(/obs_time_int) doesn't show the 
; new time, but im_time_int does, and so the plot shows the time, 
; but it's the same data is the original

; should not change the values:
o->set, im_time_int = '2002/02/20 11:' + ['06', '07']
ptim, o->get( /im_time_int )
; shoul not show the new time on plot
o->plot
; as it is now (with vis_file set), once you set im_time_int (which shouldn't have any effect) ,
; you can never change det_index_mask - you can set it and it remembers it,
; but as soon as you make an image, it always sets it back to whatever was in the vis file.
o->plot, det_index = [0,0,0,1,0,0,0,0,0]
o->plot, det_index = [0,0,0,1,1,0,0,0,0]

o->set, obs_time_int = '2002/02/20 11:' + ['10', '11']
ptim, o->get( /im_time_int )
ptim, o->get( /obs_time_int )
o->plot

o->set, /vis_allow
o->set, im_time_int = '2002/02/20 11:' + ['06', '07']

; now it shoudl be visible
ptim, o->get( /im_time_int )
; now it should reprocess
o->plot

; same exercise with time_range:

obj_destroy, o
visfile = findfile('hsi_visibili_*')
; now do the same test with im time
o = hsi_image()
o->set,vis_filename=visfile[0]
o->set,image_algorithm='njit'
print, o->need_update() ; should be 1
o->plot

o->set, time_range = '2002/02/20 11:' + ['10', '11']
ptim, o->get( /im_time_int )
ptim, o->get( /obs_time_int )
ptim, o->get( /time_range )
o->plot

o->set, /vis_allow
o->set, time_range = '2002/02/20 11:' + ['10', '11']
ptim, o->get( /im_time_int )
ptim, o->get( /obs_time_int )
ptim, o->get( /time_range )

; now it shoudl be visible
ptim, o->get( /im_time_int )
; now it should reprocess
o->plot

;----



;------------------------------
 
obj = hsi_image()
o = obj
;obj-> set, im_time_int = ['27-May-2002 18:06:28.000', '27-May-2002 18:06:40.000']
obj-> set, im_time_interval= ['21-Apr-2003 07:25:30.000', '21-Apr-2003 07:25:34.000']
obj-> set, image_algorithm= 'Clean'
obj-> set, clean_niter= 3
d=obj->getdata()
help,obj->get(/image_units)    ;HERE image_units is correct
obj->fitswrite, im_out='test.fits'
 
o=hsi_image(im_in='test.fits')
d=o->getdata()
help,o->get(/image_units)   ;HERE image_units is blank

o2 = hsi_image()

; this does not work yet:
;visfile = findfile('../fits/*visi*')
; it strops the directory redirection away.

visfile = findfile('hsi_visibili_*')
o2->set,vis_filename=visfile[0]
o2->set,image_algorithm='njit'
d = o2->getdata()
o2->plot
print,o2->need_update()
; 1 BUT SHOULD BE 0

print, o2->Getaxis( /ut ) ; should not trigger reprocessing

;Also in the above example, if I don't set image_dim=[128,128] which
;is what the vis file was made at, it crashes as follows.  
;(I think it had to do with some params being stored in two places, so
;the default, like image_dim=64,64, was used in one place in the code,
;while the value from the visfile is used in another place, or
;something like that)

o2->plot, pixel_size = 1
; is this needed at all??
;o->plot, pixel_scale =1
o2->plot, image_dim = 256, /limb

obj_destroy, o2

;%MEM_NJIT: Successful end.
;Array subscript for IMAGE_ARR must have same size as source expression.
;Execution halted at: HSI_IMAGE_RAW::PROCESS 414 C:\ssw\hessi\idl\atest\hsi_image_raw__define.pro
;FRAMEWORK::GETDATA 819 C:\ssw\gen\idl\objects\framework\framework__define.pro
;HSI_IMAGE_STRATEGY::GETDATA 295 C:\ssw\hessi\idl\atest\hsi_image_strategy__define.pro
;STRATEGY_HOLDER_TOOLS::GETDATA 331 C:\ssw\gen\idl\objects\framework\strategy_holder_tools__define.pro
;$MAIN$ 
;Compiled module: FRAMEWORK_IS_OBJ_NESTED.
;HSI_IMAGE_STRATEGY::GETDATA: Aborting . Returning -1.
 
; ----
; displays of mem njit -- should do the same as the commented stuff
; taken from http://hesperia.gsfc.nasa.gov/~schmahl/MEM_NJIT/mem_njit_tutorial.html

det_index_mask = [1,1,1, 1,1,1, 1,1,1]
xyoffset = [905.64,260.42]
time_range = ' 20-Feb-2002 '+['11:06:00','11:06:36']
energy_band = [25.,50.]
; hsi_vis = hsi_vis_usershell(det_index_mask=det_index_mask,xyoffset=xyoffset, $
;                            time_range=time_range,energy_band=energy_band)

o = hsi_image( det_index_mask=det_index_mask,xyoffset=xyoffset, $
               time_range=time_range,energy_band=energy_band, $
               image_alg = 'fwd' )

o->set, image_dim = 128

grids=[2,7]                     ; subcollimators 3,4,5,6,7,8
pixel_size=2                    ; 2 x 2 arcsec
;flux=-1.                        ; establishes the variable name 
;mem_map,hsi_vis,vis_out, hsi_vis.u, hsi_vis.v,image,grids,pixel_size,flux,/show 

o->set, det_index_mask = [0,0,1,1,1,1,1,1,0]
o->set, pixel_size = 2

; this is what we want to test
;o->set, nj_show = 1

o->set, vis_out = 'gaga.fits'
o->plot
;---

obj_destroy, o

o = hsi_image( vis_in  = 'gaga.fits' ) 
o->plot

; 2007-08-07 from kim, askes the file in a dialog_pickfile although it
; has been set

f = findfile( '~/idl/fits/*image*fits' )
o = hsi_image( im_in = f[1], pixel_size = 2 )
o->plot

; visibility tests, 2007-01 to 2007-06
; 
o =hsi_image( im_time_interval = '20-feb-02 11:06:' + ['00','08'], $
              im_energy_binning = [6,12], image_alg = 'vis_fwdfit', pixel_size =1  )

print, '--- generate some data ...'
data = o->getdata()
o->plot, 0, 0
o->plot, 0, 1


obj_destroy, o

image_alg = 'mem_njit'

o =hsi_image( im_time_interval = '20-feb-02 11:06:' + ['00', '36'], $
              im_energy_binning = [6,12,21], image_alg = image_alg, pixel_size = 1 )

o->set, nj_show = 1
o->plot
obj_destroy, o

image_alg = 'vis_fwdfit'
;image_alg = 'mem_njit'


o =hsi_image( im_time_interval = '20-feb-02 11:06:' + ['00', '04', '08'], $
              im_energy_binning = [6,12,50], image_alg = image_alg, pixel_size = 1 )

;o =hsi_image( im_time_interval = '20-feb-02 11:' + ['06', '10'], $
;              im_energy_binning = [6,12,50], image_alg = image_alg, pixel_size = 1 )

o->panel_display

o->panel_display, image_dim = 128
o->panel_display, image_alg = 'mem_njit'

oo = o->get( class = 'hsi_visibility', /obj )
oo->write, vis_out = 'test.fits'

obj_destroy, o

o = hsi_image( vis_file = 'test.fits' )

end
