Skip to content

pycnal_toolbox ocean_in

Kate Hedstrom edited this page Nov 13, 2016 · 2 revisions

ocean_in

Dictionary

This is a tool for dealing with the the ROMS ocean.in file using Python. You create a Python data structure with the constructor:

    from pycnal_toolbox import ocean_in
    ocean_1 = ocean_in("ocean_foo.in")

This object (ocean_1) now contains a Python dictionary where the keys are all the "left-hand-side" strings from the ocean.in file:

    print(ocean_1.var_dict['Lm'])
    ['688']
    print(ocean_1.var_dict['LBC(isFsur)'])
    ['Che     Che     Che     Che']

You can not only query the values but change them too:

    print(ocean_1.var_dict['NPT'])
    ['6']
    ocean_1.var_dict['NPT'] = ['8']
    print(ocean_1.var_dict['NPT'])
    ['8']

'Lm' and 'NPT' have to be lists to handle ocean.in files for multiple grids or for fields with many values.

Once you've modified the file, you can write it back out, either as an ocean.in file or as a json file:

    ocean_1.write_ocean_in('ocean_foo.in')
    ocean_1.write_json_dict('ocean_foo.jsn')

Someone once suggested that the ocean.in should have an xml representation with a gui interface - why not json instead?

Nesting

For nested grid applications, you can generate a two-grid file very easily:

    ocean_1 = ocean_in('ocean_palau1.in')
    ocean_2 = ocean_in('ocean_palau2.in')
    ocean_1.merge_dicts([ocean_2])
    ocean_1.write_ocean_in('ocean_palau_1_2.in')

If you haven't yet worked on the ocean.in for the nest(s), you can create an ocean.in with one or more nests that are duplicates of the original, then edit it later:

    ocean_1 = ocean_in("ocean_palau1.in")
    ocean_1.merge_dicts([ocean_1, ocean_1])
    ocean_1.write_ocean_in('ocean_3grids.in')

Beware of shallow copies

If you were planning to read in the parent grid, copy it, modify it, then write out the merged grid, you might get this:

    ocean_1 = ocean_in("ocean_palau1.in")
    print(ocean_1.var_dict['Lm'])
    ['720']
    ocean_2 = ocean_1
    ocean_2.var_dict['Lm'] = ['480']
    ocean_1.merge_dicts([ocean_2])
    print(ocean_1.var_dict['Lm'])
    ['480', '480']

Here is how to do what you want:

    import copy
    ocean_1 = ocean_in("ocean_palau1.in")
    ocean_2 = copy.deepcopy(ocean_1)
    print(ocean_1.var_dict['Lm'])
    ['720']
    ocean_2.var_dict['Lm'] = ['480']
    ocean_1.merge_dicts([ocean_2])
    print(ocean_1.var_dict['Lm'])
    ['720', '480']
    ocean_1.write_ocean_in('ocean_2grids.in')
Clone this wiki locally