mikonvergence
commited on
Commit
•
93185d1
1
Parent(s):
e108957
Create extras/thumbnail_s1rtc.py
Browse files- extras/thumbnail_s1rtc.py +80 -0
extras/thumbnail_s1rtc.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
NOTE: Major TOM standard does not require any specific type of thumbnail to be computed.
|
3 |
+
|
4 |
+
Instead these are shared as optional help since this is how the Core dataset thumbnails have been computed.
|
5 |
+
"""
|
6 |
+
|
7 |
+
from rasterio.io import MemoryFile
|
8 |
+
from PIL import Image
|
9 |
+
import numpy as np
|
10 |
+
|
11 |
+
def s1rtc_thumbnail(vv, vh, vv_NODATA = -32768.0, vh_NODATA = -32768.0):
|
12 |
+
"""
|
13 |
+
Takes vv and vh numpy arrays along with the corresponding NODATA values (default is -32768.0)
|
14 |
+
|
15 |
+
Returns a numpy array with the thumbnail
|
16 |
+
"""
|
17 |
+
|
18 |
+
# valid data masks
|
19 |
+
vv_mask = vv != vv_NODATA
|
20 |
+
vh_mask = vh != vh_NODATA
|
21 |
+
|
22 |
+
# remove invalid values before log op
|
23 |
+
vv[vv<0] = vv[vv>=0].min()
|
24 |
+
vh[vh<0] = vh[vh>=0].min()
|
25 |
+
|
26 |
+
# apply log op
|
27 |
+
vv_dB = 10*np.log10(vv)
|
28 |
+
vh_dB = 10*np.log10(vh)
|
29 |
+
|
30 |
+
# scale to 0-255
|
31 |
+
vv_dB = (vv_dB - vv_dB[vv_mask].min()) / (vv_dB[vv_mask].max() - vv_dB[vv_mask].min()) * 255
|
32 |
+
vh_dB = (vh_dB - vh_dB[vh_mask].min()) / (vh_dB[vh_mask].max() - vh_dB[vh_mask].min()) * 255
|
33 |
+
|
34 |
+
# represent nodata as 0
|
35 |
+
vv_dB[vv_mask==0] = 0
|
36 |
+
vh_dB[vh_mask==0] = 0
|
37 |
+
|
38 |
+
# false colour composite
|
39 |
+
return np.stack([vv_dB,
|
40 |
+
255*(vv_dB+vh_dB)/np.max(vv_dB+vh_dB),
|
41 |
+
vh_dB
|
42 |
+
],-1).astype(np.uint8)
|
43 |
+
|
44 |
+
def s1rtc_thumbnail_from_datarow(datarow):
|
45 |
+
"""
|
46 |
+
Takes a datarow directly from one of the data parquet files
|
47 |
+
|
48 |
+
Returns a PIL Image
|
49 |
+
"""
|
50 |
+
|
51 |
+
with MemoryFile(datarow['vv'][0].as_py()) as mem_f:
|
52 |
+
with mem_f.open(driver='GTiff') as f:
|
53 |
+
vv=f.read().squeeze()
|
54 |
+
vv_NODATA = f.nodata
|
55 |
+
|
56 |
+
with MemoryFile(datarow['vh'][0].as_py()) as mem_f:
|
57 |
+
with mem_f.open(driver='GTiff') as f:
|
58 |
+
vh=f.read().squeeze()
|
59 |
+
vh_NODATA = f.nodata
|
60 |
+
|
61 |
+
img = s1rtc_thumbnail(vv, vh, vv_NODATA=vv_NODATA, vh_NODATA=vh_NODATA)
|
62 |
+
|
63 |
+
return Image.fromarray(img)
|
64 |
+
|
65 |
+
if __name__ == '__main__':
|
66 |
+
from fsspec.parquet import open_parquet_file
|
67 |
+
import pyarrow.parquet as pq
|
68 |
+
|
69 |
+
print('[example run] reading file from HuggingFace...')
|
70 |
+
url = "https://huggingface.co/datasets/Major-TOM/Core-S1RTC/resolve/main/images/part_00001.parquet"
|
71 |
+
with open_parquet_file(url) as f:
|
72 |
+
with pq.ParquetFile(f) as pf:
|
73 |
+
first_row_group = pf.read_row_group(1)
|
74 |
+
|
75 |
+
print('[example run] computing the thumbnail...')
|
76 |
+
thumbnail = s1rtc_thumbnail_from_datarow(first_row_group)
|
77 |
+
|
78 |
+
thumbnail_fname = 'example_thumbnail.png'
|
79 |
+
thumbnail.save(thumbnail_fname, format = 'PNG')
|
80 |
+
print('[example run] saved as "{}"'.format(thumbnail_fname))
|