forked from emit-sds/emit-ghg
-
Notifications
You must be signed in to change notification settings - Fork 2
/
masked_plume_delineator.py
395 lines (304 loc) · 16.5 KB
/
masked_plume_delineator.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#! /usr/bin/env python
#
# Copyright 2023 California Institute of Technology
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Authors: Philip G. Brodrick, [email protected]
import argparse
import os
import scipy
import numpy as np
from osgeo import gdal
import matplotlib.pyplot as plt
from scipy import signal
from scipy.ndimage import gaussian_filter
from apply_glt import single_image_ortho
import json
import glob
import shapely
from shapely.geometry import Point
from shapely.geometry.polygon import Polygon
import shapely.ops
from datetime import datetime, timedelta
import subprocess
class SerialEncoder(json.JSONEncoder):
"""Encoder for json to help ensure json objects can be passed to the workflow manager.
"""
def default(self, obj):
if isinstance(obj, np.integer):
return int(obj)
elif isinstance(obj, np.floating):
return float(obj)
else:
return super(SerialEncoder, self).default(obj)
def write_science_cog(output_img, output_file, geotransform, projection):
tmp_file = os.path.splitext(output_file)[0] + '_tmp.tif'
driver = gdal.GetDriverByName('GTiff')
driver.Register()
outDataset = driver.Create(tmp_file,output_img.shape[1],output_img.shape[0],1,gdal.GDT_Float32,options = ['COMPRESS=LZW'])
outDataset.GetRasterBand(1).WriteArray(output_img)
outDataset.GetRasterBand(1).SetNoDataValue(-9999)
outDataset.SetProjection(projection)
outDataset.SetGeoTransform(geotransform)
del outDataset
subprocess.call(f'sh /home/brodrick/bin/cog.sh {tmp_file} {output_file}',shell=True)
subprocess.call(f'rm {tmp_file}',shell=True)
def write_output_file(source_ds, output_img, output_file):
driver = gdal.GetDriverByName('GTiff')
driver.Register()
if len(output_img.shape) == 2:
outDataset = driver.Create(output_file,source_ds.RasterXSize,source_ds.RasterYSize,1,gdal.GDT_Byte,options = ['COMPRESS=LZW'])
outDataset.GetRasterBand(1).WriteArray(output_img)
else:
outDataset = driver.Create(output_file,source_ds.RasterXSize,source_ds.RasterYSize,3,gdal.GDT_Byte,options = ['COMPRESS=LZW'])
for n in range(1,4):
outDataset.GetRasterBand(n).WriteArray(output_img[...,n-1])
outDataset.GetRasterBand(n).SetNoDataValue(0)
outDataset.SetProjection(source_ds.GetProjection())
outDataset.SetGeoTransform(source_ds.GetGeoTransform())
del outDataset
def plume_mask(input: np.array, pm, style='ch4'):
y_locs = np.where(np.sum(pm > 0, axis=1))[0]
x_locs = np.where(np.sum(pm > 0, axis=0))[0]
plume_dat = input[y_locs[0]:y_locs[-1],x_locs[0]:x_locs[-1]].copy()
plume_dat[pm[y_locs[0]:y_locs[-1],x_locs[0]:x_locs[-1]] == 0] = 0
if style == 'ch4':
plume_dat = gauss_blur(plume_dat, 3, preserve_nans=False)
local_output_mask = plume_dat > 50
else:
plume_dat = gauss_blur(plume_dat, 5, preserve_nans=False)
local_output_mask = plume_dat > 10000
output_plume_mask = np.zeros(pm.shape,dtype=bool)
output_plume_mask[y_locs[0]:y_locs[-1],x_locs[0]:x_locs[-1]] = local_output_mask
#output_plume_mask[signal.convolve2d(output_plume_mask,np.ones((10,10)),mode='same') > 10*10*0.25] = True
labels, label_counts = scipy.ndimage.label(local_output_mask)
out_labels = np.zeros(pm.shape,dtype=int)
out_labels[y_locs[0]:y_locs[-1],x_locs[0]:x_locs[-1]] = labels
return output_plume_mask, out_labels
def gauss_blur(img, sigma, preserve_nans=False):
V=img.copy()
V[np.isnan(img)]=0
VV=gaussian_filter(V,sigma=sigma)
W=0*img.copy()+1
W[np.isnan(img)]=0
WW=gaussian_filter(W,sigma=sigma)
img_smooth=VV/WW
if preserve_nans:
img_smooth[np.isnan(img)] = np.nan
return img_smooth
def main(input_args=None):
parser = argparse.ArgumentParser(description="Delineate/colorize plume")
parser.add_argument('input_file', type=str, metavar='INPUT', help='path to input image')
parser.add_argument('gltfile', type=str, metavar='glt file', help='confining location of plume')
#parser.add_argument('igmfile', type=str, metavar='igm file', help='lat,lon,elev of plume')
parser.add_argument('output_file', type=str, metavar='OUTPUT', help='path to output image')
parser.add_argument('maskfiles', type=str, nargs='+', metavar='plume_mask', help='confining location of plume')
parser.add_argument('-output_mask', type=str, default=None, metavar='output mask', help='path to output mask image')
parser.add_argument('-vmax', type=float, nargs=1, default=1500)
parser.add_argument('-blur_sigma', type=float, default=0)
parser.add_argument('-daac_demo_dir', type=str, default=None)
args = parser.parse_args(input_args)
# Load Data
ds = gdal.Open(args.input_file,gdal.GA_ReadOnly)
dat = ds.ReadAsArray().astype(np.float32)
input_plume_mask = None
for mask_file in args.maskfiles:
if input_plume_mask is None:
input_plume_mask = np.load(mask_file)
else:
input_plume_mask = input_plume_mask + np.load(mask_file)
input_plume_mask = input_plume_mask >= 1
# Load GLT
glt_ds = gdal.Open(args.gltfile)
glt = glt_ds.ReadAsArray().transpose((1,2,0))
trans = glt_ds.GetGeoTransform()
# Load IGM
#igm_ds = gdal.Open(args.igmfile)
#igm = igm_ds.ReadAsArray().transpose((1,2,0))
# Create Mask
if args.daac_demo_dir is not None:
out_l2b_name = os.path.join(args.daac_demo_dir, 'l2b', os.path.splitext(os.path.basename(args.gltfile).replace('glt','ch4mf'))[0] + '_mv0.tif')
write_science_cog(single_image_ortho(dat.reshape((dat.shape[0],dat.shape[1],1)), glt)[...,0], out_l2b_name, glt_ds.GetGeoTransform(), glt_ds.GetProjection())
dat[dat == ds.GetRasterBand(1).GetNoDataValue()] = np.nan
plume, plume_labels = plume_mask(dat, input_plume_mask)
# Make the output colored plume image
rawdat = dat.copy()
background = np.round(np.nanstd(rawdat[np.logical_and(np.logical_not(plume), rawdat != -9999) ]))
rawdat[np.logical_not(plume)] = -9999
print(f'rawdat 0: {np.sum(rawdat == 0)}')
dat[np.logical_not(plume)] = np.nan
dat = gauss_blur(dat, args.blur_sigma)
dat[np.logical_not(plume)] = 0
print(f'rawdat 0: {np.sum(rawdat == 0)}')
dat /= 1500
dat[dat > 1] = 1
dat[dat < 0] = 0
dat[dat == 0] = 0.01
if args.blur_sigma > 0:
dat[np.logical_not(plume)] = np.nan
dat = gauss_blur(dat, args.blur_sigma)
colorized = np.zeros((dat.shape[0],dat.shape[1],3))
colorized[plume,:] = plt.cm.plasma(dat[plume])[...,:3]
colorized = np.round(colorized * 255).astype(np.uint8)
colorized[plume,:] = np.maximum(1, colorized[plume,:])
colorized = single_image_ortho(colorized, glt)
rawshape = (rawdat.shape[0],rawdat.shape[1],1)
print(f'rawdat 0: {np.sum(rawdat == 0)}')
rawdat = single_image_ortho(rawdat.reshape(rawshape), glt)[...,0]
print(f'rawdat 0: {np.sum(rawdat == 0)}, {rawdat[1570, 934]}')
plume_labels = single_image_ortho(plume_labels.reshape(rawshape), glt)[...,0]
plume_labels[plume_labels == -9999] = 0
rawdat[rawdat == -9999] = np.nan
print(f'rawdat 0: {np.sum(rawdat == 0)}, {rawdat[1570, 934]}')
#rawdat[rawdat < 0] = 0
write_output_file(glt_ds, colorized, args.output_file)
plume_polygons = None
if args.output_mask is not None:
write_output_file(ds, plume, args.output_mask)
outmask_ort_file = os.path.splitext(args.output_mask)[0] + '_ort.tif'
outmask_poly_file = os.path.splitext(args.output_mask)[0] + '_polygon.json'
write_output_file(glt_ds, plume_labels, outmask_ort_file)
subprocess.call(f'rm {outmask_poly_file}',shell=True)
subprocess.call(f'gdal_polygonize.py {outmask_ort_file} {outmask_poly_file} -f GeoJSON -mask {outmask_ort_file}',shell=True)
raw_plume_polygons = json.load(open(outmask_poly_file))
plume_polygons = {}
for feat in raw_plume_polygons['features']:
plume_polygons[str(feat['properties']['DN'])] = feat['geometry']
print(plume_polygons)
fid = os.path.basename(args.input_file).split('_')[0]
date=fid[4:]
time=fid.split('t')[-1]
start_datetime = datetime.strptime(fid[4:], "%Y%m%dt%H%M%S")
end_datetime = start_datetime + timedelta(seconds=1)
start_datetime = start_datetime.strftime("%Y-%m-%dT%H:%M:%SZ")
end_datetime = end_datetime.strftime("%Y-%m-%dT%H:%M:%SZ")
#datetime = f'{date[:4]}-{date[4:6]}-{date[6:8]}T{time[:2]}:{time[2:4]}:{time[4:6]}Z'
#datetime_e = f'{date[:4]}-{date[4:6]}-{date[6:8]}T{time[:2]}:{time[2:4]}:{str(int(time[4:6])+1).zfill(2)}Z'
l1b_rad = glob.glob(f'/beegfs/store/emit/ops/data/acquisitions/{fid[4:12]}/{fid.split("_")[0]}/l1b/EMIT_L1B_RAD*_cnm.out')
if len(l1b_rad) > 0:
l1b_rad = os.path.basename(l1b_rad[0]).split('.')[0][:-20]
l1b_link = f'https://data.lpdaac.earthdatacloud.nasa.gov/lp-prod-protected/EMITL1BRAD.001/{l1b_rad}/{l1b_rad}.nc'
else:
l1b_link = f'Scene Not Yet Available'
proj_ds = gdal.Warp('', args.gltfile, dstSRS='EPSG:3857', format='VRT')
transform_3857 = proj_ds.GetGeoTransform()
xsize_m = transform_3857[1]
ysize_m = transform_3857[5]
del proj_ds
print(xsize_m, ysize_m)
# Make output vector
outdict = {"crs": {"properties": {"name": "urn:ogc:def:crs:OGC:1.3:CRS84"}, "type": "name"},"features":[],"name":"methane_metadata","type":"FeatureCollection" }
un_labels = np.unique(plume_labels)[1:]
loclist = []
for _lab, lab in enumerate(un_labels):
maxval = np.nanmax(rawdat[plume_labels == lab])
if np.sum(plume_labels == lab) < 15 or maxval < 200:
continue
rawloc = np.where(np.logical_and(rawdat == maxval, plume_labels == lab))
maxval = np.round(maxval)
#sum and convert to kg. conversion:
# ppm m / 1e6 ppm * x_pixel_size(m)*y_pixel_size(m) 1e3 L / m^3 * 1 mole / 22.4 L * 0.01604 kg / mole
ime_scaler = (1.0/1e6)* ((np.abs(xsize_m*ysize_m))/1.0) * (1000.0/1.0) * (1.0/22.4)*(0.01604/1.0)
ime_ss = rawdat[plume_labels == lab].copy()
ime = np.nansum(ime_ss) * ime_scaler
ime_p = np.nansum(ime_ss[ime_ss > 0]) * ime_scaler
#if np.isfinite(ime) is False:
# print(rawdat[plume_labels == lab])
# print(np.nansum(rawdat[plume_labels == lab]))
ime = np.round(ime,2)
ime_uncert = np.round(np.sum(plume_labels == lab) * background * ime_scaler,2)
y_locs = np.where(np.sum(plume_labels == lab, axis=1))[0]
x_locs = np.where(np.sum(plume_labels == lab, axis=0))[0]
radius = np.sqrt( ((y_locs[0] - y_locs[-1])*trans[5])**2 + ((x_locs[0] - x_locs[-1])*trans[1])**2)/2.
center_y = trans[3] + trans[5] * (y_locs[0] + (y_locs[-1]-y_locs[0])/2)
center_x = trans[0] + trans[1] * (x_locs[0] + (x_locs[-1]-x_locs[0])/2)
point = shapely.geometry.Point(center_x, center_y)
circle = shapely.geometry.polygon.Polygon(point.buffer(radius))
#loclist.append(rawloc)
max_loc_y = trans[3] + trans[5]*(rawloc[0][0]+0.5)
max_loc_x = trans[0] + trans[1]*(rawloc[1][0]+0.5)
if args.daac_demo_dir is not None:
out_l3_name = os.path.join(args.daac_demo_dir, 'l3', os.path.splitext(os.path.basename(args.gltfile).replace('glt','ch4mf'))[0] + f'_mv0_p{_lab}.tif')
outl3 = rawdat[y_locs[0]:y_locs[-1],x_locs[0]:x_locs[-1]].copy()
outl3[np.isnan(outl3)] = -9999
outtrans = list(glt_ds.GetGeoTransform()).copy()
outtrans[0] += x_locs[0] * outtrans[1]
outtrans[3] += y_locs[0] * outtrans[5]
print(_lab, x_locs[0], y_locs[0])
write_science_cog(outl3, out_l3_name, outtrans, glt_ds.GetProjection())
#loc_z = 0
#lloc = igm[rawloc[0][0], rawloc[1][0],:].tolist()
#loc_res = {"geometry": {"coordinates": [loc_x, loc_y, loc_z], "type": "Point"},
# "type": "Feature",
# "properties": {"UTC Time Observed": start_datetime,
# "map_endtime": end_datetime,
# "Max Plume Concentration (ppm m)": maxval,
# "Concentration Uncertainty (ppm m)": background,
# "Scene FID": fid,
# "L1B Radiance Download": l1b_link,
# "label": f'UTC Time Observed: {start_datetime}\nMax Plume Concentration (ppm m): {maxval}\nConcentration Uncertainty (ppm m): {background}'}}
#
#loc_res = {"geometry": {"coordinates": [list(circle.exterior.coords)], "type": "Polygon"},
#loc_res = {"geometry": {"coordinates": [center_x, center_y, 0.0], "type": "Point"},
# "vis_style": {"radius": radius},
# "type": "Feature",
# "properties": {"UTC Time Observed": start_datetime,
# "map_endtime": end_datetime,
# "Max Plume Concentration (ppm m)": maxval,
# "Concentration Uncertainty (ppm m)": background,
# "Integrated Methane Enhancement (kg CH4)": ime,
# "Integrated Methane Enhancement Uncertainty (kg CH4)": ime_uncert,
# "Latitude of max concentration": max_loc_y,
# "Longitude of max concentration": max_loc_x,
# "Scene FID": fid,
# "L1B Radiance Download": l1b_link
# }}
content_props = {"UTC Time Observed": start_datetime,
"map_endtime": end_datetime,
"Max Plume Concentration (ppm m)": maxval,
"Concentration Uncertainty (ppm m)": background,
#"Integrated Methane Enhancement (kg CH4)": ime,
#"Integrated Methane Enhancement - Positive (kg CH4)": ime_p,
#"Integrated Methane Enhancement Uncertainty (kg CH4)": ime_uncert,
"Latitude of max concentration": max_loc_y,
"Longitude of max concentration": max_loc_x,
"Scene FID": fid,
"L1B Radiance Download": l1b_link
}
if plume_polygons is not None:
props = content_props.copy()
props['style'] = {"maxZoom": 20, "minZoom": 0, "color": "white", 'opacity': 1, 'weight': 2, 'fillOpacity': 0}
loc_res = {"geometry": plume_polygons[str(int(lab))],
"type": "Feature",
"properties": props}
outdict['features'].append(loc_res)
props = content_props.copy()
props['style'] = {"radius": 10, 'minZoom': 0, "maxZoom": 9, "color": "red", 'opacity': 1, 'weight': 2, 'fillOpacity': 0}
#loc_res = {"geometry": {"coordinates": [center_x, center_y, 0.0], "type": "Point"},
loc_res = {"geometry": {"coordinates": [max_loc_x, max_loc_y, 0.0], "type": "Point"},
"properties": props,
"type": "Feature"}
outdict['features'].append(loc_res)
with open(os.path.splitext(args.output_file)[0] + '.json', 'w') as fout:
fout.write(json.dumps(outdict, cls=SerialEncoder, indent=2, sort_keys=True))
#plt.imshow(colorized.astype(np.float32)/255.)
#for feat in outdict['features']:
# plt.scatter((feat['geometry']['coordinates'][0] - trans[0])/trans[1], (feat['geometry']['coordinates'][1] - trans[3]) / trans[5], edgecolors='red',s=10, facecolors='none')
##for loc in loclist:
## plt.scatter(loclist[0],loclist[1], edgecolors='red',s=80, facecolors='none')
#plt.ylim([750,1250])
#plt.xlim([1500,2000])
#plt.savefig(f'test_{fid}.png',dpi=300)
if __name__ == '__main__':
main()