#!/usr/bin/perl -w #------------------------------------------------------------------------------ # GEOS-Chem Global Chemical Transport Model # #------------------------------------------------------------------------------ #BOP # # !MODULE: ncCodeDef # # !DESCRIPTION: This Perl script automatically creates a Fortran subroutine # that creates a netCDF file and specifies the relevant variables and # attributes. The Fortran subroutine (named DEFINE\_NETCDF\_FILE) contains # calls to the proper NcdfUtilities library routines. #\\ #\\ # !USES: # require 5.003; # Need this version of Perl or newer use English; # Use English language use Carp; # Get detailed error messages use strict 'refs'; # Do not allow symbolic references use strict 'subs'; # Treat all barewords as syntax errors use StrTrim qw( &trim &splitLine &extractFile ); # Get string handling routines # # !PRIVATE MEMBER FUNCTIONS: # &readRcFile($) # &writeFortranVars($@) # &writeFortranCalls($@) # &handleFileName($$) # &handleGlobalAtts($$) # &handleDimensions($$) # &handleVariables($$) # # !PUBLIC MEMBER FUNCTIONS: # &main() # # !PUBLIC DATA MEMBERS: # $F_ID = ""; # netCDF file ID # # !CALLING SEQUENCE: # ncCodeCreate RESOURCE-FILE-NAME # # !REMARKS: # Some hand-editing of the output Fortran subroutine may be necessary. # # !REVISION HISTORY: # 27 Jan 2012 - R. Yantosca - Initial version # 30 Jan 2012 - R. Yantosca - Now get trim, splitline routines from the # Perl module "StrTrim.pm" # 30 Jan 2012 - R. Yantosca - Now write ProTeX comment headers # 31 Jan 2012 - R. Yantosca - Minor edits for consistency # 07 Mar 2012 - R. Yantosca - Minor fix, ignore comment lines #EOP #------------------------------------------------------------------------------ # GEOS-Chem Global Chemical Transport Model # #------------------------------------------------------------------------------ #BOP # # !IROUTINE: readRcFile # # !DESCRIPTION: Routine readRcFile reads the resource file which describes # the variables, attributes, and dimensions of the netCDF file. #\\ #\\ # !INTERFACE: # sub readRcFile($) { # # !INPUT PARAMETERS: # # $fileName : Input file that describes the netCDF file my ( $fileName ) = @_; # # !CALLING SEQUENCE: # &readRcFile( RESOURCE-FILE-NAME ); # # !REVISION HISTORY: # 27 Jan 2012 - R. Yantosca - Initial version # 27 Jan 2012 - R. Yantosca - Now get output filename from the resource file # 07 Mar 2012 - R. Yantosca - Minor fix, ignore comment lines #EOP #------------------------------------------------------------------------------ #BOC # # !LOCAL VARIABLES: # my $cmdFile = ""; my $line = ""; my @lines = (); my $name = ""; #-------------------------------------------------- # Read variable settings from the resource file #-------------------------------------------------- open( I, "<$fileName" ) or die "Cannot open $fileName!\n"; chomp( @lines = ); close( I ); #-------------------------------------------------- # Write Fortran commands to the output file #-------------------------------------------------- # Pre-get a few quantities before creating the # output file with the fortran code foreach $line ( @lines ) { # Skip comment lines if ( !( substr( $line, 0, 1 ) eq '#' ) ) { # Name of output file w/ Fortran code if ( $line =~ 'Fortran Def File' ) { ( $name, $cmdFile ) = &splitLine( $line, '=' ); } # NetCDF file ID (aka filehandle) if ( $line =~ 'netCDF FileHandle' ) { ( $name, $F_ID ) = &splitLine( $line, '=' ); } } } # Open the file that will ho open( O, ">$cmdFile" ) or die "Cannot open $cmdFile\n"; # Pass thru @lines array so that we can declare Fortran variables &writeFortranVars( \*O, @lines ); # Pass thru @lines array again to write &writeFortranCalls( \*O, @lines ); #-------------------------------------------------- # Cleanup and quit #-------------------------------------------------- # Close output file close( O ); # Return return( 0 ); } #EOC #------------------------------------------------------------------------------ # GEOS-Chem Global Chemical Transport Model # #------------------------------------------------------------------------------ #BOP # # !IROUTINE: writeFortranVars # # !DESCRIPTION: Routine writeFortranVars generates the proper Fortran # variable declarations that are needed for use with the NcdfUtilities # library routines. #\\ #\\ # !INTERFACE: # sub writeFortranVars($@) { # # !INPUT PARAMETERS: # # $O : File handle # @lines : Contents of the resource file my ( $O, @lines ) = @_; # # !CALLING SEQUENCE: # &writeFortranVars( \*O, @lines ); # # !REVISION HISTORY: # 27 Jan 2012 - R. Yantosca - Initial version #EOP #------------------------------------------------------------------------------ #BOC # # !LOCAL VARIABLES: # my @subStr = (); my $name = ""; my $value = ""; my $txt = ""; #------------------------------------------------------- # Write USE statements #------------------------------------------------------- $txt .= <