; $Id$
;
; Create light-curve plot

; Read data

s_l2 = H5_PARSE('sigorie-level2.h5', /RE)

t = s_l2.HJD._data - 2454416.5D0

F_star = s_l2.F_star._data
F_star_res = s_l2.F_star._data - s_l2.F_star_mean._data - s_l2.F_star_per._data
F_star_mean = s_l2.F_star_mean._data

mag = -2.5*ALOG10(1.D0+F_star_res/F_star_mean)

s_l2_04 = H5_PARSE('sigorie-04-level2.h5', /RE)

t_04 = s_l2_04.HJD._data - 2454416.5D0

F_star_04 = s_l2_04.F_star._data
F_star_res_04 = s_l2_04.F_star._data - s_l2_04.F_star_per._data
dF_star_04 = s_l2_04.dF_star._data

w_04 = 1./dF_star_04^2

F_star_mean_04 = TOTAL(F_star_04*w_04)/TOTAL(w_04)

mag_04 = -2.5*ALOG10(F_star_res_04/F_star_mean_04)

n = N_ELEMENTS(t)
n_04 = N_ELEMENTS(t_04)

; Set up ranges

x_ext = CEIL(MAX(t))

; Do the plot

xs=17.8
ys=5.2

SET_PLOT, 'ps'
DEVICE, FILENAME='fig_residuals.eps', /ENCAPSULATED, $
        XSIZE=xs, YSIZE=ys

!P.CHARSIZE=0.8
!P.FONT=0

common tick_formats, tick_format_x, tick_format_y, tick_format_z

tick_format_x = '(I0)'
tick_format_y = '(F5.2)'

x_0 = 1100
y_0 = 900

dx = 16000
dy = 4000
dyo = 5000

setwin, dx, dy, x_0, y_0, 250

PLOT, [0], [0], /NOERASE, /NODATA, $
      XRANGE=[0,x_ext], $
      YRANGE=[0.035,-0.025], $
      XSTYLE=1, YSTYLE=1, $
      XTITLE='xt', $
      YTITLE='yt', $
      XTICKFORMAT='fonttick', $
      YTICKFORMAT='fonttick', $
      PSYM=3

dt = 0.1

boxcar, t, mag, dt, t_mean, mag_mean, mag_stddev
boxcar, t_04, mag_04, dt, t_04_mean, mag_04_mean, mag_04_stddev

PRINT,'Stddev 8:',INTERPOL(mag_stddev,t_mean,8)
PRINT,'Stddev 9:',INTERPOL(mag_stddev,t_mean,9)
PRINT,'Stddev 10:',INTERPOL(mag_stddev,t_mean,10)
PRINT,'Stddev 11:',INTERPOL(mag_stddev,t_mean,11)
PRINT,'Stddev 12:',INTERPOL(mag_stddev,t_mean,12)
PRINT,'Stddev 13:',INTERPOL(mag_stddev,t_mean,13)
PRINT,'Stddev 14:',INTERPOL(mag_stddev,t_mean,14)
PRINT,'Stddev 15:',INTERPOL(mag_stddev,t_mean,15)

POLYFILL, [t_mean,REVERSE(t_mean)], [mag_mean+0.025-mag_stddev,REVERSE(mag_mean+0.025+mag_stddev)], COLOR=200

OPLOT, t_mean, mag_mean+0.025
OPLOT, t_04_mean, mag_04_mean+0.025, LINESTYLE=2

OPLOT, t, mag, PSYM=3

DEVICE, /CLOSE

; Fix up axis labels

latexify, 'fig_residuals.eps', $
          ['xt','yt'], $
          ['$t$ (HJD) - 2454416.5', '$\hat{m}$ (mag)'], [0.9,0.9], width=xs
