tconfig_test.py - pism - [fork] customized build of PISM, the parallel ice sheet model (tillflux branch)
 (HTM) git clone git://src.adamsgaard.dk/pism
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) LICENSE
       ---
       tconfig_test.py (1863B)
       ---
            1 #!/usr/bin/env python3
            2 # Checks the structure of a PISM configuration file. (This is used as a regression test.)
            3 
            4 
            5 def is_special(name):
            6     "Check if the name is 'special' and should not be included."
            7 
            8     if name == "long_name":
            9         return True
           10 
           11     for n in ["_doc", "_units", "_type", "_option", "_choices"]:
           12         if name.endswith(n):
           13             return True
           14 
           15     return False
           16 
           17 
           18 import netCDF4
           19 import sys
           20 import numpy as np
           21 
           22 config = netCDF4.Dataset(sys.argv[1])
           23 
           24 pism_config = config.variables['pism_config']
           25 
           26 attrs = pism_config.ncattrs()
           27 
           28 for a in attrs:
           29     if is_special(a):
           30         continue
           31 
           32     attr_value = getattr(pism_config, a)
           33 
           34     if (a + "_doc") not in attrs:
           35         print("Attribute {} is not documented".format(a))
           36         sys.exit(1)
           37 
           38     if (a + "_type") not in attrs:
           39         print("Attribute {} does not have a type".format(a))
           40         sys.exit(1)
           41 
           42     attr_type = getattr(pism_config, a + "_type")
           43 
           44     if attr_type in ["number", "integer"] and (a + "_units") not in attrs:
           45         print("Attribute {} is a number, but it does not have units".format(a))
           46         sys.exit(1)
           47 
           48     if attr_type == "flag" and attr_value not in ["yes", "no", "true", "false", "on", "off"]:
           49         print("Attribute {} is a flag, but its value is {}".format(a, attr_value))
           50         sys.exit(1)
           51 
           52     if attr_type == "keyword" and (a + "_choices") not in attrs:
           53         print("Attribute {} is a keyword, but {}_choices is not set".format(a, a))
           54         sys.exit(1)
           55 
           56     if attr_type in ["number", "integer"] and not isinstance(attr_value, (int, float, np.number)):
           57         print("Attribute {} is a number, but its value is not".format(a))
           58         print(type(attr_value))
           59         sys.exit(1)
           60 
           61     if attr_type not in ["number", "integer"] and isinstance(attr_value, (int, float)):
           62         print("Attribute {} is not a number, but its value is".format(a))