#!/usr/bin/perl -w use Time::Local; use File::Basename; local $MYFULLNAME = $0; local $MYNAME = basename($0); local $LOG_PREFIX = "[$MYNAME]:"; # Read in the environment variables. local $data = $ENV{"DATA"}; local $comout = $ENV{"COMOUT"}; local $maparea = $ENV{"MAPAREA"}; local $run = $ENV{"RUN"}; local $ruc_output_dir = $comout . "/" . $maparea . "/" . $run; local $mdates_old = "$data/mdates.$run.$maparea.old"; local $fourhour_old = "$data/fourhour.old"; local $mdates_ruc = "$data/mdates.ruc"; # Print out informational note print < " . $HoJ{$key}[0] . "-" . $HoJ{$key}[1] . "\n"; } if ( ! exists ( $HoJ{$new_key} )) { # Add the new cycle to the hours hash. $HoJ{$new_key} = [$new_cycle, $new_date]; print $LOG_PREFIX . " Processing new cycle: " . $HoJ{$new_key}[0] . "-" . $HoJ{$new_key}[1] . "\n"; open (OUTFILE,"> $mdates_ruc") || die "$LOG_PREFIX Error opening file $mdates_ruc: $!\n"; print OUTFILE $model; $count = 0; foreach $key (sort keys %HoJ) { if ( $count == 0 ) { # Store the oldest forecast cycle. &print_and_close( $fourhour_old, $HoJ{$key}[0] . "-" . $HoJ{$key}[1], 1 ); } else { # Update the model dates file. print OUTFILE ":" . $HoJ{$key}[0] . "-" . $HoJ{$key}[1]; # Add the cycle to the symlink hash. $symlinkname = "hr" . $count; if ( -e $symlinkname ) { unlink $symlinkname || die "$LOG_PREFIX Error removing symlink $symlinkname: $!\n"; } # Create the link to point to it's cycle symlink ($HoJ{$key}[0], $symlinkname); print "Doing symlink $HoJ{$key}[0], $symlinkname\n"; # Update the link text file with the symlink cycle &print_and_close( ${symlinkname} . ".cycle", $HoJ{$key}[0], 0 ); } $count++; } print OUTFILE "\n"; &close_file(OUTFILE, $mdates_ruc); } else { print "$LOG_PREFIX The new cycle($new_cycle) already exists, no need to update model dates and symlinks.\n"; } # cd to the working directory: &goto_dir($data); print "$LOG_PREFIX Finished $MYNAME.\n"; exit 0; # Open a specified file, print a string to it, then close it. sub print_and_close() { my ($filename, $line, $new_line) = @_; open (OUT, "> $filename") || die "$LOG_PREFIX Error opening file $filename: $!\n"; print OUT "$line"; if ($new_line == 1) { print OUT "\n"; } &close_file(OUT, $filename); } # Read in the most recent model date file. sub get_dates() { my ($datefile) = @_; open (INFILE, "< $datefile") || die "$LOG_PREFIX Error opening file $datefile: $!\n"; $line = ; chomp $line; &close_file(INFILE, $datefile); return $line; } # Close an open file. sub close_file() { my ($filehandle, $filename) = @_; close ($filehandle) || die "$LOG_PREFIX Error closing file $filename: $!\n"; } # Determine the day of the year for a given date. sub get_day_of_year() { my ($cycle, $date) = @_; my ($day_of_year, $year, $time, $mm, $dd, $yy); ($mm, $dd, $yy) = ( $date =~ /(\d{2})(\d{2})(\d{2})/ ); $year = $yy + 2000; $time = timegm(0, 0, $cycle, $dd, $mm - 1, $year - 1900); $day_of_year = (gmtime $time)[7]; return $year, $day_of_year; } # Create a key to be used in a hash. sub set_key() { my ($year, $day, $cycle) = @_; my ($key); $key = sprintf("%04d%03d%02d", $year, $day, $cycle); return $key; } # Change to a specified directory. sub goto_dir() { my ($dirname) = @_; chdir ("$dirname") || die "$LOG_PREFIX Can't cd to $dirname: $! \n"; }