#!/usr/bin/perl
#
#  synopticWriter 
#
# Program to split synoptic bulletins into individual reports, then write
# them to a directory structure ~/data/raw/synoptic/yyyymmdd/stn using the 
# station name as the file name. If the station name is not available then 
# the wmoId number will be used as the station name. The additional reports 
# for the hour are appended to file. 
#
#no encoding;
#
# process command line switches
while ($_ = $ARGV[0], /^-/) {
	 shift;
       last if /^--$/;
	     /^(-v)/ && $verbose++;
}
# process input parameters
if( $#ARGV == 1 ) {
	$datadir = $ARGV[ 0 ] ;
	$yyyymm = $ARGV[ 1 ] ;
	die "yyyymm must be 6 in length: $!\n" if( length( $yyyymm ) != 6 ) ;
	$theyear = substr( $yyyymm, 0, 4 ) ;
	$themonth = substr( $yyyymm, 4 ) ;
} else {
	die "usage: synopticWriter datatdir yyyymm < rawSynoptics $!\n" ;
}

# set interrupt handler
$SIG{ 'INT' }  = 'atexit' ;
$SIG{ 'KILL' }  = 'atexit' ;
$SIG{ 'TERM' }  = 'atexit' ;
$SIG{ 'QUIT' }  = 'atexit' ;

# read in station data
if( -e "etc/systns_upc.tbl" ) {
	$sfile = "etc/systns_upc.tbl" ;
} elsif( -e "./systns_upc.tbl" ) {
	$sfile = "./systns_upc.tbl" ;
} else {
	die "Can't find systns_upc.tbl station file.: $!\n" ;
}
open( STATION, "$sfile" ) || die "could not open $sfile: $!\n" ;

while( <STATION> ) {
	if ( s#^(\w{3,6})\s+(\d{4,5}) (.{39})## ) {
		$stnName = $1 ;
		$wmoId = $2 ;
		$stnLongName = $3 ;
	} elsif( s#^\s+(\d{4,5}) (.{39})## ) { # no station name, use wmoId
		$stnName = $1 ;
		$wmoId = $1 ;
		$stnLongName = $2 ;
	} else {
		next ;
	}
	$wmoId = "0" . $wmoId if( length( $wmoId ) == 4 ) ;
	( $Lat, $Lon, $elev ) = split ;
	$Lat = sprintf( "%7.2f", $Lat / 100 ) ;
	$Lon = sprintf( "%7.2f", $Lon / 100) ;

	# set these vars ( $wmoId, $Lat, $Lon, $elev ) 
	$STATIONS{ "$wmoId" } = "$stnName" ;
#	$STNS{ "$wmoId" } = "$Lat $Lon $elev, $stnName, $stnLongName" ;
}
close STATION ;

chdir( "$datadir" ) ;

# Now begin parsing file and decoding observations breaking on cntrl C
$/ = "\cC" ;

