-
Notifications
You must be signed in to change notification settings - Fork 19
/
reduce_netcdf_woa18.py
82 lines (65 loc) · 2.73 KB
/
reduce_netcdf_woa18.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
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
import logging
import os
from netCDF4 import Dataset
from hyo2.ssm2.app.gui.soundspeedmanager import AppInfo
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
input_path = r"C:\Users\gmasetti\AppData\Local\HydrOffice\Sound Speed\atlases\woa18_orig"
output_path = r"C:\Users\gmasetti\AppData\Local\HydrOffice\Sound Speed\atlases\woa18"
# """ USED TO REDUCE THE SIZE OF THE FULL WOA13 DATABASE """
# temp annual
file = "woa18_decav_t00_04.nc"
i_path = os.path.join(input_path, "temp", file)
i = Dataset(i_path, mode='r')
o_path = os.path.join(output_path, "temp", file)
if os.path.exists(o_path):
os.remove(o_path)
o = Dataset(o_path, mode='w')
for name, dim in i.dimensions.items():
o.createDimension(name, len(dim) if not dim.isunlimited() else None)
for name, var_i in i.variables.items():
if name in ['lat', 'lon', 'depth', 't_an']:
# create variable
var_o = o.createVariable(name, var_i.datatype, var_i.dimensions)
# copy attributes
var_o.setncatts({k: var_i.getncattr(k) for k in var_i.ncattrs()})
# copy data
var_o[:] = var_i[:]
# temp monthly/seasonal
for id in range(1, 17):
file = "woa18_decav_t%02d_04.nc" % id
i_path = os.path.join(input_path, "temp", file)
i = Dataset(i_path, mode='r')
o_path = os.path.join(output_path, "temp", file)
if os.path.exists(o_path):
os.remove(o_path)
o = Dataset(o_path, mode='w')
for name, dim in i.dimensions.items():
o.createDimension(name, len(dim) if not dim.isunlimited() else None)
for name, var_i in i.variables.items():
if name in ['lon', 'lat', 't_an', 's_an', 't_sd', 's_sd', 'depth']:
# create variable
var_o = o.createVariable(name, var_i.datatype, var_i.dimensions)
# copy attributes
var_o.setncatts({k: var_i.getncattr(k) for k in var_i.ncattrs()})
# copy data
var_o[:] = var_i[:]
# temp monthly/seasonal
for id in range(1, 17):
file = "woa18_decav_s%02d_04.nc" % id
i_path = os.path.join(input_path, "sal", file)
i = Dataset(i_path, mode='r')
o_path = os.path.join(output_path, "sal", file)
if os.path.exists(o_path):
os.remove(o_path)
o = Dataset(o_path, mode='w')
for name, dim in i.dimensions.items():
o.createDimension(name, len(dim) if not dim.isunlimited() else None)
for name, var_i in i.variables.items():
if name in ['lon', 'lat', 't_an', 's_an', 't_sd', 's_sd', 'depth']:
# create variable
var_o = o.createVariable(name, var_i.datatype, var_i.dimensions)
# copy attributes
var_o.setncatts({k: var_i.getncattr(k) for k in var_i.ncattrs()})
# copy data
var_o[:] = var_i[:]