-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentinel3_gridder.py
154 lines (136 loc) · 4.49 KB
/
sentinel3_gridder.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
import datetime as dt
import logging
import subprocess
from pathlib import Path
from osgeo import gdal
gdal.UseExceptions()
logger = logging.getLogger("canada_datastore")
PRODUCT_LUT = {
"F1_BT_fn.nc": ["F1_BT_fn", "geodetic_fn.nc"],
"F2_BT_in.nc": ["F2_BT_in", "geodetic_in.nc"],
"S6_radiance_an.nc": ["S6_radiance_an", "geodetic_an.nc"],
"S7_BT_in.nc": ["S7_BT_in", "geodetic_in.nc"],
"S8_BT_in.nc": ["S8_BT_in", "geodetic_in.nc"],
"S9_BT_in.nc": ["S9_BT_in", "geodetic_in.nc"],
}
def convert_coords(gdal_fname, output_fname) -> None:
cmd = [
"gdal_calc.py",
"-A",
gdal_fname,
"--outfile",
output_fname,
"--calc",
"A/1.0e6",
"--type",
"Float32",
"--overwrite",
]
try:
subprocess.run(cmd, check=True)
except subprocess.CalledProcessError as e:
print(f"Error executing gdal_calc.py: {e}")
def to_vrt(gdal_dataset: str, tag: str) -> Path:
"""Converts GDAL dataset such as 'NETCDF:"geodetic_fn.nc":latitude_fn'
to
Args:
gdal_dataset (str): _description_
tag (str): _description_
Returns:
Path: _description_
"""
layer = gdal_dataset.lstrip("NETCDF:").split(":")[0]
layer = layer.replace("'", "")
layer = layer.replace('"', "")
layer = layer.replace(".nc", f"_{tag}.tif")
_ = gdal.Translate(
layer,
gdal_dataset,
unscale=True,
outputType=gdal.GDT_Float32,
noData=-32768,
)
return Path(layer)
def to_tif(product: Path) -> Path:
if not isinstance(product, Path):
product = Path(product)
fname = product.name
path = product.parent
layer = PRODUCT_LUT[fname][0]
output_file = path / f"{layer}.vrt"
output_tif = path / f"{layer}.tif"
if output_tif.exists():
return output_tif
geo = path / (PRODUCT_LUT[fname][1])
tag = PRODUCT_LUT[fname][1].rsplit(".", 1)[0].split("_")[1]
gdal_dataset = f'NETCDF:"{product}":{layer}'
lon = f'NETCDF:"{geo}":longitude_{tag}'
lat = f'NETCDF:"{geo}":latitude_{tag}'
convert_coords(lon, path / f"longitude_{tag}.tif")
convert_coords(lat, path / f"latitude_{tag}.tif")
lon = path / f"longitude_{tag}.tif"
lat = path / f"latitude_{tag}.tif"
gdal_dataset = to_vrt(gdal_dataset, "VAL")
geocode_vrt(gdal_dataset.as_posix(), lat, lon, output_file)
_ = gdal.Warp(
output_tif.as_posix(),
output_file.as_posix(),
format="COG",
srcSRS="EPSG:4326",
dstSRS="EPSG:3347",
xRes=1000,
yRes=1000,
creationOptions=["COMPRESS=DEFLATE"],
)
_ = None
logger.info(f"Written{output_file}")
return output_tif
def geocode_vrt(
data_fname: str, lat_file: str, lon_file: str, output_file: str | Path
) -> None:
g = gdal.Open(data_fname)
xsize = g.RasterXSize
ysize = g.RasterYSize
data_str = f"""<VRTRasterBand band="1" datatype="Float32">
<SimpleSource>
<SourceFilename relativeToVRT="0">{data_fname}</SourceFilename>
<SourceBand>1</SourceBand>
<SourceProperties RasterXSize="{xsize}" RasterYSize="{ysize}" DataType="Float32"/>
<SrcRect xOff="0" yOff="0" xSize="{xsize}" ySize="{ysize}" />
<DstRect xOff="0" yOff="0" xSize="{xsize}" ySize="{ysize}" />
</SimpleSource>
</VRTRasterBand>
""" # noqa: E501
ds_str = f"""<VRTDataset rasterXSize="{xsize}" rasterYSize="{ysize}">
<metadata domain="GEOLOCATION">
<mdi key="X_DATASET">{lon_file}</mdi>
<mdi key="X_BAND">1</mdi>
<mdi key="Y_DATASET">{lat_file}</mdi>
<mdi key="Y_BAND">1</mdi>
<mdi key="PIXEL_OFFSET">0</mdi>
<mdi key="LINE_OFFSET">0</mdi>
<mdi key="PIXEL_STEP">1</mdi>
<mdi key="LINE_STEP">1</mdi>
</metadata>
{data_str}
</VRTDataset>"""
Path(output_file).write_text(ds_str)
def find_files(path: str | Path) -> dict:
path = Path(path)
for k, v in PRODUCT_LUT.items():
logger.info("Doing " + k)
to_tif((path / k))
# Clean up temporary files
for fich in path.glob("*vrt"):
fich.unlink()
for fich in path.glob("longitude*tif"):
fich.unlink()
for fich in path.glob("latitude*tif"):
fich.unlink()
fnames = [f for f in path.glob("*.tif")]
logger.info(fnames)
date = gdal.Info((path / "F2_BT_in.nc").as_posix(), format="json")[
"metadata"
][""]["NC_GLOBAL#start_time"]
date = dt.datetime.strptime(date, "%Y-%m-%dT%H:%M:%S.%fZ")
return {date: fnames}