#!/usr/bin/perl
# takes the standard athena file and puts all the data into columns
# usage: ls oc* | imet_data
# output in form *.*.dat
# currently there exists very few safety valvues, if data does not exist
# the program replaces it with 00000 to fill the field 
# last update 7/12/97 laura  
# 3/16/98 Karen changed the lat and long and date to fractions for easier
#         loadability into matlab
# 3/19/98 Checking for empty fields (sortof) in lat, long and gyro and
#         replacing with -9.9....


while (chop($file_in = <STDIN>))	{
	$file_out = $file_in.".v53";


	open (IN, "$file_in") || "cannot open $file_in";
	open (OUT,">$file_out") || "cannot create $file_out";

  	 $i=0;
		while (<IN>)	{
   		$i=$i+1;	 
		 if($i>21)	{
                       if (/002\|/)    {
                                @line_day=split;
                                ($m, $d, $y)=split(/\//,$line_day[1]);
$yd=int(275*$m/9)-int((($m+9)/12))*(1+int(($y-4*int($y/4)+2)/3))+$d-30;

			  } elsif  (/003\|/)    {
				@line_time = split;
				$time = $line_time[1];
                                ($hr, $min, $sec) = split(/:/,$line_time[1]);
		$time=$yd+((($sec/60)+$min)/60+$hr)/24;

			  } elsif (/004\|/)    {
                                @line_gyro = split;
                                $gyro = $line_gyro[1];
			                 if ($gyro != 0) {
                                          $gyro = $line_gyro[1];
                                        } else {($gyro = -9.9)}
 
		       	} elsif (/020\|/)    {
                                @imet = $';
                                @imet = split(/;/);
                                $baro = $imet[1];
                                $swra   } elsif (/030\|/)    {
                                @line_temp = split;
                                $stmp = $line_temp[1];
				$swra = $imet[3];
                                ($a, $humi, $atmp) = split(/ /,$imet[2]);
                    ($a, $last_min, $last_hr, $pr_level) = split(/ /,$imet[4]);
		
		          } elsif (/031\|/)    { 
				@line_cond = split;
				$cond = $line_cond[1];
		
			  } elsif (/030\|/)    {
				@line_temp = split;
				$stmp = $line_temp[1];

	                   } elsif (/032\|/)    {
                                @line_sal= split;
                                $sal = $line_sal[1];

		          } elsif (/011\|/)    {
			        @nmea_data = split(/,/);
                                ($d, @nmea_data) = @nmea_data;
		                $lati = @nmea_data[1];
                                        if ($lati != 0 ) {
                                $lata=substr($lati, 0,2);
                                $latm=substr($lati, 2,7);
                                $lat=$lata+$latm/60 ;
                                        } else {($lat = -9.999999)}
                                $loni = @nmea_data[3];
                                        if ($loni != 0 ) {
                                $lona=substr($loni, 0,3);
                                $lonm=substr($loni, 3,7);
                                $lon=-1*($lona+$lonm/60) ;
                                        } else {($lon = -9.999999)}

 
		          } elsif (/040\|/)    {
				@line_wind = split;
				$wspd = $line_wind[1];
                                $wdir = $line_wind[2];
		
			 } elsif (/034\|/)    {
                                @line_flur = split;
                                $flur = $line_flur[1];


printf OUT "%10.6f%10.6f%11.6f%6.1f%8.4f%8.4f%8.4f%10.2f%10.2f%10.4f%6.1f%10.2f%6.1f%6.1f%6.2f%6.2f\n", $time, $lat, $lon, $gyro, $cond, $stmp, $sal, $baro, $swra, $atmp, $humi,  $flur, $wspd, $wdir, $last_hr, $pr_level;
			}
		}
}
close (<IN>);
close (<OUT>);
}
