;+
;PROCEDURE: plastic_beacon_mom_conv
;PURPOSE: Find appropriate calibration files and read in calibration values.
;  This version is used in building CDFs for the beacon data.
;
;INPUT
;PARAMETERS:  sat: 'A' or 'B'
;
;CREATED BY: L. Ellis
;
;LAST MODIFICATION: 10/12/2006
;
;MODIFICATION HISTORY:
;    10/12/2006 Created
;    09/14/2007 LBE: Changed significantly for new calibrations (V07)
;-
PRO plastic_beacon_mom_conv, sat, timestamp
COMMON com_moment

cal_read = 1

; find most recent catalog file
;temp_files = file_search('TempCalibration/*.cal', count = num_files)
temp_files = file_search('$SSW_PLASTIC/data/calibrations/*.cal', count = num_files)
version = 1
catalog_file = ''
FOR ii = 0, num_files-1 DO BEGIN 
    temp_file = file_basename(temp_files[ii])
    temp_version = fix(strmid(temp_file, 21, 2))
    temp_sat = strmid(temp_file, 2, 1)
    IF temp_version GE version AND temp_sat EQ sat THEN BEGIN
        version = temp_version
        catalog_file = temp_files[ii]
    ENDIF 
ENDFOR 

; find calibration file
openr, cat_lun, catalog_file, /get_lun
cat_files_found = 0
cat_files = ['']
finished = 0
line = ''
mom_cal_start = [''] ; this first element will be removed
mom_cal_stop = [''] ; this first elemeent will be removed
WHILE eof(cat_lun) EQ 0 AND finished EQ 0 DO BEGIN ; need this loop to get to MOM
    readf, cat_lun, line
    parts_current = strsplit(line, /extract)
    IF parts_current[0] EQ 'MOM' THEN BEGIN 
        WHILE eof(cat_lun) EQ 0 AND finished EQ 0 DO BEGIN 
            readf, cat_lun, line
            parts_next = strsplit(line, /extract)
            IF parts_next[0] NE 'MOM' THEN BEGIN ; finished MOM section
                finished = 1
                cat_files_found = cat_files_found + 1
                cat_files = [cat_files, parts_current[2]]
                mom_cal_start = [mom_cal_start, parts_current[1]]
                mom_cal_stop = [mom_cal_stop, '3000-01-01T00:00:00.000']
            ENDIF ELSE BEGIN ; current and next are both MOM
                                ;stop
                IF timestamp GE parts_next[1] THEN parts_current = parts_next $ ; skip current file
                ELSE BEGIN 
                    cat_files_found = cat_files_found + 1
                    cat_files = [cat_files, parts_current[2]]
                    mom_cal_start = [mom_cal_start, parts_current[1]]
                    IF stop_time LT parts_next[1] THEN BEGIN ; skip rest of files
                        finished = 1 
                        mom_cal_stop = [mom_cal_stop, 'zzzzzzzzzzzzzzzzzzzzzzz']
                    ENDIF ELSE BEGIN
                        mom_cal_stop = [mom_cal_stop, parts_next[1]]
                        parts_current = parts_next
                    ENDELSE 
                ENDELSE 
            ENDELSE 
        ENDWHILE 
    ENDIF 
ENDWHILE 
IF cat_files_found GT 0 THEN cat_files = cat_files[1:cat_files_found]
close, cat_lun
free_lun, cat_lun

