;+
; Project     : ovsa
;
; Name        : write_ovsa_fits
;
; Purpose     : Write an ovsa FITS file 
;
; Category    : ovsa analysis
;
; Explanation : 
;
; Syntax      : write_ovsa_fits, avg, datasetname, f, $
;               feedinfo, sigma, srcname, tavg, tls, xcen, ycen
;
; Examples    : write_ovsa_fits, avg, 'G0281439_07263',$
;               f, feedinfo, sigma, 'AR8340', tavg, tls, xcen, ycen
;
; Inputs      : avg = data in [channel number,freq,time] format
;               datasetname = data source id
;               f = frequency array
;               feedinfo = antenna number and polarization
;               sigma = errors for avg
;               srcname = source name
;               tavg = time array
;               tls = structure containing time and date
;               xcen = antenna pointing in arcsec from sun centre
;               ycen = antenna pointing in arcsec from sun centre
;              
; Outputs     : IAU standard OVSA FITS file 
;
; Keywords    : None
;                   
; History     : Written 09 December 1999, P. T. Gallagher, NJIT
;
; Contact     : ptg@penumbra.njit.edu
;-


PRO write_ovsa_fits, avg, datasetname, f, feedinfo, sigma,$
                     srcname, tavg, tls, xcen, ycen
		     

;Convert time in 'any' format to CCSDS format 

  date = tls.date

  t = ARR2STR(SEC2HMS(tavg(0)/1000.),/trim)
  t_start = STRMID(t,0,2)+':'+STRMID(t,2,2)+':'+STRMID(t,4,2)
  
  t = ARR2STR(SEC2HMS(tavg(N_ELEMENTS(tavg) - 1)/1000.),/trim)
  t_end = STRMID(t,0,2)+':'+STRMID(t,2,2)+':'+STRMID(t,4,2)

  date_obs = ANYTIM2UTC(date+' '+t_start,/ccsds)
  date_end = ANYTIM2UTC(date+' '+t_end,/ccsds)

;Create IAU format filename
 
  filename = 'ovsa_1-18GHz_sp_'+STRMID(date_obs,0,4)+$
             STRMID(date_obs,5,2)+STRMID(date_obs,8,2)+$
	     '_'+STRMID(date_obs,11,2)+STRMID(date_obs,14,2)+$
	     '.fts'
  
;Create a primary header and write to it.
 
  FXHMAKE, header, /init, /extend, /date
  FXWRITE, filename, header

;Create a binary table extension header for a table with 1 row.

  FXBHMAKE, header, 1, 'OVSA Data', 'Binary table extension'
  FXADDPAR, header, 'FILENAME', filename,''
  FXADDPAR, header, 'ORIGIN','NJIT','Institute where file was written'
  FXADDPAR, header, 'TELESCOP','OVSA','Owens Valley Solar Array'
  FXADDPAR, header, 'SEQ_NUM',datasetname,'Unique number for this dataset'
  FXADDPAR, header, 'DATE_OBS',date_obs,'Start date/time of observation'
  FXADDPAR, header, 'DATE_END',date_end,'End date/time of observation '
  FXADDPAR, header, 'OBJ_ID',srcname,'Object ID'
  FXADDPAR, header, 'XCEN',xcen,'Antenna pointing in arcsec from Sun centre'
  FXADDPAR, header, 'YCEN',ycen,'Antenna pointing in arcsec from Sun centre'
  FXADDPAR, header, 'FREQMIN',1.2,'Min freq in observation (GHz)'
  FXADDPAR, header, 'FREQMAX',18.0,'Max freq in observation (GHz)'

;Create 6 columns 

  FXBADDCOL, acol, header, avg, 'Channel,Freq,Time'
  FXBADDCOL, bcol, header, f, 'Freq/GHz'
  FXBADDCOL, ccol, header, feedinfo, 'Feed number and polarization'
  FXBADDCOL, dcol, header, sigma, 'Errors'
  FXBADDCOL, ecol, header, tavg, 'Time/msec'

;Write out the binary table extension header .

  fxbcreate, unit, filename, header
  
;Write out the data into the 6 pre-defined columns.

  FXBWRITE, unit, avg, acol, 1
  FXBWRITE, unit, f, bcol, 1
  FXBWRITE, unit, feedinfo, ccol, 1
  FXBWRITE, unit, sigma, dcol, 1
  FXBWRITE, unit, tavg, ecol, 1

;Close the binary extension.

  FXBFINISH, unit

END 
 

 
