Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a script to diff namelists #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions conda/dev-environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ dependencies:
- mule
- cfunits
- dask
- f90nml
1 change: 1 addition & 0 deletions conda/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ requirements:
- scipy
- sparse
- xarray
- f90nml

test:
requires:
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@
],
entry_points = {
'console_scripts': [
'diffnml = coecms.diffnml:main'
]}
)
74 changes: 74 additions & 0 deletions src/coecms/diffnml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/env python
# Copyright 2019 ARC Centre of Excellence for Climate Extremes
# author: Scott Wales <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import f90nml
import argparse
from collections import OrderedDict


def diffnml(a, b):
"""
Create a diff of two f90nml instances

Returns an ordered dict mapping tuples

(section, key) -> (left, right)

for (section, key) pairs where there is a difference, with left and right
being the values of that (section, key) from namelists a and b respectively

If that (section, key) is missing from one file then that file will report
None
"""
groups = set([*a.keys(), *b.keys()])
output = OrderedDict()

for g in groups:
a_g = a.get(g, {})
b_g = b.get(g, {})

keys = set([*a_g.keys(), *b_g.keys()])
for k in keys:
a_k = a_g.get(k, None)
b_k = b_g.get(k, None)

if a_k != b_k:
output[(g,k)] = (a_k, b_k)

return output


def main(argv=None):
parser = argparse.ArgumentParser(description="Creates a diff of two namelist files")
parser.add_argument('filea', metavar='FILE1')
parser.add_argument('fileb', metavar='FILE2')
args = parser.parse_args(argv)

print("< %s"%args.filea)
print("> %s"%args.fileb)

a = f90nml.read(args.filea)
b = f90nml.read(args.fileb)

for section, diff in diffnml(a,b).items():
group, key = section
left, right = diff
print(f'&{group}%{key}:\n\t< {left}\n\t> {right}')


if __name__ == '__main__':
main()

69 changes: 69 additions & 0 deletions test/test_diffnml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/env python
# Copyright 2019 ARC Centre of Excellence for Climate Extremes
# author: Scott Wales <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import print_function

from coecms.diffnml import *
import f90nml
import io

def make_nml(string):
return f90nml.read(io.StringIO(string))


def test_diffnml():
"""
Check diff logic
"""
a = make_nml("""
&foo
bar = 1
/
""")
b = make_nml("""
&foo
bar = 'a'
baz = 1
/
""")

d = diffnml(a,b)

assert d[('foo','bar')] == (1, 'a')
assert d[('foo','baz')] == (None, 1)


def test_main(tmpdir, capsys):
"""
Check output formatting
"""
a = tmpdir.join('a')
b = tmpdir.join('b')

a.write("""
&foo
bar = 1
/
""")
b.write("""
&foo
bar = 2
/
""")

main([str(a),str(b)])

captured = capsys.readouterr()
assert captured.out == f"< {str(a)}\n> {str(b)}\n&foo%bar:\n\t< 1\n\t> 2\n"