#!/bin/sh # -*-Perl-*- (for Emacs) vim:set filetype=perl: (for vim) #======================================================================# # Run the right perl version: if [ -x /usr/local/bin/perl ]; then perl=/usr/local/bin/perl elif [ -x /usr/bin/perl ]; then perl=/usr/bin/perl else perl=`which perl| sed 's/.*aliased to *//'` fi exec $perl -x -S $0 "$@" # -x: start from the following line #======================================================================# #! /Good_Path/perl -w # line 17 # Name: nl2python # Description: # Convert F90 namelists into a Python class that provides all namelist # slots as fields. # Usage: # nl2python [options] [ [..]] > # Options: # -h # --help Show usage overview # -c # --classname= Name the Python class (default is `Params') # -d # --double Mark floating-point values as double precision # -m # --minimize Minimize strings by trimming trailing whitespace # --version Write version number of nl2python and exit use strict; BEGIN { # Make sure ${PENCIL_HOME}/lib/perl is in the Perl path if (-d "$ENV{PENCIL_HOME}/lib/perl") { unshift @INC, "$ENV{PENCIL_HOME}/lib/perl"; } else { if ($0 =~ m!(.*[/\\])!) { unshift @INC, "$1../lib/perl"; } } } use Pencil::Util; Pencil::Util::use_pencil_perl_modules('Fortran::F90Namelist') or die; use Getopt::Long; # Allow for `-Plp' as equivalent to `-P lp' etc: Getopt::Long::config("bundling"); my (%opts); # Variables written by GetOptions my $debug=0; # Activate with (undocumented) `--debug' option my $doll='\$'; # Need this to trick CVS ## Process command line GetOptions(\%opts, qw( -h --help --debug -c=s --classname=s -d --double -m --minimize -q --quiet -v --version ) ) or die "Aborting.\n"; if ($opts{'debug'}) { $debug = 1 } else { $debug = 0 } if ($debug) { printopts(\%opts); print "\@ARGV = `@ARGV'\n"; } if ($opts{'h'} || $opts{'help'}) { die usage(); } if ($opts{'v'} || $opts{'version'}) { die version(); } my $classname = ($opts{'c'} || $opts{'classname'} || 'Params'); my $double = ($opts{'d'} || $opts{'double'} || 0); my $minimize = ($opts{'m'} || $opts{'minimize'} || 0); my $quiet = ($opts{'q'} || $opts{'quiet'} || ''); ## ## End of generalities; here comes the real thing ## @ARGV = ('-') unless(@ARGV); my $nl = Fortran::F90Namelist->new(); my $nl2 = Fortran::F90Namelist->new(); $nl2->debug(1) if ($debug); foreach my $f (@ARGV) { $nl2->parse(file => $f, all => 1, merge => 1) or die "Couldn't parse file <$f>\n"; $nl->merge($nl2); } print <<"HERE"; # # Provide parameters from Pencil code namelist files (typically param.nml # and param2.nml). # # This file was generated by nl2python, so you will probably not want to # edit it. # HERE print $nl->output('format' => 'python', 'name' => $classname, 'trim' => $minimize, 'double' => $double, ); print "\n"; # ---------------------------------------------------------------------- # sub printopts { # Print command line options my $optsref = shift; my %opts = %$optsref; foreach my $opt (keys(%opts)) { print STDERR "\$opts{$opt} = `$opts{$opt}'\n"; } } # ---------------------------------------------------------------------- # sub usage { # Extract description and usage information from this file's header. my $thisfile = __FILE__; local $/ = ''; # Read paragraphs open(FILE, "<$thisfile") or die "Cannot open $thisfile\n"; while () { # Paragraph _must_ contain `Description:' or `Usage:' next unless /^\s*\#\s*(Description|Usage):/m; # Drop `Author:', etc. (anything before `Description:' or `Usage:') s/.*?\n(\s*\#\s*(Description|Usage):\s*\n.*)/$1/s; # Don't print comment sign: s/^\s*# ?//mg; last; # ignore body } $_ or "\n"; } # ---------------------------------------------------------------------- # sub version { # Return CVS data and version info. my $doll='\$'; # Need this to trick CVS my $cmdname = (split('/', $0))[-1]; my $rev = '$Revision: 1.3 $'; my $date = '$Date: 2012-09-16 10:29:36 $'; $rev =~ s/${doll}Revision:\s*(\S+).*/$1/; $date =~ s/${doll}Date:\s*(\S+).*/$1/; "$cmdname version $rev ($date)\n"; } # ---------------------------------------------------------------------- # # End of file nl2idl