Quick Calculation of SPI Index Using GMA Based on Raster Data

1 Preparation

1.1 Environment

Python: 3.12 gma: 2.1.10a1 (Download: https://pypi.org/project/gma/)

**Note: Must be greater than version 2.1.10a1 to execute properly

1.2 Data

The example uses monthly precipitation raster data from Luoyang City from 1980 to 2020. Sample data can be obtained from:

Link: https://pan.baidu.com/s/1reTBTdBdhGlTmXEC23f9qw?pwd=dfub

Extraction code: dfub

2 Functions and Help

(Raster file read by io.ReadRaster)

DataSet.AlgebraicCalculus(AggFun, Block=(None, 256, 256), SelBands=None, OutNoData=None, **kwargs)

Functionality

Applies mathematical calculation functions or methods to the raster dataset.

Parameters

AggFun: def.

  Raster algebra function. For example:

  def calFun(in_ar):

    return in_ar[0] * 2.1

  Where in_ar is a NumPy 3D array extracted from the original data in chunks (dimensions are: bands, rows, columns), which can be operated on and return the calculation result. The size of the 3D array is determined by the Block parameter.

Optional Parameters

Block = tuple. Default is (None, 256, 256).

  Chunk size (bands, rows, columns). Where None means to take all, indicating no chunking in that dimension. Band chunking will be performed from the bands selected in SelBands.

SelBands = list||None. Default is None.

  Select the required bands (starting from 1). When not all bands are needed, selecting only the used bands can further improve computational efficiency. Default is all bands (None).

OutNoData = float||int||None. Default is None.

  No data value for the output result. Default is not set (None).

Tip: If the raster data contains NoData values, please handle them in AggFun and configure this parameter.

kwargs:

  Other parameters passed to algos.dataio.utils.CreateRasterEx.

Returns

Raster dataset (DataSet).

3 Code

3.1 Based on a Multi-band File

from gma import io, climet

ds = io.ReadRaster(r"1.一个多波段文件\PRE_Luoyang_1981-2020.tif")
opt = "dsSPI1_Luoyang_1981-2020.tif"
def calSPI(in_ar):
    ### in_ar is a 3D array (bands × rows × columns), size determined by the Block parameter of the AlgebraicCalculus method
    SPI1 = climet.Index.SPI(in_ar, Axis = 0, Scale = 1)
    if ds.NoData: # Handle NoData
        SPI1[in_ar == ds.NoData] = ds.NoData
    return SPI1
    
## Method 1: Save the result separately
dsSPI1 = ds.AlgebraicCalculus(calSPI, OutNoData = ds.NoData)
dsSPI1.SaveAs(opt)
## Method 2: Directly output to file
ds.AlgebraicCalculus(calSPI, OutNoData = ds.NoData, Output = opt)

3.2 Based on Multiple Single-band Files

from gma import io, climet
import glob

inFiles = glob.glob(r"2.多个单波段文件\*.tif")
vds = io.BuildVirtualRsterDataSet(inFiles)
opt = "vdsSPI1_Luoyang_1981-2020.tif"
def calSPI(in_ar):
    ### in_ar is a 3D array (bands × rows × columns), size determined by the Block parameter of the AlgebraicCalculus method
    SPI1 = climet.Index.SPI(in_ar, Axis = 0, Scale = 1)
    if ds.NoData: # Handle NoData
        SPI1[in_ar == ds.NoData] = ds.NoData
    return SPI1
    
## Method 1: Save the result separately
vdsSPI1 = ds.AlgebraicCalculus(calSPI, OutNoData = ds.NoData)
vdsSPI1.SaveAs(opt)
## Method 2: Directly output to file
vds.AlgebraicCalculus(calSPI, OutNoData = ds.NoData, Output = opt)

Leave a Comment