;  $Id: htplot.pro,v 1.1 2024/10/08 19:35:48 nathan Exp $
;  $Log: htplot.pro,v $
;  Revision 1.1  2024/10/08 19:35:48  nathan
;  contents of SATPLOT_2.3.tar.gz
;
;  Revision 2.3 2024/09/09 00:00:00 penteado
;  Updating satplot package to V2.3, added documentation
;+
; htplot.pro
;
; PURPOSE:      plot height-time files that have been output by satplot.pro.
;
; USAGE:        htplot, 'satplot__20100806_120000__20100809_113000__pa58_d5_A.ht'
;               file name breakdown:
;                'satplot__' with double underscore
;                yyyymmdd date of interval start
;                single underscore
;                hhmmss time of interval start
;                double underscore
;                yyyymmdd date of interval end
;                single underscore
;                hhmmss time of interval end
;                double underscore
;                'pa' stands for position angle
;                value of position angle can be 1-3 digits
;                single underscore
;                'd' stands for delta or width, can by 1-3 digits
;                single underscore
;                'A' or 'B' stands for STEREO-A or STEREO-B
;                '.ht' file name extension stands for height time.
;
; CONSTRAINTS:  ht filename must conform to filename specification as output by 
;               satplot.pro, see USAGE (above) for example.
;               Requires Solarsoft.
; 
; WRITTEN BY:   Jeffrey.R.Hall@jpl.nasa.gov
;               Paulett.Liewer@jpl.nasa.gov
;               2010-2012
;               From embedded code in satplot.pro, this version is stand-alone.
;-
FUNCTION tick_string_function,axis,index,value
 ; Plotter tick function to return correct string per tickmark.

 ;-------------------------------------------------------------------------------------------------------
 ; Kluge to force major tick marks to midnight instead of noon as per julian date convention.
 ; See matching (*SELF.time_ptr)-0.5d in redraw routine.
 ; Re-correct time forward by 12 hours.
 value=value+0.5d
 ;-------------------------------------------------------------------------------------------------------

 CALDAT,value,month,day,year,hour,minute,sec
 ; Fix an odd problem where sometimes time is one minute early.
 minute=CEIL(minute)
 IF (minute GE 59) THEN BEGIN
  minute=0
  hour=hour+1
 ENDIF
 ; Pad the minute string with a zero, if necessary.
 IF (minute LT 10) THEN minute='0'+STRTRIM(minute,2) ELSE minute=STRTRIM(minute,2)
 IF ((hour EQ 0 OR hour EQ 24) AND minute EQ 0) THEN BEGIN
  ; Date.
  tick_string=STRTRIM(month,2)+'/'+STRTRIM(day,2)+'/'+STRMID(STRTRIM(year,2),2,2)
 ENDIF ELSE BEGIN
  ; Time.
  tick_string=STRTRIM(hour,2)+':'+minute
 ENDELSE
 RETURN,tick_string
END

; Example input filename:  satplot__20101004_120000__20101010_120000__pa70_d10_A.ht
PRO htplot, ht_filename

 ; Read some information that is embedded in the filename.
 position_angle=(STRSPLIT(ht_filename,'_',/EXTRACT))[5]
 position_angle=STRMID(position_angle,2,STRLEN(position_angle)-1)
 delta=(STRSPLIT(ht_filename,'_',/EXTRACT))[6]
 delta=STRMID(delta,1,STRLEN(delta)-1)
 spacecraft=(STRSPLIT(ht_filename,'_',/EXTRACT))[7]
 spacecraft=STRMID(spacecraft,1,STRLEN(spacecraft)-1)

 ; Read the height-time file.
 SPAWN,'wc -l '+ht_filename,lines
 text=STRARR(lines)
 OPENR,lun,ht_filename,/GET_LUN
 READF,lun,text
 FREE_LUN,lun
 ndx=WHERE(STRMID(text,0,1) NE '#',npoints)
 data=text[ndx]

 curwin=!D.WINDOW
 WINDOW,/FREE
 ERASE,!D.N_COLORS-1

 IF (npoints EQ 0) THEN BEGIN
  ; No tiepoints, update plot with this message.
  XYOUTS,0.5,0.5,'NO TIEPOINTS',ALIGN=0.5,CHARSIZE=5,COLOR=0,/NORMAL
  WSET,curwin
 ENDIF
 ;-------------------------------------------------------
 ; Plot all the tiepoints.
 ;-------------------------------------------------------
 IF (npoints EQ 0) THEN BEGIN
  RETURN
 ENDIF
 column=LONARR(npoints)
 rowrow=LONARR(npoints)
 actual_time_values=STRARR(npoints)
 actual_jd_values=DBLARR(npoints)
 observed_angle=DBLARR(npoints)
 x_points=LONARR(npoints)
 y_points=LONARR(npoints)
 FOR i=0,npoints-1 DO BEGIN
  data_string_elements=STRSPLIT(data[i],' ',/EXTRACT)
  actual_time_values[i]=data_string_elements[1]
  observed_angle[i]=data_string_elements[0]
  column[i]=data_string_elements[N_ELEMENTS(data_string_elements)-2]
  rowrow[i]=data_string_elements[N_ELEMENTS(data_string_elements)-1]
