File: earthbigdata.py

package info (click to toggle)
tifffile 20260224-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,456 kB
  • sloc: python: 41,034; makefile: 7
file content (556 lines) | stat: -rw-r--r-- 15,040 bytes parent folder | download
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
# tifffile/examples/earthbigdata.py

# Copyright (c) 2021-2026, Christoph Gohlke
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
#    contributors may be used to endorse or promote products derived from
#    this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

# This file uses VSCode Jupyter-like code cells
# https://code.visualstudio.com/docs/python/jupyter-support-py

# %% [markdown]
"""
# Create an fsspec ReferenceFileSystem for a large set of remote GeoTIFF files

by [Christoph Gohlke](https://www.cgohlke.com)

Published October 9, 2021. Last updated February 24, 2026.

This Python script uses the [tifffile](https://github.com/cgohlke/tifffile) and
[imagecodecs](https://github.com/cgohlke/imagecodecs) packages to create a
[fsspec ReferenceFileSystem](https://github.com/fsspec/kerchunk) file in
JSON format for the [Earthbigdata](
http://sentinel-1-global-coherence-earthbigdata.s3-website-us-west-2.amazonaws.com
) set, which consists of 1,033,422 GeoTIFF files stored on AWS.
The ReferenceFileSystem is used to create a multi-dimensional Xarray dataset.

Refer to the discussion at [kerchunk/issues/78](
https://github.com/fsspec/kerchunk/issues/78).

Source code is available in the [tifffile repository](
https://github.com/cgohlke/tifffile/blob/master/examples/earthbigdata.py).
"""

# %%
import base64
import os

import imagecodecs.numcodecs
import kerchunk.utils
import matplotlib.pyplot
import xarray
import zarr  # >= 3

import tifffile
import tifffile.zarr

# %% [markdown]
"""
## Get a list of all remote TIFF files

Call the AWS command line app to recursively list all files in the Earthbigdata
set. Cache the output in a local file. Filter the list for TIFF files and
remove the common path.
"""

# %%
if not os.path.exists('earthbigdata.txt'):
    os.system(
        'aws s3 ls sentinel-1-global-coherence-earthbigdata/data/tiles '
        '--recursive > earthbigdata.txt'
    )

with open('earthbigdata.txt', encoding='utf-8') as fh:
    tiff_files = [line.split()[-1][11:] for line in fh if '.tif' in line]
print('Number of TIFF files:', len(tiff_files))


# %% [markdown]
"""
## Define metadata to describe the dataset

Define labels, coordinate arrays, file name regular expression patterns, and
categories for all dimensions in the Earthbigdata set.
"""

# %%
baseurl = (
    'https://'
    'sentinel-1-global-coherence-earthbigdata.s3.us-west-2.amazonaws.com'
    '/data/tiles/'
)

chunkshape = (1200, 1200)
fillvalue = 0

latitude_label = 'latitude'
latitude_pattern = rf'(?P<{latitude_label}>[NS]\d+)'
latitude_coordinates = [
    (j * -0.00083333333 - 0.000416666665 + i)
    for i in range(82, -79, -1)
    for j in range(1200)
]
latitude_category = {}
i = 0
for j in range(82, -1, -1):
    latitude_category[f'N{j:02}'] = i
    i += 1
for j in range(1, 79):
    latitude_category[f'S{j:02}'] = i
    i += 1

longitude_label = 'longitude'
longitude_pattern = rf'(?P<{longitude_label}>[EW]\d+)'
longitude_coordinates = [
    (j * 0.00083333333 + 0.000416666665 + i)
    for i in range(-180, 180)
    for j in range(1200)
]
longitude_category = {}
i = 0
for j in range(180, 0, -1):
    longitude_category[f'W{j:03}'] = i
    i += 1
for j in range(180):
    longitude_category[f'E{j:03}'] = i
    i += 1

season_label = 'season'
season_category = {'winter': 0, 'spring': 1, 'summer': 2, 'fall': 3}
season_coordinates = list(season_category.keys())
season_pattern = rf'_(?P<{season_label}>{"|".join(season_category)})'

polarization_label = 'polarization'
polarization_category = {'vv': 0, 'vh': 1, 'hv': 2, 'hh': 3}
polarization_coordinates = list(polarization_category.keys())
polarization_pattern = (
    rf'_(?P<{polarization_label}>{"|".join(polarization_category)})'
)

coherence_label = 'coherence'
coherence_category = {
    '06': 0,
    '12': 1,
    '18': 2,
    '24': 3,
    '36': 4,
    '48': 5,
}
coherence_coordinates = [int(i) for i in coherence_category]
coherence_pattern = (
    rf'_COH(?P<{coherence_label}>{"|".join(coherence_category)})'
)

orbit_label = 'orbit'
orbit_coordinates = list(range(1, 176))
orbit_pattern = rf'_(?P<{orbit_label}>\d+)'

