# ctycode.pl # # Parse the code to determine the counties # affected by the NWS statement. # Revised by: Govindakrishnan Kannan since Oct 01, 2003 # Included subroutines for handling RDF, AFMs and utility routines. ##Added by GK - 10/14/2003 $weather_service = "";$time_stamp=""; sub get_county_code { $ctycode = $_[0]; $stcode = substr($ctycode,0,2); $ctynum = substr($ctycode,3,3); ($stcode, $ctynum); } sub parse_county_code { local(@list,@newlist); $code_list = $_[0]; #$code_list = $_; #print ("parse_county_code: the code-list is $code_list \n"); $code_list =~ s/^(.*)-$/$1/; #GK: Splitting across the "-" separator. @elements = split(/-/, $code_list); #Possible patterns include {Class, pattern}: #Class-1: INZ076>079. #Class-2: 083, 084 !^.{6} - prepend code like "INZ" #Class-3: 089>092 - - prepend code like "INZ" #Class-4: 032256 - ^.{6} - added at the last #Class-5 KYZ026 - ^.{6} foreach ( @elements ) { #GK: Matching any string that begins with a non-digit eg:-INZ076>079 if ( /^\D/ ) { # Push fips code on list if KY zone code. $zcode = substr($_,0,3); #GK: Pushing the strings like "INZ". push(@list, $_); } else { if ( /^.{6}$/ ) { push(@list, $_); } else { #GK: Pushing strings like INZ076 push(@list, "$zcode$_"); } } } foreach (@list) { if ( /.{10}/ ) { $zcode = substr($_,0,3); $ctyrange = substr($_,3,7); $start = $ctyrange; $end = $start; #GK: Get the start county number and the end county numbers within, say, INZ076>079 $start =~ s/^(...)\>.../$1/; $end =~ s/^...\>(...)/$1/; #GK: Put the corresponding zone names as INZ09 for ($i = $start; $i <= $end; $i++) { $i = sprintf("%03d",$i); push(@newlist,"$zcode$i"); } } else { push(@newlist,$_); } } @newlist; } sub detect_format { my($source_file) = @_; my($fmt); print ("Detect_format fous file is $source_file \n"); open(SFILE,"/d2-agwx/ldmdata/models/ngm/$source_file") || die "ERROR: could not find $source_file: $!\n"; # Read WMO header line and time stamp. $hline = ; print ("1 - $hline"); $hline = ; print ("2 - $hline"); $time_stamp1 = ; print ("3 - $hline"); $hline = ; print ("4 - $hline"); $fmt = substr($hline,0,3); $hline = ; print ("5 - $hline"); $hline = ; print ("6 - $hline"); $weather_service = ; print("\n\n 7 - weather-service = $weather_service"); $time_stamp = ; print("\n\n 8 - time stamp = $time_stamp"); close(SFILE); print ("Detected the format as $fmt \n"); return ($fmt); } sub parse_fous { my($source_file) = @_; my($format); $format = &detect_format($source_file); print ("FOUS file-format=$format\n"); if($format eq "RDF") { &sub_rdf($source_file); } elsif($format eq "AFM") { &sub_afm($source_file); } else { &sub_pfm($source_file); } } sub sub_rdf { my($fous_file) = @_; my($i); open(INP_MODEL,"/d2-agwx/ldmdata/models/ngm/$fous_file") || die "ERROR: could not find $fous_file: $!\n"; print ("The input file is $fous_file \n"); $i = 0; while ($i <= 6) { ; ++$i; } # Read rest of file into that array global var, such that $_ has the individual element. while () { #print ("out-g var is $_ \n"); if ( /^\w\wZ/ ) { #print ("if-global var is $_ \n"); @code_list = &parse_afm_county_code($_); #&print_array(@code_list); $content = $weather_service."\n"; #print ("header = $header \n"); ## Copy rest of the file until $$ is found. while (($_ = ) && ($_ !~ /^\$\$/)) { $content .= $_; } #print ("content = $content \n"); #GK: Take off the expiry time added as the last element in the list $expire_time = pop(@code_list); print ("expiry time = $expire_time \n"); foreach $ctycode (@code_list) { ($stcode,$ctynum) = &get_county_code($ctycode); if ( $stcode eq "KY" || $stcode eq "IN" || $stcode eq "IL" || $stcode eq "MO" || $stcode eq "TN" || $stcode eq "OH" || $stcode eq "WV" || $stcode eq "AR" || $stcode eq "NC" || $stcode eq "SC" || $stcode eq "VA" || $stcode eq "MS" || $stcode eq "AL" || $stcode eq "GA" || $stcode eq "IA" || $stcode eq "LA" || $stcode eq "PA") { $dest_file = "/d2-agwx/ldmdata/public/parse-rdf/$stcode"."RDF$ctynum"; open(FPTR, ">$dest_file"); print ("dest-file is $dest_file\n"); print FPTR $content; close(FPTR); } } #foreach } #if } #while close(INP_MODEL); } #sub sub sub_afm { my($fous_file) = @_; my($i); open(INP_MODEL,"/d2-agwx/ldmdata/models/ngm/$fous_file") || die "ERROR: could not find $fous_file: $!\n"; $i = 0; while ($i <= 6) { ; ++$i; } # Read rest of file into that array global var, such that $_ has the individual element. while () { #print ("out-g var is $_ \n"); if ( /^\w\wZ/ ) { #print ("if-global var is $_ \n"); @code_list = &parse_afm_county_code($_); #&print_array(@code_list); $content = $weather_service."\n"; #print ("header = $header \n"); ## Copy rest of the file until $$ is found. while (($_ = ) && ($_ !~ /^\$\$/)) { #GK: omit "DATE" string, "UTC" 3hr row. if ( ($_ !~ /UTC 3HRLY/) && ($_ !~ /UTC 6HRLY/) ) { if ( $_ =~ /DATE/) { #print ("b4 sub is $_ \n"); #Replace "DATE" by empty space. $_ =~ s/DATE//; $_ = " ".$_; #print ("after sub is $_ \n"); } $content .= $_; } } print ("content = $content \n"); $expire_time = pop(@code_list); foreach $ctycode (@code_list) { ($stcode,$ctynum) = &get_county_code($ctycode); if ( $stcode eq "KY" || $stcode eq "IN" || $stcode eq "IL" || $stcode eq "MO" || $stcode eq "TN" || $stcode eq "OH" || $stcode eq "WV" || $stcode eq "AR" || $stcode eq "NC" || $stcode eq "SC" || $stcode eq "VA" || $stcode eq "MS" || $stcode eq "AL" || $stcode eq "GA" || $stcode eq "IA" || $stcode eq "LA" || $stcode eq "PA") { $dest_file = "/d2-agwx/ldmdata/public/parse-afm/$stcode"."AFM$ctynum"; open(FPTR, ">$dest_file"); print ("dest-file is $dest_file\n"); print FPTR $content; close(FPTR); } } #foreach } #if } #while close(INP_MODEL); } #sub sub parse_afm_county_code { local(@list,@newlist); $code_list = $_[0]; $code_list =~ s/^(.*)-$/$1/; #GK: Splitting across the "-" separator. @elements = split(/-/, $code_list); #Possible patterns include {Class, pattern}: #Class-1: INZ076>079. #Class-2: 083, 084 !^.{6} - prepend code like "INZ" #Class-3: 089>092 - - prepend code like "INZ" #Class-4: 032256 - ^.{6} - added at the last #Class-5 KYZ026 - ^.{6} foreach ( @elements ) { #GK: Matching any string that begins with a non-digit eg:-INZ076>079 if ( /^\D/ ) { # Push fips code on list if KY zone code. $zcode = substr($_,0,3); #GK: Pushing the strings like "INZ". push(@list, $_); } else { if ( /^.{6}$/ ) { push(@list, $_); } else { #GK: Pushing strings like INZ076 push(@list, "$zcode$_"); } } } foreach (@list) { if ( /.{10}/ ) { $zcode = substr($_,0,3); $ctyrange = substr($_,3,7); $start = $ctyrange; $end = $start; #GK: Get the start county number and the end county numbers within INZ076>079 $start =~ s/^(...)\>.../$1/; $end =~ s/^...\>(...)/$1/; #GK: Put the corresponding zone names as INZ09 for ($i = $start; $i <= $end; $i++) { $i = sprintf("%03d",$i); push(@newlist,"$zcode$i"); } } else { push(@newlist,$_); } } @newlist; } sub sub_pfm { printf ("This sub. still not complete \n"); #exit; } sub print_array { my(@temp) = @_; my($temp, $i); $temp = @temp; $i = 0; print ("^^^^Printing array\n"); while($i < $temp) {#foreach $ele (@temp) { print ("Element is $temp[$i++] \n"); } print ("^^^^^^\n"); } sub get_format { print ("The format is $fmt \n"); return ($fmt); } # Subroutine that takes the FOUS file as argument and returns the list of # all counties within the FOUS file. sub get_county_list { my($fous_file, $arg_st_code, $format) = @_; my(@ret_code_list, @code_list, $ctycode, $stcode, $ctynum, $i); # /** Inserting a delay for some-time **/ #sleep(5); #$format = &detect_format($fous_file); print ("The fous_file is $fous_file, format is $format \n\n"); open(INP_MODEL,"/d2-agwx/ldmdata/models/ngm/$fous_file") || die "ERROR: could not find $fous_file: $!\n"; $i = 0; while ($i <= 6) { ; ++$i; } # /** For this version, we need the model (RDF/AFM/PFM) and the county numbers # within that FOUS-file; call a sub-routine such that, it returns the list of all # county-numbers within that FOUS file. # **/ #Input: FOUS file; #output: list of all county numbers. # Read rest of file. while () { if ( /^\w\wZ/ ) { #print ("global var is $_ \n"); @code_list = &parse_county_code($_); pop(@code_list); foreach $ctycode (@code_list) { ($stcode,$ctynum) = &get_county_code($ctycode); # /** Ignoring ALL states except KY **/ if ( $stcode eq $arg_st_code ) { # /** Should call the process_html here: # INPUT: the RDF file name **/ #$inp_file = "$inp_path"."$stcode"."$ctynum"; #$out_file = "$out_path"."$stcode"."STORY"."$ctynum"; #&process_vars_html($inp_file, $out_file); #close($out_file); push(@ret_code_list, "$stcode"."$format"."$ctynum"); } #if } #foreach } #if } close(INP_MODEL); &print_array(@ret_code_list); return (@ret_code_list); } 1;