Viewing contents of file '../idllib/contrib/groupk/mk_foldlc.pro'
;+
; NAME:
;        MK_FOLDLC
;
; PURPOSE:
;        Processes the data structure returned by FIDUCIAL to generate
;        plots of the folded light curves of the HEAO A-1 data.
;
; CATEGORY:
;        HEAO A-1.
;
; OUTPUTS:
;
;        This routine generates postscript files of FOLDED light curve plots of
;        the HEAO A-1 data and then sends these files to the printer
;        defined by the PRINTER environment variable.
;
; COMMON:
;        MK_FOLDLC:  For internal use only.
;
; MODIFICATION HISTORY:
;   February, 1994          H.C. Wen
;   06-FEB-1995              Implemented GET_DATA, added TRN_CUT option
;   08-FEB-1995              Got rid of GETHALVES, simplified to using WHEREGION
;-

pro MK_FOLDLC_

         common MK_FOLDLC, MKFLC_

         LASS2JY   = 230          ;LASS to microJy at 5 keV
                                  ;0.001 LASS flux units = 0.23E-6 Jy at 5 keV

;   Unpack common

         Data      = MKFLC_.data
         trn_cut   = MKFLC_.trn_cut
         srcnum    = MKFLC_.srcnum
         Period    = MKFLC_.Period
         here      = MKFLC_.here

;   Extract relevant tags

         t77i      = data.t77i(here,Srcnum)
         t77f      = data.t77f(here,Srcnum)
         tcorr     = data.tcorr(*,here,Srcnum)
         intensity = data.intensity(here,Srcnum)
         sigma     = data.sigma(here,Srcnum)
         Name      = data.name( Srcnum )

         mode      = data.mode
         sec       = mode/1000.   ;seconds in a bin

         module    = data.module
         CASE module OF
              1    : cm2 = 1650   ;Area of HEAO A-1 module
              2    : cm2 = 1650   ;in CM^2
              3    : cm2 = 1650
              5    : cm2 = 1350
              6    : cm2 = 1350
              7    : cm2 = 1900
              else : message,'Invalid module number:'+string(module)
         ENDCASE

;   Further restrict range of data by transmission cut

         trn_max   = PK_TRNS( Data )
         trn_max   = trn_max( here )
         here_cut  = where( trn_max ge trn_cut, n_ge )
         if n_ge eq 0 then begin
              xmsg, 'No scans have a transmission peak > cutoff',$
                   TITLE=pro_title
              return
         endif

         t77i      = t77i(here_cut)
         t77f      = t77f(here_cut)
         tcorr     = tcorr(*,here_cut)
         intensity = intensity(here_cut)
         sigma     = sigma(here_cut)

;   Make folded light curves

         Units= '[!4l!XJy at 5 keV]'
         tc0  = tcorr(0, *)
         tc1  = tcorr(1, *)
         tcorr= (tc0+tc1)*0.5
         time = double(t77i)+double(t77f)+double(tcorr)

         Flux = (Intensity/cm2/sec)*LASS2JY
         Err  = (Sigma/cm2/sec)*LASS2JY
         FOLD_LC, time, flux, err, name, Period, FLUX_UNITS=units

end


pro MK_FOLDLC


         common MK_FOLDLC, MKFLC_

         d_display = !D.NAME
         Period    = 1.0

;   Define all widget texts

         pro_title = 'Mk_FoldLC'
         err_title = pro_title+' ERROR'
         ret_title = 'Return to '+pro_title+' Main Menu'

         main      = [$
                   'File',$
                   'Options',$
                   'Create Folded Light Curves',$
                   'Quit']

         options   = [$
                   'Display settings',$
                   'Folding Period',$
                   'Transmission cutoff',$
                   'Source number',$
                   'Data region',$
                   ret_title]


;   Define option defaults

         srcnum    = 0                 ;source number
         trn_cut   = 0                 ;Lowest acceptable transmission peak.
         here      = -1                ;data region indices

         YN        = ['No','Yes']
         file      = ''

