PRO singer01

; Thomas J. Spirock, tom@bbso.njit.edu
; Big Bear Solar Observatory, New Jersey Institute of Technology

; Edited 01AUG06, Thomas J. Spirock
; Edited 01AUG27, Thomas J. Spirock
; Edited 01AUG29, Thomas J. Spirock
; Edited 01AUG30, Thomas J. Spirock
; Edited 01SEP16, Guo Yang

; #######################################################################
; # Files this program will write.										#
; #######################################################################

; YYMMMDD_fd_halpha.sav		- An IDL save file containing the diagnostic vectors.
; YYMMMDD_fd_halpha.ps		- A post script file plotting the desired diagnostic vectors.
; YYMMMDD_fd_halpha.mpg		- The MPEG movie.
; YYMMMDD_fd_halpha.txt		- Log file for error diagnostics.
; log.txt					- Log file for error diagnostics.

; #######################################################################
; # Functions this program uses.										#
; #######################################################################

; ALIGNOFFSET.PRO ( written by Jungchul Chae )
; SHIFT_SUB.PRO ( written by Jungchul Chae )
; READFITS.PRO

; #######################################################################
; # Initialization.														#
; #######################################################################

; Set the day, relative to the day when the program is run, that the images should be from.
;  0 == today, 1 == yesterday, etc...
; Due to the weather condition here, we might not observe everyday, so

; Please change this value to the value that you need. Guo Yang
; Suggestion: please validate time on every machine, it should be very easy
; to establish a time server for the this LAN. Guo Yang

how_many_days_ago = 1

; Rebin the 2k x 2k images to 512 x 512.
xy = 512

; Display messages while programs runs?
display = 'y'

; Set the host name where the orignal data is from, by Guo Yang
data_host = '192.9.200.5'

; Set the addresses to send diagnostic email messages to.
; After change this to some address here and connect this computer
; to the internet, please change the use_address1 to 'y'

; change the email addresses to the ones you would use and change the use_address varialbes to 'y'
; then the program will send email to these people during the processing Guo Yang

email_address1 = 'tom@bbso.njit.edu'
email_address2 = 'vayur@bbso.njit.edu'

use_address1 = 'n'
use_address2 = 'n'

; Set minimum and maximum levels for the display.
min_display = 0.0
max_display = 2.6

; Set program run parameters.

; Use auto_ftp and mail_on_error and mail_on_startup to 
; control whether transfer the final product to the website
; and send mails on error and startup. Guo Yang

use_unsharp_mask = 'y'
auto_make_mpeg = 'y'
auto_delete_jpgs = 'y'
mail_when_finished = 'n'
save_diagnostics = 'y'
auto_ftp = 'n'
mail_on_error = 'n'
mail_on_startup = 'n'
write_running_log = 'y'

; fd_movies_02 - is the directory the program will write the JPG etc.. files.
; /home/httpd/html/Research/Halpha/fd_movies/ - is the base directory for the images
;  available on the web. 
; This feature has been disabled now since we
; don't have website now Guo Yang.

; Note: Don't put the final slash in the directory path because the MPEG encode program
;  automatically adds the last slash.

temp_directory = '/solar3/guest1/halpha_movie/fd_movies'

; Change this directory to your specific website Guo Yang
www_directory = '/website/htdocs'

; Set values for the correlation derivative, the mean derivative and the offset limits.
; All these parameters are optimized for BBSO, please experiment and get
; better value here. Guo Yang

cor_der_limit = 0.30
mean_der_limit = 0.04
offset_limit = 25.0

; Set the value for the step size in the FOR loop:
;  Step = 1 if the images are taken every 60 sec.
;  Step = 2 if the images are taken every 30 sec.
step = 1

; Set the correct file size in bytes.
correct_file_size = 4123584

; Build the image type message.
f4 = 'H-ALPHA'

; Set the unsharp mask characteristics.
alpha = 5
beta = 0.6

      
; #######################################################################
; # Make the name for the files written by the program.					#
; #######################################################################

; This is a general temporary log file for error diagnostic. The current date can not
;  be used since it has not been determined yet.
name_today = temp_directory + '/log'

; #######################################################################
; # Save the starting time.												#
; #######################################################################

t = SYSTIME ( )
h_start = STRMID ( t, 11, 2 )
m_start = STRMID ( t, 14, 2 )
s_start = STRMID ( t, 17, 2 )

; #######################################################################
; # Write the running log file for error checking.						#
; #######################################################################

IF ( write_running_log EQ 'y' ) THEN BEGIN

  OPENW, 1, name_today + '.txt'

  PRINTF, 1, 'Finished saving the stating time. Start determinig the current date.'

  CLOSE, 1

ENDIF

; #######################################################################
; # Determine the current date.											#
; #######################################################################

temp = SYSTIME ( 0 )

year = STRMID ( temp, 22 ,2 )
month = STRLOWCASE ( STRMID ( temp, 4, 3 ) )
day = STRMID ( temp, 8, 2 )
hour = STRMID ( temp,  11, 2 )
minute = STRMID ( temp, 14, 2 )
second = STRMID ( temp, 17, 2 )

; #######################################################################
; # Write the running log file for error checking.						#
; #######################################################################

IF ( write_running_log EQ 'y' ) THEN BEGIN

  OPENW, 1, name_today + '.txt'

  PRINTF, 1, 'Finished determining the current date. Start determining yesterdays date.'

  CLOSE, 1

ENDIF

; #######################################################################
; # Determine yesterday's date.											#
; #######################################################################

IF ( day GT 1 ) THEN BEGIN

  ; Note the program is set up to make a movie from images taken ONE day previous to
  ;  when the program is executed.

  ; If the day of the month is greater than one then subtract one.

  day = day - how_many_days_ago
  day = STRING ( FORMAT = '(I2.2)', day )
   
