IODA Bundle
gen_single_ob.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 #
3 # (C) Copyright 2020 NOAA NWS NCEP EMC
4 #
5 # This software is licensed under the terms of the Apache Licence Version 2.0
6 # which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
7 #
8 from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
9 import numpy as np
10 from pathlib import Path
11 from datetime import datetime
12 import yaml
13 import sys
14 
15 IODA_CONV_PATH = Path(__file__).parent/"@SCRIPT_LIB_PATH@"
16 if not IODA_CONV_PATH.is_dir():
17  IODA_CONV_PATH = Path(__file__).parent/'..'/'lib-python'
18 sys.path.append(str(IODA_CONV_PATH.resolve()))
19 
20 import meteo_utils
21 import ioda_conv_ncio as iconv
22 from orddicts import DefaultOrderedDict
23 
24 
25 class singleob(object):
26  loctype = {
27  float: 'float',
28  str: 'string',
29  int: 'integer',
30  }
31 
32  def __init__(self, yamlfile):
33  # read a YAML file and generate a single observation file
34  # using the configuration in the YAML file
35  # read the YAML config into a dictionary
36  with open(yamlfile, 'r') as stream:
37  yamlconfig = yaml.safe_load(stream)
38  self.filenamefilename = yamlconfig['obsdataout']
39  self.locKeyListlocKeyList = []
40  self.AttrDataAttrData = {
41  'date_time_string': yamlconfig['lockeys']['datetime']
42  }
43  locKeys = []
44  # set up location keys based on YAML
45  for key in yamlconfig['lockeys'].keys():
46  value = yamlconfig['lockeys'][key]
47  self.locKeyListlocKeyList.append((key, self.loctypeloctype[type(value)]))
48  locKeys.append(value)
49  self.keyDictkeyDict = DefaultOrderedDict(lambda: DefaultOrderedDict(dict))
50 
51  self.writerwriter = iconv.NcWriter(self.filenamefilename, self.locKeyListlocKeyList)
52 
53  # set up variable name
54  key = 'oneob'
55  value = yamlconfig['variable']['name']
56  self.keyDictkeyDict[key]['valKey'] = value, self.writerwriter.OvalName()
57  self.keyDictkeyDict[key]['errKey'] = value, self.writerwriter.OerrName()
58  self.keyDictkeyDict[key]['qcKey'] = value, self.writerwriter.OqcName()
59  # set up the data
60  self.datadata = DefaultOrderedDict(lambda: DefaultOrderedDict(dict))
61  locKey = tuple(locKeys)
62  self.datadata[0][locKey][self.keyDictkeyDict[key]['valKey']] = float(yamlconfig['variable']['obsvalue'])
63  self.datadata[0][locKey][self.keyDictkeyDict[key]['errKey']] = float(yamlconfig['variable']['obserr'])
64  self.datadata[0][locKey][self.keyDictkeyDict[key]['qcKey']] = int(yamlconfig['variable']['preqc'])
65 
66  # call the IODA API and write the file
67  (ObsVars, LocMdata, VarMdata) = self.writerwriter.ExtractObsData(self.datadata)
68  self.writerwriter.BuildNetcdf(ObsVars, LocMdata, VarMdata, self.AttrDataAttrData)
69  return
70 
71 
72 def main():
73 
74  desc = 'Generate single observation IODA file from YAML input'
75  parser = ArgumentParser(
76  description=desc,
77  formatter_class=ArgumentDefaultsHelpFormatter)
78  parser.add_argument(
79  '-y', '--yaml', help='path to input YAML file', type=str, required=True)
80 
81  args = parser.parse_args()
82 
83  yamlfile = args.yaml
84 
85  singleob(yamlfile)
86 
87 
88 if __name__ == '__main__':
89  main()
def __init__(self, yamlfile)