fortnml

fortnml is a Python script to manage fortran namelists, modifiying them from the command line and can also be used as a namelist "beautifier". Also checks for WRF recomendations. The core namelist parser is based on that of pywrf, but this was enhanced to support weirdly written namelists (e.g. namelist.output) and is pure python code using objects (no need for extra packages), so it can be run on any machine.

Download

Usage

usage: fortnml [options]

options:
  -h, --help            show this help message and exit
  -fNAMELIST, --file=NAMELIST
                        Namelist file
  -q, --quiet           Don't dump the namelist to stdout
  -w, --wrf             WRF namelist.input parse mode
  -aVARIABLE[@RECORD], --append=VARIABLE[@RECORD]
                        Append the provided values to this namelist entry. A
                        specific record can be specified through
                        variable@record syntax
  -sVARIABLE[@RECORD], --set=VARIABLE[@RECORD]
                        Set the provided values to this namelist entry. A
                        specific record can be specified through
                        variable@record syntax
  -mVARIABLE[@RECORD], --max-dom=VARIABLE[@RECORD]
                        Set the provided (single) value to this namelist entry
                        as many times as the max_dom@domains specifies. WRF
                        mode (--wrf) implied even if not set
  -nNITEMS              Repeat the value provided NITEMS times. Only used when
                        in set (-s) or append (-a) mode
  -dVARIABLE[@RECORD], --delete=VARIABLE[@RECORD]
                        Delete a VARIABLE. The record can be specified through
                        the VARIABLE@RECORD syntax
  -o, --overwrite       Overwrite the provided namelist with the result
  -t, --trim            Trim the length of the array variables to the maximum
                        number of domains

fortnml -tf namelist.output > namelist.output.txt

Examples

Consider this sample namelist:

 &domains
  time_step = 90,
  max_dom = 3,
  e_we =  100,  91,  101, 
  e_sn =  92,  88,  106, 
 e_vert                              = 28, 28, 28, 28,
  num_metgrid_levels = 14,
  dx =  15000.00000,  5000.00000,  1000.00000, 
  dy =  15000.00000,  5000.00000,  1000.00000, 
  i_parent_start =  1,  33,  37, 
  j_parent_start =  1,  35,  35, 
  parent_grid_ratio =  1,  3,  5, 
  parent_time_step_ratio =  1,  3,  5, 
 feedback                            = 0,
 /

 &physics
 mp_physics                          = 3,     3,     3,
 ra_lw_physics                       = 3,     3,     3,
 ra_sw_physics                       = 3,     3,     3,
 radt                                = 30,    30,    30,
 sf_sfclay_physics                   = 2,     2,     2,
 bl_pbl_physics                      = 2,     2,     2,
 bldt                                = 0,     0,     0,
 cu_physics                          = 1,     1,     0,
 cudt                                = 5,     5,     5,
sf_surface_physics                  = 2,     2,     2,
 num_soil_layers                     = 4,
 /

The --trim (-t) option trims the multivalued items to the max_dom specified. This enables the --wrf (-w) option even if not set, thus, it performs some checks and even fixes some problems found:

shell$ fortnml -f namelist.input -t
WRF Check Warning: radt is larger than dx (2.000000)
WRF Check Warning: CAM radiation selected but paerlev/levsiz/cam_abs_dim1/cam_abs_dim2 was not set. Fixing...
&domains
  time_step                = 90,
  max_dom                  = 3,
  e_we                     = 100, 91, 101,
  e_sn                     = 92, 88, 106,
  e_vert                   = 28, 28, 28,
  num_metgrid_levels       = 14,
  dx                       = 15000.0, 5000.0, 1000.0,
  dy                       = 15000.0, 5000.0, 1000.0,
  i_parent_start           = 1, 33, 37,
  j_parent_start           = 1, 35, 35,
  parent_grid_ratio        = 1, 3, 5,
  parent_time_step_ratio   = 1, 3, 5,
  feedback                 = 0,
/

&physics
  mp_physics               = 3, 3, 3,
  ra_lw_physics            = 3, 3, 3,
  ra_sw_physics            = 3, 3, 3,
  radt                     = 30, 30, 30,
  sf_sfclay_physics        = 2, 2, 2,
  bl_pbl_physics           = 2, 2, 2,
  bldt                     = 0, 0, 0,
  cu_physics               = 1, 1, 0,
  cudt                     = 5, 5, 5,
  sf_surface_physics       = 2, 2, 2,
  num_soil_layers          = 4,
  paerlev                  = 29,
  levsiz                   = 59,
  cam_abs_dim1             = 4,
  cam_abs_dim2             = 28,
/

We can change the radt to 15. Changing with -m will set as many 15's as required depending on the max_dom setting:

shell$ fortnml -f namelist.input -m radt 15
WRF Check Warning: CAM radiation selected but paerlev/levsiz/cam_abs_dim1/cam_abs_dim2 was not set. Fixing...
&domains
  time_step                = 90,
  max_dom                  = 3,
  e_we                     = 100, 91, 101,
  e_sn                     = 92, 88, 106,
  e_vert                   = 28, 28, 28, 28,
  num_metgrid_levels       = 14,
  dx                       = 15000.0, 5000.0, 1000.0,
  dy                       = 15000.0, 5000.0, 1000.0,
  i_parent_start           = 1, 33, 37,
  j_parent_start           = 1, 35, 35,
  parent_grid_ratio        = 1, 3, 5,
  parent_time_step_ratio   = 1, 3, 5,
  feedback                 = 0,
/

&physics
  mp_physics               = 3, 3, 3,
  ra_lw_physics            = 3, 3, 3,
  ra_sw_physics            = 3, 3, 3,
  radt                     = 15, 15, 15,
  sf_sfclay_physics        = 2, 2, 2,
  bl_pbl_physics           = 2, 2, 2,
  bldt                     = 0, 0, 0,
  cu_physics               = 1, 1, 0,
  cudt                     = 5, 5, 5,
  sf_surface_physics       = 2, 2, 2,
  num_soil_layers          = 4,
  paerlev                  = 29,
  levsiz                   = 59,
  cam_abs_dim1             = 4,
  cam_abs_dim2             = 28,
/

If you need to assing a currently nonexisting option you must specify the record where it should go (it can also not exist and will be created):

shell$ fortnml -f namelist.input -s fdda_opt@fdda 1
&domains
  time_step                = 90,
  max_dom                  = 3,
  e_we                     = 100, 91, 101,
  e_sn                     = 92, 88, 106,
  e_vert                   = 28, 28, 28, 28,
  num_metgrid_levels       = 14,
  dx                       = 15000.0, 5000.0, 1000.0,
  dy                       = 15000.0, 5000.0, 1000.0,
  i_parent_start           = 1, 33, 37,
  j_parent_start           = 1, 35, 35,
  parent_grid_ratio        = 1, 3, 5,
  parent_time_step_ratio   = 1, 3, 5,
  feedback                 = 0,
/

&physics
  mp_physics               = 3, 3, 3,
  ra_lw_physics            = 3, 3, 3,
  ra_sw_physics            = 3, 3, 3,
  radt                     = 30, 30, 30,
  sf_sfclay_physics        = 2, 2, 2,
  bl_pbl_physics           = 2, 2, 2,
  bldt                     = 0, 0, 0,
  cu_physics               = 1, 1, 0,
  cudt                     = 5, 5, 5,
  sf_surface_physics       = 2, 2, 2,
  num_soil_layers          = 4,
/

&fdda
  fdda_opt                 = 1,
/