flightdirection_label = 'flightdirection'
flightdirection_category = {'A': 0, 'D': 1}
flightdirection_coordinates = list(flightdirection_category.keys())
flightdirection_pattern = (
    rf'(?P<{flightdirection_label}>{"|".join(flightdirection_category)})_'
)


# %% [markdown]
"""
## Open a file for writing the fsspec ReferenceFileSystem in JSON format
"""

# %%
jsonfile = open('earthbigdata.json', 'w', encoding='utf-8', newline='\n')

# %% [markdown]
"""
## Write the coordinate arrays

Add the coordinate arrays to a Zarr group, convert it to a fsspec
ReferenceFileSystem JSON string, and write it to the open file.
"""

# %%
coordinates = {}  # type: ignore[var-annotated]
zarrgroup = zarr.open_group(coordinates, use_consolidated=False, zarr_format=2)
zararray = zarrgroup.create_array(
    longitude_label,
    shape=[len(longitude_coordinates)],
    dtype='float32',
    # compressor='zlib',
)
zararray[:] = longitude_coordinates
zararray.attrs['_ARRAY_DIMENSIONS'] = [longitude_label]

zararray = zarrgroup.create_array(
    latitude_label,
    shape=[len(latitude_coordinates)],
    dtype='float32',
    # compressor='zlib',
)
zararray[:] = latitude_coordinates
zararray.attrs['_ARRAY_DIMENSIONS'] = [latitude_label]

zararray = zarrgroup.create_array(
    season_label,
    shape=[len(season_coordinates)],
    dtype='|U6',
    compressor=None,
)
zararray[:] = season_coordinates
zararray.attrs['_ARRAY_DIMENSIONS'] = [season_label]

zararray = zarrgroup.create_array(
    polarization_label,
    shape=[len(polarization_coordinates)],
    dtype='|U2',
    compressor=None,
)
zararray[:] = polarization_coordinates
zararray.attrs['_ARRAY_DIMENSIONS'] = [polarization_label]

zararray = zarrgroup.create_array(
    coherence_label,
    shape=[len(coherence_coordinates)],
    dtype='uint8',
    compressor=None,
)
zararray[:] = coherence_coordinates
zararray.attrs['_ARRAY_DIMENSIONS'] = [coherence_label]

zararray = zarrgroup.create_array(
    orbit_label, shape=[len(orbit_coordinates)], dtype='int32'
)
zararray[:] = orbit_coordinates
zararray.attrs['_ARRAY_DIMENSIONS'] = [orbit_label]

zararray = zarrgroup.create_array(
    flightdirection_label,
    shape=[len(flightdirection_coordinates)],
    dtype='|U1',
    compressor=None,
)
zararray[:] = flightdirection_coordinates
zararray.attrs['_ARRAY_DIMENSIONS'] = [flightdirection_label]

# base64 encode any values containing non-ascii characters
for key, value in coordinates.items():
    val = value.to_bytes()
    try:
        coordinates[key] = val.decode()
    except UnicodeDecodeError:
        coordinates[key] = 'base64:' + base64.b64encode(val).decode()

coordinates_json = tifffile.zarr._json_dumps(coordinates).decode()

jsonfile.write(coordinates_json[:-2])  # skip the last newline and brace

# %% [markdown]
"""
## Create a TiffSequence from a list of file names

Filter the list of GeoTIFF files for files containing coherence 'COH' data.
The regular expression pattern and categories are used to parse the file names
for chunk indices.

Note: the created TiffSequence cannot be used to access any files. The file
names do not refer to existing files. The `baseurl` is later used to get
the real location of the files.
"""

# %%
mode = 'COH'
fileseq = tifffile.TiffSequence(
    [file for file in tiff_files if '_' + mode in file],
    pattern=(
        latitude_pattern
        + longitude_pattern
        + season_pattern
        + polarization_pattern
        + coherence_pattern
    ),
    categories={
        latitude_label: latitude_category,
        longitude_label: longitude_category,
        season_label: season_category,
        polarization_label: polarization_category,
        coherence_label: coherence_category,
    },
)
assert len(fileseq) == 444821
assert fileseq.files_missing == 5119339
assert fileseq.shape == (161, 360, 4, 4, 6)
assert fileseq.dims == (
    'latitude',
    'longitude',
    'season',
    'polarization',
    'coherence',
)
print(fileseq)


# %% [markdown]
"""
## Create a ZarrTiffStore from the TiffSequence

Define `axestiled` to tile the latitude and longitude dimensions of the
TiffSequence with the first and second image/chunk dimensions.
Define extra `zattrs` to create an Xarray compatible store.
"""

# %%
store = fileseq.aszarr(
    chunkdtype='uint8',
    chunkshape=chunkshape,
    fillvalue=fillvalue,
    axestiled={0: 0, 1: 1},
    zattrs={
        '_ARRAY_DIMENSIONS': [
            season_label,
            polarization_label,
            coherence_label,
            latitude_label,
            longitude_label,
        ]
    },
)
print(store)

