#!/usr/bin/perl =head1 NAME Convert NCL parameter table text file to GEMPAK-compatible decoder. =head1 SYNOPSIS Usage: tblconvert [-hv] -i inputFileName [-o outputFileName] [-f format] =head1 DESCRIPTION This program converts formatted GRIB Tables to GEMPAK standard. =head2 ARGUMENTS =over 12 =item C<-h> or C<--help> Print help message and exit =item C<-f> or C<--format> Input file format (currently only 'ncl' implemented) =item C<-i> or C<--input> Name of the input file to process. =item C<-o> or C<--output> Name of the output file to write (default STDOUT). =item C<-v> or C<--verbose> Verbose output. =head1 LICENSE This is released under the GPL. See L. =head1 AUTHOR Ron McTaggart-Cowan =head1 SEE ALSO =cut use strict; use Getopt::Long; use IO::File; use IO::Handle; use Pod::Usage; # Get command line arguments and parse my $help = ''; my $man = ''; my $verbose = ''; my $format = 'ncl'; my $input = 'noInputFile'; my $output = 'STDOUT'; GetOptions ('help|h' => \$help, 'verbose|v' => \$verbose, 'format|f' => \$format, 'input|i=s' => \$input, 'output|o=s' => \$output) or pod2usage(2); pod2usage(1) if $help; # Use CL argument to describe input and output files FIXME die "Input file must be specified with -i or --input option. Use --help for help.\n" if $input eq 'noInputFile'; my $nclTable = $input; #my $gempakTable = ($output eq 'STDOUT') ? fileno(STDOUT) : $output; my $gempakTable = $output; # Open and read NCL table my @inTable; my $IN = IO::File->new($nclTable,O_RDONLY) or die "Cannot open $nclTable for reading.\n"; if ($IN->opened){ @inTable= $IN->getlines; } else{ my $error = $IN->error; die "Open command for file $nclTable died with error message: $error\n"; } undef $IN; # Fixed fields my $scale = 0; my $missing = -9999.00; my $hzremap = 0; my $direction = 0; # Open output file and write header my $OUT; if ($gempakTable ne 'STDOUT'){ open(GEMTABLE,">$gempakTable") or die "Cannot open $gempakTable for writing.\n"; select(GEMTABLE); } print "!\n! GRIB Table auto-generated from NCL-formatted table by tblconvert\n"; print "!\n!ID# NAME UNITS GNAM SCALE MISSING HZREMAP DIRECTION\n!\n"; # Parse input fields and write $~ = 'GEMFORMAT'; foreach my $line (@inTable){ if ($line =~ /^!/){ print $line ;} else{ my ($id,$name,$units,$desc); ($id,$name,$units,$desc) = split(/:/,$line) if $format eq 'ncl'; # Set format format GEMFORMAT = @<<@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<@<<<<<<<<<<<<<<<<<<<<@<<<<<<<<<<<<<@###@#######.##@########@######### $id,$desc,$units,$name,$scale,$missing,$hzremap,$direction . write; } } # Close output file select(STDOUT); close(GEMTABLE);