Module pytools4dart.tools.hstools
This module contains the functions to stasck simulated bands into an ENVI HDR file. It allows to read .hdr file and returns a dictionary.
Functions
gdal_drivers
def gdal_drivers(
)
List of available GDAL drivers with there corresponding
default extension (ext), possible extensions (exts) and
GDAL writable capacity (writable).
https://gdal.org/drivers/raster/index.html
Returns
-------
pandas.DataFrame
gdal_file
def gdal_file(
file,
driver
)
Check driver is writable and if extension is one expected by gdal.
If not it is replaced by the default extension of gdal driver.
If the default extension is not defined, then the driver name in lower case is taken instead.
Parameters
----------
file: str
path to file
driver: str
A driver name, see pytools4dart.hstools.gdal_drivers().
Returns
-------
str
get_hdr_bands
def get_hdr_bands(
hdr,
nm_to_um=True
)
Extract band specifications from .hdr file.
Parameters
----------
hdr: DataFrame
in the format given by function read_ENVI_hdr.
nm_to_um: bool
to convert wavelengths and bandwidth from nm and ยตm,
which are the default units for wavelengths in ENVI files and DART respectively
Returns
-------
get_wavelengths
def get_wavelengths(
phasefile
)
Get the band wavelength values of a simulation.
Parameters
----------
phasefile: str
path to the input phase.xml file of DART simulation.
Returns
-------
: DataFrame
band number, wavelength, full width at half maximum.
normalize
def normalize(
array
)
Normalize array: (array-min)/(max-min)
Parameters
----------
array: numpy.ndarray
Returns
-------
numpy.ndarray
read_ENVI_hdr
def read_ENVI_hdr(
path,
dartlabels=False
)
Read hyperspectral ENVI file header
Parameters
----------
path: str
Path to .hdr file.
dartlabels: bool
???
Returns
-------
rotate_raster
def rotate_raster(
src_file,
dst_file
)
Rotate raster to a more standard GIS orientation
DART has the following axis specific convention:
o -- y+
:
x+
Thus the image are converted to a more standard raster definition:
y+
:
o -- x+
Parameters
----------
src_file: str
dst_file: str
Examples
--------
>>> import pytools4dart as ptd
>>> import rasterio as rio
>>> from rasterio.plot import show
>>> import matplotlib.pyplot as plt
>>> simu = ptd.simulation('use_case_6') #doctest:+ELLIPSIS
Loading plot file: ...use_case_6/input/plots.txt
Updating plot file properties index...
Updating plot file properties name...
>>> bandlist = ptd.hstools.get_bands_files(simu.output_dir)
>>> raster_file = bandlist.loc[(bandlist.azimuth==0) & (bandlist.band_num==0), 'path'].iloc[0]
>>> rotated_file = '/tmp/raster_rotate.mpr'
>>> ptd.hstools.rotate_raster(raster_file, rotated_file)
>>> r = rio.open(raster_file)
>>> rr = rio.open(rotated_file)
>>> print([rr.width, rr.height] == [r.height, r.width])
True
>>> print(rr.transform[4] == -r.transform[0])
True
>>> print(rr.transform[5] == r.transform[2] + r.width * r.transform[0])
True
stack_dart_bands
def stack_dart_bands(
band_files,
outputfile,
driver='ENVI',
rotate=True,
wavelengths=None,
fwhm=None,
verbose=False
)
Stack simulated bands into an ENVI file.
Parameters
----------
band_files: list
list of DART file paths to stack into the output ENVI file.
outputfile: str
file path to be written. Output format is ENVI thus file should be with .bil extension.
driver: str
GDAL driver, see pytools4dart.hstools.gdal_drivers().
If driver='ENVI' it adds the wavelength and bandwidth of bands to the .hdr file.
rotate: bool
rotate bands from DART orientation convention to a standard GIS orientation convention.
See pytools4dart.hstools.rotate_raster for details.
wavelengths: list
list of wavelength values (in nm) corresponding to the bands (in the same order).
They will be written in .hdr file.
fwhm: list
list of Full Width at Half Maximum (in nm) corresponding to the bands (in the same order).
They will be written in .hdr file.
verbose: bool
if True, it gives a message when files are written.
Returns
-------