; Extracts an input 2Dimage which can be given as an input or as a
; file name to be read. The output is a 1D spectrum in the yaaar format.

; FILENAME   I    name of the file to be read
; IMAGE      I    2Dimage (if set then the header must be provided)
; HEADER     I    header of the 2Dimage
; SIGMA      I    Sarah is not sure... I think it's the threshold value for
;                 detecting the source when doing column or gaussian extraction
; SKY        I    Sky removal option: 0 --> none
;                                     1 --> autosky0
;                                     2 --> autosky1
;                                     1 --> querysky (full aperture extraction)
; FULL       I    Full extraction
; COLUMN     I    Column extraction (variable column)
; GAUSSIAN   I    Gaussian extraction
; SSC        I    Mimicking the SSC extraction 
; ONFLY      I    onfly calibration. Calstar options: hr634r8, hd173511, ksidra 
; OUTYAAAR   O    output yaaar file

function irs_extract, FILENAME=filename, IMAGE=image, HEADER=header, SIGMA=sigma, SKY=sky,$
                      FULL=full, COLUMN=column, GAUSSIAN=gaussian, SSC=ssc, ONFLY=onfly,$
                      FLUXCON1=fluxcon1, FILETYPE=filetype, ZODIISUB=zodiisub,$
                      EXTRACTCAL=extractcal, GPLOT=gplot, V=v,droop=droop,optimal=optimal,ext_type=ext_type,smart_v=smart_v


; Read the file in and make a pointer bcd of the data
image = readfits(filename, header)
noise = fltarr(128, 128)
bmask = fltarr(128, 128)

data = [[[image]], [[noise]]]

ptr_bcd = ptr_new({bcd:ptr_new(data), header:ptr_new(header), filename:ptr_new(filename), $
                   bmask:ptr_new(bmask)})

calversion=strcompress(strtrim(sxpar(header,'CAL_SET'),2),/remove_all)
calversion=strmid(calversion,1,4)
scalversion=strsplit(calversion,'.',/extract)
smart_v=scalversion[0]+'_'+scalversion[1]

if ((calversion ne '15.3') and (calversion ne '17.2') and (calversion ne '18.7')) then stop
sm_cal_set, cal_version=smart_v



!sm_fwhmfactor=-1.                      ; setting the extraction to SSC

if not keyword_set(sky) then sky=0      ; setting the sky extraction
if keyword_set(column) and not keyword_set(ssc) then fluxcon1=1  
if keyword_set(onfly) then fluxcon1=1  


; Set calfiles and adding fields to the header
sm_wavesamp, cal, /restore_cal, /save_cal
sm_preset_bcd, ptr_bcd                    


if keyword_set(optimal) then begin
    !sm_opt_rsrf = 0            ;SSC aploss + RSRF
    ;!sm_opt_rsrf = 1            ;SSC aploss
    ;!sm_opt_rsrf = 2            ;no fux calibration (e-/sec)
    ;---------------------------------------------------------
    !sm_opt_wave_regrid = 0     ;wavelength grid is based on wavesamp elements (interpolation)
    ;!sm_opt_wave_regrid = 1     ;wavelength grid is defined at the source's position
    ;aploss fluxcon table is chosen automatically by sm_cal_set
endif 

; Saving and changing the definition of the psffov and fluxcon files

path='~/Scratch/IDL/IRS/Tables/'

save_sl_psfov=!sm_sl_psffov_f
save_ll_psfov=!sm_ll_psffov_f
save_sl_fluxcon_f=!sm_sl_fluxcon_f
save_ll_fluxcon_f=!sm_ll_fluxcon_f
save_sh_fluxcon_f=!sm_sh_fluxcon_f
save_lh_fluxcon_f=!sm_lh_fluxcon_f


; Setting the width of the column for extraction

path='~/Scratch/IDL/IRS/Tables/'

if keyword_set(column) and not keyword_set(ssc) then begin
    psffov,column=column
    defsysv,'!sm_sl_psffov_f',path+'irs_sl_psf-fov_jero2.tbl'
    defsysv,'!sm_ll_psffov_f',path+'irs_ll_psf-fov_jero2.tbl'
endif

if keyword_set(fluxcon1) then begin
    defsysv,'!sm_sl_fluxcon_f',path+'irs_sl_fluxcon_s1_jero.tbl'
    defsysv,'!sm_ll_fluxcon_f',path+'irs_ll_fluxcon_s1_jero.tbl'
    defsysv,'!sm_sh_fluxcon_f',path+'irs_sh_fluxcon_s1_jero.tbl'
    defsysv,'!sm_lh_fluxcon_f',path+'irs_lh_fluxcon_s1_jero.tbl'
