FUNCTION hsi_vis_usershell, SAVEFILE=savefile, PLOTFIT=plotfit, DET_INDEX_MASK=det_index_mask, $
			XYOFFSET=xyoffset, TIME_RANGE=time_range, ENERGY_BAND=energy_band, PHZ_RADIUS=phz_radius, USE_FLUX_VAR=use_flux_var
;+
; PROJECT:
;	HESSI
;
; NAME:
;   hsi_vis_usershell
;
; PURPOSE:
;   This is an interim procedure, designed to be edited by the user, that can be used to calculate RHESSI visibilities.
;   It will become obsolete when visibilties are fully integrated into the RHESSI software package.
;
; CATEGORY:
;   hessi/idl/image/visibility/gh
;
; CALLING SEQUENCE:
;   vis = hsi_vis_usershell([SAVEFILE=savefile]      [, PLOTFIT=plotfit]           [, DET_INDEX_MASK=det_index_mask] $
;                           [, XYOFFSET=xyoffset]    [, TIME_RANGE=time_range]     [, ENERGY_BAND=energy_band]       $
;                           [, PHZ_RADIUS=phz_radius][, USE_FLUX_VAR=use_flux_var]
;
; CALLS:
;	Creates an image object.
;   hsi_vis_gen
;   hsi_vis_fwdfit
;
; INPUTS:
;   These 3 keywords should be specified; the others are optional.
;   XYOFFSET    = 2-element vector specifying the approximate source offset from sun center. (arcsec, +ve W,N)
;   TIME_RANGE  = 2-element vector specifying the time range (ANYTIM format)
;   ENERGY_BAND = 2-element vector specifying the energy band (keV).
;
; OPTIONAL INPUTS:
;   DET_INDEX_MASK = 9-element vector selecting RMCs (Default = [1,0,1, 0,0,0, 0,0,0] excludes RMC2
;   PHZ_RADIUS  = Maximum distance between source components (arcsec) (used to determine number of roll bins)
;                   If not specified, a fixed number 32 roll bins is used for each RMC.
;   /USE_FLUX_VAR invokes a correction for flare time variability.
;   /PLOTFIT if set, fits to phase bins for each roll RMC and each roll bin are plotted in visfit.ps.
;   SAVEFILE = names a .sav file in which output visibilities will be stored.
;
; OUTPUTS:
;   vis = array of visibility structures (viz, a 'bag' of visibilities in no particular order)
;
; OPTIONAL OUTPUTS:
;	none
;
; KEYWORDS:
;	See optional and required inputs.
;
; COMMON BLOCKS:
;	Call to hsi_fwdfit will create a common block, uvdat.
;
; SIDE EFFECTS:
;	none
;
; RESTRICTIONS:
;
; MODIFICATION HISTORY:
; 13-Sep-05 gh	Initial version that replaces 15-Aug-05 version of newvis_test
; 15-Sep-05 gh	Save the results.
; 16_Sep-05 gh	Add PLOTFIT keyword
; 26-Sep-05 gh	Open, close plot output here.
; 21-Nov-05 gh	Add XYOFFSET, TIME_RANGE and ENERGY_BAND keywords and use DEFAULTS.
; 22-Dec-05 gh	Add det_index_mask keyword
; 17-Feb-06 gh  Add PHZ_RADIUS keyword.  If set, program sets number of roll bins for a source of extent=phz_radius.
;  6-Mar-06 gh  Add USE_FLUX_VAR keyword to activate time-variation correction
; 28-Mar-06 gh  Clean up code and add ssw template.
;
;-
;
; Default flare-independent parameters
DEFAULT, savefile,			'vis.sav'
DEFAULT, plotfit, 			0
DEFAULT, det_index_mask,  	[1,0,1, 1,1,1, 1,1,1]			; default suppresses grid 2
DEFAULT, use_flux_var,      0
nroll 						= 32							; number of roll bins, if not automatically set.
IF plotfit NE 0 THEN BEGIN
	old_device=!D.NAME
	set_plot, 'ps'
	DEVICE, FILE='visfit.ps'
ENDIF
;
; Default flare parameters - should be replaced by user values.
;
; Aug 28, 2002
DEFAULT, time_range,	['2002-aug-28 18:57:50', '2002-aug-28 18:58:50']
DEFAULT, xyoffset,		[-938.8, 166.5]									; map center good to < 0.1 arcsec
DEFAULT, energy_band,	[12,25]
;
; Create an image object from which to get the calibrated eventlist.
oim = hsi_image()
IF KEYWORD_SET(phz_radius) NE 0                                 $
        THEN oim->set, phz_radius               = phz_radius    $
        ELSE oim->set, phz_n_roll_bins_control 	= INTARR(9)+nroll							; 1==>User specifies number of roll bins; otherwise 0
oim->set, phz_report_roll_bins 		= 1							; 1==> display number of roll bins in output log
oim->set, xyoffset					= xyoffset
oim->set, cbe_digital_quality 		= 0.95
oim->set, time_range				= time_range
oim->set, energy_band				= energy_band
oim->set, det_index_mask 			= det_index_mask
ocbe=oim->get(/obj, class='hsi_calib_eventlist')
vis = hsi_vis_gen(ocbe, PLOTFIT=plotfit)                            ; Generates array of visibilities from the cbe.
;
; Optionally correct visibilities for time variations.
IF use_flux_var NE 0 THEN BEGIN
    hsi_vis_fwdfit, vis, SRCOUT=prelim_fit, /NOPLOTFIT
    flux = prelim_fit.srcflux                                       ; estimate of source flux based on uncorrected visibilities
    relcor = hsi_vis_gen(ocbe, PLOTFIT=plotfit, /USE_FLUX_VAR)      ; should come out in the same sequence as vis
    vis.obsvis = vis.obsvis - flux*relcor.obsvis
ENDIF
;
; Save and optionally plot results.
nvis = N_ELEMENTS(vis)
SAVE, vis, FILENAME=savefile
PRINT, nvis,  '-element visibility structure array, "vis", saved in  ', savefile
IF plotfit NE 0 THEN BEGIN
	PRINT, 'Fits are plotted in visfit.ps'
	DEVICE, /CLOSE
	SET_PLOT, old_device
ENDIF
RETURN, vis											    ; Returns array of visibility structures
END