;+
;
; FILE :  if present, save variables to an IDL save file.
;	  Will ignore other keywords.
;
; DATA :  The 6 dimensional data array. 
;		For 1x1 experiments :
;		 
;
;
;-

pro uvspsave, 	file=file, data=data, hd=hd, abscissa=abscissa, $
       		start_time=start_time, ytitle=ytitle, $
       		xtitle=xtitle, mtitle=mtitle, type=type 

@uvspsave.common

  ;
  ; If FILE was passed then just save to a file and return
  ; else load into memory
  ;

  if n_elements(file) ne 0 then begin
        comments = [ $
		['SLICE plotting data save: ',                         $
		 'UVSP_DATA(# slice pixel X, # slice pixel Y, 1, 1, # repeats)', $
		 'UVSP_HD experiments header',          		   $
		 'UVSP_TIME zero', 			$
		 'UVSP_START_TIME detector start times',                   $
		 'UVSP_Y_TITLE y-axis string',                            $
		 'UVSP_X_TITLE x-axis string',                            $
		 'UVSP_M_TITLE main title string',                         $
		 'SAVE_TYPE  type of save, this case is "SLICE"'],	   $
		['AREA plotting data save: ',                              $
		 'UVSP_DATA(# pixel X, # pixel Y, 1, 1, # repeats)', $
		 'UVSP_HD experiments header',          		   $
		 'UVSP_TIME time is seconds of day or data points',        $
		 'UVSP_START_TIME detector start times',                   $
		 'UVSP_Y_TITLE y-axis string',                            $
		 'UVSP_X_TITLE x-axis string',                            $
		 'UVSP_M_TITLE main title string(s)',                      $
		 'SAVE_TYPE  type of save, this case is "AREA"'],	   $
		['RASTER MAXIMUMS plotting data save: ',                   $
		 'UVSP_DATA(1, 1, 1, 1, # repeats, # detectors)', $
		 'UVSP_HD experiments header',          		   $
		 'UVSP_TIME time is seconds of day or data points',        $
		 'UVSP_START_TIME detector start times',                   $
		 'UVSP_Y_TITLE y-axis string',                            $
		 'UVSP_X_TITLE x-axis string',                            $
		 'UVSP_M_TITLE main title string(s)',                      $
		 'SAVE_TYPE  type of save, this case is "RASTER MAXS"'],  $
		['RASTER MEANS plotting data save: ',                   $
		 'UVSP_DATA(1, 1, 1, 1, # repeats, # detectors)', $
		 'UVSP_HD experiments header',          		   $
		 'UVSP_TIME time is seconds of day or data points',        $
		 'UVSP_START_TIME detector start times',                   $
		 'UVSP_Y_TITLE y-axis string',                            $
		 'UVSP_X_TITLE x-axis string',                            $
		 'UVSP_M_TITLE main title string(s)',                      $
		 'SAVE_TYPE  type of save, this case is "RASTER MEANS"'],  $
		['GENERIC plotting experiment data save: ',                $
		 'UVSP_DATA(1, 1, 1, 1, # repeats, detectors)', $
		 'UVSP_HD experiments header',          		   $
		 'UVSP_TIME time is seconds of day or data points',        $
		 'UVSP_START_TIME detector start times',                   $
		 'UVSP_Y_TITLE y-axis string',                            $
		 'UVSP_X_TITLE x-axis string',                            $
		 'UVSP_M_TITLE main title string(s)',                    $
		 'SAVE_TYPE  type of save, this case is "GENERIC PLOT"'],  $
		['AVERAGED image data save: ',                $
		 'UVSP_DATA(X pixel size, Y pixel size, det)', $
		 'UVSP_HD experiments header',          		   $
		 'UVSP_TIME zero', 				$
		 'UVSP_START_TIME detector start times',                   $
		 'UVSP_Y_TITLE null', $
		 'UVSP_X_TITLE null', $
		 'UVSP_M_TITLE null', $
		 'SAVE_TYPE  type of save, this case is "AVERAGE"'],  $
		['SEPARATED image data save: ',                $
		 'UVSP_DATA(# X pixels, # Y pixels, 1, 1, repeats, det)', $
		 'UVSP_HD experiments header',          		   $
		 'UVSP_TIME zero', 				$
		 'UVSP_START_TIME detector start times',                   $
		 'UVSP_Y_TITLE null', $
		 'UVSP_X_TITLE null', $
		 'UVSP_M_TITLE null', $
		 'SAVE_TYPE  type of save, this case is "SEPARATE"'],  $
		['UNPROCESSABLE data save: ',                $
		 'UVSP_DATA(# X pixels, # Y pixels, pol, wld, repeats, det)', $
		 'UVSP_HD experiments header',          		   $
		 'UVSP_TIME zero', 				$
		 'UVSP_START_TIME null',                   $
		 'UVSP_Y_TITLE null', $
		 'UVSP_X_TITLE null', $
		 'UVSP_M_TITLE null', $
		 'SAVE_TYPE  type of save, this case is "UNPROCESSABLE"']  $
		]

	case save_type of
	    'SLICE' : begin
			readme = comments(*,0)
			ext = '.SLICE'
		      end
	    'AREA' : begin
			readme = comments(*,1)
			ext = '.AREA'
		      end
	    'RASTER MAXS' : begin
			readme = comments(*,2)
			ext = '.MAX'
		      end
	    'RASTER MEANS' : begin
			readme = comments(*,3)
			ext = '.MEAN'
		      end
	    'GENERIC PLOT' : begin
			readme = comments(*,4)
			ext = '.PLOT'
		      end
	    'AVERAGE' : begin
			readme = comments(*,5)
			ext = '.AVER'
		      end
	    'SEPARATE' : begin
			readme = comments(*,5)
			ext = '.SEPAR'
		      end
	    'UNPROCESSABLE' : begin
			readme = comments(*,6)
			ext = '.UNPROC'
		      end
	     else: return 
	endcase

	print,'** IDL SAVE FILE : ' + file + ext + '  **'
	save, /xdr, /ver, FILE=file+ext, 		$
				uvsp_data, 	$ 	; the data 6 dimensions
				uvsp_hd,	$	; the exp header
				uvsp_time, 	$	; plot times for light 
							;  curves, if 1x1 reps.
				uvsp_start_time,$ 	; exp start in /YOHKOH
				uvsp_y_title,	$	; y-axis title  
				uvsp_x_title,   $	; x-axis title  
				uvsp_m_title,   $	; main title  
				save_type,	$       ; program origin
				readme			; comments
  endif else begin
     ;
     ; Load the current experiment values into memory (common) for 
     ; possible save to file by user.
     ; Values not passed are not applicable to current experiment
     ; and are set to 0 or null.
     ;

     if n_elements(data) eq 0 then data=0
     if n_elements(hd)   eq 0 then hd=0
     if n_elements(abscissa)  eq 0 then abscissa=0
     if n_elements(start_time) eq 0 then start_time=''
     if n_elements(ytitle) eq 0 then ytitle=''
     if n_elements(xtitle) eq 0 then xtitle=''
     if n_elements(mtitle) eq 0 then mtitle=''
     if n_elements(type) eq 0 then type='UNKNOWN'


     ;
     ; Load the plot arrays into memory (common) for possible save 
     ; to file by user.
     ;

     uvsp_data = data                         		; the data
     uvsp_hd   = hd                        		; the header
     uvsp_time = abscissa				; seconds since day
							;  else data points
     uvsp_start_time = start_time 			; utplot /YOHKOH format
     uvsp_m_title = mtitle 				; main title
     uvsp_x_title = xtitle 				; x-axis title
     uvsp_y_title = ytitle  				; y-axis title

     save_type=type                                     ; type of data to save
  endelse

return
end
