foo
This commit is contained in:
parent
4c0bc59480
commit
99a8686d52
2 changed files with 64 additions and 44 deletions
|
|
@ -20,21 +20,32 @@ search = client.search(
|
||||||
max_items=1000,
|
max_items=1000,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
items_by_key = {}
|
||||||
for item in search.items():
|
for item in search.items():
|
||||||
|
date = item.datetime.strftime("%Y%m%d")
|
||||||
|
parts = item.id.split("_")
|
||||||
|
increment = parts[3] if len(parts) > 3 else "0"
|
||||||
|
key = (date, increment)
|
||||||
|
if key not in items_by_key:
|
||||||
|
items_by_key[key] = item
|
||||||
|
|
||||||
|
for (date, increment), item in items_by_key.items():
|
||||||
|
filepath = os.path.join(output_dir, f"{date}_{increment}.geotiff")
|
||||||
|
if os.path.exists(filepath):
|
||||||
|
continue
|
||||||
|
|
||||||
|
band_data = {}
|
||||||
|
profile = None
|
||||||
|
|
||||||
for band_name, asset_name in bands.items():
|
for band_name, asset_name in bands.items():
|
||||||
if asset_name in item.assets:
|
if asset_name in item.assets:
|
||||||
asset = item.assets[asset_name]
|
asset = item.assets[asset_name]
|
||||||
filepath = os.path.join(output_dir, f"{item.id}_{band_name}.tif")
|
|
||||||
if not os.path.exists(filepath):
|
|
||||||
with rasterio.open(asset.href) as src:
|
with rasterio.open(asset.href) as src:
|
||||||
bbox_geom = {
|
bbox_geom = {
|
||||||
"type": "Polygon",
|
"type": "Polygon",
|
||||||
"coordinates": [[
|
"coordinates": [[
|
||||||
[bbox[0], bbox[1]],
|
[bbox[0], bbox[1]], [bbox[2], bbox[1]],
|
||||||
[bbox[2], bbox[1]],
|
[bbox[2], bbox[3]], [bbox[0], bbox[3]], [bbox[0], bbox[1]]
|
||||||
[bbox[2], bbox[3]],
|
|
||||||
[bbox[0], bbox[3]],
|
|
||||||
[bbox[0], bbox[1]]
|
|
||||||
]]
|
]]
|
||||||
}
|
}
|
||||||
bbox_transformed = transform_geom("EPSG:4326", src.crs, bbox_geom)
|
bbox_transformed = transform_geom("EPSG:4326", src.crs, bbox_geom)
|
||||||
|
|
@ -44,26 +55,28 @@ for item in search.items():
|
||||||
bbox_crs = [min(x_coords), min(y_coords), max(x_coords), max(y_coords)]
|
bbox_crs = [min(x_coords), min(y_coords), max(x_coords), max(y_coords)]
|
||||||
src_bounds = src.bounds
|
src_bounds = src.bounds
|
||||||
intersect_bbox = [
|
intersect_bbox = [
|
||||||
max(bbox_crs[0], src_bounds.left),
|
max(bbox_crs[0], src_bounds.left), max(bbox_crs[1], src_bounds.bottom),
|
||||||
max(bbox_crs[1], src_bounds.bottom),
|
min(bbox_crs[2], src_bounds.right), min(bbox_crs[3], src_bounds.top),
|
||||||
min(bbox_crs[2], src_bounds.right),
|
|
||||||
min(bbox_crs[3], src_bounds.top),
|
|
||||||
]
|
]
|
||||||
window = from_bounds(*intersect_bbox, src.transform)
|
window = from_bounds(*intersect_bbox, src.transform)
|
||||||
if window.height > 0 and window.width > 0:
|
if window.height > 0 and window.width > 0:
|
||||||
data = src.read(window=window)
|
data = src.read(window=window)
|
||||||
new_transform = window_transform(window, src.transform)
|
new_transform = window_transform(window, src.transform)
|
||||||
with rasterio.open(
|
if profile is None:
|
||||||
filepath, "w",
|
profile = {
|
||||||
driver="COG",
|
"driver": "GTiff", "height": window.height, "width": window.width,
|
||||||
height=window.height,
|
"count": len(bands), "dtype": data.dtype, "crs": src.crs,
|
||||||
width=window.width,
|
"transform": new_transform, "compress": "lzw"
|
||||||
count=src.count,
|
}
|
||||||
dtype=data.dtype,
|
band_idx = list(bands.keys()).index(band_name)
|
||||||
crs=src.crs,
|
band_data[band_idx] = data[0]
|
||||||
transform=new_transform,
|
|
||||||
compress="lzw",
|
if profile and len(band_data) == len(bands):
|
||||||
) as dst:
|
stacked = [band_data[i] for i in sorted(band_data.keys())]
|
||||||
dst.write(data)
|
band_names = [list(bands.keys())[i] for i in sorted(band_data.keys())]
|
||||||
print(f"Downloaded: {filepath}")
|
with rasterio.open(filepath, "w", **profile) as dst:
|
||||||
|
for i, data in enumerate(stacked, 1):
|
||||||
|
dst.write(data, i)
|
||||||
|
dst.set_band_description(i, band_names[i-1])
|
||||||
|
print(f"Saved: {filepath}")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -59,6 +59,7 @@ times = netCDF4.num2date(nc.variables['t'][:], nc.variables['t'].units)
|
||||||
x_coords = nc.variables['x'][:]
|
x_coords = nc.variables['x'][:]
|
||||||
y_coords = nc.variables['y'][:]
|
y_coords = nc.variables['y'][:]
|
||||||
band_vars = sorted([v for v in nc.variables.keys() if v.startswith('B') and v[1:].isdigit()])
|
band_vars = sorted([v for v in nc.variables.keys() if v.startswith('B') and v[1:].isdigit()])
|
||||||
|
band_names = [list(bands.keys())[openeo_bands.index(b)] for b in band_vars]
|
||||||
|
|
||||||
transform = from_bounds(
|
transform = from_bounds(
|
||||||
float(x_coords.min()), float(y_coords.min()),
|
float(x_coords.min()), float(y_coords.min()),
|
||||||
|
|
@ -66,13 +67,17 @@ transform = from_bounds(
|
||||||
len(x_coords), len(y_coords)
|
len(x_coords), len(y_coords)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
date_counts = {}
|
||||||
for t_idx, time_val in enumerate(times):
|
for t_idx, time_val in enumerate(times):
|
||||||
date_str = time_val.strftime("%Y%m%dT%H%M%S") if isinstance(time_val, datetime) else netCDF4.num2date(nc.variables['t'][t_idx], nc.variables['t'].units).strftime("%Y%m%dT%H%M%S")
|
dt = time_val if isinstance(time_val, datetime) else netCDF4.num2date(nc.variables['t'][t_idx], nc.variables['t'].units)
|
||||||
|
date_str = dt.strftime("%Y%m%d")
|
||||||
|
increment = date_counts.get(date_str, 0)
|
||||||
|
date_counts[date_str] = increment + 1
|
||||||
|
|
||||||
band_data = [nc.variables[b][t_idx, :, :] for b in band_vars]
|
band_data = [nc.variables[b][t_idx, :, :] for b in band_vars]
|
||||||
stacked = np.stack(band_data, axis=0)
|
stacked = np.stack(band_data, axis=0)
|
||||||
|
|
||||||
output_path = output_dir / f"S3_OLCI__{date_str}.tif"
|
output_path = output_dir / f"{date_str}_{increment}.geotiff"
|
||||||
with rasterio.open(
|
with rasterio.open(
|
||||||
output_path, 'w',
|
output_path, 'w',
|
||||||
driver='GTiff', height=len(y_coords), width=len(x_coords),
|
driver='GTiff', height=len(y_coords), width=len(x_coords),
|
||||||
|
|
@ -80,6 +85,8 @@ for t_idx, time_val in enumerate(times):
|
||||||
transform=transform, compress='lzw'
|
transform=transform, compress='lzw'
|
||||||
) as dst:
|
) as dst:
|
||||||
dst.write(stacked)
|
dst.write(stacked)
|
||||||
|
for i, band_name in enumerate(band_names, 1):
|
||||||
|
dst.set_band_description(i, band_name)
|
||||||
print(f"Saved: {output_path}")
|
print(f"Saved: {output_path}")
|
||||||
|
|
||||||
nc.close()
|
nc.close()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue