#!/bin/sh  
# \
exec oagtclsh "$0" "$@"

#
# $Log: not supported by cvs2svn $
# Revision 1.3  1996/11/21 16:27:42  borland
# Added code to implement (re)processing of all charge logs as an option.
# NOT TESTED.
#
# Revision 1.2  1996/11/21 15:44:36  borland
# Renamed some routines with proper APS names.  Added implementation
# of keepGenerations flag for post-processing.
#
# Revision 1.1  1996/11/21 15:14:17  borland
# First version of scripts and procedures to manage time-series data logging
# cron jobs.
#
#

set auto_path [linsert $auto_path 0 /usr/local/oag/apps/lib/$env(HOST_ARCH)]
set auto_path [linsert $auto_path 0 /usr/local/oag/lib_patch/$env(HOST_ARCH)]
APSStandardSetup
APSDebugPath


if [llength $argv]<2 {
    puts stderr "usage: $argv0 {-year <YYYY> -julianDay <JJJ> | -tag <YYYY>-<JJJ>-<MM><DD> | -all 1} \[-positrons {0 | 1}\] \[-targetDir <name>\] \[-force {0 | 1}\] \[-first 1\]"
    exit 1
} 

set args $argv
set all 0
APSParseArguments {all}
if !$all {
    if [catch {eval APSPostprocessChargeFastLog -positrons 1 $args} result] {
        puts stderr "$argv0: $result"
        exit 1
    }
} else {
    set first 1
    set year 1995
    set tmpFile /tmp/[APSTmpString]
    while 1 {
        set dayMax 365
        if [APSLeapYear $year] {
            set dayMax 366
        }
        for {set day 1} {$day<=$dayMax} {incr day} {
            set jday [format %03ld $day]
            if {[llength [glob -nocomplain ChargeFast-$year-$jday-????.gz]]==0} {
                if {!$firstFile} {
                    puts stderr "postprocessChargeFastLog: No file for $day/$year--stopping"
                    exit
                }
                continue
            }
            if [catch {exec sddsprocess -nowarning /home/helios/oagData/monitoring/polarity/polarity.sdds \
                         -match=column,Tag=$year-$jday-???? $tmpFile \
                         -process=Polarity,first,%s \
                         -define=param,Rows,n_rows,type=long} result] {
                puts stderr "postprocessChargeFastLog: $result"
            }
            if [catch {APSGetSDDSParameter -fileName $tmpFile -parameter Rows} rows] {
                puts stderr "postprocessChargeFastLog: $rows"
            }
            if !$rows {
                set positrons 1
            } else {
                if [catch {APSGetSDDSParameter -fileName $tmpFile -parameter Polarity} polarity] {
                    puts stderr "postprocessChargeFastLog: $polarity"
                }
                set positrons [expr $polarity==-1?0:1]
            }
            if [catch {exec APSPostprocessChargeFastLog -year $year -julianDay $day \
                         -first $firstFile -positrons $positrons -force 1} result] {
                puts stderr "postprocessChargeFastLog: $result"
            }
        set firstFile 0
        }
        incr year
    }
}
