PRO WRITE_HT,Callt,htfile,hdr,r,pa,x,y
;+
; NAME:
;	WRITE_HT
;
; PURPOSE:
;	This procedure writes out the height-time
;	file.  It is used by the movie program and is not intended to be
;	used in a standalone fashion.
;
; CATEGORY:
;	MOVIE
;
; CALLING SEQUENCE:
;	WRITE_HT,Callt,Htfile,Hdr,R,Pa,Feat,X,Y
;
; INPUTS:
;	Callt:	Parameter indicating whether header or observation 
;		information is being written
;		0 = header, 1 = observation
;	Htfile:	String containing the file name to be written to
;	Hdr:	Structure containing the image header information
;	R:	Radius of observation in units of solar radii (float)
;	Pa:	Position angle of observation in degrees (float)
;	X:	Column of observation (pixels)
;	Y:	Row of observation (pixels)
;
;
; SIDE EFFECTS:
;	Appends information to height-time file if existing or opens a new
;	one if none exists.
;
; MODIFICATION HISTORY:
; 	Written by:	Scott Hawley, NRL summer student, July 1996
;	V2  5/3/97	RAHoward,NRL	Combined all writes to ht-file
;	V3  9/30/97	RAHoward,NRL	Defined Version 2 for HT file
;	7/25/01, N.Rich - Add CHECK_PERMISSION for htfile and check first line
;		of htfile for validity
;       10/02/06        Ed Esfandiari - Modified to handle SECCHI headers.
; 
; @(#)write_ht.pro	1.9 07/25/01 :NRL Solar Physics
;
;-
COMMON com_height_time,curr_feat,curr_time,filename,npts,tel,time_tai, $
       radii,posangle,desc,col,row, features


IF (STRPOS(hdr.date_obs,'T') GT 0) THEN BEGIN ; SECCHI hdr (.fts type)
  hdr_date_obs= STRMID(hdr.date_obs,0,STRPOS(hdr.date_obs,'T'))
  hdr_time_obs= STRMID(hdr.date_obs,STRPOS(hdr.date_obs,'T')+1,20)
ENDIF ELSE BEGIN
  hdr_date_obs= hdr.date_obs
  hdr_time_obs= hdr.time_obs
ENDELSE

IF (callt EQ 0)  THEN BEGIN
  redo:
  tmp=''
  first_line=''
  desc = STRARR(100)
  READ, 'Output filename for Height-Time Plot? ', tmp
  htfile = tmp
  filename = htfile
  f = FINDFILE(htfile)

  ok = CHECK_PERMISSION(htfile)
  IF not ok THEN BEGIN
	break_file,htfile,a,dir,name,ext
	htfile = getenv_slash('HOME')+name
  ENDIF

  IF (f(0) NE '')  THEN BEGIN
     openr,lu,htfile,/get_lun
     readf,lu,first_line
     close,lu
     free_lun,lu
     IF STRPOS(first_line,'VERSION') NE -1 THEN BEGIN
     	READ_HT,htfile,tai,rs,pa,comment,tel,curr_feat,col,row,date_obs,time_obs,fd
     	desc = fd
     ENDIF ELSE BEGIN
     	print,'A non-HT file exists with this name...please try again.'
     	GOTO, redo
     ENDELSE
  ENDIF
  print,'Saving ',htfile
ENDIF

OPENW,outfile,htfile,/get_lun,/APPEND

CASE Callt OF
  0: BEGIN
       npts = 0
       PRINTF,outfile,'#VERSION=3'
       PRINTF,outfile,'#DATE-OBS: ',hdr_date_obs
       PRINTF,outfile,'#TIME-OBS: ',hdr_time_obs
       PRINTF,outfile,'#DETECTOR: ',hdr.detector
       PRINTF,outfile,'#FILTER: ',hdr.filter
       IF (hdr.detector eq 'EIT') THEN PRINTF,outfile,'#SECTOR: ',hdr.sector $
                                     ELSE PRINTF,outfile,'#POLAR: ',hdr.polar
       PRINTF,outfile,'#OBSERVER: ',getenv('USER')
       tmp=''
       READ,'Enter feature code (e.g. LE for Leading Edge): ',tmp
       tmp = STRUPCASE(tmp)
       nf = 0
       IF (STRMID(tmp,0,2) EQ 'LE')  THEN nf=1 ELSE $	;Leading Edge
       IF (STRMID(tmp,0,3) EQ 'RHB') THEN nf=2 ELSE $	;Right Hand Boundary
       IF (STRMID(tmp,0,3) EQ 'LHB') THEN nf=3 ELSE $	;Left Hand Boundary
       IF (STRMID(tmp,0,2) EQ 'BW')  THEN nf=4 ELSE $	;Black/White Boundary
       IF (STRMID(tmp,0,3) EQ 'COR') THEN nf=5 ELSE $	;Prominence Core
       IF (STRMID(tmp,0,3) EQ 'DIS') THEN nf=6 ELSE $	;Disconnection Front
       BEGIN
          w = WHERE(desc(10:*) EQ '')	; permit up to 9 predefined codes
          nf = w(0)+10
       ENDELSE
       desc(nf) = tmp
       PRINTF,outfile,'#FEAT_CODE: ',STRTRIM(STRING(nf,format='(i3)'),2),'=',tmp
       curr_feat = nf
       tmp = ''
       PRINT,'Type of images? (difference, running difference, unsharp masking, etc.'
       READ,'If difference, also enter date & time of base frame): ',tmp
       PRINTF,outfile,'#IMAGE_TYPE: ',STRUPCASE(tmp)
       READ,'Comment? ',tmp
       PRINTF,outfile,'#COMMENT: '+tmp
       IF (STRLEN(hdr.filename) GT 15) THEN $ ; secchi hdr
         PRINTF,outfile,'# HEIGHT   DATE     TIME   ANGLE  TEL   FC    COL     ROW' $
       ELSE $
         PRINTF,outfile,'# HEIGHT   DATE     TIME   ANGLE  TEL  FC    COL     ROW'
     END
  
  1: BEGIN
       npts = npts+1
       IF (npts EQ 1) THEN BEGIN
          tel =[hdr.detector]
          time_tai = [UTC2TAI(STR2UTC(hdr_date_obs+' '+hdr_time_obs))]
          posangle = [pa]
          radii = [r]
          features = [curr_feat]
          col = [x]
          row = [y]
       ENDIF ELSE BEGIN
          tel =[tel,hdr.detector]
          time_tai = [time_tai,UTC2TAI(STR2UTC(hdr_date_obs+' '+hdr_time_obs))]
          posangle = [posangle,pa]
          radii = [radii,r]
          features = [features,curr_feat]
          col = [col,x]
          row = [row,y]
       ENDELSE
       IF (STRLEN(hdr.filename) GT 15) THEN BEGIN ; secchi hdr
         PRINT,r,hdr_date_obs,hdr_time_obs,pa,hdr.detector,curr_feat $
              ,x,y,FORMAT='(f6.2,a11,1x,a8,f6.1,a4,i3,2f8.2)'
         PRINTF,outfile,r,hdr_date_obs,hdr_time_obs,pa,hdr.detector, $
               curr_feat,x,y,FORMAT='(f6.2,a11,1x,a8,f6.1,2x,a4,i4,2f8.1)'
       ENDIF ELSE BEGIN
         PRINT,r,hdr_date_obs,hdr_time_obs,pa,hdr.detector,curr_feat $
              ,x,y,FORMAT='(f6.2,a11,1x,a8,f6.1,a3,i3,2f8.2)'
         PRINTF,outfile,r,hdr_date_obs,hdr_time_obs,pa,hdr.detector, $
               curr_feat,x,y,FORMAT='(f6.2,a11,1x,a8,f6.1,2x,a3,i4,2f8.1)'
       ENDELSE
     END
  
ENDCASE
  
FREE_LUN,outfile

RETURN
END
