netxsimdg
Loading...
Searching...
No Matches
ParaGridIO_input_test.py
1import netCDF4
2import numpy as np
3import time
4import calendar
5import math
6from errno import ENXIO
7
8sec_per_hr = 3600
9hr_per_day = 24
10
11if __name__ == "__main__":
12
13 target_structure = "parametric_rectangular"
14
15 nx = 9
16 ny = 11
17 element_shape = (nx, ny)
18 x1d = np.arange(nx)
19 y1d = np.arange(ny)
20
21 element_x = np.tile(x1d, (ny, 1))
22 element_y = np.transpose(np.tile(y1d, (nx, 1)))
23
24 time_data = 10 * element_x + element_y
25 field_name = "index2d"
26
27 out_file = "ParaGridIO_input_test.nc"
28 out_root = netCDF4.Dataset(out_file, "w", format="NETCDF4")
29 structgrp = out_root.createGroup("structure")
30 structgrp.type = target_structure
31
32 metagrp = out_root.createGroup("metadata")
33 metagrp.type = target_structure
34 confgrp = metagrp.createGroup("configuration") # But add nothing to it
35 timegrp = metagrp.createGroup("time")
36 # Use the start time as the timestamp for the file
37 formatted = timegrp.createVariable("formatted", str)
38 formatted.format = "%Y-%m-%dT%H:%M:%SZ"
39 formatted[0] = "1970-01-01T00:00:00Z"
40 time_attr = timegrp.createVariable("time", "i8")
41 the_time = 0
42 time_attr[:] = the_time
43 time_attr.units = "seconds since 1970-01-01T00:00:00Z"
44
45 datagrp = out_root.createGroup("data")
46 xDim = datagrp.createDimension("x", nx)
47 yDim = datagrp.createDimension("y", ny)
48 tDim = datagrp.createDimension("time", None)
49
50 # Position and time variables
51 nc_lons = datagrp.createVariable("longitude", "f8", ("y", "x"))
52 nc_lons[:, :] = element_x
53 nc_lats = datagrp.createVariable("latitude", "f8", ("y", "x"))
54 nc_lats[:, :] = element_y
55
56 nc_times = datagrp.createVariable("time", "f8", ("time"))
57
58 #(unix_times_e, era5_times) = create_era5_times(start_time, stop_time)
59 # For each field and time, get the corresponding file name for each dataset
60 # get the source data
61 data = datagrp.createVariable(field_name, "f8", ("time", "y", "x"))
62 data[0, :, :] = time_data
63 # 'fill' the time axis
64 nc_times[0] = the_time
65 out_root.close()