Skip to content

Commit 45f36fb

Browse files
committed
Latest corrections.
1 parent 4aff1d9 commit 45f36fb

File tree

3 files changed

+193
-120
lines changed

3 files changed

+193
-120
lines changed

ncldDump/aliases.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,15 @@
9797
"time_coverage_resolution" : "http://wiki.esipfed.org/index.php/Attribute_Convention_for_Data_Discovery_1-3#time_coverage_resolution",
9898
"time_coverage_start" : "http://wiki.esipfed.org/index.php/Attribute_Convention_for_Data_Discovery_1-3#time_coverage_start",
9999
"title" : "http://wiki.esipfed.org/index.php/Attribute_Convention_for_Data_Discovery_1-3#title",
100-
"title" : "http://www.unidata.ucar.edu/netcdf/docs/netcdf.html#Attribute-Conventions",
101100
"units" : "http://cfconventions.org/cf-conventions/v1.6.0/cf-conventions.html#units",
102101
"valid_max" : "http://www.unidata.ucar.edu/netcdf/docs/netcdf.html#Attribute-Conventions",
103102
"valid_min" : "http://www.unidata.ucar.edu/netcdf/docs/netcdf.html#Attribute-Conventions",
104103
"valid_range" : "http://www.unidata.ucar.edu/netcdf/docs/netcdf.html#Attribute-Conventions",
105-
"SDN_ParameterDiscoveryCode" : "http://vocab.nerc.ac.uk/isoCodelists/sdnCodelists/cdicsrCodeList.xml#{}"
104+
"SDN_ParameterDiscoveryCode" : "http://vocab.nerc.ac.uk/isoCodelists/sdnCodelists/cdicsrCodeList.xml#{}",
105+
"unit_id" : "http://qudt.org/1.1/schema/qudt#unit",
106+
"medium_id" : "http://environment.data.gov.au/def/op#matrix",
107+
"scaledQuantityKind_id" : "http://environment.data.gov.au/def/op#propertyKind",
108+
"substanceOrTaxon_id" : "http://environment.data.gov.au/def/op#objectOfInterest"
106109
},
107110
"values" :
108111
{

ncldDump/ncldDump.py

Lines changed: 86 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -285,24 +285,44 @@ def resolveValue(name, value, aliasDict):
285285
#
286286
result = None
287287

288-
# If the value is a string, attempt to resolve it.
288+
# Breakout context
289289
#
290-
if True == isinstance(value, str) or True == isinstance(value, unicode):
291-
# Attempt to split the value on '__'.
290+
done = False
291+
292+
while False == done:
293+
done = True
294+
295+
# If the value is not a string, get a string representation.
296+
#
297+
if False == isinstance(value, str) and False == isinstance(value, unicode):
298+
value = str(value)
299+
300+
# If the value starts with 'http', interpret the entire string as a
301+
# resolved URL.
302+
#
303+
if value[0:4] == 'http':
304+
result = value
305+
306+
break
307+
308+
# Attempt to split the value on '__' to see if there is a context
309+
# part to the value.
292310
#
293311
valueParts = value.split('__')
294312

295313
# If there is a context part, resolve the value as a name.
296314
#
297315
if 2 == len(valueParts):
298316
result = resolveName(value, aliasDict)
317+
318+
break
299319

300320
# If the name exists in the alias dictionary, and if the value exists
301321
# in the sub-dictionary for the name, create a URL using the pattern
302322
# for the value. A wildcard (*) for a value key in the dictionary
303323
# matches any value.
304324
#
305-
elif name in aliasDict['values']:
325+
if name in aliasDict['values']:
306326
subDict = aliasDict['values'][name]
307327

308328
pattern = None
@@ -314,6 +334,8 @@ def resolveValue(name, value, aliasDict):
314334

315335
if pattern is not None:
316336
result = makeURL(value, pattern)
337+
338+
break
317339

318340
# Return the resolved name if one was found.
319341
#
@@ -342,39 +364,33 @@ def parseAttributes(ncObj, aliasDict):
342364
attrValue = ncObj.getncattr(attrName)
343365
attrType = parseType(attrValue)
344366

345-
# If the value is not an array, make it a list.
367+
# If the value is an array, make it a list.
346368
#
347-
if False == isinstance(attrValue, numpy.ndarray):
348-
attrValue = [attrValue]
369+
if True == isinstance(attrValue, numpy.ndarray):
370+
attrValue = list(attrValue)
349371

350372
# Get the URL (if any) for the attribute.
351373
#
352374
nameURL = resolveName(attrName, aliasDict)
353375

354-
# Construct a list of attribute value and URL pairs.
376+
# Get the URL (if any) for the value.
355377
#
356-
valueList = []
378+
valueURL = resolveValue(attrName, attrValue, aliasDict)
357379

358-
for value in attrValue:
359-
# Get the URL (if any) for the value.
360-
#
361-
valueURL = resolveValue(attrName, value, aliasDict)
362-
363-
# If the value is a string, wrap it in '"' characters.
364-
#
365-
if True == isinstance(value, str) or True == isinstance(value, unicode):
366-
value = '"' + str(value) + '"'
367-
368-
valueEntry = { 'element' : value }
380+
# If the value is a string, wrap it in '"' characters.
381+
#
382+
if True == isinstance(attrValue, str) or True == isinstance(attrValue, unicode):
383+
attrValue = '"' + str(attrValue) + '"'
384+
385+
valueEntry = { 'element' : attrValue }
369386

370-
if valueURL is not None:
371-
valueEntry['url'] = valueURL
372-
373-
valueList.append(valueEntry)
387+
if valueURL is not None:
388+
valueEntry['url'] = valueURL
389+
374390

375391
# Build the attribute entry. If there is a name URL add it.
376392
#
377-
attrEntry = {'name' : attrName, 'value' : valueList, 'type' : attrType}
393+
attrEntry = {'name' : attrName, 'value' : valueEntry, 'type' : attrType}
378394

379395
if nameURL is not None:
380396
attrEntry['url'] = nameURL
@@ -388,11 +404,11 @@ def parseAttributes(ncObj, aliasDict):
388404
return attrList
389405

390406

391-
def parseNcObj(ncObj, aliasDict):
407+
def parseGroup(ncObj, aliasDict):
392408
'''
393-
Build dimension, variable, and attribute lists for the object.
409+
Build dimension, variable, and attribute lists for the group object.
394410
395-
ncObj [in] The netCDF4 object to parse.
411+
ncObj [in] The netCDF4 group object to parse.
396412
aliasDict [in] A dictionary of URI patterns keyed by the elements they
397413
replace.
398414
returns A nested set of dictionaries and lists describing the object
@@ -482,6 +498,46 @@ def parseNcObj(ncObj, aliasDict):
482498
return dataDict
483499

484500

501+
def parseDataset(ncObj, aliasDict):
502+
'''
503+
Build a set of group dictionaries for the netCDF4 Dataset object.
504+
505+
ncObj [in] The netCDF4 Dataset object to parse.
506+
aliasDict [in] A dictionary of URI patterns keyed by the elements they
507+
replace.
508+
returns A nested set of dictionaries and lists describing the object
509+
contents.
510+
'''
511+
512+
# Parse the contents of the root group of the netCDF file. Add a groupName
513+
# element and store it in a groups list.
514+
#
515+
groupList = []
516+
517+
groupEntry = parseGroup(ncObj, aliasDict)
518+
519+
groupEntry['groupName'] = 'global'
520+
521+
groupList.append(groupEntry)
522+
523+
# If there are any other groups, add them as well.
524+
#
525+
for groupName, groupObj in ncObj.groups.iteritems():
526+
groupEntry = parseGroup(groupObj, aliasDict)
527+
528+
groupEntry['groupName'] = groupName
529+
530+
groupList.append(groupEntry)
531+
532+
# Add the group list to a top-level dictionary.
533+
#
534+
dataDict = { 'groups' : groupList }
535+
536+
# Return the dictionary.
537+
#
538+
return dataDict
539+
540+
485541
def ncldDump(inputFile, aliasFile, outputFile):
486542
'''
487543
Generate an HTML page from a netCDF-LD file. The page will contain CDL
@@ -504,9 +560,9 @@ def ncldDump(inputFile, aliasFile, outputFile):
504560
#
505561
ncObj = netCDF4.Dataset(inputFile, 'r')
506562

507-
# Parse the contents of the netCDF file into a dictionary.
563+
# Parse the contents into a dictionary.
508564
#
509-
ncDict = parseNcObj(ncObj, aliasDict)
565+
ncDict = parseDataset(ncObj, aliasDict)
510566

511567
# Add a filePath entry.
512568
#

0 commit comments

Comments
 (0)