From b19ff4b7e821ff004487e50c3b1a17e94e1f0eb5 Mon Sep 17 00:00:00 2001 From: Zhang Yunjun Date: Fri, 8 Sep 2023 10:35:14 +0800 Subject: [PATCH] `readfile`: support GDAL `int8` dtype (#1083) + 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 I did not replace the usage of dict conversion with the two functions above yet, because cint16/32 is supported by GDAL but not in numpy yet, thus, the dict conversion may be more generic at the moment for translation purposes. --------- Co-authored-by: Scott Staniewicz --- src/mintpy/utils/readfile.py | 49 +++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/src/mintpy/utils/readfile.py b/src/mintpy/utils/readfile.py index 205df4c35..796f310fd 100644 --- a/src/mintpy/utils/readfile.py +++ b/src/mintpy/utils/readfile.py @@ -15,9 +15,11 @@ import sys import warnings import xml.etree.ElementTree as ET +from typing import Union import h5py import numpy as np +from numpy.typing import DTypeLike from mintpy.objects import ( DSET_UNIT_DICT, @@ -135,11 +137,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 +154,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 +207,47 @@ } -########################################################################### +######################################################################### +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`. + """ + 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. + + Modified from dolphin.utils.gdal_to_numpy_type(). + + 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): + 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 +295,7 @@ ## recons-20150115 ## cmask ## -########################################################################### +#########################################################################