;   Bring up Main Menu

         REPEAT BEGIN

              rp = XBUTTON(Main,Main,/ROW,$
                           TITLE=pro_title+' Main Menu')

              CASE rp OF
              'File'   : BEGIN
                   ; Restore IDL save session file

                   data1= GET_DATA(OFILE=File1,TITLE=pro_title+' Message')
                   here_NULL = where( TAG_NAMES(data1) eq 'NULL', nNULL )
                   if nNULL eq 0 then begin
                        File=File1
                        data=data1
                        here= lindgen( data.npass )
                   endif
                   END

              'Options': BEGIN
                   repeat begin
                        title_opt = pro_title+' Options'
                        rpopt = XBUTTON(options,options,/COLUMN,$
                                  TITLE=pro_title+' Options Menu')
                        CASE rpopt OF
                        'Display settings': BEGIN
                                 if here(0) eq -1 then begin
                                       t0   = 'N/A'
                                       t1   = 'N/A'
                                 endif else begin
                                       nh   = n_elements(here)
                                       i0   = here(0)
                                       i1   = here(nh-1)
                                       t0   = data.t77i(i0,srcnum)/86400.
                                       t1   = data.t77i(i1,srcnum)/86400.
                                       t0   = arr2str(t0,4)
                                       t1   = arr2str(t1,4)
                                 endelse
                                 setstrs = [$
                                      'IDL .sav file: '+file,$
                                      'Folding Period [hr]: '+arr2str(Period,4),$
                                      'Transmission cutoff: '+arr2str(trn_cut,3),$
                                      'Source number: '+strtrim(srcnum,2),$
                                      'Data region: '+t0+' , '+t1+' [days]']
                                 xmsg,setstrs,TITLE=pro_title+' Settings',/ALIGN
                                 END
                        'Folding Period': BEGIN
                             Period= XQUERY('Enter Folding Period [hrs]:',$
                                       DEFAULT=Period, TITLE=title_opt, $
                                       /LEFT, /TOP)
                             Period=double(Period)
                             END
                        'Transmission cutoff':BEGIN
                             qy_trn    = 'Enter lowest acceptable tranmission '+$
                                         'peak value:'
                             trn_cut   = XQUERY(qy_trn,default=trn_cut,$
                                                 TITLE='Mk_AvgLC Settings')
                             trn_cut   = trn_cut(0)
                             END
                        'Source number': BEGIN
                             srcnum= XQUERY('Enter source number:',$
                                       DEFAULT=srcnum, TITLE=title_opt, $
                                       /LEFT, /TOP)
                             srcnum=fix(srcnum)
                             END
                        'Data region':BEGIN
                             if N_ELEMENTS( Data ) eq 0 then begin
                                  xmsg,['An IDL SAVE session file must first',$
                                        'be selected. (Select the File button',$
                                        'in the Main Menu.)'],$
                                        TITLE=err_title
                             endif else begin
                                  here = WHEREGION( Data.t77i(*,srcnum)/86400.,$
                                            PK_TRNS( Data, srcnum), $
                                            XTITLE='Time [Days]',$
                                            YTITLE='Peak Transmission/Scan for '+$
                                                   Data.name(srcnum), $
                                            TITLE='Distribution of Times' )
                             endelse
                             END
                        ret_title:
                        ENDCASE
                   endrep until rpopt eq ret_title
                   END

              'Create Folded Light Curves':BEGIN
                   if N_ELEMENTS( Data ) eq 0 then $
                        xmsg,'No IDL Save Session file selected.',$
                              TITLE=err_title $
                   else if (here(0) eq -1) then $
                        xmsg,'No data within USER selected region.',$
                             TITLE=err_title $
                   else begin

                        MKFLC_ = { $
                                  Data      : data, $
                                  trn_cut   : trn_cut, $
                                  srcnum    : srcnum,$
                                  Period    : Period,$
                                  here      : here }
                        MK_FOLDLC_
                   endelse
                   END
              'Quit'    : print,'Quitting '+pro_title+'...'
              ENDCASE

         ENDREP Until rp eq 'Quit'
         MKFLC_     = 0
end