pro set_new_db, newdir, reset=reset, leak=leak, dark=dark
;+
;NAME:
;	set_new_db
;PURPOSE:
;	To redirect to a new dark or leak image data base
;SAMPLE CALLING SEQUENCE:
;	set_nev_db, newdir
;	set_new_db, '/0p/acton/new_db', /dark
;	set_new_db, '/0p/acton/new_db', /leak
;	set_new_db, /reset, /dark
;	set_new_db, /reset, /leak
;INPUT:
;	newdir	- The directory where the new database file will
;		  reside
;OPTIONAL KEYWORD INPUT:
;	reset	- Go back to using the old standard directory
;METHOD:
;	1. Create a file with the dark and leak images you wish
;	   to use.  The prefix for the file MUST BE:
;		sdc	- for dark images
;		sfc	- for leak images
;	   * There can be multiple files.  **ALL** files with that prefix
;	     in that new directory are checked for the best match
;	   * NONE of the standard dark or leak images are checked if
;	     you use this new option (ie: you must have ALL of the dark
;	     images you want in your off-line dark/leak image file).
;	     Extract images from the standard SDC set and put them into
;	     your off-line set if you need them
;	2. Redirect the environment variables to point to your offline
;	   area by calling SET_NEW_DB.  The routine deletes the common
;	   block saved info so that it is re-built on the next DARK
;	   or LEAK call.  This takes many seconds for the DARK calls
;	   so swapping back and forth between official and off-line
;	   databases will be very slow.
;	3. Run SXT_PREP and/or LEAK_SUB and/or DARK_SUB
;	4. To return to using the original official database, use the
;	   /RESET option on the SET_NEW_DB command.  This assumes that
;	   you had not done a SETENV outside of this routine.
;EXAMPLE:
;	The following is an example of how to use PFIs for the source
;	dark frames.  CAUTION: If the ROI location on the CCD changes,
;	the DARK_SUB routine will probably not choose the optimal
;	pointing dark frame (it chooses the closest in TIME)
;		infil = /yd5/flares/spr930625.0131
;		rd_roadmap, infil, rmap
;		ss = where(gt_expmode(rmap) eq 1)
;		rd_xda, infil, ss, ii, dd
;		--- Note: You can average the dark images before saving if desired
;		sav_sda, '/yd3/morrison/newdb/sdc_test2', ii, dd
;		set_new_db, '/yd3/morrison/newdb'
;		ss = where((gt_dp_mode(rmap) eq 13) and (gt_filtb(rmap) eq 2))
;		rd_xda, infil, ss, index, data
;		sxt_prep, index, data, index2, data2
;HISTORY:
;	Written 15-May-95 by M.Morrison
;-
;
common sel_dc_image_blk, dc_infil, dc_roadmap, temp, dc_filidx
common sel_leak_image,   leak_files0, leak_rmap0
common set_new_db_blk, orig_dark, orig_leak

qleak = keyword_set(leak)	;default is the dark database
;
if (qleak) then begin
    if (keyword_set(reset)) then begin
	if (n_elements(orig_leak) eq 0) then begin
	    print, 'SET_NEW_DB: /RESET used without having set anything'
	end else begin
	    setenv, 'DIR_SXT_SFC='+orig_leak
	    print, 'Re-defining DIR_SXT_SFC to point to: ' + orig_leak
	end
    end else begin
	if (n_elements(orig_leak) eq 0) then orig_leak = getenv('DIR_SXT_SFC')
	setenv, 'DIR_SXT_SFC='+newdir
	print, 'Re-defining DIR_SXT_SFC to point to: ' + newdir
    end
    delvarx, leak_rmap0		;reset so files are re-checked
end else begin
    if (keyword_set(reset)) then begin
	if (n_elements(orig_dark) eq 0) then begin
	    print, 'SET_NEW_DB: /RESET used without having set anything'
	end else begin
	    setenv, 'DIR_SXT_SDC='+orig_dark
	    print, 'Re-defining DIR_SXT_SDC to point to: ' + orig_dark
	end
    end else begin
	if (n_elements(orig_dark) eq 0) then orig_dark = getenv('DIR_SXT_SDC')
	setenv, 'DIR_SXT_SDC='+newdir
	print, 'Re-defining DIR_SXT_SDC to point to: ' + newdir
    end
    delvarx, dc_roadmap		;reset so files are re-checked
end

end