|
1 | 1 | # aodn-netcdf-tools |
2 | 2 | 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 | +``` |
0 commit comments