!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!Midas program to reduce the Special Dome Flat of SOFI     !
!use @@ midas_specialflat input                            !
!                                                          !
!input is a file with the list of the 8 flat frames        !
!generated by the SpecialDomeFlat template of SOFI         !
!                                                          !
!                                                          !
!September 2001 - Leo Vanzi                                !
!                                                          !
!                                                          !
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


!definition of variable

define/loc list/c/1/5 p


!a table is created including the 8 input frames

crea/tab flat 1 8 {p1} form.fmt


!catalogs are created including the couples of frames to
!be averaged, the frames are averaged

sel/tab flat SEQ.eq.1.or.SEQ.eq.8
crea/acat cat18 flat,:file_name
ave/ima flat_off = cat18.cat

sel/tab flat SEQ.eq.2.or.SEQ.eq.7
crea/acat cat27 flat,:file_name
ave/ima flat_off_mask = cat27.cat

sel/tab flat SEQ.eq.3.or.SEQ.eq.6
crea/acat cat36 flat,:file_name
ave/ima flat_on_mask = cat36.cat

sel/tab flat SEQ.eq.4.or.SEQ.eq.5
crea/acat cat45 flat,:file_name
ave/ima flat_on = cat45.cat

!correction for bias is calculated for the flats ON
!suitable strips are extracted from the images
extra/ima temp_onA = flat_on[500,1:600,1024]
extra/ima temp_onB = flat_on_mask[50,1:150,1024]
extra/ima temp_onC = flat_on_mask[500,1:600,1024]

!squized to 1D
shrink/ima temp_onA temp_onA_1
shrink/ima temp_onB temp_onB_1
shrink/ima temp_onC temp_onC_1

!some calculations are performed
compu/pix temp_onAC = temp_onA_1 - temp_onC_1
compu/pix temp_onACB = temp_onAC + temp_onB_1

!the result is expanded to 2D
grow/ima temp_onACB_2 = temp_onACB 1.,1.,1024 1,LINE C

!more calculations
compu/pix temp_on_bias = flat_on - temp_onACB_2

!correction for bias is calculated for the flats OFF
extra/ima temp_offA = flat_off[500,1:600,1024]
extra/ima temp_offB = flat_off_mask[50,1:150,1024]
extra/ima temp_offC = flat_off_mask[500,1:600,1024]

shrink/ima temp_offA temp_offA_1
shrink/ima temp_offB temp_offB_1
shrink/ima temp_offC temp_offC_1

compu/pix temp_offAC = temp_offA_1 - temp_offC_1
compu/pix temp_offACB = temp_offAC + temp_offB_1
grow/ima temp_offACB_2 = temp_offACB 1.,1.,1024 1,LINE C
compu/pix temp_off_bias = flat_off - temp_offACB_2

!the flat is calculated as ON - OFF
compu/pix midas_specialflat = temp_on_bias - temp_off_bias

!the flat is normalize
stat/ima midas_specialflat
compu/ima midas_specialflat = midas_specialflat / 'outputr(3)'

!the bdf image is converted to fits format
outdisk/fits midas_specialflat.bdf midas_specialflat.fits

!temp frames are deletes
$rm temp*.bdf
