Files
CSIROBoeingPhase5-Vietnam/deafrica_tools/__pycache__/bandindices.cpython-310.pyc
T

240 lines
19 KiB
Plaintext
Raw Normal View History

2024-08-28 05:10:02 +00:00
o
2024-09-18 14:26:05 +00:00
ÙnÚfä_ã@sFdZddlZddlZ       d ddZ      d d d
2024-08-28 05:10:02 +00:00
ZdS)
zS
Functions for computing remote sensing band indices on Digital Earth Africa
data.
éNTFc
s|rˆjdd|rtˆjƒ}td|ƒiddddddd d
dd d dd
dddddddddddddddddddddddddddd dd!d"dd#d$dd%d&dd'dd(dd)dd*dd+dd,dd-dd.dd/dd0dd1dd2œ ¥} d3d4„d5d6„

fd7d8„
fd9d:„}
|
| d;<d<d=„d>d?„d@dA„fdBdC„ dDdE„fdFdG„fdHdI„‰ dJdK„‡ ‡ fdLdM„} | | dN<t|tƒrè|n|g} | D]Ð}|  t|ƒ¡}
|dOurþtdPƒ|dQvr|st  dR|dS¡n
|
dOurtdT|dUƒ|dOurBtj dVt
dWdX|dYkr2dZ}n|d[kr:d[}ntd\|d]ƒ|dOurKtd^ƒ|dZkrgd_}d`dadbdcdddedfœ}fdgdh„|  ¡Dƒ}n(|d[kr‡di}dcd`dadbdjdkdldcdddedmœ
}fdndh„|  ¡Dƒ}ntd\|doƒz|r•|nd_}|
ˆ  |¡|ƒ}Wnt
tdp|dqƒw|r·|n|}|ˆ|<qí|rƈ |¡ˆS)ra

