Commit 012dacc0 authored by Niklas Bohn's avatar Niklas Bohn
Browse files

Improved extraction to netcdf file

parent de3f1e9f
Pipeline #2616 passed with stages
in 17 minutes and 4 seconds
...@@ -581,65 +581,98 @@ def json_to_netcdf(out_mode, api_result, outpath, out_prefix, geo_ll, geo_ur, st ...@@ -581,65 +581,98 @@ def json_to_netcdf(out_mode, api_result, outpath, out_prefix, geo_ll, geo_ur, st
"interest, time of interest and wanted band combination" "interest, time of interest and wanted band combination"
f.acknowledgement = api_result['Acknowledgement'] f.acknowledgement = api_result['Acknowledgement']
for key in api_result.keys(): for key in api_result.keys():
if key == 'Results' or key == 'Metadata': if key == 'ControlValues':
key_group = f.createGroup(key) key_group = f.createGroup(key)
if key == 'Results': [setattr(key_group, attr, api_result[key][attr]) for attr in api_result[key].keys()]
for tile in api_result[key].keys(): if key == 'Request':
tile_group = key_group.createGroup(tile) key_group = f.createGroup(key)
for band in api_result[key][tile].keys(): [setattr(key_group, attr.replace("-", "_"), str(api_result[key][attr]))
band_group = tile_group.createGroup(band) for attr in api_result[key].keys() if attr != 'bands' and attr != 'resolution']
band_group.time = api_result[key][tile][band]['time'] key_group.setncattr_string('bands', api_result[key]['bands'])
# band_group.sensor = api_result[key][tile][band]['sensor'] <-- please include this key_group.setncattr_string('resolution', api_result[key]['resolution'])
for data_info in api_result[key][tile][band].keys(): if key == 'RequestGeoInfo':
if data_info != 'time': key_group = f.createGroup(key)
data_info_group = band_group.createGroup(data_info) tile_group = key_group.createGroup('Tiles')
if data_info == 'data': [setattr(tile_group, attr, api_result[key]['Tiles'][attr])
band_arr = np.asarray(api_result[key][tile][band][data_info][0]) for attr in api_result[key]['Tiles'].keys()]
data_info_group.createDimension('x', band_arr.shape[0]) if key == 'Results':
data_info_group.createDimension('y', band_arr.shape[1]) key_group = f.createGroup(key)
data = data_info_group.createVariable('Data', 'i4', ('x', 'y'), for tile in api_result[key].keys():
fill_value=-1) tile_group = key_group.createGroup(tile)
data.units = "None" for band in api_result[key][tile].keys():
if level == "L1C": band_group = tile_group.createGroup(band)
data.long_name = "Top of Atmosphere Reflectance" band_group.setncattr_string('sensor', api_result[key][tile][band]['sensor'])
data.standard_name = "toa_reflectance" band_group.setncattr_string('time', api_result[key][tile][band]['time'])
else: for data_info in api_result[key][tile][band].keys():
data.long_name = "Surface Reflectance" if data_info == 'mapinfo':
data.standard_name = "boa_reflectance" data_info_group = band_group.createGroup(data_info)
data.valid_range = np.array((-2000.0, 16000.0)) [setattr(data_info_group, attr, api_result[key][tile][band][data_info][attr])
data.actual_range = np.array((np.min(band_arr), np.max(band_arr))) for attr in api_result[key][tile][band][data_info].keys()]
data[:, :] = band_arr if data_info == 'data':
data_info_group.Band = band.split("_")[0].split("B")[-1] data_group = band_group.createGroup(data_info)
data_info_group.Resolution = band.split("_")[-1] band_arr = np.asarray(api_result[key][tile][band][data_info])
data_info_group.Central_Wavelength = wl[str(band.split("_")[0])] data_group.createDimension('x', band_arr.shape[2])
if data_info == 'mapinfo': data_group.createDimension('y', band_arr.shape[1])
[setattr(data_info_group, attr, data_group.createDimension('t', band_arr.shape[0])
str(api_result[key][tile][band][data_info][str(attr)])) for attr data = data_group.createVariable('Data', 'i4', ('x', 'y', 't'), fill_value=255)
in api_result[key][tile][band][data_info].keys()] data.units = "None"
if key == 'Metadata': if level == "L1C":
for tile in api_result[key].keys(): data.long_name = "Top of Atmosphere Reflectance"
tile_group = key_group.createGroup(tile) data.standard_name = "toa_reflectance"
else:
data.long_name = "Surface Reflectance"
data.standard_name = "boa_reflectance"
data.valid_range = np.array((-2000.0, 16000.0))
data.actual_range = np.array((np.min(band_arr), np.max(band_arr)))
data[:, :, :] = band_arr
data_group.Band = band.split("_")[0].split("B")[-1]
data_group.Resolution = band.split("_")[-1]
data_group.Central_Wavelength = wl[str(band.split("_")[0])]
if key == 'Metadata':
key_group = f.createGroup(key)
for tile in api_result[key].keys():
tile_group = key_group.createGroup(tile)
if tile == 'MSK_legend':
[setattr(tile_group, "Class_" + str(attr), api_result[key][tile][attr])
for attr in api_result[key][tile].keys()]
else:
for msk in api_result[key][tile].keys(): for msk in api_result[key][tile].keys():
mask_group = tile_group.createGroup(msk) mask_group = tile_group.createGroup(msk)
if msk == 'MSK_20m': mask_group.setncattr_string('time', api_result[key][tile][msk]['time'])
[setattr(mask_group, attr, for data_info in api_result[key][tile][msk].keys():
str(api_result[key][tile][msk][str(attr)])) for attr in if data_info == 'cloud_stats':
api_result[key][tile][msk].keys() if attr != 'data'] data_info_group = mask_group.createGroup(data_info)
data_group = mask_group.createGroup('data') data_info_group.setncattr_string('cloud_frac',
mask_arr = np.asarray(api_result[key][tile][msk]['data'][0]) api_result[key][tile][msk][data_info]
data_group.createDimension('x', mask_arr.shape[0]) ['cloud_frac'])
data_group.createDimension('y', mask_arr.shape[1]) if data_info == 'mapinfo':
data = data_group.createVariable('Data', 'i4', ('x', 'y'), fill_value=255) data_info_group = mask_group.createGroup(data_info)
data.units = "None" [setattr(data_info_group, attr, api_result[key][tile][msk][data_info][attr])
data.long_name = "Mask classes" for attr in api_result[key][tile][msk][data_info].keys()]
data.standard_name = "classes" if data_info == 'stats':
data.valid_range = np.array((10.0, 60.0)) data_info_group = mask_group.createGroup(data_info)
data.actual_range = np.array((np.min(mask_arr), np.max(mask_arr))) data_info_group.setncattr_string('cloud_frac',
data[:, :] = mask_arr api_result[key][tile][msk][data_info]
if msk == 'MSK_legend': ['cloud_frac'])
[setattr(mask_group, attr, data_info_group.setncattr_string('data_frac',
str(api_result[key][tile][msk][str(attr)])) for attr in api_result[key][tile][msk][data_info]
api_result[key][tile][msk].keys()] ['data_frac'])
data_info_group.setncattr_string('time',
api_result[key][tile][msk][data_info]['time'])
if data_info == 'data':
data_group = mask_group.createGroup(data_info)
mask_arr = np.asarray(api_result[key][tile][msk][data_info])
data_group.createDimension('x', mask_arr.shape[2])
data_group.createDimension('y', mask_arr.shape[1])
data_group.createDimension('t', mask_arr.shape[0])
data = data_group.createVariable('Data', 'i4', ('x', 'y', 't'), fill_value=255)
data.units = "None"
data.long_name = "Mask classes"
data.standard_name = "classes"
data.valid_range = np.array((10.0, 60.0))
data.actual_range = np.array((np.min(mask_arr), np.max(mask_arr)))
data[:, :, :] = mask_arr
f.close() f.close()
except: except:
raise Exception("Something went wrong while saving as netcdf. " + traceback.format_exc()) raise Exception("Something went wrong while saving as netcdf. " + traceback.format_exc())
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment