* Macro Name: TRACE_NOGHOST_BATCH                   Calls: TRACE_NOGHOST
* Version: 1  Author: G. Schneider  Date: 17 June 2004
* Function: "Post-Process multiple TRACE WL images for a single S/C Pointing
* Notes: 1 - All data images amd ref image must be heliocentrically astrometrically aligned
*        2 - Reference image must be subtracted from each frame before invoking this process
*
user_interactive       = "false"
*
***** DIRECTORIES AND REFERENCE FILE *********************************
data_disk              = "STARS2" ! Device name of data disk
reference_folder       = data_disk//":VENUS:INGRESS:IN_REF_DATA"
input_folder           = data_disk//":VENUS:INGRESS:IN_NO_LDARKEN_ALIGN"
tiff_out_folder        = data_disk//":VENUS:INGRESS:IN_PROCESSED_TIFFS"
hdf_out_folder         = data_disk//":VENUS:INGRESS:IN_PROCESSED_HDF"
median_background_file = "INGRESS_REFERENCE.fits"
*
***** TUNABLE PARAMETERS *********************************************
* values set for: black drop ! ring
phot_weight            =  .5 ! 0.1 Attenuation factor for photosphere
limb_edge              = 500 ! 500 DN defining solar limb
phot_offset            = 250 !  25 DN median photospheric offset from zero
*
***** MAKE SKY:PHOTOSPHERE ATTENUATION WEIGHT MASK********************
* Read in the Median Combined Aligned Photosphere + Sky Reference Data
* Create final image weight MASK
  call setfolder(reference_folder)
  call open(median_background_file)
  temp = currentdataset
background_reference = var(temp)
  call close(temp)
