|
| 1 | +#!/usr/bin/env python |
| 2 | +"""Input/Output functions""" |
| 3 | +from netCDF4 import Dataset |
| 4 | +try: |
| 5 | + from cyordereddict import OrderedDict |
| 6 | +except: |
| 7 | + from collections import OrderedDict |
| 8 | +try: |
| 9 | + from configparser import SafeConfigParser |
| 10 | +except: |
| 11 | + from ConfigParser import SafeConfigParser |
| 12 | +import configobj |
| 13 | + |
| 14 | + |
| 15 | +# -------------------------------------------------------------------- # |
| 16 | +def read_config(config_file, default_config=None): |
| 17 | + """ |
| 18 | + Return a dictionary with subdictionaries of all configFile options/values |
| 19 | + """ |
| 20 | + config = SafeConfigParser() |
| 21 | + config.optionxform = str |
| 22 | + config.read(config_file) |
| 23 | + sections = config.sections() |
| 24 | + dict1 = OrderedDict() |
| 25 | + for section in sections: |
| 26 | + options = config.options(section) |
| 27 | + dict2 = OrderedDict() |
| 28 | + for option in options: |
| 29 | + dict2[option] = config_type(config.get(section, option)) |
| 30 | + dict1[section] = dict2 |
| 31 | + |
| 32 | + if default_config is not None: |
| 33 | + for name, section in dict1.items(): |
| 34 | + if name in default_config.keys(): |
| 35 | + for option, key in default_config[name].items(): |
| 36 | + if option not in section.keys(): |
| 37 | + dict1[name][option] = key |
| 38 | + |
| 39 | + return dict1 |
| 40 | +# -------------------------------------------------------------------- # |
| 41 | + |
| 42 | + |
| 43 | +# -------------------------------------------------------------------- # |
| 44 | +def read_configobj(config_file, default_config=None): |
| 45 | + """ |
| 46 | + Return a dictionary with nested dictionaries |
| 47 | + """ |
| 48 | + |
| 49 | + config = configobj.ConfigObj(config_file) |
| 50 | + |
| 51 | + config = type_configobj(config) |
| 52 | + |
| 53 | + return config |
| 54 | +# -------------------------------------------------------------------- # |
| 55 | + |
| 56 | + |
| 57 | +# -------------------------------------------------------------------- # |
| 58 | +def type_configobj(d): |
| 59 | + """recursively loop through dictionary calling config_type""" |
| 60 | + for k, v in d.items(): |
| 61 | + if isinstance(v, dict): |
| 62 | + type_configobj(v) |
| 63 | + else: |
| 64 | + d[k] = config_type(v) |
| 65 | + return d |
| 66 | +# -------------------------------------------------------------------- # |
| 67 | + |
| 68 | + |
| 69 | +# -------------------------------------------------------------------- # |
| 70 | +def config_type(value): |
| 71 | + """ |
| 72 | + Parse the type of the configuration file option. |
| 73 | + First see the value is a bool, then try float, finally return a string. |
| 74 | + """ |
| 75 | + val_list = [x.strip() for x in value.split(',')] |
| 76 | + if len(val_list) == 1: |
| 77 | + value = val_list[0] |
| 78 | + if value in ['true', 'True', 'TRUE', 'T']: |
| 79 | + return True |
| 80 | + elif value in ['false', 'False', 'FALSE', 'F']: |
| 81 | + return False |
| 82 | + elif value in ['none', 'None', 'NONE', '']: |
| 83 | + return None |
| 84 | + else: |
| 85 | + try: |
| 86 | + return int(value) |
| 87 | + except: |
| 88 | + pass |
| 89 | + try: |
| 90 | + return float(value) |
| 91 | + except: |
| 92 | + return value |
| 93 | + else: |
| 94 | + try: |
| 95 | + return list(map(int, val_list)) |
| 96 | + except: |
| 97 | + pass |
| 98 | + try: |
| 99 | + return list(map(float, val_list)) |
| 100 | + except: |
| 101 | + return val_list |
| 102 | +# -------------------------------------------------------------------- # |
| 103 | + |
| 104 | + |
| 105 | +# -------------------------------------------------------------------- # |
| 106 | +def read_netcdf(nc_file, variables=[], coords=False, verbose=True): |
| 107 | + """ |
| 108 | + Read data from input netCDF. Will read all variables if none provided. |
| 109 | + Will also return all variable attributes. |
| 110 | + Both variables (data and attributes) are returned as dictionaries named by |
| 111 | + variable. |
| 112 | + """ |
| 113 | + |
| 114 | + f = Dataset(nc_file, 'r') |
| 115 | + |
| 116 | + if variables == []: |
| 117 | + variables = f.variables.keys() |
| 118 | + |
| 119 | + if verbose: |
| 120 | + print('Reading input data variables: ' |
| 121 | + ' {0} from file: {1}'.format(variables, nc_file)) |
| 122 | + |
| 123 | + d = OrderedDict() |
| 124 | + a = OrderedDict() |
| 125 | + |
| 126 | + if coords: |
| 127 | + if isinstance(variables, str): |
| 128 | + d[variables] = f.variables[variables][coords] |
| 129 | + a[variables] = f.variables[variables].__dict__ |
| 130 | + else: |
| 131 | + for var in variables: |
| 132 | + d[var] = f.variables[var][coords] |
| 133 | + a[var] = f.variables[var].__dict__ |
| 134 | + else: |
| 135 | + if isinstance(variables, str): |
| 136 | + d[variables] = f.variables[variables][:] |
| 137 | + a[variables] = f.variables[variables].__dict__ |
| 138 | + else: |
| 139 | + for var in variables: |
| 140 | + d[var] = f.variables[var][:] |
| 141 | + a[var] = f.variables[var].__dict__ |
| 142 | + f.close() |
| 143 | + return d, a |
| 144 | +# -------------------------------------------------------------------- # |
0 commit comments