Skip to content

Commit 4fbf590

Browse files
pulkkinsdnerini
andauthored
Fmi geotiff importer fixes (#284)
* Remove unnecessary encoding line * Remove unncecessary encoding line * Change imports to be compatible with the current version of GDAL * Add NoneType checks for scale and offset * Add zr relation parameters to metadata * Set unit to dBZ instead of None returned by GetUnitType * Set transform to dB * Update FMI PGM directory * Update black in pre-commit to latest version * Remove unused offset and scale lines * Add section for FMI GeoTIFF files * Add tests for the FMI GeoTIFF importer * Add gdal to requirements * Skip test if GDAL is missing * Install GDAL to test geotiff importer * Remove gdal from requirements * Add gdal as optional dependency in user guide Co-authored-by: Daniele Nerini <daniele.nerini@gmail.com>
1 parent f3216b2 commit 4fbf590

File tree

7 files changed

+66
-12
lines changed

7 files changed

+66
-12
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/psf/black
3-
rev: 21.7b0
3+
rev: 22.3.0
44
hooks:
55
- id: black
6-
language_version: python3
6+
language_version: python3

ci/ci_test_env.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ dependencies:
2727
- pandas
2828
- scikit-image
2929
- rasterio
30+
- gdal
3031
# Test dependencies
3132
- pytest
3233
- pytest-cov

doc/source/user_guide/install_pysteps.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Other optional dependencies include:
3333
visualization)
3434
* `h5py <https://www.h5py.org/>`_ (for importing HDF5 data)
3535
* `pygrib <https://jswhit.github.io/pygrib/docs/index.html>`_ (for importing MRMS data)
36+
* `gdal <https://gdal.org/>`_ (for importing GeoTIFF data)
3637
* `pywavelets <https://pywavelets.readthedocs.io/en/latest/>`_
3738
(for intensity-scale verification)
3839
* `pandas <https://pandas.pydata.org/>`_ and

pysteps/io/importers.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
"""
32
pysteps.io.importers
43
====================
@@ -99,8 +98,7 @@
9998
from pysteps.utils import aggregate_fields
10099

101100
try:
102-
import gdalconst
103-
from osgeo import gdal, osr
101+
from osgeo import gdal, gdalconst, osr
104102

105103
GDAL_IMPORTED = True
106104
except ImportError:
@@ -579,9 +577,8 @@ def import_fmi_geotiff(filename, **kwargs):
579577
f = gdal.Open(filename, gdalconst.GA_ReadOnly)
580578

581579
rb = f.GetRasterBand(1)
582-
precip = rb.ReadAsArray()
580+
precip = rb.ReadAsArray().astype(float)
583581
mask = precip == 255
584-
precip = precip.astype(float) * rb.GetScale() + rb.GetOffset()
585582
precip = (precip - 64.0) / 2.0
586583
precip[mask] = np.nan
587584

@@ -607,12 +604,14 @@ def import_fmi_geotiff(filename, **kwargs):
607604
else:
608605
metadata["yorigin"] = "lower"
609606
metadata["institution"] = "Finnish Meteorological Institute"
610-
metadata["unit"] = rb.GetUnitType()
611-
metadata["transform"] = None
607+
metadata["unit"] = "dBZ"
608+
metadata["transform"] = "dB"
612609
metadata["accutime"] = 5.0
613610
metadata["threshold"] = _get_threshold_value(precip)
614611
metadata["zerovalue"] = np.nanmin(precip)
615612
metadata["cartesian_unit"] = "m"
613+
metadata["zr_a"] = 223.0
614+
metadata["zr_b"] = 1.53
616615

617616
return precip, None, metadata
618617

pysteps/pystepsrc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
}
2626
},
2727
"fmi": {
28-
"root_path": "./radar/fmi",
28+
"root_path": "./radar/fmi/pgm",
2929
"path_fmt": "%Y%m%d",
3030
"fn_pattern": "%Y%m%d%H%M_fmi.radar.composite.lowest_FIN_SUOMI1",
3131
"fn_ext": "pgm.gz",
@@ -35,6 +35,15 @@
3535
"gzipped": true
3636
}
3737
},
38+
"fmi_geotiff": {
39+
"root_path": "./radar/fmi/geotiff",
40+
"path_fmt": "%Y%m%d",
41+
"fn_pattern": "%Y%m%d%H%M_FINUTM.tif",
42+
"fn_ext": "tif",
43+
"importer": "geotiff",
44+
"timestep": 5,
45+
"importer_kwargs": {}
46+
},
3847
"mch": {
3948
"root_path": "./radar/mch",
4049
"path_fmt": "%Y%m%d",

pysteps/tests/test_io_fmi_geotiff.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import os
2+
3+
import pytest
4+
5+
import pysteps
6+
from pysteps.tests.helpers import smart_assert
7+
8+
pytest.importorskip("pyproj")
9+
pytest.importorskip("osgeo")
10+
11+
root_path = pysteps.rcparams.data_sources["fmi_geotiff"]["root_path"]
12+
filename = os.path.join(
13+
root_path,
14+
"20160928",
15+
"201609281600_FINUTM.tif",
16+
)
17+
precip, _, metadata = pysteps.io.import_fmi_geotiff(filename)
18+
19+
20+
def test_io_import_fmi_geotiff_shape():
21+
"""Test the shape of the read file."""
22+
assert precip.shape == (7316, 4963)
23+
24+
25+
expected_proj = (
26+
"+proj=utm +zone=35 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs"
27+
)
28+
29+
# test_geodata: list of (variable,expected,tolerance) tuples
30+
test_geodata = [
31+
("projection", expected_proj, None),
32+
("x1", -196593.0043142295908183, 1e-10),
33+
("x2", 1044176.9413554778, 1e-10),
34+
("y1", 6255329.6988206729292870, 1e-10),
35+
("y2", 8084432.005259146, 1e-10),
36+
("xpixelsize", 250.0040188736061566, 1e-6),
37+
("ypixelsize", 250.0139839309011904, 1e-6),
38+
("cartesian_unit", "m", None),
39+
("yorigin", "upper", None),
40+
]
41+
42+
43+
@pytest.mark.parametrize("variable, expected, tolerance", test_geodata)
44+
def test_io_import_fmi_pgm_geodata(variable, expected, tolerance):
45+
"""Test the GeoTIFF and metadata reading."""
46+
smart_assert(metadata[variable], expected, tolerance)

pysteps/tests/test_io_fmi_pgm.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# -*- coding: utf-8 -*-
2-
31
import os
42

53
import pytest

0 commit comments

Comments
 (0)