-
Notifications
You must be signed in to change notification settings - Fork 0
/
netcdf_api.py
36 lines (29 loc) · 986 Bytes
/
netcdf_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import numpy as np
from netCDF4 import Dataset
def load_grid(filename, keep_mask = False):
grid = Dataset(filename, mode = 'r')
values = grid.variables['z'][:]
grid.close()
if keep_mask:
return values
else:
return values.data
def write_netcdf(x, y, data, filename, units = ''):
nc_file = Dataset(filename, 'w', format = 'NETCDF4')
nc_file.node_offset = 0
nc_file.institution = 'Ashton Flinders, USGS, HVO, [email protected]'
nc_file.units = units
# dimensions
nc_file.createDimension('x', len(x))
nc_file.createDimension('y', len(y))
# variables
longitudes = nc_file.createVariable('x', 'd', ('x',), zlib=True)
longitudes.long_name = "x"
latitudes = nc_file.createVariable('y', 'd', ('y',), zlib=True)
latitudes.long_name = "y"
output = nc_file.createVariable('z', 'd', ('y', 'x',), fill_value = 'NaN', zlib=True)
latitudes[:] = y
longitudes[:] = x
output[:,:] = data
output.actual_range = np.array((np.nanmin(data), np.nanmax(data)))
nc_file.close()