-
Notifications
You must be signed in to change notification settings - Fork 0
/
h5_to_csv_day.py
158 lines (134 loc) · 5.12 KB
/
h5_to_csv_day.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
import h5py
import os
import csv
from threading import Thread
from points import points_h5
import random
def get_month(file=''):
date = list(file.split('_')[4])
m1 = date[4]
m2 = date[5]
return m1 + m2
def get_day(file=''):
date = list(file.split('_')[4])
m1 = date[6]
m2 = date[7]
return m1 + m2
added_latlng = {}
def exists(latLong, csv_des):
if latLong in added_latlng:
return added_latlng[latLong]
return -1
csv_dest = []
csv_columns = ['latitude', 'longitude']
for i in range(1, 13):
csv_columns.append('soil_moisture_'+str(i).zfill((2)))
csv_columns.append('surface_temperature_'+str(i).zfill((2)))
files = []
lens = [0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
f_dest = 'S_DEST'
upper = [11.696331, -86.003182]
lower = [-55.715245, -19.998368]
# Compute indexes
indexes = []
h5f = h5py.File('smapl3/2016/01/SMAP_L3_SM_P_20160101_R16510_001.h5', 'r')
ds_lat = h5f['Soil_Moisture_Retrieval_Data_AM']['latitude']
ds_lng = h5f['Soil_Moisture_Retrieval_Data_AM']['longitude']
print("Computing indexes")
for i in range(0, 406):
for j in range(0, 964):
lat = ds_lat[i][j]
lng = ds_lng[i][j]
if str(lat) + ',' + str(lng) in points_h5:
indexes.append((i, j))
print("Computed indexes: " + str(len(indexes)))
targets = (
[{'f': 'SMAP_L3_SM_P_20160101_R16510_001.h5', 'm': '01', 'd': '01'}], [],)
#r = root, d = directories, f = files
# for r, d, f in os.walk('smapl3/2016'):
# for file in f:
# if file.endswith('.h5'):
# m = get_month(file)
# d = get_day(file)
# if int(m) % 2 == 0:
# targets[0].append({'f': file, 'm': m, 'd': d})
# else:
# targets[1].append({'f': file, 'm': m, 'd': d})
def crawl(files=[], csv_dest=[], t=''):
for data in files:
file = data['f']
m = data['m']
d = data['d']
h5f = h5py.File('smapl3/2016/' + m + '/' + file, 'r')
# h5f = h5py.File('smapl3/2017/' + file, 'r')
ds_sm = h5f['Soil_Moisture_Retrieval_Data_AM']['soil_moisture']
ds_st = h5f['Soil_Moisture_Retrieval_Data_AM']['surface_temperature']
ds_lat = h5f['Soil_Moisture_Retrieval_Data_AM']['latitude']
ds_lng = h5f['Soil_Moisture_Retrieval_Data_AM']['longitude']
indexes_indexes = list(range(len(indexes)))
random.shuffle(indexes_indexes)
soil_key = 'soil_moisture_' + m
tmp_key = 'surface_temperature_' + m
for iii in indexes_indexes:
elem = indexes[iii]
i = elem[0]
j = elem[1]
lat = ds_lat[i][j]
lng = ds_lng[i][j]
sm = ds_sm[i][j]
st = ds_st[i][j]
lat_lng_str = str(lat)+","+str(lng)
lat_str = str(lat)
lng_str = str(lng)
index = exists(lat_lng_str, csv_dest)
if index != -1:
# print(t + ": Exists " + lat_lng_str + " m:" + m)
if (soil_key) in csv_dest[index]:
csv_dest[index][soil_key] = (
csv_dest[index][soil_key][0] + sm, csv_dest[index][soil_key][1] + 1)
csv_dest[index][tmp_key] = (
csv_dest[index][tmp_key][0] + st, csv_dest[index][tmp_key][1] + 1)
else:
csv_dest[index][soil_key] = (sm, 1)
csv_dest[index][tmp_key] = (st, 1)
else:
print(t + ": Doesnt exists " + lat_lng_str + " m:" + m)
csv_dest.append({
'latitude': lat_str,
'longitude': lng_str,
soil_key: (sm, 1),
tmp_key: (st, 1),
})
added_latlng[lat_lng_str] = len(csv_dest) - 1
iindex = exists(lat_lng_str, csv_dest)
count = csv_dest[iindex][soil_key][1]
print(csv_dest[iindex][soil_key][0])
if csv_dest[iindex][soil_key][0] == -9999.0:
csv_dest[iindex][soil_key] = (0, count - 1)
if csv_dest[iindex][tmp_key][0] == -9999.0:
csv_dest[iindex][tmp_key] = (0, csv_dest[iindex][tmp_key][1] - 1)
else:
csv_dest[iindex][tmp_key] = (
csv_dest[iindex][tmp_key][0] - 273.15, csv_dest[iindex][tmp_key][1])
if lens[int(m)] == count:
print(t + ": Done with " + m)
csv_dest[iindex][soil_key] = (
csv_dest[iindex][soil_key] / lens[int(m)], 99)
csv_dest[iindex][tmp_key] = (
csv_dest[iindex][tmp_key] / lens[int(m)], 99)
h5f.close()
threads = []
for ii in range(len(targets)):
# We start one thread per url present.
data = targets[ii]
process = Thread(target=crawl, args=[data, csv_dest, str(ii)])
process.start()
threads.append(process)
for process in threads:
process.join()
currentPath = os.getcwd()
csv_file = currentPath + "/csv/Names23.csv"
with open('names232016.csv', 'w', newline='') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=csv_columns)
writer.writeheader()
writer.writerows(csv_dest)