-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_nrcan_climatic_gridded_data.py
150 lines (117 loc) · 3.88 KB
/
read_nrcan_climatic_gridded_data.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
# -*- coding: utf-8 -*-
"""
Script to read and format NRCAN climate gridded data.
ftp://ftp.nrcan.gc.ca/pub/outgoing/canada_daily_grids/
"""
import csv
import numpy as np
import matplotlib.pyplot as plt
import re
import os.path as osp
import datetime as dt
from time import strftime
def read_daily_data(filename):
"""
Fonction pour lire un feuillet de données météo de la grille NRCAN
pour 1 journée.
"""
with open(filename, 'r') as f:
reader = list(csv.reader(f, delimiter=','))
reader = reader[6:]
data = [[]]
for row in reader:
values = re.findall('.{8}', row[0])
data[-1].extend(values)
if len(data[-1]) == 1068:
data.append([])
del data[-1]
data = np.array(data).astype(float)
data[data == -999] = np.nan
return data
# %%
# Définir la position des centroïdes des mailles de la grille.
delta_deg = 300 / 3600
latitudes = np.array(
list(reversed([41 + delta_deg/2 + delta_deg * i for i in range(510)])))
longitudes = np.array(
[-141 + delta_deg/2 + delta_deg * i for i in range(1068)])
# %%
# Définir les indexes à utiliser pour générer une sous-grille.
idx_latitudes = np.where((latitudes >= 45) & (latitudes <= 47))[0]
idx_longitudes = np.where((longitudes >= -66) & (longitudes <= -64))[0]
latitudes = latitudes[idx_latitudes]
longitudes = longitudes[idx_longitudes]
# %%
data_stack = []
dirname = "D:/Data/grill_meteo_nrcan/canada_mintemperature"
for year in range(2009, 2011):
for day in range(10):
print('\rReading day {} of year {} '.format(day + 1, year))
filename = dirname + '/{}/min{}_{}.asc'.format(year, year, day + 1)
if osp.exists(filename):
data = read_daily_data(filename)
data = data[idx_latitudes, :]
data = data[:, idx_longitudes]
data_stack.append(data)
print('')
data = np.stack(data_stack, axis=2)
_, _, nt = np.shape(data)
datetimes = [dt.datetime(2009, 1, 1) + dt.timedelta(days=1) * i for
i in range(nt)]
# %%
npfname = ("D:/Data/grill_meteo_nrcan/data_tmin_sussex_2009-2017.npy")
content = {'data': data,
'latitude': latitudes,
'longitude': longitudes,
'datetime': datetimes}
np.save(npfname, content, allow_pickle=True)
# %%
content = np.load(npfname, allow_pickle=True)
data = content.item()['data']
longitudes = content.item()['longitude']
latitudes = content.item()['latitude']
datetimes = content.item()['datetime']
fig, ax = plt.subplots(figsize=(7, 4))
ax.plot(datetimes, data[2, 2, :], '.-', lw=1,)
fig.autofmt_xdate()
fig.tight_layout()
fig2, ax2 = plt.subplots()
ax2.imshow(data[:, :, 5])
fig.tight_layout()
plt.show()
# %%
# Production des fichiers d'entrée pour PyHelp.
# Create an array of datestring and lat/lon
datestrings = [dt.strftime("%d/%m/%Y") for dt in datetimes]
ny, nx, nt = np.shape(data)
idx_row = np.repeat(np.arange(ny), nx)
idx_col = np.tile(np.arange(nx), ny)
# Reshape data into a 2d matrix.
data_2d = data[idx_row, idx_col, :].transpose()
lat_dd = latitudes[idx_row]
lon_dd = longitudes[idx_col]
# Remove cells with nan.
non_nan_idx = np.where(~np.isnan(data_2d[0, :]))[0]
data_2d = data_2d[:, non_nan_idx]
lat_dd = lat_dd[non_nan_idx]
lon_dd = lon_dd[non_nan_idx]
# Save the data to a csv file.
data_2d = data_2d.tolist()
lat_dd = lat_dd.tolist()
lon_dd = lon_dd.tolist()
varname = 'Minimum daily air temperature in \u00B0C'
fheader = [
[varname],
['', ''],
['Created on ' + strftime("%d/%m/%Y")],
['Created from NRCAN grid'],
['', ''],
['Latitude (dd)'] + lat_dd,
['Longitude (dd)'] + lon_dd,
['', '']]
fdata = [[datestrings[i]] + data_2d[i] for i in range(nt)]
fcontent = fheader + fdata
fname = 'D:/Data/grill_meteo_nrcan/min_airtemp_input_data.csv'
with open(fname, 'w', encoding='utf8') as csvfile:
writer = csv.writer(csvfile, delimiter=',', lineterminator='\n')
writer.writerows(fcontent)