-
Notifications
You must be signed in to change notification settings - Fork 18
/
gda_lib.py
286 lines (262 loc) · 11.2 KB
/
gda_lib.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import h5py
import numpy as np
import geopandas as gpd
import pandas as pd
from shapely.geometry import Point, Polygon
import rasterio
from rasterio import features
#import rasterstats as rs
# TODO sort functions alphabetically and add docstring
#this function is from Ben
def ATL06_to_dict(filename, dataset_dict):
"""
Read selected datasets from an ATL06 file
Input arguments:
filename: ATl06 file to read
dataset_dict: A dictinary describing the fields to be read
keys give the group names to be read,
entries are lists of datasets within the groups
Output argument:
D6: dictionary containing ATL06 data. Each dataset in
dataset_dict has its own entry in D6. Each dataset
in D6 contains a list of numpy arrays containing the
data
"""
D6=[]
pairs=[1, 2, 3]
beams=['l','r']
# open the HDF5 file
with h5py.File(filename) as h5f:
# loop over beam pairs
for pair in pairs:
# loop over beams
for beam_ind, beam in enumerate(beams):
# check if a beam exists, if not, skip it
if '/gt%d%s/land_ice_segments' % (pair, beam) not in h5f:
continue
# loop over the groups in the dataset dictionary
temp={}
for group in dataset_dict.keys():
for dataset in dataset_dict[group]:
DS='/gt%d%s/%s/%s' % (pair, beam, group, dataset)
# since a dataset may not exist in a file, we're going to try to read it, and if it doesn't work, we'll move on to the next:
try:
temp[dataset]=np.array(h5f[DS])
# some parameters have a _FillValue attribute. If it exists, use it to identify bad values, and set them to np.NaN
if '_FillValue' in h5f[DS].attrs:
fill_value=h5f[DS].attrs['_FillValue']
bad = temp[dataset]==fill_value
temp[dataset]=np.float64(temp[dataset])
temp[dataset][bad]=np.NaN
except KeyError as e:
pass
if len(temp) > 0:
# it's sometimes convenient to have the beam and the pair as part of the output data structure: This is how we put them there.
temp['pair']=np.zeros_like(temp['h_li'])+pair
temp['beam']=np.zeros_like(temp['h_li'])+beam_ind
#temp['filename']=filename
D6.append(temp)
return D6
#this function is from Ben
def ATL08_to_dict(filename, dataset_dict):
"""
Read selected datasets from an ATL06 file
Input arguments:
filename: ATl06 file to read
dataset_dict: A dictinary describing the fields to be read
keys give the group names to be read,
entries are lists of datasets within the groups
Output argument:
D6: dictionary containing ATL06 data. Each dataset in
dataset_dict has its own entry in D6. Each dataset
in D6 contains a list of numpy arrays containing the
data
"""
D6=[]
pairs=[1, 2, 3]
beams=['l','r']
# open the HDF5 file
with h5py.File(filename) as h5f:
# loop over beam pairs
for pair in pairs:
# loop over beams
for beam_ind, beam in enumerate(beams):
# check if a beam exists, if not, skip it
if '/gt%d%s/land_segments' % (pair, beam) not in h5f:
continue
# loop over the groups in the dataset dictionary
temp={}
for group in dataset_dict.keys():
for dataset in dataset_dict[group]:
DS='/gt%d%s/%s/%s' % (pair, beam, group, dataset)
# since a dataset may not exist in a file, we're going to try to read it, and if it doesn't work, we'll move on to the next:
try:
temp[dataset]=np.array(h5f[DS])
# some parameters have a _FillValue attribute. If it exists, use it to identify bad values, and set them to np.NaN
if '_FillValue' in h5f[DS].attrs:
fill_value=h5f[DS].attrs['_FillValue']
bad = temp[dataset]==fill_value
temp[dataset]=np.float64(temp[dataset])
temp[dataset][bad]=np.NaN
except KeyError as e:
pass
if len(temp) > 0:
# it's sometimes convenient to have the beam and the pair as part of the output data structure: This is how we put them there.
#a = np.zeros_like(temp['h_te_best_fit'])
#print(a)
temp['pair']=np.zeros_like(temp['h_te_best_fit'])+pair
temp['beam']=np.zeros_like(temp['h_te_best_fit'])+beam_ind
#temp['filename']=filename
D6.append(temp)
return D6
def ATL08_2_gdf(ATL06_fn,dataset_dict):
"""
function to convert ATL06 hdf5 to geopandas dataframe, containing columns as passed in dataset dict
Used Ben's ATL06_to_dict function
"""
if ('latitude' in dataset_dict['land_segments']) != True:
dataset_dict['land_segments'].append('latitude')
if ('longitude' in dataset_dict['land_segments']) != True:
dataset_dict['land_segments'].append('longitude')
#use Ben's Scripts to convert to dict
data_dict = ATL08_to_dict(ATL06_fn,dataset_dict)
#this will give us 6 tracks
i = 0
for track in data_dict:
#1 track
#convert to datafrmae
df = pd.DataFrame(track)
df['p_b'] = str(track['pair'][0])+'_'+str(track['beam'][0])
df['geometry'] = df.apply(point_covert,axis=1)
if i==0:
df_final = df.copy()
else:
df_final = df_final.append(df)
i = i+1
gdf_final = gpd.GeoDataFrame(df_final,geometry='geometry',crs={'init':'epsg:4326'})
return gdf_final
def dem2polygon(dem_file_name):
"""
Take DEM and return polygon geodataframe matching the extent and coordinate system of the input DEM.
Input parameters:
dem_file_name: Absolute path to a DEM file
Output parameters:
dem_polygon: A polygon geodataframe matching the extent and coordinate system of the input DEM.
"""
# read in dem using rasterio
dem = rasterio.open(dem_file_name)
# extact total bounds of dem
bbox = dem.bounds
# convert to corner points
p1 = Point(bbox[0], bbox[3])
p2 = Point(bbox[2], bbox[3])
p3 = Point(bbox[2], bbox[1])
p4 = Point(bbox[0], bbox[1])
# extract corner coordinates
np1 = (p1.coords.xy[0][0], p1.coords.xy[1][0])
np2 = (p2.coords.xy[0][0], p2.coords.xy[1][0])
np3 = (p3.coords.xy[0][0], p3.coords.xy[1][0])
np4 = (p4.coords.xy[0][0], p4.coords.xy[1][0])
# convert to polygon
bb_polygon = Polygon([np1, np2, np3, np4])
# create geodataframe
dem_polygon_gdf = gpd.GeoDataFrame(gpd.GeoSeries(bb_polygon), columns=['geometry'])
dem_polygon_gdf.crs = dem.crs
return dem_polygon_gdf
def point_covert(row):
geom = Point(row['longitude'],row['latitude'])
return geom
def ATL06_2_gdf(ATL06_fn,dataset_dict):
"""
function to convert ATL06 hdf5 to geopandas dataframe, containing columns as passed in dataset dict
Used Ben's ATL06_to_dict function
"""
if ('latitude' in dataset_dict['land_ice_segments']) != True:
dataset_dict['land_ice_segments'].append('latitude')
if ('longitude' in dataset_dict['land_ice_segments']) != True:
dataset_dict['land_ice_segments'].append('longitude')
#use Ben's Scripts to convert to dict
data_dict = ATL06_to_dict(ATL06_fn,dataset_dict)
#this will give us 6 tracks
i = 0
for track in data_dict:
#1 track
#convert to datafrmae
df = pd.DataFrame(track)
df['p_b'] = str(track['pair'][0])+'_'+str(track['beam'][0])
df['geometry'] = df.apply(point_covert,axis=1)
if i==0:
df_final = df.copy()
else:
df_final = df_final.append(df)
i = i+1
gdf_final = gpd.GeoDataFrame(df_final,geometry='geometry',crs={'init':'epsg:4326'})
return gdf_final
def get_ndv(ds):
no_data = ds.nodatavals[0]
if no_data == None:
#this means no data is not set in tif tag, nead to cheat it from raster
ndv = ds.read(1)[0,0]
else:
ndv = no_data
return ndv
def sample_near_nbor(ds,geom):
"""
sample values from raster at the given ICESat-2 points
using nearest neighbour algorithm
Inputs are a rasterio dataset and a geodataframe of ice_sat2 points
"""
# reproject the shapefile to raster projection
x_min,y_min,x_max,y_max = ds.bounds
geom = geom.to_crs(ds.crs)
#filter geom outside bounds
geom = geom.cx[x_min:x_max,y_min:y_max]
X = geom.geometry.x.values
Y = geom.geometry.y.values
xy = np.vstack((X,Y)).T
sampled_values = np.array(list(ds.sample(xy)))
no_data = get_ndv(ds)
sample = np.ma.fix_invalid(np.reshape(sampled_values,np.shape(sampled_values)[0]))
sample = np.ma.masked_equal(sample,no_data)
x_atc = np.ma.array(geom.x_atc.values,mask = sample.mask)
return x_atc, sample
def buffer_sampler(ds,geom,buffer,val='median',ret_gdf=False):
"""
sample values from raster at the given ICESat-2 points
using a buffer distance, and return median/mean or a full gdf ( if return gdf=True)
Inputs = rasterio dataset, Geodataframe containing points, buffer distance, output value = median/mean (default median)
and output format list of x_atc,output_value arrays (default) or full gdf
"""
import rasterstats as rs
ndv = get_ndv(ds)
array = ds.read(1)
gt = ds.transform
stat = val
geom = geom.to_crs(ds.crs)
x_min,y_min,x_max,y_max = ds.bounds
geom = geom.cx[x_min:x_max, y_min:y_max]
geom['geometry'] = geom.geometry.buffer(buffer)
json_stats = rs.zonal_stats(geom,array,affine=gt,geojson_out=True,stats=stat,nodata=ndv)
gdf = gpd.GeoDataFrame.from_features(json_stats)
if val =='median':
gdf = gdf.rename(columns={'median':'med'})
call = 'med'
else:
gdf = gdf.rename(columns={'mean':'avg'})
call = 'avg'
if ret_gdf:
out_file = gdf
else:
out_file = [gdf.x_atc.values,gdf[call].values]
return out_file
def concat_gdf(gdf_list):
"""
concatanate geodataframes into 1 geodataframe
Assumes all input geodataframes have same projection
Inputs : list of geodataframes in same projection
Output : 1 geodataframe containing everything having the same projection
"""
#from https://stackoverflow.com/questions/48874113/concat-multiple-shapefiles-via-geopandas
gdf = pd.concat([gdf for gdf in gdf_list]).pipe(gpd.GeoDataFrame)
gdf.crs = (gdf_list[0].crs)
return gdf