; $Id$
;
; Create the period distribution plot

; Read data

common cdf, cdf_freq, cdf_n, freq

s2 = H5_PARSE('sigorie-level2.h5', /RE)
sm = H5_PARSE('sigorie-mc.h5', /RE)

HJD = s2.HJD._data

freq = s2.freq._data
period = 1.D0/freq

; Set up the period PDF

freq_mc = sm.freq_mc._data
period_mc = 1.D0/freq_mc

pdf_n = DOUBLE(HISTOGRAM(period_mc, BINSIZE=2E-6, LOCATIONS=pdf_period))

dperiod = pdf_period[1]-pdf_period[0]
pdf_n = pdf_n/sm.n_mc._data/dperiod/1E4

PRINT, 'PDF Scale:', MAX(pdf_n)

; Set up the frequency CDF

freq_mc = sm.freq_mc._data

cdf_period = period_mc[sort(period_mc)]
cdf_n = (FINDGEN(sm.n_mc._data)+1)/sm.n_mc._data

cdf_freq = freq_mc[sort(freq_mc)]

; Work out the "1-sigma" bounds

dfreq = MAX(cdf_freq) - MIN(cdf_freq)
dfreq = FX_ROOT([1,2,3]*1e-5, 'dfreq_func')

period_lo = period*(1 - 0.5*dfreq/freq)
period_hi = period*(1 + 0.5*dfreq/freq)

period_Tow = eval_P(0.5D0*(MAX(HJD)+MIN(HJD)))

; Do the plot

xs=8.5
ys=5.0

SET_PLOT, 'ps'
DEVICE, FILENAME='fig_period_dist.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 = '(F8.5)'
tick_format_y = '(F5.2)'

x_0 = 900
y_0 = 700

dx = 6500
dy = 4000

setwin, dx, dy, x_0, y_0, 250

PLOT, [0], [0], /NODATA, $
      XRANGE=[1.19075d0,1.19095d0], $
      YRANGE=[0,3.5], $
      XSTYLE=5, YSTYLE=5, $
      PSYM=10

i_lo = MIN(WHERE(pdf_period ge period_lo))
i_hi = MAX(WHERE(pdf_period le period_hi))

print,'is:',pdf_period[i_lo],pdf_period[i_hi]

hist_box, pdf_period[i_lo:i_hi],pdf_n[i_lo:i_hi], color=192

PLOT, [0], [0], /NOERASE, /NODATA, $
      XRANGE=[1.19075d0,1.19095d0], $
      YRANGE=[0,3.5], $
      XSTYLE=1, YSTYLE=1, $
      XTITLE='xt', $
      YTITLE='yt', $
      XTICKFORMAT='fonttick', $
      YTICKFORMAT='fonttick', $
      PSYM=10

OPLOT, pdf_period, pdf_n, PSYM=10
OPLOT, period*[1,1], !Y.CRANGE
OPLOT, period_Tow*[1,1], !Y.CRANGE, LINESTYLE=1

DEVICE, /CLOSE

; Fix up axis labels

latexify, 'fig_period_dist.eps', $
          ['xt','yt'], $
          ['$P\ ({\rm d})$', '${\rm d}p/{\rm d}P\ \times\ 10^{-4}\ ({\rm d^{-1}})$'], [0.9,0.9], width=xs