MASK = (GEmask(background_reference,limb_edge) * phot_weight) + LTmask(background_reference,limb_edge)
*
**** DO ALL FILES IN INPUT DIRECTORY *********************************
call setfolder(input_folder)
call loop (2,100,1,"TRACE_NOGHOST")
*
* ===== END MACRO ============
*
*
* TRACE SCATTERED LIGHT REDUCTION - FILE LEVEL MACRO (the guts)
* Macro Name: TRACE_NOGHOST         Called by: TRACE_NOGHOST_BATCH
* Version: 3  Author: G. Schneider  Date: 17 June 2004
* Function: Post-Processes TRACE WL Image
*           1 - Removes double afocal optical ghosts
*           2 - Removes diffuse scattered light
*           3 - Eliminates Cosmic Ray Hits external to solar limb
*           4 - Collapses image dynamic range - attenuates photosphere
*           5 - Establishes zero sky background and offsets photosphere from sky
*
user_interactive       = "false"
*
***** GET FRAME TO PROCESS **********************************
call                     setfolder(input_folder)
file_number            = loop_index
input_file_name        = getnthfilename(file_number)
call                     open(input_file_name)
IM                     = var(currentdataset)
*
***** MODEL & SUBTRACT DOUBLE IMAGE GHOST *******************
GHOST = (IM - (0.013*rotcols(rotrows(smooth(IM,9),33),-1))) - (0.0035*rotcols(rotrows(smooth(IM,11),28),-15))
*
***** RESCALE PHOTOSPHERIC PIXELS BY MASK WEIGHT & SET SKY MEDIAN = 0, PHOTOSPHERIC MEDIAN = phot_offset
MIB = phot_offset + (-phot_offset * LTmask(background_reference,limb_edge)) + (GHOST * MASK)
*
***** FIND & REMOVE COSMIC RAY HITS EXTERNAL TO SOLAR LIMB *******************
*     Definitive CRs ABOVE ring brightness (in presence of residual scattered light) > 8DN
*     Images have median background offsets of -0.8 DN (image bias previously removed)
*     Set CR hit pixels in sky with zero after removing median background bias
SKY         = MIB * EQmask(Mask,1)
NOCR_SKY    = (GEmask(SKY,8) * -0.8 ) + (SKY * LEmask(SKY,8))
*
***** CONTIGUOUSLY CONCATINATE WITH SCALED PHOTOSPHERIC IMAGE TO ALLOW SMOOTH INTERPOLATION ACROSS LIMB
phot_scale  = 1 ! Emperical trans-limb scattered light weighting ratio - if needed
PHOT        = (EQmask(MASK,phot_weight) * MIB) - (phot_offset * EQmask(MASK,phot_weight))
NOCR        = NOCR_SKY + (PHOT * phot_scale)  ! CR Removed sky and scaled photospheric image
*
***** LOCATE & MODEL THE RESIDUAL "DARK" SCATTERED LIGHT ARTIFACT
*     Compute the ratio of each pixel to a 5 pixel boxcar of neighbors
BACK_RATIO  = abs(NOCR/((NOCR - smooth(NOCR,5))))  ! Exclude ring region itself from excess background
*
level = 4 ! This is two sigma per pixel
* Scattered light in pixels which exceed 5x5 box of neighbors by a factor of 'level'
* Atmospheric arc will have a value of zero, sky will have a value of zero
SCATTER     = GEmask(BACK_RATIO,level) * NOCR  ! Scattered light residual adjacent to ring
*
***** MODEL THE DIFFUSE SSCATTERED LIGHT BACKGROUND SMOOTHLY ACROSS RING **********
* Fill zero (ring location) values by 1/r^2 (fill_function 2) weighted fill (fill_method 4).
* Then smooth over interpolated image after zero replacement with a 7 pixel kernal
fill_method    = 4
fill_function  = 2
fill_range     = 7
fill_preserve  = false
fill_value     = 0
fill_equal     = 1
BACK_MODEL     = fillmissing(SCATTER)
*
****** SUBTRACT BACKGROUND SCATTERED LIGHT MODEL FROM CR ELIMIMNATED FRAME ********
SKY_NO_SCAT = EQmask(MASK,1) * (NOCR - BACK_MODEL)
*
*   Note: This will leave a thin band of dark pixels at the limb interior to Venus' disk
*         Remove by interpolating all pixels with intensities < - 6 ADU
fill_method     = 4
fill_function   = 4
fill_range      = 3
fill_value      = -6
fill_equal      = 3
fill_preserve   = true
FINAL_SKY_IMAGE = fillmissing(SKY_NO_SCAT)
*
*
***** COMBINE SKY AND WEIGHTED PHOTOSPHERIC REGIONS (blurring edge with TRCE FWHM PSF)
psf_fwhm = 2 ! In pixels
EDGE_WEIGHT  = smooth(EQmask(MASK,phot_weight),psf_fwhm)
PHOT_PART    = EDGE_WEIGHT * EQmask(MASK,phot_weight) * MIB
SKY_PART     = (1 - EDGE_WEIGHT) * FINAL_SKY_IMAGE
WHOLE_IMAGE  = PHOT_PART + SKY_PART
DISK_IMAGE   = (255 + PHOT_PART) * EDGE_WEIGHT
*
***** GENERATE THE IMAGES ******
color_table = "GrayScale"
data_autominmax = "false"
data_max        =  55
data_min        = -15
w = image('WHOLE_IMAGE') ! Atmospheric ring display
*
data_max        = 600
data_min        = 100
w = image('DISK_IMAGE') ! Venus disk transit display
*
***** GENERATE THE TIFF IMAGES & SAVE THE DATA FILE AS HDF *******
call setsavefolder(tiff_out_folder)
*
*     Uncoment below to generate ring images - tunable parameters for ring
***** call saveas('WHOLE_IMAGE.image1',input_file_name//"RING.TIFF",6)
*
*     Uncomment below to generate disk images - tunable parameters for B.D.
***** call saveas('DISK_IMAGE.image1',input_file_name//"TRAN.TIFF",6)
*
*     Create the HDF data files
call  setsavefolder(hdf_out_folder)
call  saveas("WHOLE_IMAGE",input_file_name//".HDF",2)
*
***** CLEAN UP ************
call close('IM')
call close('GHOST')
call close('MIB')
call close('SKY')
call close('NOCR_SKY')
call close('NOCR')
call close('PHOT')
call close('BACK_RATIO')
call close('SCATTER')
call close('BACK_MODEL')
call close('SKY_NO_SCAT')
call close('FINAL_SKY_IMAGE')
call close('EDGE_WEIGHT')
call close('PHOT_PART')
call close('SKY_PART')
call close('WHOLE_IMAGE')
call close('DISK_IMAGE')
*
*===== END MACRO ============