; read calibration file
IF cat_files_found GT 0 THEN BEGIN 
    step_var = dblarr(cat_files_found)
    table_norm = dblarr(cat_files_found, 4) ; D,V,P,H
    geom = dblarr(cat_files_found, 2) ; M,S
    ra_trig_eff = dblarr(cat_files_found, 128, 4) ; 128 lines, esa step, Kristin vel, table vel, efficiency
    vel_groups = dblarr(cat_files_found, 32) ; average of our velocities in ra_trig_eff
    FOR ii = 0, cat_files_found-1 DO BEGIN 
        filename = '$SSW_PLASTIC/data/calibrations/'+cat_files[ii]
        openr, cal_lun, filename, /get_lun
        found_step = 0
        WHILE eof(cal_lun) EQ 0 AND found_step EQ 0 DO BEGIN 
            readf, cal_lun, line
            parts = strsplit(line, /extract)
            IF parts[0] EQ 'step_variable' THEN BEGIN 
                step_var[ii] = double(parts[1])
                found_step = 1
            ENDIF
        ENDWHILE 
        found_table_norm = 0
        WHILE eof(cal_lun) EQ 0 AND found_table_norm EQ 0 DO BEGIN 
            readf, cal_lun, line
            parts = strsplit(line, /extract)
            IF parts[0] EQ 'table_norm_values' THEN BEGIN 
                IF parts[1] NE 'D' THEN BEGIN
                    print, 'Error in reading moments calibration'
                ENDIF ELSE table_norm[ii, 0] = double(parts[2])
                readf, cal_lun, line
                parts = strsplit(line, /extract)
                IF parts[0] NE 'table_norm_values' OR parts[1] NE 'V' THEN BEGIN
                    print, 'Error in reading moments calibration'
                ENDIF ELSE table_norm[ii, 1] = double(parts[2])
                readf, cal_lun, line
                parts = strsplit(line, /extract)
                IF parts[0] NE 'table_norm_values' OR parts[1] NE 'P' THEN BEGIN
                    print, 'Error in reading moments calibration'
                ENDIF ELSE table_norm[ii, 2] = double(parts[2])
                readf, cal_lun, line
                parts = strsplit(line, /extract)
                IF parts[0] NE 'table_norm_values' OR parts[1] NE 'H' THEN BEGIN
                    print, 'Error in reading moments calibration'
                ENDIF ELSE table_norm[ii, 3] = double(parts[2])
                found_table_norm = 1
            ENDIF
        ENDWHILE 
        found_geom_factor = 0
        WHILE eof(cal_lun) EQ 0 AND found_geom_factor EQ 0 DO BEGIN 
            readf, cal_lun, line
            parts = strsplit(line, /extract)
            IF parts[0] EQ 'geom_factor' THEN BEGIN 
                IF parts[1] NE 'S' THEN BEGIN
                    print, 'Error in reading moments calibration'
                ENDIF ELSE geom[ii, 1] = double(parts[2])
                readf, cal_lun, line
                parts = strsplit(line, /extract)
                IF parts[0] NE 'geom_factor' OR parts[1] NE 'M' THEN BEGIN
                    print, 'Error in reading moments calibration'
                ENDIF ELSE geom[ii, 0] = double(parts[2])
                found_geom_factor = 1
            ENDIF
        ENDWHILE 
        found_ra_trig_eff = 0
        WHILE eof(cal_lun) EQ 0 AND found_ra_trig_eff EQ 0 DO BEGIN 
            readf, cal_lun, line
            parts = strsplit(line, /extract)
            IF parts[0] EQ 'ra_trig' THEN BEGIN 
                ra_trig_eff[ii, 0, *] = double(parts[1:4])
                FOR jj = 1, 127 DO BEGIN ; 128 lines total
                    readf, cal_lun, line
                    parts = strsplit(line, /extract)
                    IF parts[0] NE  'ra_trig' THEN BEGIN 
                        print, "unexpected number of lines"
                    ENDIF ELSE ra_trig_eff[ii, jj, *] = double(parts[1:4])
                ENDFOR
                found_ra_trig_eff = 1
            ENDIF
        ENDWHILE
        FOR jj = 0, 31 DO BEGIN 
            vel_groups[ii, jj] = total(ra_trig_eff[ii, (jj*4):(jj*4+3), 2])/4
        ENDFOR 
        close, cal_lun
        free_lun, cal_lun
    ENDFOR 
ENDIF 
IF sat EQ 'A' THEN BEGIN
    IF cat_files_found GT 0 THEN BEGIN 
        mom_cal_start_a = mom_cal_start[1:n_elements(mom_cal_start)-1]
        mom_cal_stop_a = mom_cal_stop[1:n_elements(mom_cal_stop)-1]
        step_var_a = step_var
        table_norm_a = table_norm
        geom_a = geom
        ra_trig_eff_a = ra_trig_eff
        vel_groups_a = vel_groups
    ENDIF 
ENDIF ELSE BEGIN
    IF cat_files_found GT 0 THEN BEGIN 
        mom_cal_start_b = mom_cal_start[1:n_elements(mom_cal_start)-1]
        mom_cal_stop_b = mom_cal_stop[1:n_elements(mom_cal_stop)-1]
        step_var_b = step_var
        table_norm_b = table_norm
        geom_b = geom
        ra_trig_eff_b = ra_trig_eff
        vel_groups_b = vel_groups
    ENDIF 
ENDELSE 

END 
