之前遇到的一个客户需求就是计算标准化降水指数(SPI)。
主要参考的论文为:洪兴骏,等 “标准化降水指数SPI分布函数的适用性研究”。原文可以很容易搜索到。
论文将计算方法其实写的比较清楚了,本文主要提供其C++实现。
头文件
//SPI_Calculator.h
#include<iostream>
#include<vector>
#include<utility>
#include<fstream>
#include<cmath>
using namespace std;
#ifndef SPICALCULATOR_H
#define SPICALCULATOR_H
class SPI_Calculator{
public:
/**
path is the file that provides the data of rain fall
file format:
year amount of precipitation
year amount of precipitation
.
.
.
*/
SPI_Calculator(char* path);
/**
parameter is the year to get the SPI
return SPI
*/
double getSPI(int year);
double f_probabilityDensity(double x);
double f_gamma(double z, unsigned Ntermsx);
double integral(double a,double b);
void printYears();
void printSPI();
void test();
private:
bool readData(char* path);
void calculateParameter();
vector<pair<int,double>> dataTable;

本文介绍如何根据洪兴骏等人的研究,用C++实现标准化降水指数(SPI)计算。代码中涉及积分和gamma函数的实现,通过输入每年降水量调用getSPI(int year)函数获取SPI值。程序假设输入数据合理,不合理数据可能导致意外结果。
最低0.47元/天 解锁文章
2776

被折叠的 条评论
为什么被折叠?