# set select processing here from STDIN
START:
while( 1 ) {
	open( STDIN, '-' ) ;
	vec($rin,fileno(STDIN),1) = 1;
	$timeout = 1200 ; # 20 minutes
	$nfound = select( $rout = $rin, undef, undef, $timeout );
	# timed out
	if( ! $nfound ) {
		print "Shut down, time out 20 minutes\n" ;
		atexit() ;
	}
	atexit( "eof" ) if( eof( STDIN ) ) ;

	# Process each line of synoptic bulletins, header first
	$_ = <STDIN> ;
	s#\cC## ;
	s#\cM##g ;
	s#\cA\n## ;
	s#\c^##g ;
 
	s#\d\d\d \n## ;
	s#\w{4}\d{1,2} \w{4} (\d{2})(\d{2})(\d{2}).*\n## ;
	$bday = $1 ;
	$bhour = $2 ;
	$bhour = "23" if( $bhour eq "24" ) ;
	$bmin = $3 ;
	next unless ( $bday && defined( $bhour ) && defined( $bmin ) ) ;
	next if( $bmin > 59 || $bhour > 23 || $bday > 31 ) ;
	# check for valid bulletin times against current time
	$cday = (gmtime())[ 3 ] ;
	$chour = (gmtime())[ 2 ] ;
	# skip bulletins over 24 hours old or in the future
	if( $bday == $cday ) {
		next if( $bhour > $chour ) ;
	} else { #  $bday != $cday, skip over day old reports
 		next if( $bday < $cday -1 ) ;
 		if( $bday > $cday ) {
			next if( $cday != 1 || $bday < 28) ;
		}
 		next if( $bhour < $chour ) ;
	}
	if( s#(AAXX)\s+(\d\d)(\d\d)(\d)?\s*\n## ) {
		$rep_type = $1 ;
		$rday = $2 ;
		$rhour = $3 ;
		if( $4 ) {
			$Windicator = $4 ;
		} else {
			$Windicator = 4 ;
		}
	} elsif( s#(AAXX)\s*\n## ) {
		$rep_type = $1 ;
		$rday = $bday ;
		$rhour = $bhour ;
		$Windicator = 4 ; # default is knots
	} else {
		next ;
	}
	# check for valid times
	next unless ($rday && defined( $rhour )) ;
	next if( $rhour > 23 || $rday > 31 ) ;
	# skip reports over 24 hours old
	$tmpyyyymm = $yyyymm ;
	if( $rday == $cday ) {
		next if( $rhour > $chour ) ;
	} else { #  $rday != $cday, skip over day old reports
		next if( $rday < ( $cday -1 ) ) ;
	 	if( $rday > $cday ) {
			next if( $cday != 1 || $rday < 28) ;
			# cday = 1, reset month and year
			$tmpmonth = sprintf( "%02d", $themonth -1 ) ;
			if( $tmpmonth == 0 ) {
				$tmpmonth = "12" ;
				$tmpyear = 
					sprintf( "%04d", $theyear -1 );
			} else {
				$tmpyear = $theyear ;
			}
			$tmpyyyymm = $tmpyear .  $tmpmonth ;
		}
	 	next if( $rhour < $chour ) ;
	}
	$yyyymmdd = $tmpyyyymm . sprintf( "%02d", $rday ) ;
	# Separate bulletins into reports 
	if( /=\n/ ) {
		s#=\s+\n#=\n#g ;
	} else {
		s#\n# #g ;
	}
	@reports = split( /=\n/ ) ;
	for ( @reports ) { # Process each report in the bulletin
		undef( $stn_name ) ;
		next if( /^\n|NIL/ ) ;
		s#\n# #g ;
		# decode Section 0 to get wmoId for station
		m#^(\d{5})# ;
		$wmoId = $1 ;
		next unless( defined( $wmoId ) ) ;
		# extract Lat, Lon, elev, stn_name
		if( defined( $STATIONS{ "$wmoId" } ) ) {
			( $stn_name ) = $STATIONS{ "$wmoId" }  ;
		} else {
			next ;
		}
		# Prepend stnName, Z time and WindIndicator to report
		$_ = $stn_name . " $yyyymm" . $bday . $bhour . "Z " . "$_" ;
		mkdir( "$yyyymmdd", 0775 ) if( ! -e "$yyyymmdd" ) ;
		chdir( "$yyyymmdd" ) ;
		open( STN, ">>$stn_name" ) ;
		print STN "$_\n" ;
		close STN ;
		chdir( ".." ) ;

	} # end foreach report
} # end while( 1 )
atexit( "eof" );
exit( 0 ) ; #should never get here

# execute at exit
sub atexit
{
local( $sig ) = @_ ;

if( $sig eq "eof" ) {
	print "eof on STDIN --shutting down\n" ;
} elsif( defined( $sig )) {
	print "Caught SIG$sig --shutting down\n" ;
}
exit( 0 ) ;

} #end atexit

