;;-----------------------------------------------------------------------------
;; PURPOSE:
;;	Displays a dialog box, which provides help for the currently selected 
;;	module.
;;
;; CALLING SEQUENCE:
;;	gui_modHelp, mod
;;
;; REQUIRED INPUTS:
;;	top - the top level base of the application
;;	modIdx - Index in the list of the currently selected module
;;
;; OUTPUTS:
;;
;; OPTIONAL INPUT KEYWORDS:
;;
;; EXAMPLE:
;;      IDL> gui_modHelp, mod
;;
;; PROCEDURES USED (i.e. called directly!):
;;
;; MODIFICATION HISTORY:
;;   2004-08-02  M. Desnoyer    Created
;;
;;-----------------------------------------------------------------------------

;;------------------------
;; Event Handler
;;------------------------
PRO gui_modHelp_event, ev

widget_control, ev.TOP, /destroy

END

;;------------------------
;; Creates and displays the dialog box
;;------------------------
PRO gui_modHelp, top, modIdx

nl = string(10B)

;; Determine the text to display
CASE modIdx OF
0:BEGIN
	title = 'Calibrate Voltages and Temperatures
	str = 'The raw files contain the on-board voltages and temperatures measured in '+$
		'counts. These counts need to be converted to physical values and inserted '+$
		'into the header for later calibration steps to work correctly. This is done '+$
		'by default when the raw FITS file was created but if the calibration function '+$
		'has changed, this step is need in the tool to update the physical values'
END
1:BEGIN
	title = 'Decompress'
	str = 'Some images arrive in a compressed 8-bit format. These images '+$
		'need to be decompressed into their native 14-bit format '+$
		'before they can be calibrated. This module performs this '+$
		'decompression using a lookup table (LUT). The appropriate '+$
		'LUT is found by querying a SQL database and using the '+$
		'IMGH001 keyword. This module will always be applied if the image is '+$
		'compressed. The options given refer to how an 8-bit number is converted '+$
		'to the 14-bit value. Since there are a range of 14-bit values corresponding '+$
		'to each 8-bit value, one of those needs to be selected. This can be done by '+$
		'using the average value, or randomly choosing the value with a uniform or '+$
		'gaussian distribution.'
END
2: BEGIN
	title = 'Flag Saturated Pixels'
	str = 'This module adds flags to the first image extension specifying if a given pixel '+$
		'is saturated'
END
3: BEGIN
	title = 'Bit Weighting Correction'
	str = 'Correct for uneven ADC bit weighting using a lookup '+$
		'table to convert from measured DN values to the value '+$
		'that would have been outputed if all DN intervals '+$
		'represented exactly equal number of signal electrons.'
END
4: BEGIN
	title = 'Linearize DN Values'
	str = "The IR instrument's reading is not linear with respect "+$
		'to input radiance. Thus, the measured DN values must be '+$
		'divided by a polynomial so that they react linearily.'
END
5: BEGIN
	title = 'Subtract Dark Frame'
	str = 'One of the most important calibration steps is removing '+$
		'any extraneous signal produced by the instrument beyond that detected '+$
		'from the object being observed. This includes electronics bias, silicon '+$
		'dark current and in the case of the IR, heat from the optical bench. '+$
		'This signal is removed using a dark image subtraction technique so that for each '+$
		'pixel, the approriate level is subtracted. The dark frame '+ $
		'used in the subtraction can be found in three different ways:'+$
		nl+' '+nl+'Pre Calculated Model - Based on ground and flight '+$
		'calibrations, the dark level response has been modeled for '+$
		'each pixel. Use this model to generate a dark frame.'+nl+' '+nl+$
		"Team's Best Dark - For critical observations where the model was not optimal, "+$
		'the DI Science Team has manually created dark frames. If the team did not create '+$
		'a dark for your observation, this will use the model instead. '+nl+' '+nl+$
		'File - User specifies a FITS file to use for dark subtraction. If it is an IR image, '+$
		'the dark frame specified must be linearized first, but NOT bias subtracted.'
END
6: BEGIN
	title='Remove Crosstalk'
	str = 'In the VIS CCDs, there is some electrical cross talk when the pixels are read out. '+$
		'This results in ghosts appearing in other quadrants of the image. This module '+$
		'removes those ghosts'
END
7: BEGIN
	title='Normalize Quadrant Gain'
	str = 'Each quadrant of each detector uses a different electrical amplifier with slightly '+$
		'different gains. This module normalizes these gains if they changed in flight.'
