-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
165 lines (129 loc) · 5.29 KB
/
utils.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import subprocess
import os
import pandas as pd
import xarray as xr
import numpy as np
import netCDF4 as nc
def shell_cmd(cmd,lowarn=False):
"""
Send shell command through subprocess.Popen and returns a string
containing the cmd output
"""
# send cmd to be executed
p = subprocess.Popen(cmd, shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True)
# gets the output of the cmd
out, err = p.communicate()
print(str(out))
print(str(err))
return p.returncode,(str(out) + str(err))
def generate_data(input_dir,folder,identifier,field):
files_generated = []
data_location = os.path.join(input_dir,folder)
os.makedirs(data_location,exist_ok=True)
for year in range(2000,2006):
time = pd.date_range('{}-01-01'.format(year),
freq="D",
periods=365)
lat = np.arange(10,20,0.1)
lon = np.arange(10,20,0.1)
test = xr.DataArray(np.random.rand(365,100,100),
coords=[time,lat, lon],
dims=['time','lat','lon'],
name=field)
months = range(1,13)
for m in months:
month = '{:0>2d}'.format(m)
filename = '{}/{}_{}_{}.nc'.format(data_location,
year,
month,
identifier)
one_month = test.sel(time=test['time.month'] == m)
one_month.to_netcdf(filename)
files_generated.append(filename)
return files_generated
def generate_identical_data(input_dir,folder,identifier,field):
files_generated = []
data_location = os.path.join(input_dir,folder)
os.makedirs(data_location,exist_ok=True)
for year in range(2000,2006):
time = pd.date_range('{}-01-01'.format(year), freq="d", periods=365)
lat = np.arange(10,20,0.1)
lon = np.arange(10,20,0.1)
test = xr.dataarray(np.full((365,100,100),0.45),
coords=[time,lat, lon],
dims=['time','lat','lon'],
name=field)
months = range(1,13)
for m in months:
month = '{:0>2d}'.format(m)
filename = '{}/{}_{}_{}.nc'.format(data_location,
year,month,
identifier)
one_month = test.sel(time=test['time.month'] == m)
one_month.to_netcdf(filename)
files_generated.append(filename)
return files_generated
def generate_ref(ref_dir,name,fields):
os.makedirs(ref_dir,exist_ok=True)
files_generated = []
time = pd.date_range('2002-12-31 12:00:00', freq="D", periods=1)
lat = np.arange(10,20,0.1)
lon = np.arange(10,20,0.1)
test = xr.DataArray()
ds_to_merge = []
for field in fields:
data = np.full((1,100,100),0.45)
test = xr.DataArray(data,
coords=[time,lat, lon],
dims=['time','lat','lon'],
name=field)
ds_to_merge.append(test)
merged_ds = xr.merge(ds_to_merge)
filename_raw = '{}/{}_raw'.format(ref_dir,name)
merged_ds.to_netcdf(filename_raw)
files_generated.append(filename_raw)
filename = '{}/{}'.format(ref_dir,name)
cmd = 'cdo -setctomiss,-9e+33 {} {}'.format(filename_raw,filename)
status, _ = shell_cmd(cmd)
assert status == 0
files_generated.append(filename)
return files_generated
def generate_test(input_dir,folder,identifier,field):
files_generated = []
data_location = os.path.join(input_dir,folder)
os.makedirs(data_location,exist_ok=True)
data = np.full((1,100,100),0.45)
for year in range(2000,2006):
lat = np.arange(10,20,0.1)
lon = np.arange(10,20,0.1)
months = range(1,13)
for m in months:
month = '{:0>2d}'.format(m)
filename = '{}/{}_{}_{}.nc'.format(data_location,
year,
month,
identifier)
timestring = '{}{}01'.format(year,month)
time = np.float32(int(timestring))
netcdf = nc.Dataset(filename, "w", format='NETCDF4')
netcdf.createDimension('lon',100)
netcdf.createDimension('lat',100)
netcdf.createDimension('time', None)
time_var = netcdf.createVariable('time', np.float32, ('time',))
time_var.calendar = 'proleptic_gregorian'
time_var[0] = timestring
nc.date2num(time,calendar='proleptic_gregorian')
field_var = netcdf.createVariable(field,
np.float32,('time',
'lat',
'lon'))
field_var[0,:,:] = data
netcdf.close()
files_generated.append(filename)
return files_generated
def delete_data(files_to_delete):
for file in files_to_delete:
os.remove(file)