ENDIF ELSE BEGIN

  ; If the day of the month equals one then determine that last day of the previous month.

  CASE month OF

    'jan' : BEGIN

      year = year - 1  
      month = 'dec'
      day = 31
      day = STRING ( FORMAT = '(I2.2)', day )
      
    END

    'feb' : BEGIN
  
      month = 'jan'
      day = 31
      day = STRING ( FORMAT = '(I2.2)', day )
        
    END

    'mar' : BEGIN
  
      month = 'feb'
      day = 28
      day = STRING ( FORMAT = '(I2.2)', day )
   
    END

    'apr' : BEGIN
  
      month = 'mar'
      day = 31
      day = STRING ( FORMAT = '(I2.2)', day )
   
    END

    'may' : BEGIN
  
      month = 'apr'
      day = 30
      day = STRING ( FORMAT = '(I2.2)', day )
   
    END

    'jun' : BEGIN
  
      month = 'may'
      day = 31
      day = STRING ( FORMAT = '(I2.2)', day )
   
    END

    'jul' : BEGIN
  
      month = 'jun'
      day = 30
      day = STRING ( FORMAT = '(I2.2)', day )
   
    END

    'aug' : BEGIN
  
      month = 'jul'
      day = 31
      day = STRING ( FORMAT = '(I2.2)', day )
   
    END

    'sep' : BEGIN
  
      month = 'aug'
      day = 31
      day = STRING ( FORMAT = '(I2.2)', day )
   
    END

    'oct' : BEGIN
  
      month = 'sep'
      day = 30
      day = STRING ( FORMAT = '(I2.2)', day )
   
    END

    'nov' : BEGIN
  
      month = 'oct'
      day = 31
      day = STRING ( FORMAT = '(I2.2)', day )
   
    END

    'dec' : BEGIN
  
      month = 'nov'
      day = 30
      day = STRING ( FORMAT = '(I2.2)', day )
   
    END

  ENDCASE

ENDELSE

; #######################################################################
; # Write the running log file for error checking.						#
; #######################################################################

IF ( write_running_log EQ 'y' ) THEN BEGIN

  OPENW, 1, name_today + '.txt'

  PRINTF, 1, 'Finished determining yesterdays date. Start sending email that program started.'

  CLOSE, 1

ENDIF

; #######################################################################
; # Send an email that the program started.								#
; #######################################################################

IF ( mail_on_startup EQ 'y' ) THEN BEGIN

  f85 = temp_directory + '/temp_mail.txt'

  OPENW, 1, f85

  PRINTF, 1,  'The Singer Full Disk Movie program for ' + year + month + day + ' started at ' + hour + ':' + minute + ':' + second + '.'

  CLOSE, 1

  subject = 'Full Disk Movie Started'
  
  address = email_address1

  IF ( use_address1 EQ 'y' ) THEN BEGIN

    f86 = 'mail -s "' + subject + '" ' + address + ' < ' + f85

    SPAWN, f86 

  ENDIF

  address = email_address2

  IF ( use_address1 EQ 'y' ) THEN BEGIN
  
    f86 = 'mail -s "' + subject + '" ' + address + ' < ' + f85

    SPAWN, f86

  ENDIF

ENDIF

; #######################################################################
; # Write the running log file for error checking.						#
; #######################################################################

IF ( write_running_log EQ 'y' ) THEN BEGIN

  OPENW, 1, name_today + '.txt'

  PRINTF, 1, 'Finished sending email that program started. Start making a numerical month value.'

  CLOSE, 1

ENDIF

; #######################################################################
; # Make a numerical "month" variable for the data directory.			#
; #######################################################################

CASE month OF

  'jan' : BEGIN
  
    month2 = '01'
  
  END

  'feb' : BEGIN
  
    month2 = '02'
  
  END
  
  'mar' : BEGIN
  
    month2 = '03'
  
  END
  
  'apr' : BEGIN
  
    month2 = '04'
  
  END
  
  'may' : BEGIN
  
    month2 = '05'
  
  END
  
  'jun' : BEGIN
  
    month2 = '06'
  
  END
  
  'jul' : BEGIN
  
    month2 = '07'
  
  END
  
  'aug' : BEGIN
  
    month2 = '08'
  
  END
  
  'sep' : BEGIN
  
    month2 = '09'
  
  END
  
  'oct' : BEGIN
  
    month2 = '10'
  
  END
    
  'nov' : BEGIN
  
    month2 = '11'
  
  END
  
  'dec' : BEGIN
  
    month2 = '12'
  
  END
  
ENDCASE

; #######################################################################
; # Write the running log file for error checking.						#
; #######################################################################

IF ( write_running_log EQ 'y' ) THEN BEGIN

  OPENW, 1, name_today + '.txt'

  PRINTF, 1, 'Finished making a numerical month value. Start looking for the data files.'

  CLOSE, 1

ENDIF

; ######################################################################
; first we transfer the data to the temporary directory
; edited by Guo Yang
; ######################################################################


OPENW, 1, temp_directory + '/data_ftp.txt'
PRINTF, 1, '#!/bin/sh -f'
PRINTF, 1, 'cd ' + temp_directory + '/data'
PRINTF, 1, 'ftp -in << @'
PRINTF, 1, 'open ' + data_host
PRINTF, 1, 'user lg lg'
PRINTF, 1, 'binary'
PRINTF, 1, 'cd ' + month2 + day ; make such a variable
PRINTF, 1, 'mget *full.fit'
PRINTF, 1, 'get ' + month2 + day + 'dark.fts'
PRINTF, 1, 'get ' + month2 + day + 'flat.fts'
PRINTF, 1, 'bye'
PRINTF, 1, '@'
CLOSE, 1

SPAWN, 'chmod +x ' + temp_directory + '/data_ftp.txt'
SPAWN, 'exec ' + temp_directory + '/data_ftp.txt' 

; #######################################################################
; # Find the data files.												#
; #######################################################################

; Check if there is any data to be processed Guo Yang
data_name = temp_directory + '/data/' + month2 + day + '*full.fit'
f2 = FINDFILE ( data_name, COUNT = number_of_data_files )

data_name = f2

; If the number of data files is zero after checking the four directories
;  then no data files were found.
IF ( number_of_data_files EQ 0 ) THEN BEGIN

  IF ( display EQ 'y' ) THEN PRINT, ' No data files were found.'

  IF ( mail_on_error EQ 'y' ) THEN BEGIN

    f85 = temp_directory + '/temp_mail.txt'

    OPENW, 1, f85

    PRINTF, 1,  'Cold not find DATA images for ' + year + month + day + '.'

    CLOSE, 1

    subject = 'HSOS Halpha Movie Error'
  
    address = email_address1

    IF ( use_address1 EQ 'y' ) THEN BEGIN
    
      f86 = 'mail -s "' + subject + '" ' + address + ' < ' + f85

      SPAWN, f86

    ENDIF

  ENDIF

  STOP

ENDIF

; #######################################################################
; # Write the running log file for error checking.						#
; #######################################################################

IF ( write_running_log EQ 'y' ) THEN BEGIN

  OPENW, 1, name_today + '.txt'

  PRINTF, 1, 'Finished looking for the data files. Start looking for the dark file.'

  CLOSE, 1

ENDIF