;  x_points[i]=SELF.x1_margin+((column[i]-SELF.x_start_value)*SELF.zoom_value)+(SELF.zoom_value/2)
;  y_points[i]=SELF.y1_margin+((rowrow[i]-SELF.y_start_value)*SELF.zoom_value)+(SELF.zoom_value/2)
  x_points[i]=column[i]
  y_points[i]=rowrow[i]
 ENDFOR
; valid_ndx=WHERE((x_points GE SELF.x1_margin) AND (x_points LE (SELF.plot_x_pixels+SELF.x1_margin)) $
;             AND (y_points GE SELF.y1_margin) AND (y_points LE (SELF.plot_y_pixels+SELF.y1_margin)),valid_count)
; IF (valid_count GT 0) THEN BEGIN
;  x_points=x_points[valid_ndx]
;  y_points=y_points[valid_ndx]
  ; Plot tiepoints over jplot.
  PLOTS,x_points,y_points,PSYM=7,/DEVICE

   ; Plot the actual FITS times from which each jpixel was derived.
   FOR which_point=0,npoints-1 DO BEGIN
;    observed_angle[which_point]=(*SELF.observed_angle_master_ptr)[rowrow[which_point]]
    actual_time_jd=ANYTIM2JD(actual_time_values[which_point])
    actual_jd_values[which_point]=actual_time_jd.(0)+actual_time_jd.(1)
   ENDFOR

   ;-------------------------------------------------------------------------------------------------------
   ; Kluge to force major tick marks to midnight instead of noon as per julian date convention.
   ; Artificially set time back by 12 hours.
   actual_jd_values=actual_jd_values-0.5d
   ;-------------------------------------------------------------------------------------------------------

   xmin=MIN(actual_jd_values)-((MAX(actual_jd_values)-MIN(actual_jd_values))*0.1)
   xmax=MAX(actual_jd_values)+((MAX(actual_jd_values)-MIN(actual_jd_values))*0.1)
   ymin=MIN(observed_angle)-((MAX(observed_angle)-MIN(observed_angle))*0.1)
   ymax=MAX(observed_angle)+((MAX(observed_angle)-MIN(observed_angle))*0.1)

   ;-------------------------------------------------------------------------------------------------------
   ; Kluge to force major tick marks to midnight instead of noon as per julian date convention.
   ; Re-correct time forward by 12 hours.
   title_jd=xmin+0.5d
   ;-------------------------------------------------------------------------------------------------------

   CALDAT,title_jd,title_month,title_day,title_year,title_hour,title_minute
   IF title_hour LT 10 THEN title_hour='0'+STRTRIM(title_hour,2) ELSE title_hour=STRTRIM(title_hour,2)
   IF title_minute LT 10 THEN title_minute='0'+STRTRIM(title_minute,2) ELSE title_minute=STRTRIM(title_minute,2)
   title_datetime=STRTRIM(title_month,2)+'/'+STRTRIM(title_day,2)+'/'+STRTRIM(title_year,2)+' '+title_hour+':'+title_minute
   PLOT,[actual_jd_values],[observed_angle],TITLE=title_datetime+$
    ', PA '+position_angle+', D '+delta+', '+spacecraft,$
    XTITLE='Date_Obs',$
    YTITLE='Elongation (degrees)',CHARSIZE=1.5,PSYM=7,$
    XTICKFORMAT='tick_string_function',XRANGE=[xmin,xmax],YRANGE=[ymin,ymax],XSTYLE=1,YSTYLE=1,$
    COLOR=0,BACKGROUND=!D.N_COLORS-1
   IF (npoints GT 1) THEN BEGIN
    time_sorted_ndx=SORT(actual_jd_values)
    time=actual_jd_values[time_sorted_ndx]
    height=observed_angle[time_sorted_ndx]
    slope = (REGRESS( (time - time[0]), height, CONST=offset, /DOUBLE ))[0]
    OPLOT, [time[0],time[npoints-1]], [offset,(time[npoints-1]-time[0])*slope+offset],LINESTYLE=1,COLOR=0
   ENDIF
   WSET,curwin
; ENDIF
END

