pro daily_forecast2, infil, indir=indir, qdebug=qdebug, status=status, $
	ignore_log=ignore_log, noftp=noftp, useimage=useimage, $
	testing=testing, nonorm=nonorm, tstop=tstop, ftptest=ftptest, $
	ftponly=ftponly, mail_users=mail_users
;
;+
;NAME:
;	daily_forecast
;PURPOSE:
;	To select the daily images to be sent to the forecast stations and
;	send them.
;SAMPLE CALLING SEQUENCE:
;	daily_forecast
;	daily_forecast, /qdebug, /noftp
;       daily_forecast, /ftponly		; skips image creation
;
;OPTIONAL INPUT:
;	infil	- The list of SFR files to chose from when selecting the
;		  images to send
;OPTIONAL KEYWORD INPUT:
;	indir	- The input directory to find the SFR files.  IF not defined,
;		  it will use DIR_SITE_NEWDATA and $DIR_SITE_NEWDATA2
;	ignorelog - If set, it will ignore the log file checks (see below).
;	noftp	- If set, do not do the FTP step
;       testing - If set, redirect output, inhibit ftp and log use, etc.
;       useimage - User supplied times or index to match - may be an SFD
;		   index with history composite times to duplicate.
;       ftponly  - if switch, user selects files to send, and sites to receive
;		   if file names, user selects sites to recieve
;
;OPTIONAL KEYWORD OUTPUT:
;	status	- The completion status
;			1 = found and transfered files ok
;			10 = The program ran successfully in the last 12 hours
;			     and therefore will not run again until pass 4 or 5.
;			11 = Could not find any matching images
;			12 = The program ran successfully in the last 12 hours
;			     and it was for pass 4 or 5.  Done for the day.
;METHOD:
;		The newest possible image is selected which is:
;			1. AlMg
;			2. Half Res
;			3. Non-SAA
;			4. Non-night image
;			5. MBE 8 and 18 (DPE 15 and 25)
;			6. Images must be newer than the last image sent
;
;		The log file is checked to see when the program was last run 
;		successfully and will not run if it was run within the 
;		last 12 hours.  It also uses the log file to know the date and
;		time of the last image sent and will only chose images new
;		than that image.
;HISTORY:
;	Written 11-Oct-93 by M.Morrison (replacing NN_RD3_SXT_NOAA by Nitta)
;	12-Oct-93 (MDM) - Added file purging of temporary files
;			- Added removal of "Local Directory changed" messages
;			  from the FTP_LOG that is mailed
;	14-Oct-93 (MDM) - Added check that PERCENTD > 240
;			- Save two pairs of images per
;			- Added /NOFTP option
;	16-Oct-93 (MDM) - Modified so that it could do two transmissions per day.
;			  The program transmits TWO images at a time
;	19-Oct-93 (MDM) - Modified so that a mail message is sent if there is
;			  an error during the FTP
;			- Put all of the site/account/password information
;			  into a data file
;	20-Oct-93 (MDM) - Changed to write the daily forecast images to the
;			  ftp directory.
;	26-Oct-93 (MDM) - Enabled sending mail message if there is an error during
;			  FTP copy (prior to this time, it was being tested)
;	 8-Nov-93 (MDM) - Implemented purging of the IDL run log files
;	 6-Dec-93 (MDM) - Implemented the saving of composite (SFD) images.
;	 3-Jan-94 (MDM) - modified the mailing list
;       11-May-94 (SLF) - change rd_tfile call (allow free form commnents)
;       22-Jun-94 (SLF) - add TESTING and NONORM keyword, /sfd switch 
;			  added to sxt_composite call, scale logo to max data
;       23-Jun-94 (SLF) - do registration in sxt_composite, not sxt_prep
;       24-Jun-94 (SLF) - add USEIMAGE keyword
;	25-Jun-94 (SLF) - add capability to vary image cadence for each institute
;			  (specify contact numbers for each remote site)
;	26-Jun-94 (SLF) - move file_append to agree with 25-jun changes to log
;	26-Jun-94 (SLF) - add FTPONLY keyword
;       27-Jun-94 (SLF) - add /nomore switch to prstr calls (avoid hang w/interctive)  
;       29-Jun-94 (SLF) - call file_append for every message for real time monitoring
;        8-Feb-95 (SLF) - add call to mk_pix (show_pix and http support)
;       20-feb-95 (SLF) - procede even if cnum=lastcnum 
;       28-APR-95 (SLF) - force pasnum > 1 < 4
;        9-May-95 (SLF) - create FITS and GIF composites 
;       11-May-95 (SLF) - changed output name to sfdYYMMDDHHMM.xxx 
;                         where xxx={fits,gif,...}
;       22-jan-97 (SLF) - simplify this ancient code to just use SFD files...
;                         (in the spirit of single point maintenence)
;-
; -------- testing modes: inhibit ftp and search of log -------------
ftptest=keyword_set(ftptest)				; check cadence / sites
tstop=keyword_set(tstop)				; pause during tests
testing=keyword_set(testing) or keyword_set(tstop)
noftp=keyword_set(noftp) or testing 			; dont send out tests!!
ignore_log=keyword_set(ignore_log) or testing		; 
; -------------------------------------------------------------------
start_time = systime(1)
status = 1
if not keyword_set(mail_users) then $
	mail_users = '$MAIL_CRONJOBS,nitta,freeland@isass2'