; #######################################################################
; # Find the dark image.												#
; #######################################################################

; When the dark image is initially taken and calculated, it is saved in:
;  /arch3/calib/YYYYMMDD/kx4/*halph_fd*.fts
; Then, when the daily backup is run, it is copied to:
;  /archX/YYMMDD//calib/kx4/*halph_fd*.fts

; Check the /arch3 directory first.
; Please change the name the dark frame, Guo Yang
dark_name = temp_directory + '/data/' + month2 + day + 'dark.fts'
f2 = FINDFILE ( dark_name, COUNT = number_of_dark_files )

dark_name = f2 ( 0 )

; If the number of dark files is zero after checking the four arch directories
;  then no dark files were found.

IF ( number_of_dark_files EQ 0 ) THEN BEGIN

  IF ( display EQ 'y' ) THEN PRINT, ' No dark files were found.'

  IF ( mail_on_error EQ 'y' ) THEN BEGIN

    f85 = temp_directory + '/temp_mail.txt'

    OPENW, 1, f85

    PRINTF, 1,  'Cold not find the DARK image for ' + year + month + day + '.'

    CLOSE, 1

    subject = 'HSOS Halpha Movie Error'
  
    address = email_address1

    IF ( use_address1 EQ 'y' ) THEN BEGIN
    
      f86 = 'mail -s "' + subject + '" ' + address + ' < ' + f85

      SPAWN, f86

    ENDIF

  ENDIF

  STOP

ENDIF

; #######################################################################
; # Write the running log file for error checking.						#
; #######################################################################

IF ( write_running_log EQ 'y' ) THEN BEGIN

  OPENW, 1, name_today + '.txt'

  PRINTF, 1, 'Finished looking for the dark file. Start looking for the flat field image.'

  CLOSE, 1

ENDIF

; #######################################################################
; # Find the flat field image.											#
; #######################################################################

; When the flat image is initially taken and calculated, it is saved in:
;  /arch3/calib/YYYYMMDD/kx4/*halph_ff*.fts
; Then, when the daily backup is run, it is copied to:
;  /archX/YYMMDD//calib/kx4/*halph_ff*.fts

; Check the /arch3 directory.
; Change the name of the flat field frame
flat_name = temp_directory + '/data/' + month2 + day + 'flat.fts'
f2 = FINDFILE ( flat_name, COUNT = number_of_flat_files )

flat_name = f2 ( 0 )

; If the number of data files is zero after checking the four arch directories
;  then no data files were found.

IF ( number_of_flat_files EQ 0 ) THEN BEGIN

  IF ( display EQ 'y' ) THEN PRINT, ' No flat field files were found.'

  IF ( mail_on_error EQ 'y' ) THEN BEGIN

    f85 = temp_directory + '/temp_mail.txt'

    OPENW, 1, f85

    PRINTF, 1,  'Cold not find the FLAT FIELD image for ' + year + month + day + '.'

    CLOSE, 1

    subject = 'Singer Movie Error'
  
    address = email_address1

    IF ( use_address1 EQ 'y' ) THEN BEGIN
    
      f86 = 'mail -s "' + subject + '" ' + address + ' < ' + f85

      SPAWN, f86

    ENDIF

  ENDIF

  STOP

ENDIF

; #######################################################################
; # Write the running log file for error checking.						#
; #######################################################################

IF ( write_running_log EQ 'y' ) THEN BEGIN

  OPENW, 1, name_today + '.txt'

  PRINTF, 1, 'Finished looking for the flat field image. Start loading the dark image.'

  CLOSE, 1

ENDIF

; #######################################################################
; # Load and prepare the dark image.									#
; #######################################################################

; Load the dark image.
dark = ( READFITS ( dark_name, header, /SILENT ) ) * 1.0

; Determine the size of the dark image to later test the size of the data files.
; Note that these values are used later to check if the data files are of the correct image size.
s48 = SIZE ( dark )
correct_x_size = s48 ( 1 )
correct_y_size = s48 ( 2 )

; Resize the darm image.
dark = CONGRID ( dark, xy, xy )

; Determine the exposure time of the dark image.

; Because we don't have EXPTIME keyword in our fits files head here in
; BAO, I comment this out now. Guo Yang

;dark_exp = FXPAR ( header, 'EXPTIME' )

; #######################################################################
; # Write the running log file for error checking.						#
; #######################################################################

IF ( write_running_log EQ 'y' ) THEN BEGIN

  OPENW, 1, name_today + '.txt'

  PRINTF, 1, 'Finished loading the dark image. Start loading the flat field image.'

  CLOSE, 1

ENDIF

; #######################################################################
; # Load and prepare the flat image.									#
; #######################################################################

; Load and resize the flat image.
flat = ( READFITS ( flat_name, /SILENT ) ) * 1.0
flat = CONGRID ( flat, xy, xy )

; #######################################################################
; # Write the running log file for error checking.						#
; #######################################################################

IF ( write_running_log EQ 'y' ) THEN BEGIN

  OPENW, 1, name_today + '.txt'

  PRINTF, 1, 'Finished loading the flat field image. Start preparing the gain image.'

  CLOSE, 1

ENDIF

; #######################################################################
; # Prepare the gain image.												#
; #######################################################################

; Calculate the gain image.
gain = flat - dark

; Make sure there are no zero or negative values in the gain image.
index = WHERE ( gain LE 0, count )
IF ( count GT 0 ) THEN gain ( index ) = MEAN ( gain )

; #######################################################################
; # Write the running log file for error checking.						#
; #######################################################################

IF ( write_running_log EQ 'y' ) THEN BEGIN

  OPENW, 1, name_today + '.txt'

  PRINTF, 1, 'Finished preparing the gain image. Start preparing the reference image.'

  CLOSE, 1

ENDIF

; #######################################################################
; # Prepare the reference image.										#
; #######################################################################

; Load and resize the reference image.
ref = ( READFITS ( data_name ( 0 ), header, /SILENT ) ) * 1.0
ref = CONGRID ( ref, xy, xy )

; Adjust the dark exposure to match the REFERENCE image.

; Because we don't have EXPTIME keyword in the head in BAO, I comment
; this out now. Guo Yang
;ref_exp = FXPAR ( header, 'EXPTIME' )
;dark_exp_ratio = ( ref_exp / dark_exp )
;dark = dark * dark_exp_ratio

; Calculate the reference image.
ref = ( ref - dark ) / gain

; #######################################################################
; # Write the running log file for error checking.						#
; #######################################################################

IF ( write_running_log EQ 'y' ) THEN BEGIN

  OPENW, 1, name_today + '.txt'

  PRINTF, 1, 'Finished preparing the reference image. Start generating the diagnostic vectors.'

  CLOSE, 1

ENDIF

; #######################################################################
; # Generate diagnostic vectors.										#
; #######################################################################

; Save the correlation of all the images tested.
save_cor1 = FINDGEN ( 1, number_of_data_files )
save_cor1 ( *, * ) = 0.0

; Save the x and y offsets from the alignment.
save_offsets = FINDGEN ( 2, number_of_data_files )
save_offsets ( *, * ) = 0.0

; Indicate which images were included in the movie and which were not.
save_good = FINDGEN ( 1, number_of_data_files )
save_good ( *, * ) = 0.0

; Save the running correlation limit.
save_limit = FINDGEN ( 1, number_of_data_files )
save_limit ( *, * ) = 0.0

; Save the derivative of the correlation.
save_der = FINDGEN ( 1, number_of_data_files )
save_der ( *, * ) = 0.0

; Save the mean of all images.
save_mean = FINDGEN ( 1, number_of_data_files )
save_mean ( *, * ) = 0.0

; Save the derivative of the mean of all images.
save_mean_der = FINDGEN ( 1, number_of_data_files )
save_mean_der ( *, * ) = 0.0

; Save the ratio to correct for changing exposures.

; Because we don't have EXPOSURE infomation in the head, I 
; comment these out for now. Guo Yang
;save_data_exp  = FINDGEN ( 1, number_of_data_files )
;save_data_exp ( *, * ) = 0.0
; Save the ratio to correct for changing exposures.
;save_data_exp_ratio  = FINDGEN ( 1, number_of_data_files )
;save_data_exp_ratio ( *, * ) = 0.0

; #######################################################################
; # Write the running log file for error checking.						#
; #######################################################################

IF ( write_running_log EQ 'y' ) THEN BEGIN

  OPENW, 1, name_today + '.txt'

  PRINTF, 1, 'Finished generating the diagnostic vectors. Start setting the font.'

  CLOSE, 1

ENDIF

; #######################################################################
; # Set the font writing to the display.								#
; #######################################################################

SET_PLOT, 'X'

; Use the PIXMAP key word to use the window in memory only.
WINDOW, 0, XS = 512, YS = 512, /PIXMAP
 
font = '-adobe-courier-bold-r-normal--24-240-75-75-m-150-iso8859-1'
!P.FONT = 0
DEVICE, SET_FONT = font

; #######################################################################
; # Write the running log file for error checking.						#
; #######################################################################

IF ( write_running_log EQ 'y' ) THEN BEGIN

  OPENW, 1, name_today + '.txt'

  PRINTF, 1, 'Finished setting the font. Start the initialization.'

  CLOSE, 1

ENDIF

; #######################################################################
; # Initialization.														#
; #######################################################################

; Increment with every run thru the loop.
ticker1 = 0

; Increment with every image included in the movie.
ticker2 = 0

; Starting values for the correlationa and mean flags.
cor_0 = 100.0
cor_1 = 100.0
mean_0 = 0.0
mean_1 = 0.0

; Set the range for the FOR loop.
start = 0
stop = number_of_data_files - 1

; Set flag for the first run thru the loop.
first_loop = 'y'

; #######################################################################
; # Make the name for the files written by the program.					#
; #######################################################################

name_today = temp_directory + '/' + year + month + day + '_fd_halpha'

; #######################################################################
; # Write the running log file for error checking.						#
; #######################################################################

IF ( write_running_log EQ 'y' ) THEN BEGIN

  OPENW, 1, name_today + '.txt'

  PRINTF, 1, 'Finished making the name for the files to be written. Start the main loop.'

  CLOSE, 1

ENDIF

; #######################################################################
; # Main loop.															#
; #######################################################################

;stop = 10

; Start the loop.
FOR i = start, stop, step DO BEGIN

  ; #########################################################################
  ; # Write the running log file for error checking.						#
  ; #########################################################################

  IF ( write_running_log EQ 'y' ) THEN BEGIN

    OPENW, 1, name_today + '.txt'

    PRINTF, 1, 'Latest I = ' + STRING ( FORMAT = '( I4.4 ) ' ,i ) + '.'
    PRINTF, 1, 'Latest Ticker1 = ' + STRING ( FORMAT = '( I4.4 ) ' ,ticker1 ) + '.'
    PRINTF, 1, 'Latest Ticker2 = ' + STRING ( FORMAT = '( I4.4 ) ' ,ticker2 ) + '.'

    CLOSE, 1

  ENDIF

  ; Remember the last good set of correlations.
  save_last_good_cor_0 = cor_0
  save_last_good_cor_1 = cor_1

  ; Remember the last good set of means.  
  save_last_good_mean_0 = mean_0
  save_last_good_mean_1 = mean_1

  ; Check if the file size is correct before reading the file.

  is_file_size_correct = 'n'

  ; Read the status vector for the current image.
  OPENR, 1, data_name ( i )
  status = FSTAT ( 1 )
  CLOSE, 1

  ; Pull out the file size from the status vector.  
  file_size = status.size

  ; If the file size is correct change the file size flag to 'y'
  IF ( file_size EQ correct_file_size ) THEN is_file_size_correct = 'y'

  ; If the file size flag is 'y' then continue.
  IF ( is_file_size_correct EQ 'y' ) THEN BEGIN ; - #1

    ; Read the current image and header.
    img = ( READFITS ( data_name ( i ), header, /SILENT ) ) * 1.0

    ; Determine the exposure time from the FITS header and save.

    ; Because we don't have EXPOSURE infomation in the header, so 
    ; I comment these out for now. Guo Yang
    ;data_exp = FXPAR ( header, 'EXPTIME' )
    ;save_data_exp ( 0, ticker1 ) = data_exp

    ; Check the x and y dimensions of the image to make sure they are correct.
    ;  If they are not then skip the image.

    s72 = SIZE ( img )
    current_x_size = s72 ( 1 )
    current_y_size = s72 ( 2 )

    IF ( ( current_x_size EQ correct_x_size ) AND ( current_y_size EQ correct_y_size ) ) THEN BEGIN ; - #2

      ; Resize the image.
      img = CONGRID ( img, xy, xy )

      ; Adjust the data exposure to match the REFERENCE image.
      ; Because we don't have EXPOSURE information in the header, I 
      ; comment these out for now. Guo Yang
      ;data_exp_ratio = ref_exp / data_exp    
      ;save_data_exp_ratio ( 0, ticker1 ) = data_exp_ratio

      ; Create the corrected image.
      ; no exposure information
      ; img = ( ( img * data_exp_ratio ) - dark ) / gain
      img = (img - dark) / gain    

      ; Calculate the RA and DEC offsets from the reference image and save..
      offset = ALIGNOFFSET ( img, ref )
      save_offsets ( 0, ticker1 ) = - offset ( 0 )
      save_offsets ( 1, ticker1 ) = - offset ( 1 )

      ; Calculate the total offset in pixels.
      total_offset = SQRT ( ( offset ( 0 ) ^ 2 ) + ( offset ( 1 ) ^ 2 ) )

      ; Align the current image with the reference image.    
      img = SHIFT_SUB ( img, - offset ( 0 ), - offset ( 1 ) )

      ; Remember the previous correlation.
      cor_1 = cor_0

      ; Calculate the current correlation and save.
      cor_0 = ( CORRELATE ( img, ref ) ) * 100.0
      save_cor1 ( 0, ticker1 ) = cor_0

      ; Calculate the derivative of the correlation from the last good
      ;  correlation and save.
      der = cor_1 - cor_0
      save_der ( 0, ticker1 ) = der

      ; Save the previous mean.
      mean_1 = mean_0

      ; Calculate the current mean and save.
      mean_0 = MEAN ( img )
      save_mean ( 0, ticker1 ) = mean_0
 
      ; If clouds - the current mean will drop so the derivative will be positive.
      ; If a flare - the current mean will rise so the derivative will be negative.

      ; Calculate the derivative of the mean and save.
      der_mean = mean_1 - mean_0
      IF ( i EQ 0 ) THEN der_mean = 0.0
      save_mean_der ( 0, ticker1 ) = der_mean  

      IF ( display EQ 'y' ) THEN BEGIN

        PRINT, ''
        PRINT, ' I = ', STRING ( FORMAT = '( I4.4 ) ' ,i )
        PRINT, ' Ticker 1 = ', STRING ( FORMAT = '( I4.4 ) ' ,ticker1 )
        PRINT, ' Ticker 2 = ', STRING ( FORMAT = '( I4.4 ) ' ,ticker2 )
        PRINT, ' Correlation = ', STRING ( FORMAT = '( F6.2 ) ' ,cor_0 )
        PRINT, ' Mean = ', STRING ( FORMAT = '( F6.2 ) ' ,mean_0 )

      ENDIF

      ; Reset the use_image flag.  
      use_image = 'n'

      ; Determine if the image should be included in the movie.
      IF ( der_mean LT mean_der_limit ) THEN BEGIN
    
        ; The derivative of the mean is LESS THAN zero if the image got brighter.

        ; If the current image is brighter than the previous GOOD image the current image
        ;  is ok.

        IF ( total_offset LT offset_limit ) THEN use_image = 'y'

      ENDIF ELSE BEGIN

        ; The derivative of the mean is GREATER THAN zero if the image got dimmer.    

        ;IF ( ( ABS ( der ) LT cor_der_limit ) AND ( total_offset LT offset_limit ) $
        ; AND ( der_mean LT mean_der_limit ) ) THEN use_image = 'y'

        IF ( ( ABS ( der ) LT cor_der_limit ) AND ( total_offset LT offset_limit ) ) THEN use_image = 'y'

      ENDELSE

      ; If the image should be included in the movie prepare and write the JPG file.
      IF ( use_image EQ 'y' ) THEN BEGIN ; - #3

        IF ( display EQ 'y' ) THEN PRINT, ' Good image.'

        ; #########################################################################
        ; # Extract date and time of image for the display.						#
        ; #########################################################################

        ; Grab the date and the time info from the file name.
        date_display = STRMID ( data_name ( i ), 0, 4 )
        time_display = STRMID ( data_name ( i ), 4, 6 )
  
        ; Grab the year, month and day from the date string.

        ; We don't have year in the file name, so fix it! Guo Yang 
        year_display = 2001
        month_display = STRMID ( date_display, 0, 2 )
        day_display = STRMID ( date_display, 2, 2 )
  
        ; Grab the hour and minute from the time string.
        hour_display = STRMID ( time_display, 0, 2 )
        minute_display = STRMID ( time_display, 2 , 2 )
        second_display = STRMID ( time_display, 4, 2 )

        ; #########################################################################
        ; # Convert the numerical month into characters.							#
        ; #########################################################################
    
        CASE month_display OF
  
          '01' : month_display = 'JAN'
          '02' : month_display = 'FEB'
          '03' : month_display = 'MAR'
          '04' : month_display = 'APR'
          '05' : month_display = 'MAY'
          '06' : month_display = 'JUN'
          '07' : month_display = 'JUL'
          '08' : month_display = 'AUG'
          '09' : month_display = 'SEP'
          '10' : month_display = 'OCT'
          '11' : month_display = 'NOV'
          '12' : month_display = 'DEC'

        ENDCASE

        ; #########################################################################
        ; # Build the date and time character set to plot on the image.			#
        ; #########################################################################
   
        f2 = year_display + month_display + day_display + ' ' + hour_display + ':' + minute_display + ':' + second_display

        ; #########################################################################
        ; # Build the file name to save the image.							  	#
        ; #########################################################################
    
        f3 = temp_directory + '/data/singer_' + STRING ( FORMAT = '( I4.4 ) ',ticker2 ) + '.jpg'

        ; ###################################################################################
        ; # Make sure that the ( ( x and y-dim ) / 8 ) == even integer for the MPEG movie.  #
        ; ###################################################################################

        ; Determine the size of the image.
        d9 = SIZE ( img )
        print_x_dim = d9 ( 1 )
        print_y_dim = d9 ( 2 )

        ; Calculate the values closest to the dimension that is nearest to ( ( x and y-dim ) / 8.0 ) == even integer.
        eight_x = ( FLOOR ( print_x_dim / 8.0 ) ) * 8
        eight_y = ( FLOOR ( print_y_dim / 8.0 ) ) * 8

        ; make sure that the dimensions are an even multiple factor of eight.
        IF ( ( eight_x / 8.0 ) / 2.0 ) ne FLOOR ( ( ( eight_x / 8.0 ) / 2.0 ) ) THEN eight_x = eight_x + 8
        IF ( ( eight_y / 8.0 ) / 2.0 ) ne FLOOR ( ( ( eight_y / 8.0 ) / 2.0 ) ) THEN eight_y = eight_y + 8
  
        ; Resize the image so that the ( ( x and y-dim ) / 8.0 ) == even integer.
        img = CONGRID ( img, eight_x, eight_y )

        ; #########################################################################
        ; # Apply the unsharp mask to the i-image.								#
        ; #########################################################################
    
        IF ( use_unsharp_mask EQ 'y' ) THEN BEGIN

          ; Apply unsharp masking to the i-image
          img = img - ( SMOOTH ( img, alpha, /EDGE ) ) * beta
          img = img - MIN ( img )
	   
        ENDIF

        ; #########################################################################
        ; # Normalize the image.													#
        ; #########################################################################
  	  
        img = img / mean ( img )

        ; #########################################################################
        ; # Save the current image and display in the memory window.				#
        ; ######################################################################### 
	  
        save_img = img

        tvscl, img > ( min_display ) < ( max_display )

        ; #########################################################################
        ; # Determine the position to write the title wording on the image. 		#
        ; #########################################################################

        ; The size of the image.
        s7 = SIZE ( img )
        s7x = s7 ( 1 )
        s7y = s7 ( 2 )
    
        ; Calculate the position so the image name tag is centered left to right.     
        ; The size of the image tag is 224 pixels.
        delta2 = ( ( s7x - 224 ) / 2.0 )

        ; #########################################################################
        ; # Write the title and date / time stamps on the image.					#
        ; #########################################################################
      
        ; The date and time stamp is 268 pixles long.
	    ; Calculate the position so the date/time stamp is centered left to right.
        delta1 = ( ( s7x - 268 ) / 2.0 )

	    ; Calculate the position so the image name/type tag is at the top
        delta3 = ( s7y - 22 )

	    ; Write the date/time tag.
        XYOUTS, delta1, 5, f2, CHARSIZE = 2, COLOR = 255, /DEVICE
        ;XYOUTS, 256, 5, f2, CHARSIZE = 2, COLOR = 255, ALIGNMENT = 0.5, /DEVICE
        
	    ; Write the image name/type tag.     	
        XYOUTS, delta2, delta3, f4, CHARSIZE = 2, COLOR = 255, /DEVICE
        ;XYOUTS, 256, delta3, f4, CHARSIZE = 2, COLOR = 255, ALIGNMENT = 0.5, /DEVICE
            
        ; #########################################################################
        ; # Read and save the image.												#
        ; #########################################################################

        ; The image is read from window memory buffer as a 2D image but the WRITE_JPEG function expects
        ;  a 3D color image. So, create an ( X x Y x 3 ) vector and copy the image into each.
   
        ; Read the image from the memory buffer.
        img = TVRD ( TRUE = 0 )

        ; Create the ( X x Y x 3 ) dummy vector the first run thru the loop.
        IF ( first_loop EQ 'y' ) THEN BEGIN

          ; Determine the size of the image.
          s65 = SIZE ( img )
          s65_x = s65 ( 1 )
          s65_y = s65 ( 2 )

          ; Create a three dimensional image.
          temp1 = FINDGEN ( 3, s65_x, s65_y )

          first_loop = 'n'

        ENDIF

        ; Put the image into the three dimensional psudo true color image.
        temp1 ( 0, *, * ) = img
        temp1 ( 1, *, * ) = img	  
        temp1 ( 2, *, * ) = img
	  
        img = temp1

        ; Write the JPEG file.	  
        WRITE_JPEG, f3, img, TRUE = 1

        ; #########################################################################
        ; # Loop logistics.														#
        ; #########################################################################
      
        ; Edit the "good image flag vector" for the current image.
        save_good ( 0, ticker1 ) = 1.0

        ; Increment the "good image" ticker.
        ticker2 = ticker2 + 1

      ENDIF ELSE BEGIN ; - #3

        ; #########################################################################
        ; # If the image is NOT to be included in the movie.						#
        ; #########################################################################
      
        ; If the current image is NOT to be included in the movie, reset the correlation
        ;  and mean flags.  
        cor_0 = save_last_good_cor_0
        cor_1 = save_last_good_cor_1
        mean_0 = save_last_good_mean_0
        mean_1 = save_last_good_mean_1
  
      ENDELSE ; - #3

      ; Increment the "total image" ticker.
      ticker1 = ticker1 + 1

    ENDIF ; - #2

  ENDIF ; - #1
  
ENDFOR

; #######################################################################
; # Write the running log file for error checking.						#
; #######################################################################

IF ( write_running_log EQ 'y' ) THEN BEGIN

  OPENW, 1, name_today + '.txt'

  PRINTF, 1, 'Finished the loop. Starting MPEG function.'

  CLOSE, 1

ENDIF

; #######################################################################
; # Write the mpeg_encode.param file and run the mpeg_encode program.	#
; #######################################################################

IF ( auto_make_mpeg EQ 'y' ) THEN BEGIN
 
  do_how_many = STRING ( FORMAT = '( I4.4 ) ', ticker2 - 1 )

  ; write the mpeg_encode.param file

  f7 = temp_directory + '/data/mpeg_encode.param_singer'

  OPENW, 1, f7

  PRINTF, 1, 'OUTPUT				' + name_today + '.mpg'
  PRINTF, 1, 'INPUT_DIR				' + temp_directory + '/data'
  PRINTF, 1, 'INPUT'
  PRINTF, 1, 'singer_*.jpg		[0000-' + do_how_many + ']'
  PRINTF, 1, 'END_INPUT'
  PRINTF, 1, 'INPUT_CONVERT			*'
  PRINTF, 1, 'PATTERN				IBBBBP'
  PRINTF, 1, 'GOP_SIZE				6'
  PRINTF, 1, 'SLICES_PER_FRAME			2'
  PRINTF, 1, 'BASE_FILE_FORMAT			JPEG'
  PRINTF, 1, 'PIXEL				HALF'
  PRINTF, 1, 'RANGE				5'
  PRINTF, 1, 'PSEARCH_ALG			LOGARITHMIC'
  PRINTF, 1, 'BSEARCH_ALG			SIMPLE'
  PRINTF, 1, 'IQSCALE				8'
  PRINTF, 1, 'PQSCALE				8'
  PRINTF, 1, 'BQSCALE				8'
  PRINTF, 1, 'REFERENCE_FRAME			DECODED'
  PRINTF, 1, 'RCE_ENCODE_LAST_FRAME'

  CLOSE, 1

  ; #################################################################
  ; # Make the MPEG movie.											#
  ; #################################################################

  ; spawn the mpeg_encode.param function

  SPAWN, 'mpeg_encode ' + f7

ENDIF

; #######################################################################
; # Write the running log file for error checking.						#
; #######################################################################

IF ( write_running_log EQ 'y' ) THEN BEGIN

  OPENW, 1, name_today + '.txt'

  PRINTF, 1, 'Finished the MPEG function. Starting FTP function.'

  CLOSE, 1

ENDIF

; #######################################################################
; # Automatically FTP the movie to the Halpha directory					#
; #######################################################################

; since we don't have website yet, so comment out this section now
; turn it on once we have it, but remember to change the correspondant
; file name Guo Yang 0916
; Bao Xingming provide a place to put the final movie so now we put the movie
; there Guo Yang 0917

IF ( auto_ftp EQ 'y' ) THEN BEGIN

  OPENW, 1, temp_directory + '/temp_ftp.txt'

  PRINTF, 1, '#! /bin/csh -f'
  PRINTF, 1, 'cd ' + temp_directory + '/data'
  PRINTF, 1, 'cp ' + year + month + day + '_fd_halpha.mpg current_fd_halpha.mpg'
  PRINTF, 1, 'ftp 192.9.200.45 -in << @'
  PRINTF, 1, 'user halpha halpha'
  PRINTF, 1, 'binary'
  PRINTF, 1, 'cd ' + www_directory
  PRINTF, 1, 'put current_fd_halpha.mpg'
;  PRINTF, 1, 'cd /home/httpd/html/Research/Halpha/fd_movies/20' + year + '/' + month2 + '/'
;  PRINTF, 1, 'put ' + year + month + day + '_fd_halpha.mpg'
  PRINTF, 1, 'bye'
  PRINTF, 1, '@'

  CLOSE, 1

  SPAWN, 'chmod +x ' + temp_directory + '/temp_ftp.txt'
  SPAWN, 'exec '+ temp_directory + '/temp_ftp.txt'

ENDIF

; #######################################################################
; # Write the running log file for error checking.						#
; #######################################################################

IF ( write_running_log EQ 'y' ) THEN BEGIN

  OPENW, 1, name_today + '.txt'

  PRINTF, 1, 'Finished the FTP function. Start deleting the JPG files.'

  CLOSE, 1

ENDIF

; #######################################################################
; # Automatically delete the jpeg files.								#
; #######################################################################

IF ( auto_make_mpeg EQ 'y' ) AND ( auto_delete_jpgs EQ 'y' ) THEN BEGIN

  IF ( display EQ 'y' ) THEN PRINT, ' Deleting the jpg files.'

  f9 = 'rm ' + temp_directory + '/data/singer_*.jpg'
  SPAWN, f9

ENDIF

; #######################################################################
; # Write the running log file for error checking.						#
; #######################################################################

IF ( write_running_log EQ 'y' ) THEN BEGIN

  OPENW, 1, name_today + '.txt'

  PRINTF, 1, 'Finished deleting the JPG files. Start saving the diagnostics.'

  CLOSE, 1

ENDIF

; ###############################################################
; # Save the diagnostic variables.								#
; ###############################################################

IF ( save_diagnostics EQ 'y' ) THEN BEGIN

  ; Trim off the ends of the vectors that are not used.
  save_cor1 = save_cor1 ( * , 0 : ticker1 - 1 )
  save_offsets = save_offsets ( * , 0 : ticker1 - 1 )
  save_good  = save_good ( * , 0 : ticker1 - 1 )
  save_limit = save_limit ( * , 0 : ticker1 - 1 )
  save_der =  save_der ( * , 0 : ticker1 - 1 )
  save_mean =  save_mean ( * , 0 : ticker1 - 1 )
  save_mean_der =  save_mean_der ( * , 0 : ticker1 - 1 )
  
  ; Because we don't have EXPOSURE infomation, I comment this out for now.
  ; Guo Yang
  ;save_data_exp = save_data_exp ( * , 0 : ticker1 - 1 )
  ;save_data_exp_ratio = save_data_exp_ratio ( * , 0 : ticker1 - 1 )

  ; Save the vectors.
  
  ; Note: in the future if we add the exposure information, we can also save
  ; save_data_exp and save_data_exp_ration Guo Yang
  SAVE, save_cor1, save_offsets, save_good, save_limit, save_der, $
   save_mean, save_mean_der, ticker1, ticker2, $
   FILENAME = name_today + '.sav'

ENDIF

; #######################################################################
; # Write the running log file for error checking.						#
; #######################################################################

IF ( write_running_log EQ 'y' ) THEN BEGIN

  OPENW, 1, name_today + '.txt'

  PRINTF, 1, 'Finished saving the diagnostics. Start writing the PS file.'

  CLOSE, 1

ENDIF

; ###############################################################
; # Make a "ps" file plot of the diagnostic variables.			#
; ###############################################################

; Set font to PS format.
!P.FONT = 0

; Size of the paper.
px = 21.6
py = 27.9

; size of the plots.
ix_size = 17
iy_size = 26

; Set the PS file.
SET_PLOT, 'ps'

; Set the DEVICE flags.
DEVICE, BIT = 8, $
  /BOLD, $
  /TIMES, $
  FILENAME = name_today + '.ps', $
  XSIZE = ix_size, $
  YSIZE = iy_size, $
  ENCAPSULATED = 0, $
  LANDSCAPE = 0, $
  XOFF = 2, $
  YOFF = 1
  
; Multiple plot ( 1 x 7 ).
!P.MULTI = [ 0, 1, 7 ]

; Line thickness.
t = 5

; Character size.
t2 = 1.4

; First plot - if each particular images is put in the movie or not.

PLOT, save_good, XRANGE = [ 0, ticker2 - 1 ], YRANGE = [ -0.2, 1.2 ], $
 XSTYLE = 1, YSTYLE = 1, YTITLE = '1 = Image Used', TITLE = 'Good Image Flag - ' + year + month + day + '.', $
 CHARSIZE = t2, THICK = t, XTHICK = t, YTHICK = t, PSYM = 3, YTICKS = 4

; Second plot - the correlation.

PLOT, save_cor1, XRANGE = [ 0, ticker2 - 1 ], YRANGE = [ 98.0, 100.0 ], $
 XSTYLE = 1, YSTYLE = 1, YTITLE = '%', $
 TITLE = 'Correlation', $
 CHARSIZE = t2, THICK = t, XTHICK = t, YTHICK = t, YTICKS = 4

; Third plot - the derivative of the correlation.

PLOT, save_der, XRANGE = [ 0, ticker2 - 1 ], $
 YRANGE = [ -1.0, 1.0 ], XSTYLE = 1, YSTYLE = 1, YTITLE = '%', $
 TITLE = 'Cor Derivative', $
 CHARSIZE = t2, THICK = t, XTHICK = t, YTHICK = t, YTICKS = 4

; Plot the correlation limits as dotted lines.

temp = FINDGEN ( 1, ticker2 )
temp ( *, * ) = cor_der_limit

OPLOT, temp, LINESTYLE = 2
OPLOT, - temp, LINESTYLE = 2

; Fourth plot - the offsets.

PLOT, save_offsets ( 0, * ), XRANGE = [ 0, ticker2 - 1 ], $
 YRANGE = [ -50, 50 ], XSTYLE = 1, YSTYLE = 1, YTITLE = 'Pixles', $
 TITLE = 'RA and DEC Offset', $
 CHARSIZE = t2, THICK = t, XTHICK = t, YTHICK = t, YTICKS = 4

OPLOT, save_offsets ( 1, * ), LINESTYLE = 1

; Plot the offset limits as dotted lines.

temp = FINDGEN ( 1, ticker2 )
temp ( *, * ) = offset_limit

OPLOT, temp, LINESTYLE = 2
OPLOT, - temp, LINESTYLE = 2

; Fifth plot - the mean.

min_y = MIN ( save_mean )
max_y = MAX ( save_mean )

PLOT, save_mean, XRANGE = [ 0, ticker2 - 1 ], $
 YRANGE = [min_y, max_y ], XSTYLE = 1, YSTYLE = 0, $
 YTITLE = 'Counts', $
 TITLE = 'Mean', XTITLE = 'Image Number', $
 CHARSIZE = t2, THICK = t, XTHICK = t, YTHICK = t, YTICKS = 4

; Sixth plot - the derivative of the mean.

PLOT, save_mean_der, XRANGE = [ 0, ticker2 - 1 ], $
 YRANGE = [ -0.1, 0.1 ], XSTYLE = 1, YSTYLE = 1, $
 YTITLE = 'Counts', $
 TITLE = 'Derivative of Mean', $
 CHARSIZE = t2, THICK = t, XTHICK = t, YTHICK = t, YTICKS = 4

; Plot the derivative mean limit as a dotted line.

temp = FINDGEN ( 1, ticker2 )
temp ( *, * ) = mean_der_limit

OPLOT, temp, LINESTYLE = 2

; Seventh plot - the ratio fo correct for chnging exposures.

; Convert the exposures to mili-seconds.

; Because we don't have EXPORSURE information, I comment this section 
; out for now. GUO YANG
;temp = save_data_exp * 1000
;
;min_y = MIN ( temp )
;max_y = MAX ( temp )
;
;PLOT, temp, XRANGE = [ 0, ticker2 - 1 ], $
; YRANGE = [ min_y, max_y ], XSTYLE = 1, YSTYLE = 0, $
; YTITLE = 'mSec', $
; TITLE = 'Exposure.', XTITLE = 'Image Number', $
; CHARSIZE = t2, THICK = t, XTHICK = t, YTHICK = t, YTICKS = 4

DEVICE, /CLOSE
SET_PLOT, 'x'

; #######################################################################
; # Write the running log file for error checking.						#
; #######################################################################

IF ( write_running_log EQ 'y' ) THEN BEGIN

  OPENW, 1, name_today + '.txt'

  PRINTF, 1, 'Finished writing the PS file. Start calculating the program running time.'

  CLOSE, 1

ENDIF

; ###############################################################
; # Calculate the running time of the program.					#
; ###############################################################

; Time the program ended.
t = SYSTIME ( )
h_end = STRMID ( t, 11, 2 )
m_end = STRMID ( t, 14, 2 )
s_end = STRMID ( t, 17, 2 )

; If the programs was started before 0:00 UT but ends after 0:00 UT then add 24 to the ending hour.
IF ( h_end LT h_start ) THEN h_end = h_end + 24

; Determine the total run time in seconds.
starting_seconds = 3600 * ( h_start ) + 60 * ( m_start ) + s_start
ending_seconds = 3600 * ( h_end ) + 60 * ( m_end ) + s_end
run_time = ending_seconds - starting_seconds

; Pull out the hours, minutes and seconds from the total running time.
run_hours = FLOOR ( run_time / 3600.0 )
run_time = run_time - ( run_hours * 3600 )
run_minutes = FLOOR ( run_time / 60.0 )
run_time = run_time - ( run_minutes * 60 )
run_seconds = run_time

; Print out the running time.
IF ( display EQ 'y' ) THEN BEGIN

  PRINT, ' Run time = ' + STRING ( FORMAT = ' ( I2.2 ) ', run_hours ) + ':' $
   + STRING ( FORMAT = ' ( I2.2 ) ',run_minutes ) + ':' $
   + STRING ( FORMAT = ' ( I2.2 ) ', run_seconds )

ENDIF

; #######################################################################
; # Write the running log file for error checking.						#
; #######################################################################

IF ( write_running_log EQ 'y' ) THEN BEGIN

  OPENW, 1, name_today + '.txt'

  PRINTF, 1, 'Finished calculating the program running time. Start sending the email that the program finished.'

  CLOSE, 1

ENDIF

; ###############################################################
; # Send an email when the program is finished.					#
; ###############################################################

IF ( mail_when_finished EQ 'y' ) THEN BEGIN

  f85 = temp_directory + '/temp_mail.txt'

  OPENW, 1, f85

  PRINTF, 1,  'The Singer full disk movie for ' + year + month + day + ' is complete.'

  CLOSE, 1

  subject = 'Singer Movie'
  
  address = email_address1

  IF ( use_address1 EQ 'y' ) THEN BEGIN
    
    f86 = 'mail -s "' + subject + '" ' + address + ' < ' + f85

    SPAWN, f86

  ENDIF

  address = email_address2

  IF ( use_address2 EQ 'y' ) THEN BEGIN
  
    f86 = 'mail -s "' + subject + '" ' + address + ' < ' + f85

    SPAWN, f86

  ENDIF

ENDIF

; #######################################################################
; # Write the running log file for error checking.						#
; #######################################################################

IF ( write_running_log EQ 'y' ) THEN BEGIN

  OPENW, 1, name_today + '.txt'

  PRINTF, 1, 'Finished sending the email that the program finished. Thanks, Bill!!!'
  ;PRINTF, ' Run time = ' + run_hours + ':' + run_minutes + ':' + run_seconds + '.'

  CLOSE, 1

ENDIF

; ###############################################################
; # Thank Bill!!												#
; ###############################################################

!P.MULTI=[0,1,1]

; Thanks, Bill!!

END



