-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathnetcdf4_class_examples.py
More file actions
94 lines (76 loc) · 3.22 KB
/
netcdf4_class_examples.py
File metadata and controls
94 lines (76 loc) · 3.22 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from netCDF4 import Dataset
import numpy as np
# Creating testing NetCDF file
root = Dataset("heat_map.nc", "w", format="NETCDF4")
# Adding a second group
test_grp = root.createGroup("testing_data")
print('\ngroups = ' + str(root.groups.keys()))
# Adding 4 Dimensions
time = root.createDimension("time", None)
layer = root.createDimension("layer", 11)
lat = root.createDimension("lat", 321)
lon = root.createDimension("lon", 291)
print('\ndimensions = ' + str(root.dimensions.keys()))
# Looking at dimension sizes
print('\nnumber of layers = ' + str(len(layer)))
print('Is layer unlimited in dimension? ' + str(layer.isunlimited()))
print('Is time unlimited in dimension? ' + str(time.isunlimited()))
# Add two variables to the file
temp = root.createVariable("temp", "f4", ("time","layer","lat","lon"), least_significant_digit=3)
rh = root.createVariable("rh", "f", ("time","layer","lat","lon"))
# Adding a variable for each dimension in the file
times = root.createVariable("time", "f8", ("time"))
layers = root.createVariable("layer", "i4", ("layer"))
latitudes = root.createVariable("latitude", "f4", ("lat"))
longitudes = root.createVariable("longitude", "f4", ("lon"))
# Adding scalar variable to file
num_fires = root.createVariable("num_fires", 'i')
print('\nvariables = ' + str(root.variables.keys()))
# Adding global attributes
root.description = 'Heat map in California during the final days.'
root.problem = "It's hot. So hot."
# Adding variable attributes
latitudes.units = "degrees north"
longitudes.units = "degrees east"
layers.units = "hPa"
rh.units = 'percent'
temp.units = "F"
times.units = "hours since 0001-01-01 00:00:00.0"
times.calendar = "gregorian"
# Looking at attributes
print('\nAttributes = ' + str(root.ncattrs()))
for name in root.ncattrs():
print('attribute: ' + name + ', value: ' + str(getattr(root, name)))
# Writing Data
temp[0:1,0:11,:,:] = 100.0 * np.random(size=(1, 11, 321, 291)) + 70.0
print('temp.shape = ' + str(temp.shape))
# Altering Data
temp[0][0][0][0] = -24.5
temp[0][10][34][56:78] = 2.0 * np.arange(56, 78)
# Reading Data
print('\ntemp[0][0][0][0] = ' + str(temp[0][0][0][0]))
print('temp[0][0][50][50] = ' + str(temp[0][0][50][50]))
print('temp[0][0][79][25:29] = ' + str(temp[0][0][79][25:29]))
# Filling Time Coordinates
from datetime import datetime, timedelta
from netCDF4 import date2num, num2date
dates = [datetime(2015, 6, 21) + n*timedelta(hours=12) for n in range(temp.shape[0])]
times[:] = date2num(dates,units=times.units,calendar=times.calendar)
print('\ntimes = ' + str(times[:]))
# Convert numbers back to datetimes
dates = num2date(times[:],units=times.units,calendar=times.calendar)
print('\ndates = ' + str(dates))
# Showing how to modify time dimensions
print('temp.shape before = ' + str(temp.shape))
temp[0:5,0:11,:,:] = random(size=(5, 11, 321, 291))
print('temp.shape after = ' + str(temp.shape))
print('length of time = ' + str(len(time)))
# create compressed variable
pressure = root.createVariable("pressure",
"f4",
("time", "layer", "lat", "lon"),
zlib=True,
least_significant_digit=3,
complevel=9)
# don't forget to close the file!
root.close()