END
8: BEGIN
	title = 'Flat Field Normalization'
	str = 'Each image must be divided by a flat field image to '+$
		'account for the different sensitivity of each pixel. The '+$
		'flat can be found in two different ways:'+nl+' '+nl+$
		'Archived File - The appropriate flat field image found in '+$
		'the archive. If there is no appropriate file, an '+$
		'error is returned.'+nl+' '+nl+$
		'User Specified File - The user selects the flat field FITS '+$
		'image to use.'
END
9: BEGIN
	title = 'Remove Transfer Smear'
	str = 'As the image is transfered on and off the VIS CCD, extra '+$
		'charge is accumulated in bright areas, creating a smear '+$
		'effect. This can be remove in two different ways: '+nl+' '+nl+$
		'POC Rows - There are rows in each image which are only '+$
		'exposed when the transfer occurs. Thus, the smear can be '+$
		'removed by averaging these rows. However, modes 7 and 8 '+$
		'do not contain reliable POC rows, making this algorithm '+$
		'unusable in this case.'+nl+' '+nl+$
		'Column Averaging - Since each pixel in a column will '+$
		'receive the same amount of transfer smear, the smear can '+$
		'be removed by subtracting a fraction of the column average. '+$
		'This method is not as accurate as the POC algorithm and will '+$
		'only work if the edges of the image are dark, but it can be '+$
		'used for all modes.'
END
10: BEGIN
	title = 'Flag Bad Pixels'
	str = 'Known bad pixels in the CCD have been identified during '+$
		'by the science team. This module flags those pixels as bad '+$
		'in the first FITS extension of the output file.'
END
11: BEGIN
	title = 'Convert to Physical Units'
	str = 'This is absolute calibration that converts corrected DNs into '+$
		'physically significant values. Depending on the outputs selected '+$
		'this will produce radiance values [W/(m^2 sr um)] or I/F values.'
END
12: BEGIN
	title = 'Interpolation'
	str = 'This module interpolates over known gaps in the data and/or know bad pixels. '+$
		'The interpolation is done using unsaturated good pixels in the vicinity of '+$
		'the area being interpolated over. In the IR, the interpolation is linear and '+$
		'is only done in the spatial (vertical) directory. In the VIS, a thin plate '+$
		'spline interpolation is used that is anchored using the pixels surrounding the '+$
		'gap.'
END
13: BEGIN
	title = 'Despike'
	str = 'Cosmic ray hits produce small areas of high intensity in the '+$
		'image. This module attempts to remove these spikes by '+$
		'replacing local outliers with the mean/median of the pixels '+$
		'in that area. The options in this module allow you to choose '+$
		'how large an area to consider "local", what threshold to use to '+$
		'identify outliers, what type of averaging statistic to use and '+$
		'the maximum number of times to iterate the algorithm.'
END
14: BEGIN
	title = 'Denoise'
	str = 'Attempts to remove random gaussian noise using wavelet '+$
		'thresholding.'
END
15: BEGIN
	title = 'Deconvolution'
	str = "Corrects for the instrument's PSF by applying a deconvolution routine. "+$
		'Two algorithms can be used:'+nl+' '+nl+'Constrained Least Squares - A fast '+$
		'simple routine that works well on extendended sources but produces significant '+$
		'amounts of ringing.'+nl+' '+nl+'Richardson-Lucy - A slower and more complicated '+$
		'algorithm that produces better results. This is the preferred choice of the '+$
		'science team but requires that you understand how the options work.'
END
16: BEGIN
	title = 'Geometric Calibration'
	str = 'Performs a rubber sheet geometric calibration in the spatial '+$
		'dimensions.'
END
ELSE: RETURN
ENDCASE

;; Break the display string into lines 60 chars wide at most
str = strsplit(str, nl, /extract)
FOR i=0, (size(str,/dimensions))[0]-1 DO BEGIN
	wrds = strsplit(str[i], ' ')
	wrds = [wrds, max(wrds)+strlen(strmid(str[i],max(wrds)))+1]
	cur = 0
	lines = ''
	WHILE max(cur LT wrds) EQ 1 DO BEGIN
		next = wrds[max(where(wrds LT cur+60))]
		lines = [lines, strmid(str[i], cur, next-cur-1)]
		cur = next
	ENDWHILE
	str[i] = strjoin(lines[1:*], nl)
ENDFOR
str = strjoin(str,nl)

;; Build the modal dialog box
dialog = widget_base(/modal, group_leader=top, title='Help for: '+title, $
	/column)
trash = widget_label(dialog, /align_center, value=title)
trash = widget_label(dialog, /align_left, value=str)
okbut = widget_button(dialog, /align_center, value='Ok')
widget_control, dialog, default_button=okbut, /realize

;; Create the event handler for the dialog box
xmanager, 'gui_modhelp', dialog, group_leader=top, $
	event_handler='gui_modHelp_event'

RETURN
END