Takes an xarray dataset containing spectral bands, calculates one of
a set of remote sensing indices, and adds the resulting array as a
new variable in the original dataset.
Last modified: July 2022
Parameters
----------
ds : xarray Dataset
A two-dimensional or multi-dimensional array with containing the
spectral bands required to calculate the index. These bands are
used as inputs to calculate the selected water index.
index : str or list of strs
A string giving the name of the index to calculate or a list of
strings giving the names of the indices to calculate:
* ``'ASI'`` (Artificial Surface Index, Yongquan Zhao & Zhe Zhu 2022)
* ``'AWEI_ns'`` (Automated Water Extraction Index, no shadows, Feyisa 2014)
* ``'AWEI_sh'`` (Automated Water Extraction Index, shadows, Feyisa 2014)
* ``'BAEI'`` (Built-Up Area Extraction Index, Bouzekri et al. 2015)
* ``'BAI'`` (Burn Area Index, Martin 1998)
* ``'BSI'`` (Bare Soil Index, Rikimaru et al. 2002)
* ``'BUI'`` (Built-Up Index, He et al. 2010)
* ``'CMR'`` (Clay Minerals Ratio, Drury 1987)
* ``'ENDISI'`` (Enhanced Normalised Difference for Impervious Surfaces Index, Chen et al. 2019)
* ``'EVI'`` (Enhanced Vegetation Index, Huete 2002)
* ``'FMR'`` (Ferrous Minerals Ratio, Segal 1982)
* ``'IOR'`` (Iron Oxide Ratio, Segal 1982)
* ``'LAI'`` (Leaf Area Index, Boegh 2002)
* ``'MBI'`` (Modified Bare Soil Index, Nguyen et al. 2021)
* ``'MNDWI'`` (Modified Normalised Difference Water Index, Xu 1996)
* ``'MSAVI'`` (Modified Soil Adjusted Vegetation Index, Qi et al. 1994)
* ``'NBI'`` (New Built-Up Index, Jieli et al. 2010)
* ``'NBR'`` (Normalised Burn Ratio, Lopez Garcia 1991)
* ``'NDBI'`` (Normalised Difference Built-Up Index, Zha 2003)
* ``'NDCI'`` (Normalised Difference Chlorophyll Index, Mishra & Mishra, 2012)
* ``'NDMI'`` (Normalised Difference Moisture Index, Gao 1996)
* ``'NDSI'`` (Normalised Difference Snow Index, Hall 1995)
* ``'NDTI'`` (Normalised Difference Turbidity Index, Lacaux et al. 2007)
* ``'NDVI'`` (Normalised Difference Vegetation Index, Rouse 1973)
* ``'NDWI'`` (Normalised Difference Water Index, McFeeters 1996)
* ``'SAVI'`` (Soil Adjusted Vegetation Index, Huete 1988)
* ``'TCB'`` (Tasseled Cap Brightness, Crist 1985)
* ``'TCG'`` (Tasseled Cap Greeness, Crist 1985)
* ``'TCW'`` (Tasseled Cap Wetness, Crist 1985)
* ``'WI'`` (Water Index, Fisher 2016)
collection : str
Deprecated in version 0.1.7. Use `satellite_mission` instead.
Valid options are:
* ``'c2'`` (for USGS Landsat Collection 2)
If 'c2', then `satellite_mission='ls'`.
* ``'s2'`` (for Sentinel-2)
If 's2', then `satellite_mission='s2'`.
satellite_mission : str
An string that tells the function which satellite mission's data is
being used to calculate the index. This is necessary because
different satellite missions use different names for bands covering
a similar spectra.
Valid options are:
* ``'ls'`` (for USGS Landsat)
* ``'s2'`` (for Copernicus Sentinel-2)
custom_varname : str, optional
By default, the original dataset will be returned with
a new index variable named after `index` (e.g. 'NDVI'). To
specify a custom name instead, you can supply e.g.
`custom_varname='custom_name'`. Defaults to None, which uses
`index` to name the variable.
normalise : bool, optional
Some coefficient-based indices (e.g. ``'WI'``, ``'BAEI'``,
``'AWEI_ns'``, ``'AWEI_sh'``, ``'TCW'``, ``'TCG'``, ``'TCB'``,
``'EVI'``, ``'LAI'``, ``'SAVI'``, ``'MSAVI'``)
produce different results if surface reflectance values are not
scaled between 0.0 and 1.0 prior to calculating the index.
Setting `normalise=True` first scales values to a 0.0-1.0 range
by dividing by 10000.0. Defaults to True.
drop : bool, optional
Provides the option to drop the original input data, thus saving
space. If `drop=True`, returns only the index and its values.
deep_copy: bool, optional
If `deep_copy=False`, calculate_indices will modify the original
array, adding bands to the input dataset and not removing them.
If the calculate_indices function is run more than once, variables
may be dropped incorrectly producing unexpected behaviour. This is
a bug and may be fixed in future releases. This is only a problem
2024-09-18 14:26:05 +00:00
when `drop=True`.
2024-08-28 05:10:02 +00:00
Returns
-------
ds : xarray Dataset
The original xarray Dataset inputted into the function, with a
new varible containing the remote sensing index as a DataArray.
If drop = True, the new variable/s as DataArrays in the
original Dataset.
ÚdeepúDropping bands ÚNDVIcSó|j|j|j|jS©ÚnirÚred©Úds©r
ú5/home/jovyan/phase5_new/deafrica_tools/bandindices.pyÚ<lambda>óz#calculate_indices.<locals>.<lambda>ÚEVIcSs.d|j|j|jd|jd|jdS)ç©r r
Úbluer r
r
rró.ÚLAIcSs6dd|j|j|jd|jd|jddS)Nglçû©ñ @rrrrgh‘í|?5¾?rr r
r
rrs
,ÿþÚSAVIcSs d|j|j|j|jdS)ø?çà?rr r
r
rró ÚMSAVIcSó8d|jdd|jddd|j|jddS©rérrr r
r
rrs2ÿÚNDMIcSrr)r Úswir_1r r
r
rr rÚNBRcSrr)r Úswir_2r r
r
rr¢rÚBAIcSs dd|jdd|jdS)ð?gš™™™™™¹?r!…ëQ¸®?)r
r r r
r
rr¤rÚNDCIcSrr)Ú
red_edge_1r
r r
r
rr§rÚNDSIcSrr©Úgreenr$r r
r
rr©rÚNDWIcSrr)r-r r r
r
rr«rÚMNDWIcSrrr,r r
r
rr­rÚNDBIcSrr©r$r r r
r
rr¯rÚBUIcSs0|j|j|j|j|j|j|j|jSr)r$r r
r r
r
rr±óÿÚBAEIcSs|jd|j|jS)Ng333333Ó?)r
r-r$r r
r
rr´sÚNBIcSs|j|j|jSr)r$r
r r r
r
rrsÚBSIcSs0|j|j|j|j|j|j|j|jSr)r$r
r rr r
r
rr¸r3cSs$d|j|jd|jd|jS)çÐ?g@)r-r$r r&r r
r
rr»s$cSs*|jd|jd|j|jd|jS)Nrrr8)rr-r r$r&r r
r
rr¿s*cSs6dd|jd|jd|jd|jd|jS)NgI€&†û?é«ééFé-éG)r-r
r r$r&r r
r
rrÃsÿþýüûcSó<d|jd|jd|jd|jd|jd|jS)NgTã¥›Ä  ?gÊÃB­iÞÉ?g_ÎQÚÓ?gH¿}8gÄ?g µ¦yÇå¿gx $(~Œã¿©rr-r
r r$r&r r
r
rrÌóÿþýüûcSr>)Ng?ÆÜµ„Ä¿g{ƒ/L¦
Ò¿gk+ö—ݓ߿gœÄ °rhé?g-Cëâ6*¿gn4€·@‚¿r?r r
r
rrÕr@cSr>)NgƒQI€&Ê?g±PkšwœÚ?gÌîÉÃB­á?gtF”ö_â?g<NÑ‘\þÓ?g
h"lxzͿr?r r
r
rrÞr@cSó |j|jSr©r$r&r r
r
rrçó cSrArr1r r
r
rrérCcSrAr)r
rr r
r
rrërCcSrr)r
r-r r
r
rrírcSó(|j|j|j|j|j|jdS©Nr©r$r&r r r
r
rrïs() ÚAWEI_nsÚAWEI_shÚWIÚTCWÚTCGÚTCBÚCMRÚFMRÚIORÚNDTIÚMBIcSrrr,r r
r
rÚmndwióóz calculate_indices.<locals>.mndwicSrArrBr r
r
rÚ swir_diffõó z$calculate_indices.<locals>.swir_diffcs0dt |j¡t ˆ|ƒ¡t ˆ|ƒd¡S©Nr!)ÚnpÚmeanrr )rRrTr
rÚalpha÷s0z calculate_indices.<locals>.alphacsDˆ|ƒ}ˆ|ƒ}ˆ|ƒ}|j|||d|j|||dSrV)r)r Úa)rYrRrTr
rÚENDISIùs,z!calculate_indices.<locals>.ENDISIr]cSsP|j|j|j|j}||jddgd|jddgd|jddgd}|S©Údim)r rÚminÚmax)r ÚAFÚAF_normr
r
rÚafs4zcalculate_indices.<locals>.afcSrrrr r
r
rÚndvirSzcalculate_indices.<locals>.ndvicSrr rr r
r
rÚmsavis8z calculate_indices.<locals>.msavicsTˆ|ƒ}ˆ|ƒ}d||}||jddgd|jddgd|jddgd}|S©Nrr_r`ra©rcrd)r rrÚVSFÚVSF_norm)rirhr
rÚvsf
s
 4zcalculate_indices.<locals>.vsfcSrDrErFr r
r
rÚmbis(zcalculate_indices.<locals>.mbics(ˆ|ƒ}ˆ|ƒ}||d||dS)Nrrr
)r rQr/)rorRr
rÚembiszcalculate_indices.<locals>.embicsHˆ|ƒ}d|}||jddgd|jddgd|jddgd}|Srjrk)r ÚEMBIÚSSFÚSSF_norm)rpr
rÚssfs4zcalculate_indices.<locals>.ssfcSsh|j|j|j|j|j|j|j|j}||jddgd|jddgd|jddgd}|Sr^)rr-r r$rcrd)r ÚMFÚMF_normr
r
rÚmfs04zcalculate_indices.<locals>.mfcs0ˆ|ƒ}ˆ|ƒ}ˆ|ƒ}ˆ|ƒ}||||Srr
)r rerlrrru)rgrwrtrnr
rÚASI s
zcalculate_indices.<locals>.ASIrxNzŽNo remote sensing `index` was provided. Please refer to the function
documentation for a full list of valid options for `index` (e.g. 'NDVI'))rIr4rGrHrrrrz
A coefficient-based index ('zÏ') normally applied to surface reflectance values in the
0.0-1.0 range was applied to values in the 0-10000 range. This can produce unexpected results;
if required, resolve this by setting `normalise=True`úThe selected index 'ú' is not one of the valid remote sensing index options.
Please refer to the function documentation for a full list of valid options for `index`zM`collection` was deprecated in version 0.1.7. Use `satelite_mission` instead.r!)Ú
stacklevelÚc2ÚlsÚs2ú'zN' is not a valid option for `collection`. Please specify either
'c2' or 's2'.z”No `satellite_mission` was provided. Please specify either 'ls' or 's2' to ensure the
function calculates indices using the correct spectral bands.r(rr-r
r r$r&)ÚSR_B1ÚSR_B2ÚSR_B3ÚSR_B4ÚSR_B5ÚSR_B7có i|] \}}|ˆjvr||qSr
©Ú variables©Ú.0r\Úbr r
rÚ
<dictcomp>Žóz%calculate_indices.<locals>.<dictcomp>i'r*Ú
red_edge_2Ú
red_edge_3)
Únir_1ÚB02ÚB03ÚB04ÚB05ÚB06ÚB07ÚB08ÚB11ÚB12cr†r
r‡r‰r r
r£rzT' is not a valid option for `satellite_mission`. Please specify either
'ls' or 's2'z1Please verify that all bands required to compute z are present in `ds`.)ÚcopyÚlistÚ data_varsÚprintÚ