;
if testing then begin
   outdir =  '/0p/ftp/pub/daily_forecast/test'
   message,/info,"Testing - output to: " + outdir
   message,/info,"Inhibiting ftp and use of last_log"
endif else outdir = '/0p/ftp/pub/daily_forecast'

logfil = concat_dir(outdir, 'last_run.log')
if (n_elements(indir) eq 0) then indir = [getenv('DIR_SITE_NEWDATA'), getenv('DIR_SITE_NEWDATA2')]       ;the reformatted data directories
if (n_elements(infil) eq 0) then infil = newfiles(/sfd,/last)
;
pfiles = file_list(outdir, 'x*')
file_purge, pfiles, /bydate, keep=30			; slf,increased cadence
;
pfiles = file_list('$ys/site/logs', 'forecast.log*')
file_purge, pfiles, /bydate, keep=10
;
;--------------------- Search for images ------------------------------
;
if not ftptest and (1-keyword_set(ftponly)) then begin	; ****************************************

rd_roadmap, infil, rmap
tim2orbit, rmap, saa=saa, tim2night=tim2night
filtb = gt_filtb(rmap)
res = gt_res(rmap)
ss=where((filtb eq 3) and (res eq 1) and (not saa) and (tim2night gt 1),count)

if count eq 0 then begin
    message,/info,"no images satisfy criteria, returning.."
    return
endif    
rd_xda, infil, ss(count-1), index, data

;   slf - 9-may-1995 (SLF) - create GIF and FITS versions of composite
outfits=get_logenv('DIR_HTTP_IMAGES_FITS')

if not file_exist(outfits) or outfits eq '' then begin
   message,/info,"Problem with FITS output directory..." 
endif else begin
   outfile_fits=concat_dir(outfits,'sfd' + str_replace(ex2fid(anytim2ex(index)),'.','') + '.fits')
   message,/info,"Creating FITS file: " + outfile_fits
   sxt2fits,index, data, outfil=outfile_fits
   ftp_copy2sites,outfile_fits
endelse

;
;---------------- Save the images ------------------------------
;
; get the Yohkoh logo
logo = bytarr(512,512)
openr, lun, concat_dir(outdir, 'logo'), /get_lun
readu, lun, logo
free_lun, lun
ss_logo = where(logo gt 0)
;
; ----------------- create show_pix file -------------------------
outpix=data
outpix(ss_logo) = bytscl(logo(ss_logo),min=20,top=max(outpix))
ptemp=!d.name
set_plot,'z'
wdef,image=outpix,/zbuff
loadct,3
stretch,255,0
tvscl,outpix
tvlct,r,g,b,/get
outpix=tvrd()
set_plot,ptemp
mk_pix,outpix,r,g,b, subdir='new_data',file='Last_SXT_Image', text= 	$
   ['Most Recent SXT Composite Image','',				$
     sxt_img_summary(index),'',					$
     rd_tfile('$DIR_SXT_DOC/composite.txt')],/replace
delvarx,outpix
; ---------------------------------------------------------------

; derive output file names (historic, xFID=xray, xcFID=xray composite)
fids = ex2fid( anytim2ex(index) )
filnams = ['xc'+fids]
if testing then filnams='t'+filnams	; slf, prepend 't' for test images
outfil = concat_dir(outdir, filnams)
;

data_out=data
info=get_info3(index)
i=0
;
    ; acient code....
    binfo = byte(info)
    header = bytarr(76)+32b
    header(0:56) = binfo(0:56)		;   0 15-NOV-91  22:34:44  FL/H  Open /Al.1  Qrtr Norm  12
    header(59:75) = binfo(57:73)	;    58.0   64x 64
    if (i eq n_elements(dset)) then header(58) = byte('C')
    ;
;   slf, 25-Jun, Scale the logo to the max of the data
    data_out(ss_logo) = bytscl(logo(ss_logo),min=20,top=max(data_out))
