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

    while ( defined( $line = <STDIN> ) ) {
        if ( $line =~ "/2003  " ) {
            @fields = split( /\s+/, $line );
            $nfields = @fields;
            if ( $nfields >= 8 ) {
                $time           = @fields[ 3 ];
                $date           = @fields[ 4 ];
                $oxygen_uml     = @fields[ $nfields - 3 ];
                $oxygen_percent = @fields[ $nfields - 2 ];
                $temperature    = @fields[ $nfields - 1 ];
                if ( ( $time           =~ /:/ ) &&
                     ( $date           =~ /\// ) &&
                     ( $oxygen_uml     =~ /\./ ) &&
                     ( $oxygen_percent =~ /\./ ) &&
                     ( $temperature    =~ /\./ ) ) {
                    @datesplit = split( /\//, $date );
                    $month = @datesplit[ 0 ];
                    if ( length( $month ) < 2 ) {
                        $month = "0" . $month;
                    }
                    $day   = @datesplit[ 1 ];
                    if ( length( $day ) < 2 ) {
                        $day = "0" . $day;
                    }
                    $year  = @datesplit[ 2 ];
                    if ( length( $time ) < 8 ) {
                        $time = "0" . $time;
                    }
                    $oxygen_mgl = $oxygen_uml / 32;
                    write;     # see format below
                }
            }
        }
    }

format STDOUT =
@>>>/@>/@> @>>>>>>>  @>>>>> @>>>>> @>>>>>  @#.###
$year, $month, $day, $time, $oxygen_uml, $oxygen_percent, $temperature, $oxygen_mgl
.

