#!/usr/bin/perl
#
# format_temp.pl --> extracts apv temperature (cf. format_apv.sh)

# ****************
# * Declarations *
# ****************

# Number of days in month of normal year & leap year

    @month1 = ( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
    @month2 = ( 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );


# *************
# * Main Loop *
# *************

MainLoop:

    while ( defined( $line = <STDIN> ) ) {

# Get next upload of data

        next if ( $line !~ /Upload/ );

# Extract number of samples and starting scan number

        @fields = split( /\s+/, $line );
        $nfields = @fields;
        next if ( $nfields != 11 );

        $nsamples = @fields[ 2 ];
        $scan     = @fields[ 10 ];

# Get date & time for first scan from next data line

        $line = <STDIN>;
        @fields = split( /\s+/, $line );
        $nfields = @fields;
        next if ( $nfields != 8 );

        $time = @fields[ 5 ];
        $date = @fields[ 6 ];

        @fields = split( /\//, $date );
        $month = @fields[ 0 ];
        $day   = @fields[ 1 ];
        $year  = @fields[ 2 ];

        @fields = split( /:/, $time );
        $hour = @fields[ 0 ];
        $min  = @fields[ 1 ];
        $sec  = @fields[ 2 ];

# If not first scan, increment time to match scan

        if ( $scan == 0 ) {
            $line = <STDIN>;
            $scan = 1;
        }
        while ( $scan > 1 ) {
            inc_time();
            $scan--;
        }

# Skip blank line

        $line = <STDIN>;

# Read each data line

        for ( $i = 0; $i < $nsamples; $i++ ) {

            $data = <STDIN>;
            chomp( $data );
            goto MainLoop if ( length( $data) != 28 );
            goto MainLoop if ( $data !~ /\./ );

# Ignore data if battery voltage < 10 volts

            @fields = split( /\s+/, $data );
            $battery = @fields[ 1 ];
            if ( $battery >= 10 ) {

# Write out data line appending time information

                $month = "0" . $month if ( length( $month ) < 2 );
                $day   = "0" . $day   if ( length( $day )   < 2 );
                $hour  = "0" . $hour  if ( length( $hour )  < 2 );
                $min   = "0" . $min   if ( length( $min )   < 2 );
                $sec   = "0" . $sec   if ( length( $sec )   < 2 );

                print ( $year, "/", $month, "/", $day, " ",
                        $hour, ":", $min,   ":", $sec, " ",
                        $data, "\n" );

            }

# Increment time by one second

            inc_time();
            
        }
    }


# ******************************
# * Increment Time by 1 Second *
# ******************************

sub inc_time {
    $sec++;
    if ( $sec >= 60 ) {
        $sec = 0;
        $min++;
        if ( $min >= 60 ) {
            $min = 0;
            $hour++;
            if ( $hour >= 24 ) {
                $hour = 0;
                $day++;
                if ( ( $year % 4 ) != 0 ) {
                    $ndays = @month1[ $month - 1 ];
                } else {
                    $ndays = @month2[ $month - 1 ];
                }
                if ( $day > $ndays ) {
                    $day = 1;
                    $month++;
                    if ( $month > 12 ) {
                        $month = 1;
                        $year++;
                    }
                }
            }
        }
    }
}