;
    print, 'Saving ' + outfil(i)
    file_delete, outfil(i)
    openw, lun, outfil(i), /get_lun
    writeu, lun, header
    writeu, lun, data_out
    free_lun, lun

    x = int2secarr(index)
dummy = max(x, imax)

cnum=1

out = [fmt_tim(index(imax)), !stime, string(cnum)]		;save information on what images were processed
if (not keyword_set(qdebug)) and (1-ftptest) then begin
    file_delete, logfil
    prstr, out, file=logfil
end
endif else begin	; **************************** ftptest *********
   case 1 of
      ftptest: outfil='ftptest_dummy' + strtrim(sindgen(3),2)   
      data_chk(ftponly,/string): outfil=ftponly
      data_chk(ftponly,/defined): begin
	 tbeep
	 existfiles=file_list(outdir,['x*.????'])
         if existfiles(0) eq '' then message,"No files found...
         message,/info,"Select files to ftp..."
         filesss=wmenu_sel(existfiles)
         if filesss(0) eq -1 then message,"No files selected...
         outfil=existfiles(filesss)
      endcase
   endcase
endelse
;
;------------ FTP the images out to the sites --------------------
;
if (keyword_set(noftp)) then return
;
ftp_log = concat_dir(getenv('DIR_SITE_LOGS'), 'forecast_ftp.log')
sites_fil = concat_dir(outdir, 'daily_forecast.sites')
file_delete, ftp_log
;
; slf, add pass number column to data file
sites = rd_tfile(sites_fil,6,nocomment=';')		;need nocomment='&' so that it doesn't act on the #
							;sites = 5xN where N is the number of sites
if keyword_set(ftponly) then begin
   message,/info,"Select sites for ftp..."
   sitesss=wmenu_sel(sites(0,*))
   if sitesss(0) eq -1 then message,"No sites selected..."
   sites=sites(*,sitesss)
   prstr,["Sending files...",'   ' + outfil,'',	$
	  "To FTP Sites....",'   ' + reform(sites(0,*)),''],/nomore
   yesnox,'OK to proceed',ok
   if not ok then begin
      message,/info,"Aborting ftp transmission on request..."
      return
   endif   
endif

nsites = n_elements(sites(0,*))
ss = where(sites(3,*) eq '-') 
if (ss(0) ne -1) then sites(3,ss) = ''		;blank out the - used to mark the columns
;
passno=strtrim(cnum,2)

for i=0,nsites-1 do begin
    ftppass=strpos(sites(4,i),strtrim(cnum,2)) ne -1	;slf allow pass specification
    pinfo=!stime + ' (Pass# ' + passno + ')'
    if ftppass(0) or keyword_set(ftponly) then begin    
       mess = '** FTP ENABLED To: ' + sites(0,i) + ' at ' + pinfo
       message,/info,mess
       file_append, ftp_log, mess
       ftp_result = ''
       if not ftptest then $
	   ftp_copy, outfil, sites(0,i), sites(1,i), sites(2,i), sites(3,i), ftp_result=ftp_result
       ss = where(strmid(ftp_result, 0, 5) ne 'Local')
       if (ss(0) eq -1) then ftp_result = '' else ftp_result = ftp_result(ss)
       file_append, ftp_log, ftp_result
       if (ftp_result(0) ne '') then begin
	   message = ['Yohkoh DAILY_FORECAST IDL Program Mail Message', $
	     'The program encountered a problem when trying to copy the following files:', $
		'   ' + outfil, $
		'to the NODE: ' + sites(0,i) + ' and ACCOUNT: ' + sites(1,i), $
		'   ', $
		'You can manually copy these images using anonymous FTP to isass0 machine', $
		'and setting default to "pub/daily_forecast"', $
		'   ', $
		'The error message was: ', $
		'   ' + ftp_result]
	   mail, message, user=[sites(5,i),mail_users], subj='DAILY_FORECAST Error During FTP'
       end
   endif else begin
       mess = '-- FTP DISABLED To: ' + sites(0,i) +  ' at ' + pinfo
       message,/info,mess
       file_append, ftp_log, mess
   endelse
endfor

;
end_time = systime(1)
run_time = (end_time-start_time)/60.	;minutes
;
logmess=rd_tfile(ftp_log)
message = ['DAILY_FORECAST image transmission has finished for KSC contact: ' + string(cnum), $
		'It took ' + string(run_time) + ' minutes to run', ' ', $
		'The following files were sent: ', '   ' + outfil, ' ', $
		'FTP Log Messages:', ' ', logmess]

prstr, message,/nomore
if (not keyword_set(qdebug)) and (1-keyword_set(nomail)) then $
	mail, message, user=mail_users, subj='DAILY_FORECAST'
;
end
