-
Notifications
You must be signed in to change notification settings - Fork 2
/
spei.c
178 lines (160 loc) · 5.51 KB
/
spei.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include "auxiliary.c"
#include "lmoments.c"
#include "pdfs.c"
#include "thornthwaite.c"
// Max size of raw rainfall and events matrices
#define NUMDATOSMAX 5000
#define NUMRESULTMAX 5000
#define NUMSEASONSMAX 12
// Define max() and min() functions
#ifndef max
#define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
#endif
#ifndef min
#define min( a, b ) ( ((a) < (b)) ? (a) : (b) )
#endif
// Function prototypes
void spei(float dataSeries[], int n, int seasons, float speiSeries[]);
// Main program:
// Calculate the Standardized Precipitation Index
//int main(int argc, char **argv) {
int main(int argc, char **argv) {
FILE *entrada,*salida;
char pathOrigen[30],pathDestino[30],estacion[36],latitud[6];
float lat,rainSeries[NUMDATOSMAX],tempSeries[NUMDATOSMAX],
etpSeries[NUMDATOSMAX],balanceSeries[NUMDATOSMAX],
acumSeries[NUMDATOSMAX],speiSeries[NUMDATOSMAX];
int anio,mes,seasonality,acumulated,numRegistros,acumRegistros,
indice,jndice;
// Initialize variables
anio=mes=seasonality=acumulated=numRegistros=acumRegistros=indice=jndice=0;
lat=0.0;
for (indice=0; indice<NUMDATOSMAX; indice++) {
rainSeries[indice]=tempSeries[indice]=etpSeries[indice]=
balanceSeries[indice]=acumSeries[indice]=speiSeries[indice]=0.0;
}
// Read in-line arguments
if (argc!=4) {
printf("\nUsage:\tspei [acumulated] [source file] [result file]\n");
exit(1);
}
sscanf(argv[1], "%u", &acumulated);
sscanf(argv[2], "%s", pathOrigen);
sscanf(argv[3], "%s", pathDestino);
// Open input file
if((entrada=fopen(pathOrigen,"rt"))==NULL)
{
printf("\nError: File can't be opened");
exit(1);
}
// Read heading
fgets(estacion, 36, entrada);
fscanf(entrada, "%f\n", &lat);
fscanf(entrada, "%u;%u\n", &anio,&mes);
fscanf(entrada, "%u\n", &seasonality);
if(seasonality>NUMSEASONSMAX) {
printf("\nError: Too many seasons. Maximum is %d", NUMSEASONSMAX);
exit(1);
}
// Read data
indice=0;
while(!feof(entrada)) {
if(indice==NUMDATOSMAX) {
printf("\nError: Too many data in input file. Maximum is %d", NUMDATOSMAX);
exit(1);
}
fscanf(entrada,"%f;%f\n", &rainSeries[indice],&tempSeries[indice]);
indice++;
}
numRegistros=indice;
if(tempSeries[1]==0) numRegistros-=1;
// Close file
fclose(entrada);
// Print metadata (just to check)
printf("\nseries: %s", estacion);
printf("latitude: %.3f\n", lat);
printf("initial date: %d/%d\n", mes, anio);
printf("seasonality: %d\n", seasonality);
printf("%d registers\n", numRegistros);
printf("calculating SPEI at %d month", acumulated);
if (acumulated>1) printf("s");
printf("\n");
// Compute the climatic balance: precipitation minus potential evapotranspiration
if (tempSeries[1]!=0 && tempSeries[2]!=0) {
thornthwaite(tempSeries, numRegistros, lat, etpSeries);
for (indice=0; indice<numRegistros; indice++) {
balanceSeries[indice] = rainSeries[indice]-etpSeries[indice];
}
}
else {
for (indice=0; indice<numRegistros; indice++) {
balanceSeries[indice] = rainSeries[indice];
}
}
// Compute the cumulative series
anio += (acumulated-1)/12;
mes += acumulated-1;
while (mes>12) mes-=12;
acumRegistros = numRegistros-acumulated+1;
for (indice=acumulated-1; indice<numRegistros; indice++) {
for (jndice=0; jndice<acumulated; jndice++) {
acumSeries[indice-acumulated+1] += balanceSeries[indice-jndice];
}
}
// Compute the SPEI series
spei(acumSeries, acumRegistros, seasonality, speiSeries);
// Write results to file
if((salida=fopen(pathDestino,"wt"))==NULL) {
printf("\nError: Output file could not be opened");
exit(1);
}
fprintf(salida,"%s%f\n%u;%u\n%u", estacion,lat,anio,mes,seasonality);
//for (jndice=1; jndice<=seasonality; jndice++) {
// fprintf(salida,"\n%f;%f;%f", logLogisticParams[jndice][0],
// logLogisticParams[jndice][1], logLogisticParams[jndice][2]);
//}
for (indice=0; indice<acumRegistros; indice++) {
fprintf(salida,"\n%f", speiSeries[indice]);
}
fclose(salida);
// Quit
return(0);
}
// spei()
// Calculates the Standardized Precipitation-Evapotransporation Index
// from a series of climatic balance (precipitation minus etp). The
// SPEI is the standardized value of the climatic balance (P-ETP),
// computed following a Log Logistic probability distribution.
void spei(float dataSeries[], int n, int seasons, float speiSeries[]) {
int i, j, k, nSeason;
float seasonSeries[NUMDATOSMAX], beta[3], logLogisticParams[NUMSEASONSMAX][3];
// Loop through all seasons defined by seasons
for (j=1; j<=seasons; j++) {
// Extract and sort the seasonal series
k = 0;
for (i=j-1; i<n; i+=seasons) {
seasonSeries[k] = dataSeries[i];
k++;
}
nSeason = k;
upward(seasonSeries, nSeason);
// Compute probability weighted moments
//pwm(seasonSeries, nSeason, beta, -0.35, 0, 0);
pwm(seasonSeries, nSeason, beta, 0, 0, 0);
// Fit a Log Logistic probability function
logLogisticFit(beta, logLogisticParams[j]);
//printf("\nSeason %u", jndice);
//printf("\nLogLogistic beta param.: %.4f", logLogisticParams[jndice][0]);
//printf("\nLogLogistic alpha param.: %.4f", logLogisticParams[jndice][1]);
//printf("\nLogLogistic gamma param.: %.4f\n", logLogisticParams[jndice][2]);
// Calculate the standardized values
for (i=j-1; i<n; i+=seasons) {
speiSeries[i] = logLogisticCDF(dataSeries[i], logLogisticParams[j]);
speiSeries[i] = -standardGaussianInvCDF(speiSeries[i]);
}
}
}