Viewing contents of file '../idllib/ssw/allpro/add_synop.pro'
;+
; Project     :	HESSI
;
; Name        :	ADD_SYNOP()
;
; Purpose     :	Adds a record to synoptic database
;
; Explanation :	
;
; Syntax      : ADD_SYNOP, DEF, STATUS
;
; Inputs      :	DEF = This is an anonymous structure containing
;		      tags defined by DEF_SYNOP
;
; Opt. Inputs :	None.
;
; Outputs     :	STATUS = logical value representing
;		whether or not the operation was successful, where 1 is
;		successful and 0 is unsuccessful.
;
; Opt. Outputs:	None.
;
; Keywords    : ERR    = error string (blank if no errors).
;               REPLATE = set to replace a record if it already exists
;
; Restrictions:	None.
;
; Side effects:	If input catalog number is already in DB, then input
;               DEF will replace current entry, otherwise it
;               will be added with a new catalog number.
;
; Category    :	HESSI Synoptic Analysis Database
;
; Written     :	Dominic Zarro, SM&A/GSFC, 8 May 1999
;
; Contact     : dzarro@solar.stanford.edu
;-

	pro add_synop, def,status, err=err,replace=replace,verbose=verbose

	on_error, 1
        err=''
	status = 0 
        defsysv,'!priv',3
        verbose=keyword_set(verbose)

;
;  Check the input parameters
;
        if datatype(def) ne 'STC' then begin
         err= 'SYNTAX:  STATUS = ADD_SYNOP(DEF)'
         message,err,/cont & return
        endif

@check_synop

;
; Check if identical entry already in the database
; If input catalog number gt 0 and different entry found in DB then replace it
;
        replacing=0
        if def.cat_num ge 0 then begin
         get_synop,def.cat_num,db_def,err=err,/quiet
         if err eq '' then begin
          if match_struct(def,db_def,exclude='date_mod') then begin
           if verbose then $
            message,'identical synoptic entry already in Database',/cont
          endif
          if keyword_set(replace) then replacing=1 else begin
           message,'use /replace to replace existing entry',/cont
           goto,done
          endelse
         endif
        endif

; Check each of the structure components 

        tdef=def
        tdef.filename=trim(def.filename)
        if tdef.filename eq '' then begin
         err='FILENAME field is missing'
         goto,done
        endif

        if (def.date_obs le 0.) then begin
         err='DATE_OBS field (TAI format) is missing'
         goto,done
        endif

        tdef.date_obs=double(str_format(tdef.date_obs))
        tdef.date_end=double(str_format(tdef.date_end))
        tdef.source=trim(tdef.source)
        tdef.sub_class=trim(tdef.sub_class)
        tdef.class=trim(tdef.class)
        tdef.type=trim(tdef.type)


; Optional pointer to HESSI event database 

        tdef.event_num = long(tdef.event_num)

;
;  Open the database for write access.
;
	dbopen, 'synop', 1
;
;  Find the largest catalog number currently in the database, and add one
;  to it or, if replacing, delete the old entry and give old number to new entry
;

	n_entries = db_info('entries','synop')
	if n_entries eq 0 then cat_num = 0L else begin
         if replacing then begin
          cat_num=tdef.cat_num 
          entries = dbfind('cat_num='+trim(long(cat_num))+',deleted=n',/silent)
          if !err ne 0 then begin
           for i=0,n_elements(entries)-1 do dbupdate, entries(i), 'deleted', 'y'
          endif else begin
           err='Could not replace old entry'
           goto,done
          endelse
         endif else begin
          dbext, -1, 'cat_num', cat_nums
	  cat_num = max(cat_nums) + 1L
         endelse
	endelse
;
;  Add the record to the database.
;
        date_mod=double(str_format(anytim2tai(!stime)))
	dbbuild,cat_num,tdef.filename,tdef.event_num,tdef.date_obs,tdef.date_end,$
         tdef.xcen,tdef.ycen,tdef.roll,$
         tdef.xsize,tdef.ysize,tdef.source,$
         tdef.type,tdef.class,tdef.sub_class,'n',date_mod,$
         status=status
	if status eq 0 then begin
         err = 'Write to synoptic database was unsuccessful'
         goto,done
	endif
;
;  Update the catalog number in the structure and signal success.
;
	def.cat_num = cat_num
	status = 1

done:	dbclose

	return & end