-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_netcdf.py
154 lines (140 loc) · 4.61 KB
/
read_netcdf.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
# reading data
from nclib2.dataset import DataSet
from read_metadata import (
read_channels_dir_and_pattern,
read_satellite_name,
read_start_slot,
)
from read_metadata import read_indexes_dir_and_pattern, read_satellite_step
from read_metadata import read_mask_dir_and_pattern
from utils import get_nb_slots_per_day, np
def read_channels(
channels, latitudes, longitudes, dfb_beginning, dfb_ending, slot_step=1
):
dir, pattern = read_channels_dir_and_pattern()
satellite = read_satellite_name()
satellite_step = read_satellite_step()
nb_slots = get_nb_slots_per_day(satellite_step, slot_step)
patterns = [
pattern.replace("{SATELLITE}", satellite).replace("{CHANNEL}", chan)
for chan in channels
]
nb_days = dfb_ending - dfb_beginning + 1
content = np.empty(
(nb_slots * nb_days, len(latitudes), len(longitudes), len(patterns))
)
start = read_start_slot()
for k in range(len(patterns)):
pattern = patterns[k]
chan = channels[k]
dataset = DataSet.read(
dirs=dir,
extent={
"latitude": latitudes,
"longitude": longitudes,
"dfb": {
"start": dfb_beginning,
"end": dfb_ending,
"end_inclusive": True,
"start_inclusive": True,
},
"slot": np.arange(start, start + nb_slots, step=slot_step),
},
file_pattern=pattern,
variable_name=chan,
fill_value=np.nan,
interpolation="N",
max_processes=0,
)
data = dataset["data"].data
day_slot_b = 0
day_slot_e = nb_slots
for day in range(nb_days):
content[day_slot_b:day_slot_e, :, :, k] = data[day]
day_slot_b += nb_slots
day_slot_e += nb_slots
return content
def read_classes(latitudes, longitudes, dfb_beginning, dfb_ending, slot_step=1):
dir, pattern = read_indexes_dir_and_pattern("classes")
satellite_step = read_satellite_step()
nb_slots = get_nb_slots_per_day(satellite_step, slot_step)
nb_days = dfb_ending - dfb_beginning + 1
content = np.empty((nb_slots * nb_days, len(latitudes), len(longitudes)))
dataset = DataSet.read(
dirs=dir,
extent={
"latitude": latitudes,
"longitude": longitudes,
"dfb": {
"start": dfb_beginning,
"end": dfb_ending,
"end_inclusive": True,
"start_inclusive": True,
},
"slot": {
"enumeration": np.arange(0, nb_slots, step=slot_step),
"override_type": "slot",
},
},
file_pattern=pattern,
variable_name="Classes",
fill_value=np.nan,
interpolation="N",
max_processes=0,
)
data = dataset["data"].data
day_slot_b = 0
day_slot_e = nb_slots
for day in range(nb_days):
content[day_slot_b:day_slot_e, :, :] = data[day]
day_slot_b += nb_slots
day_slot_e += nb_slots
return content
def read_temperature_forecast(latitudes, longitudes, dfb_beginning, dfb_ending):
dir, pattern = read_mask_dir_and_pattern("temperature_forecast")
nb_days = dfb_ending - dfb_beginning + 1
content = np.empty((24 * nb_days, len(latitudes), len(longitudes)))
dataset = DataSet.read(
dirs=dir,
extent={
"latitude": latitudes % 180,
"longitude": longitudes % 360,
"day": {
"start": dfb_beginning,
"end": dfb_ending,
"end_inclusive": True,
"start_inclusive": True,
},
"time": {
"enumeration": np.linspace(0.0, 24.0, num=24, endpoint=False),
"override_type": "hours",
},
},
file_pattern=pattern,
variable_name="temp_2m",
fill_value=np.nan,
interpolation="N",
max_processes=0,
)
data = dataset["data"].data
day_slot_b = 0
day_slot_e = 24
for day in range(nb_days):
content[day_slot_b:day_slot_e, :, :] = data[day]
day_slot_b += 24
day_slot_e += 24
return content
def read_land_mask(latitudes, longitudes):
dir, pattern = read_mask_dir_and_pattern("land")
land = DataSet.read(
dirs=dir,
extent={
"lat": latitudes,
"lon": longitudes,
},
file_pattern=pattern,
variable_name="Band1",
interpolation="N",
max_processes=0,
)
return land["data"] == 1