Issue #2657: Update convertCDL2XML for unified grid.

Former-commit-id: ed22a86d9f [formerly ed22a86d9f [formerly 247536f4d83c7f7de449707916d2950ae74be551]]
Former-commit-id: d6397522c4
Former-commit-id: 777c1fba86
This commit is contained in:
David Gillingham 2014-01-08 11:02:03 -06:00
parent 93c933e2c7
commit c7085fe52b

View file

@ -19,7 +19,7 @@
## ##
## ##
# Converts A1 CDL files to A2-formatted gribParamInfo XML files. # Converts A1 CDL files to A2-formatted gridParamInfo XML files.
# #
# #
# SOFTWARE HISTORY # SOFTWARE HISTORY
@ -27,7 +27,8 @@
# Date Ticket# Engineer Description # Date Ticket# Engineer Description
# ------------ ---------- ----------- -------------------------- # ------------ ---------- ----------- --------------------------
# 08/30/12 #1117 dgilling Initial Creation. # 08/30/12 #1117 dgilling Initial Creation.
# # 01/08/14 #2657 dgilling Use new unified grid tags,
# code cleanup.
# #
## ##
@ -35,7 +36,6 @@ import errno
import glob import glob
import logging import logging
import os import os
import os.path
import re import re
import subprocess import subprocess
import sys import sys
@ -61,7 +61,9 @@ def main():
logger.info("Starting convertCDL2XML...") logger.info("Starting convertCDL2XML...")
with open(os.devnull, 'w') as DEVNULL: with open(os.devnull, 'w') as DEVNULL:
if subprocess.call("type ncgen", stdout=DEVNULL, stderr=subprocess.STDOUT, shell=True) != 0: try:
subprocess.check_call("type ncgen", stdout=DEVNULL, stderr=subprocess.STDOUT, shell=True)
except subprocess.CalledProcessError:
logger.error("This script requires the ncgen program to run. Please install before running this script again.") logger.error("This script requires the ncgen program to run. Please install before running this script again.")
sys.exit(-1) sys.exit(-1)
@ -83,16 +85,12 @@ def main():
def __initLogger(): def __initLogger():
logging.basicConfig(format="%(asctime)s %(name)s %(levelname)s: %(message)s",
datefmt="%H:%M:%S",
level=logging.INFO)
global logger global logger
logger = logging.getLogger("convertCDL2XML") logger = logging.getLogger("convertCDL2XML")
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
# Uncomment line below to enable debug-level logging
# ch.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s %(name)s %(levelname)s: %(message)s", "%H:%M:%S")
ch.setFormatter(formatter)
logger.addHandler(ch)
def __parseArgs(): def __parseArgs():
parser = UsageArgumentParser.UsageArgumentParser(conflict_handler="resolve") parser = UsageArgumentParser.UsageArgumentParser(conflict_handler="resolve")
@ -158,7 +156,9 @@ def __convertFile(cdlFile, outputDir):
def __convertToCdf(cdlFile, outputDir): def __convertToCdf(cdlFile, outputDir):
outFile = os.path.join(outputDir, 'tmpFile.nc') outFile = os.path.join(outputDir, 'tmpFile.nc')
cmd = ['ncgen', '-x', '-o', outFile, cdlFile] cmd = ['ncgen', '-x', '-o', outFile, cdlFile]
if subprocess.call(cmd) != 0: try:
subprocess.check_call(cmd)
except subprocess.CalledProcessError:
logger.error("Could not create temporary CDF file for [" + cdlFile + "].") logger.error("Could not create temporary CDF file for [" + cdlFile + "].")
return None return None
return outFile return outFile
@ -271,7 +271,7 @@ def __getParmAtts(ncVar, varName, ncFile):
return attrMap return attrMap
def __createXML(cdfData): def __createXML(cdfData):
root = ET.Element('gribParamInfo', {'xmlns:ns2': 'group'}) root = ET.Element('gridParamInfo', {'xmlns:ns2': 'group'})
fcstTimes = ET.SubElement(root, 'valtimeMINUSreftime') fcstTimes = ET.SubElement(root, 'valtimeMINUSreftime')
for time in cdfData['valtimeMINUSreftime']: for time in cdfData['valtimeMINUSreftime']:
@ -281,7 +281,7 @@ def __createXML(cdfData):
for parm in cdfData['parmNames']: for parm in cdfData['parmNames']:
atts = {'xsi:type': "parameterInfo", atts = {'xsi:type': "parameterInfo",
'xmlns:xsi': "http://www.w3.org/2001/XMLSchema-instance"} 'xmlns:xsi': "http://www.w3.org/2001/XMLSchema-instance"}
parmRoot = ET.SubElement(root, 'gribParameterInfo', atts) parmRoot = ET.SubElement(root, 'gridParameterInfo', atts)
for key in ['short_name', 'long_name', 'units', 'udunits', 'uiname', 'valid_range', 'fillValue', 'n3D', 'levelsDesc', 'levels']: for key in ['short_name', 'long_name', 'units', 'udunits', 'uiname', 'valid_range', 'fillValue', 'n3D', 'levelsDesc', 'levels']:
if key in cdfData[parm]: if key in cdfData[parm]:
if key == 'valid_range': if key == 'valid_range':