endif


; Getting the campaign number to check for LL and LH bias change

path0='~/Scratch/IDL/IrsClean/all_campaigns_roguemasks/'
restore,'~/Scratch/IDL/IrsClean/camp.tmpl'
datacamp=read_ascii(path0+'campaigns2.txt',template=camp)
datacamp.field1=strmid(datacamp.field1,3)

irscamp=strcompress(strtrim(sxpar(header,'CAMPAIGN'),2),/remove_all)
idx=where(irscamp eq datacamp.field2)
camp_num2=datacamp.field1[idx]

if (camp_num2 ge 45) then sm_cal_override_ll, ll_bvc_post45_set=1
if (camp_num2 ge 45) then sm_cal_override_lh, lh_bvc_post25_set=1



; Preparing for column, gaussian or optimal extraction
ext_type = 'FULL'
if keyword_set(column) then ext_type = 'Tapered Column'
if keyword_set(gaussian) then ext_type = 'Gaussian'
if keyword_set(optimal) then ext_type = 'Optimal'

for i = 0, n_elements(ptr_bcd) - 1 do begin
    a95_sxaddpar,*(*ptr_bcd[i]).header,'S_EXTY30', ext_type,'Extraction type originally requested'
endfor
PRINT, 'S_EXTY30: ', ext_type,': Extraction type originally requested'

if not keyword_set(full) then begin
    if not keyword_set(sigma) then sigma=5.0
    if keyword_set(column) then sm_find_pos, ptr_bcd, method = 'C', sigma = sigma, /column, /onepeak   ; new source finder
    if keyword_set(gaussian) then sm_find_pos, ptr_bcd, method = 'C', sigma = sigma, /column, /onepeak
    ;if keyword_set(column) then sm_auto_all,ptr_bcd,sigma=sigma,/column,/onepeak 
    ;if keyword_set(gaussian) then sm_auto_all,ptr_bcd,sigma=sigma,/gaussian,/onepeak 
endif


if keyword_set(optimal) then begin
    print, '-----------------------------------------------'
    print, '*** ADD SOME KEYWORDS FOR OPTIMAL EXTRACTION ***'
    print, '-----------------------------------------------'

    module_arr = strarr(n_elements(ptr_bcd))
    for bcd_num = 0, n_elements(ptr_bcd)-1 do begin
        module_arr(bcd_num) = get_module(*(*ptr_bcd[bcd_num]).header)
    endfor 
    sm_opt_key_set, ptr_bcd, module_arr    
endif


; Extract data to produce spectrum


if keyword_set(optimal) then sm_ext_opt, ptr_bcd, ptr_aar else sm_bcd_isap, cal, ptr_bcd, ptr_aar, sky,/script_mode,droop=droop,original_ext_type=ext_type

outyaaar=scriptaar2yaaar((*ptr_aar[0]))


PTR_FREE, PTR_VALID() ; free the pointer memory


; Onfly calibration

if keyword_set(onfly) then outyaaar=irs_onflycal(outyaaar, SIGMA=sigma, SKY=sky, FULL=full,$
                                                 COLUMN=column, GAUSSIAN=gaussian,ONFLY=onfly,$
                                                 EXTRACTCAL=extractcal, HEADER=header,FILETYPE=filetype, $
                                                 V=v, CAMPAIGN=camp_num,droop=droop)

 

if keyword_set(zodiisub) then outyaaar=sc_zodi_j(outyaaar, DATE_OBS=date_obs) ; zodii subtraction


; Returning the SSC extraction values and SMART psffov

!sm_fwhmfactor=-1. 

defsysv,'!sm_sl_psffov_f',save_sl_psfov
defsysv,'!sm_ll_psffov_f',save_ll_psfov
defsysv,'!sm_sl_fluxcon_f',save_sl_fluxcon_f
defsysv,'!sm_ll_fluxcon_f',save_ll_fluxcon_f
defsysv,'!sm_sh_fluxcon_f',save_sh_fluxcon_f
defsysv,'!sm_lh_fluxcon_f',save_lh_fluxcon_f


return,outyaaar




end

 
This site is powered by FoswikiCopyright © 2002-2018 by the IRS Team.
Send feedback. We use Foswiki.