Skip to content

Commit

Permalink
readfile: support GDAL int8 dtype (#1083)
Browse files Browse the repository at this point in the history
+ 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 <[email protected]>
  • Loading branch information
yunjunz and scottstanie authored Sep 8, 2023
1 parent ce93e2e commit b19ff4b
Showing 1 changed file with 46 additions and 3 deletions.
49 changes: 46 additions & 3 deletions src/mintpy/utils/readfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -252,7 +295,7 @@
## recons-20150115
## cmask
##
###########################################################################
#########################################################################



Expand Down

0 comments on commit b19ff4b

Please sign in to comment.