From 433d6eb3566a30c05bf8f997ad52a780e50bd5d7 Mon Sep 17 00:00:00 2001 From: Zhang Yunjun Date: Sun, 3 Sep 2023 21:55:06 +0800 Subject: [PATCH 1/4] readfile: support GDAL int8 dtype + add the correction int8 conversion between GDAL and NumPy in DATA_TYPE_GDAL2NUMPY/NUMPY2GDAL + add numpy_to_gdal_dtype() from dolphin + add gdal_to_numpy_dtype() from dolphin --- src/mintpy/utils/readfile.py | 44 +++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/src/mintpy/utils/readfile.py b/src/mintpy/utils/readfile.py index 205df4c35..e486082ef 100644 --- a/src/mintpy/utils/readfile.py +++ b/src/mintpy/utils/readfile.py @@ -18,6 +18,7 @@ import h5py import numpy as np +from numpy.typing import DTypeLike from mintpy.objects import ( DSET_UNIT_DICT, @@ -135,11 +136,11 @@ 11: 'complex128', 12: 'uint64', 13: 'int64', + 14: 'int8', # GDAL >= 3.7 } DATA_TYPE_NUMPY2GDAL = { "uint8" : 1, # GDT_Byte - "int8" : 1, # GDT_Int8 (GDAL >= 3.7) "uint16" : 2, # GDT_UInt16 "int16" : 3, # GDT_Int16 "uint32" : 4, # GDT_UInt32 @@ -152,6 +153,7 @@ "complex128": 11, # GDT_CFloat64 "uint64" : 12, # GDT_UInt64 (GDAL >= 3.5) "int64" : 13, # GDT_Int64 (GDAL >= 3.5) + "int8" : 14, # GDT_Int8 (GDAL >= 3.7) } # 3 - ISCE @@ -204,7 +206,43 @@ } -########################################################################### +######################################################################### +def numpy_to_gdal_dtype(np_dtype: DTypeLike) -> int: + """Convert NumPy dtype to GDAL dtype. + + Parameters: np_dtype : DTypeLike, NumPy dtype to convert. + Returns: gdal_code : int, GDAL type code corresponding to `np_dtype`. + """ + from osgeo import gdal_array, gdalconst + np_dtype = np.dtype(np_dtype) + + # convert dtype using the GDAL function/attribute + if np.issubdtype(bool, np_dtype): + gdal_code = gdalconst.GDT_Byte + else: + gdal_code = gdal_array.NumericTypeCodeToGDALTypeCode(np_dtype) + + # if provided dtype is not support GDAL, e.g. np.dtype('>i4') + if gdal_code is None: + raise TypeError(f"dtype {np_dtype} not supported by GDAL.") + + return gdal_code + + +def gdal_to_numpy_dtype(gdal_dtype: Union[str, int]) -> np.dtype: + """Convert GDAL dtype to NumPy dtype. + + Parameters: gdal_dtype : str/int, GDAL dtype to convert. + Returns: np_dtype : DTypeLike, NumPy dtype + """ + from osgeo import gdal, gdal_array + if isinstance(gdal_dtype, str): + gdal_dtype = gdal.GetDataTypeByName(gdal_dtype) + np_dtype = np.dtype(gdal_array.GDALTypeCodeToNumericTypeCode(gdal_dtype)) + return np_dtype + + +######################################################################### ## Slice-based data identification for data reading: ## ## slice : np.ndarray in 2D, with/without '-' in their names @@ -252,7 +290,7 @@ ## recons-20150115 ## cmask ## -########################################################################### +######################################################################### From 82eb38fbcda910d8de669cde267efaba133b9531 Mon Sep 17 00:00:00 2001 From: Zhang Yunjun Date: Sun, 3 Sep 2023 21:59:44 +0800 Subject: [PATCH 2/4] Update readfile.py --- src/mintpy/utils/readfile.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/mintpy/utils/readfile.py b/src/mintpy/utils/readfile.py index e486082ef..16bd55c28 100644 --- a/src/mintpy/utils/readfile.py +++ b/src/mintpy/utils/readfile.py @@ -15,6 +15,7 @@ import sys import warnings import xml.etree.ElementTree as ET +from typing import Union import h5py import numpy as np From 20efd44ede20e88248969a7ca151f6faff0c4252 Mon Sep 17 00:00:00 2001 From: Zhang Yunjun Date: Sun, 3 Sep 2023 22:08:18 +0800 Subject: [PATCH 3/4] update comment style --- src/mintpy/utils/readfile.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/mintpy/utils/readfile.py b/src/mintpy/utils/readfile.py index 16bd55c28..4ff80debe 100644 --- a/src/mintpy/utils/readfile.py +++ b/src/mintpy/utils/readfile.py @@ -211,8 +211,8 @@ def numpy_to_gdal_dtype(np_dtype: DTypeLike) -> int: """Convert NumPy dtype to GDAL dtype. - Parameters: np_dtype : DTypeLike, NumPy dtype to convert. - Returns: gdal_code : int, GDAL type code corresponding to `np_dtype`. + Parameters: np_dtype - DTypeLike, NumPy dtype to convert. + Returns: gdal_code - int, GDAL type code corresponding to `np_dtype`. """ from osgeo import gdal_array, gdalconst np_dtype = np.dtype(np_dtype) @@ -233,8 +233,8 @@ def numpy_to_gdal_dtype(np_dtype: DTypeLike) -> int: def gdal_to_numpy_dtype(gdal_dtype: Union[str, int]) -> np.dtype: """Convert GDAL dtype to NumPy dtype. - Parameters: gdal_dtype : str/int, GDAL dtype to convert. - Returns: np_dtype : DTypeLike, NumPy dtype + Parameters: gdal_dtype - str/int, GDAL dtype to convert. + Returns: np_dtype - np.dtype, NumPy dtype """ from osgeo import gdal, gdal_array if isinstance(gdal_dtype, str): From 08b81b154b402df4a65cdf52daa1fb094d83942b Mon Sep 17 00:00:00 2001 From: Zhang Yunjun Date: Tue, 5 Sep 2023 10:21:44 +0800 Subject: [PATCH 4/4] readfile: add more comments --- src/mintpy/utils/readfile.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/mintpy/utils/readfile.py b/src/mintpy/utils/readfile.py index 4ff80debe..796f310fd 100644 --- a/src/mintpy/utils/readfile.py +++ b/src/mintpy/utils/readfile.py @@ -211,6 +211,8 @@ def numpy_to_gdal_dtype(np_dtype: DTypeLike) -> int: """Convert NumPy dtype to GDAL dtype. + Modified from dolphin.utils.numpy_to_gdal_type(). + Parameters: np_dtype - DTypeLike, NumPy dtype to convert. Returns: gdal_code - int, GDAL type code corresponding to `np_dtype`. """ @@ -233,6 +235,8 @@ def numpy_to_gdal_dtype(np_dtype: DTypeLike) -> int: def gdal_to_numpy_dtype(gdal_dtype: Union[str, int]) -> np.dtype: """Convert GDAL dtype to NumPy dtype. + Modified from dolphin.utils.gdal_to_numpy_type(). + Parameters: gdal_dtype - str/int, GDAL dtype to convert. Returns: np_dtype - np.dtype, NumPy dtype """