Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

readfile: support GDAL int8 dtype #1083

Merged
merged 4 commits into from
Sep 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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