Skip to content

Commit 4536e37

Browse files
authored
Merge pull request #3 from aodn/add_tests
Add unittests to define basic functionality for templates
2 parents c03d217 + c36c75f commit 4536e37

10 files changed

Lines changed: 604 additions & 89 deletions

File tree

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,63 @@
11
# aodn-netcdf-tools
22
Repository for templates and code relating to generating standard NetCDF files for the Australia Ocean Data Network
3+
4+
## ncwrwiter
5+
6+
This module provides a basic templating tool for creating netCDF files based on templates.
7+
8+
Templates can be provided as individual Python dictionaries, or as a JSON file, e.g. `template.json`:
9+
```json
10+
{
11+
"dimensions": {
12+
"TIME": 0
13+
},
14+
"variables": {
15+
"TIME": {
16+
"dimensions": ["TIME"],
17+
"type": "float64"
18+
}
19+
},
20+
"global_attributes": {
21+
"title": "test dataset"
22+
}
23+
}
24+
```
25+
Basic usage
26+
```python
27+
from ncwriter import DatasetTemplate
28+
29+
# create template from individual dictionaries
30+
template = DatasetTemplate(dimensions=dims_dict, variables=var_dict, global_attributes=gatt_dict)
31+
# OR from a template file
32+
template = DatasetTemplate.from_json('template.json')
33+
34+
# add/update attributes
35+
template.global_attributes.update({
36+
'comment': 'this was added later',
37+
'date_created': '2018-09-20T00:00:00Z'
38+
})
39+
40+
# add/update variables
41+
template.variables["TIME"]["atttributes"] = {
42+
"standard_name": "time",
43+
"units": "days since 1950-01-01T00:00:00Z"
44+
}
45+
template.variables["TEMP"] = {
46+
"dimensions": ["TIME"],
47+
"type": "float32",
48+
"attributes": {
49+
"standard_name": "sea_water_temperature",
50+
"units": "degC",
51+
"valid_min": 0.0,
52+
"valid_max": 42.00
53+
}
54+
}
55+
56+
# add variable values (automatically updates size of corresponding dimensions)
57+
template.variables['TIME']['values'] = np.arange(10)
58+
template.variables['TEMP']['values'] = np.array([12.1, 12.2, 12.3, 12.4, 12.5, 12.6, 12.7, 12.8, 12.9, 13.0])
59+
60+
# create netCDF file
61+
template.to_netcdf('example.nc')
62+
63+
```

ncwriter/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from .template import DatasetTemplate
2+
from .schema import ValidationError
3+
4+
__all__ = [
5+
'DatasetTemplate',
6+
'ValidationError'
7+
]

ncwriter/schema.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""This module holds schema definitions for validating the various :py:class:`dicts` that make up parts of a
2+
template, and also the helper functions necessary to validate an object against their respective schema.
3+
"""
4+
5+
from jsonschema import validate, ValidationError
6+
7+
8+
NAME_PATTERN = r"^[A-Za-z][A-Za-z0-9_]*$"
9+
10+
11+
DIMENSIONS_SCHEMA = {
12+
"type": "object",
13+
"patternProperties": {
14+
NAME_PATTERN: {
15+
"type": ["integer", "null"],
16+
"minimum": 0
17+
}
18+
},
19+
"additionalProperties": False
20+
}
21+
22+
ATTRIBUTES_SCHEMA = {
23+
"type": "object",
24+
"patternProperties": {
25+
NAME_PATTERN: {
26+
"type": ["string", "number", "array"]
27+
}
28+
},
29+
"additionalProperties": False
30+
}
31+
32+
33+
VARIABLE_DEFINITION_SCHEMA = {
34+
"type": "object",
35+
"properties": {
36+
"dimensions": {
37+
"type": "array",
38+
"items": {"type": "string", "pattern": NAME_PATTERN}
39+
},
40+
"type": {"type": "string"},
41+
"attributes": ATTRIBUTES_SCHEMA
42+
},
43+
"additionalProperties": False
44+
}
45+
46+
47+
VARIABLES_SCHEMA = {
48+
"type": "object",
49+
"patternProperties": {
50+
NAME_PATTERN: VARIABLE_DEFINITION_SCHEMA
51+
},
52+
"additionalProperties": False
53+
}
54+
55+
56+
def validate_dimensions(d):
57+
validate(d, DIMENSIONS_SCHEMA)
58+
59+
60+
def validate_variables(v):
61+
validate(v, VARIABLES_SCHEMA)
62+
63+
64+
def validate_attributes(a):
65+
validate(a, ATTRIBUTES_SCHEMA)

0 commit comments

Comments
 (0)