Folders and files
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Repository files navigation
## What is this?
A ruby library to manipulate Fortran namelists
## How to use
```ruby
require './lib/nml'
# parse an existing namelist
nml = NML_Reader.read(nml_file)
# returns a hash of hashes with group names
# as keys of the second level of hashes
# namelist groups can be retrieved by
# referencing these keys
# get namelist group '&share'
share = nml[:share]
# the return value is of type NV, which is
# a Hash extended with the methods <<, del
# and keep
# <<(args) merges args with the first key
# of the reciever hash
# keep(*args) returns a new NV object which
# only contains the variables indicated through
# args array
share=share.keep(:wrf_core, :max_dom, :start_date)
# del(*args) deletes specified variables of
# a particular group
share=share.del(:wrf_core, :max_dom)
# After manipulating namelist groups, write them
# to a new namelist
# Step 1 - Create a new Namelist Group
# create a namelist group 'share'
nshare = NML_Group.create(:share)
# add group variables, these can be new hashes
# or modified NV hashes from the previous manipulation
nshare << share
nshare << {:a => 1}
# << operations can be nested
nshare << share << {:a => 1}
# Step 2 - Construct a new namelist
# Push namelist groups to NML_Writer using instance_method <<
nml_writer = NML_Writer.new
nml_writer << nshare << ngrid << ungrib << nmet
# optionally re-order group names by passing in
# an array of keys in desired order
# example retrieves group names from original
# namelist and reverses the order
# make sure all group names are included in
# the array; otherwise it won't sort the output
nml_writer.okeys=nml[:okeys].reverse # reverse the
# order of group
# names in output
# Write out to STDOUT
nml_writer >> STDOUT
# Write to a file or other object supporting puts
outfile = File.new("namelist.wps.asia","w")
nml_writer >> outfile
outfile.close
```
Enjoy!
Copyright Saji Hameed (2012) saji.uaizu@gmail.com