# %% [markdown]
"""
## Append the ZarrTiffStore to the open ReferenceFileSystem file

Use the mode name to create a Zarr subgroup.
Use the `imagecodecs_tiff` Numcodecs compatible codec for decoding TIFF files.
"""

# %%
store.write_fsspec(
    jsonfile,
    baseurl,
    groupname=mode,
    codec_id='imagecodecs_tiff',
    _append=True,
    _close=False,
)
store.close()

# %% [markdown]
"""
## Repeat for the other modes

Repeat the `TiffSequence->aszarr->write_fsspec` workflow for the other modes.
"""

# %%
for mode in (
    'AMP',
    'tau',
    'rmse',
    'rho',
):
    fileseq = tifffile.TiffSequence(
        [file for file in tiff_files if '_' + mode in file],
        pattern=(
            latitude_pattern
            + longitude_pattern
            + season_pattern
            + polarization_pattern
        ),
        categories={
            latitude_label: latitude_category,
            longitude_label: longitude_category,
            season_label: season_category,
            polarization_label: polarization_category,
        },
    )
    print(fileseq)
    with fileseq.aszarr(
        chunkdtype='uint16',
        chunkshape=chunkshape,
        fillvalue=fillvalue,
        axestiled={0: 0, 1: 1},
        zattrs={
            '_ARRAY_DIMENSIONS': [
                season_label,
                polarization_label,
                latitude_label,
                longitude_label,
            ]
        },
    ) as store:
        print(store)
        store.write_fsspec(
            jsonfile,
            baseurl,
            groupname=mode,
            codec_id='imagecodecs_tiff',
            _append=True,
            _close=False,
        )

modes = ('inc', 'lsmap')
for mode in modes:
    fileseq = tifffile.TiffSequence(
        [file for file in tiff_files if '_' + mode in file],
        pattern=(
            latitude_pattern
            + longitude_pattern
            + orbit_pattern
            + flightdirection_pattern
        ),
        categories={
            latitude_label: latitude_category,
            longitude_label: longitude_category,
            # orbit has no category
            flightdirection_label: flightdirection_category,
        },
    )
    print(fileseq)
    with fileseq.aszarr(
        chunkdtype='uint8',
        chunkshape=chunkshape,
        fillvalue=fillvalue,
        axestiled={0: 0, 1: 1},
        zattrs={
            '_ARRAY_DIMENSIONS': [
                orbit_label,
                flightdirection_label,
                latitude_label,
                longitude_label,
            ]
        },
    ) as store:
        print(store)
        store.write_fsspec(
            jsonfile,
            baseurl,
            groupname=mode,
            codec_id='imagecodecs_tiff',
            _append=True,
            _close=mode == modes[-1],
        )

# %% [markdown]
"""
## Close the JSON file
"""

# %%
jsonfile.close()

# %% [markdown]
"""
## Use the fsspec ReferenceFileSystem file to create an Xarray dataset

Register `imagecodecs.numcodecs` before using the ReferenceFileSystem.
"""

# %%
imagecodecs.numcodecs.register_codecs()

# %% [markdown]
"""
### Create an Xarray dataset from the ReferenceFileSystem file

Use `mask_and_scale` to disable conversion to floating point.
"""

# %%
store = kerchunk.utils.refs_as_store('earthbigdata.json')
dataset = xarray.open_zarr(store, consolidated=False, mask_and_scale=False)
print(dataset)

# %% [markdown]
"""
### Select the Southern California region in the dataset
"""

# %%
socal = dataset.sel(latitude=slice(36, 32.5), longitude=slice(-121, -115))
print(socal)

# %% [markdown]
"""
### Plot a selection of the dataset

The few GeoTIFF files comprising the selection are transparently downloaded,
decoded, and stitched to an in-memory NumPy array and plotted using Matplotlib.
"""

# %%
image = socal['COH'].loc['winter', 'vv', 12]
assert image[100, 100] == 53
xarray.plot.imshow(image, figsize=(10.8, 6), aspect=1.8)
matplotlib.pyplot.show()

# %% [markdown]
"""
## System information

Print information about the software used to run this script.
"""


# %%
def system_info() -> str:
    """Print information about Python environment."""
    import datetime
    import sys

    import fsspec
    import imagecodecs
    import kerchunk
    import matplotlib
    import numcodecs
    import numpy
    import xarray
    import zarr

    import tifffile

    return '\n'.join(
        (
            sys.executable,
            f'Python {sys.version}',
            '',
            f'numpy {numpy.__version__}',
            f'matplotlib {matplotlib.__version__}',
            f'tifffile {tifffile.__version__}',
            f'imagecodecs {imagecodecs.__version__}',
            f'numcodecs {numcodecs.__version__}',
            f'fsspec {fsspec.__version__}',
            f'kerchunk {kerchunk.__version__}',
            f'xarray {xarray.__version__}',
            f'zarr {zarr.__version__}',
            '',
            str(datetime.datetime.now()),
        )
    )


print(system_info())