Commit 540da4a0 authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

EcaIndices: replace malloc by std::vector.

parent 6be31219
Pipeline #4899 passed with stages
in 16 minutes and 29 seconds
......@@ -63,7 +63,6 @@
*/
// clang-format on
#include "dmemory.h"
#include "process_int.h"
#include "cdo_options.h"
#include "param_conversion.h"
......@@ -323,10 +322,12 @@ static const char HURR_LONGNAME2[] = "Greatest number of consecutive hurricane
/* ECA temperature indices */
static int addWithFrequency(std::vector<std::string> &params, const char *operatorName, size_t defaultDays)
static int
addWithFrequency(std::vector<std::string> &params, const char *operatorName, size_t defaultDays)
{
KVList kvlist;
int opID = 0;
KVList kvlist;
if (kvlist.parseArguments(1, params) != 0) cdoAbort("Argument parse error!");
auto kv = kvlist.search("freq");
if (kv && kv->nvalues > 0 )
......@@ -340,15 +341,13 @@ static int addWithFrequency(std::vector<std::string> &params, const char *operat
}
else
opID = cdoOperatorAdd(operatorName, 0, defaultDays, nullptr);
return opID;
}
void *EcaCfd(void *process)
{
ECA_REQUEST_1 request;
int ndays = 5;
char cfd_longname2[1024];
char cfd_name2[1024];
cdoInitialize(process);
......@@ -365,9 +364,13 @@ void *EcaCfd(void *process)
cdoOperatorAdd("eca_cfd", 0, 31, nullptr);
}
char cfd_longname2[1024];
char cfd_name2[1024];
sprintf(cfd_longname2, CFD_LONGNAME2, ndays);
sprintf(cfd_name2, CFD_NAME2, ndays);
ECA_REQUEST_1 request;
request.var1.name = CFD_NAME;
request.var1.longname = CFD_LONGNAME;
request.var1.refdate = 19550101;
......@@ -388,6 +391,7 @@ void *EcaCfd(void *process)
request.var2.h3 = vfarnum;
eca1(&request);
cdoFinish();
return 0;
......@@ -396,11 +400,8 @@ void *EcaCfd(void *process)
void *EcaCsu(void *process)
{
ECA_REQUEST_1 request;
double argT = 25.0;
int ndays = 5;
char csu_longname2[1024];
char csu_name2[1024];
cdoInitialize(process);
......@@ -420,9 +421,13 @@ void *EcaCsu(void *process)
else
cdoOperatorAdd("eca_csu", 0, 31, nullptr);
char csu_longname2[1024];
char csu_name2[1024];
sprintf(csu_longname2, CSU_LONGNAME2, ndays);
sprintf(csu_name2, CSU_NAME2, ndays);
ECA_REQUEST_1 request;
request.var1.name = CSU_NAME;
request.var1.longname = CSU_LONGNAME;
request.var1.refdate = 19550101;
......@@ -452,10 +457,8 @@ void *EcaCsu(void *process)
void *EcaCwdi(void *process)
{
char *longname;
int argN = 6;
double argT = 5.0;
ECA_REQUEST_2 request;
cdoInitialize(process);
......@@ -474,11 +477,13 @@ void *EcaCwdi(void *process)
cdoOperatorAdd("eca_cwdi", 0, 31, nullptr);
}
longname = (char*) Malloc(strlen(CWDI_LONGNAME) + 80);
sprintf(longname, CWDI_LONGNAME, argN, argT);
std::vector<char> longname(strlen(CWDI_LONGNAME) + 80);
sprintf(longname.data(), CWDI_LONGNAME, argN, argT);
ECA_REQUEST_2 request;
request.var1.name = CWDI_NAME;
request.var1.longname = longname;
request.var1.longname = longname.data();
request.var1.refdate = 19550101;
request.var1.units = CWDI_UNITS;
request.var1.f1 = nullptr;
......@@ -498,7 +503,6 @@ void *EcaCwdi(void *process)
eca2(&request);
Free(longname);
cdoFinish();
return 0;
......@@ -508,7 +512,6 @@ void *EcaCwdi(void *process)
void *EcaCwfi(void *process)
{
int argN = 6;
ECA_REQUEST_2 request;
cdoInitialize(process);
......@@ -528,15 +531,15 @@ void *EcaCwfi(void *process)
OPID_ETC = cdoOperatorAdd("etccdi_csdi", 0, 10, nullptr);
}
std::vector<char> longname(strlen(CWFI_LONGNAME) + 40);
sprintf(longname.data(), CWFI_LONGNAME, argN);
char *longname = (char*) Malloc(strlen(CWFI_LONGNAME) + 40);
sprintf(longname, CWFI_LONGNAME, argN);
ECA_REQUEST_2 request;
if (OPID_ECA == cdoOperatorID())
{
request.var1.name = CWFI_NAME;
request.var1.longname = longname;
request.var1.longname = longname.data();
request.var1.refdate = 19550101;
request.var1.units = CWFI_UNITS;
}
......@@ -564,7 +567,6 @@ void *EcaCwfi(void *process)
eca2(&request);
Free(longname);
cdoFinish();
return 0;
......@@ -572,12 +574,13 @@ void *EcaCwfi(void *process)
void *EcaEtr(void *process)
{
ECA_REQUEST_3 request;
{
cdoInitialize(process);
cdoOperatorAdd("eca_etr", 0, 31, nullptr);
ECA_REQUEST_3 request;
request.name = ETR_NAME;
request.longname = ETR_LONGNAME;
request.refdate = 19550101;
......@@ -594,9 +597,7 @@ void *EcaEtr(void *process)
void *EcaFd(void *process)
{
ECA_REQUEST_1 request;
{
cdoInitialize(process);
int OPID_ECA = 0, OPID_ETC = 0;
......@@ -612,6 +613,8 @@ void *EcaFd(void *process)
OPID_ETC = cdoOperatorAdd("etccdi_fd", 0, 10, nullptr);
}
ECA_REQUEST_1 request;
if ( OPID_ECA == cdoOperatorID() )
{
request.var1.name = FD_NAME;
......@@ -638,6 +641,7 @@ void *EcaFd(void *process)
request.var2.h3 = nullptr;
eca1(&request);
cdoFinish();
return 0;
......@@ -651,15 +655,13 @@ void *EcaFd(void *process)
* in the second half.
* ATTENTION: Year of the northern hemisphere starts in january to
* december, whereas for the southern hemisphere is goes from july to june!
* Hence, at least 18 Month of data is needed for computing the gsl of the
* whole earth.
* Hence, at least 18 Month of data is needed for computing the gsl of the whole earth.
*/
void *EcaGsl(void *process)
{
int argN = 6;
double argT = 5.0;
double minLandFraction = 0.5;
ECA_REQUEST_4 request;
cdoInitialize(process);
cdoOperatorAdd("eca_gsl", 0, 10, nullptr);
......@@ -668,11 +670,13 @@ void *EcaGsl(void *process)
if ( operatorArgc() > 1 ) argT = parameter2double(cdoOperatorArgv(1));
if ( operatorArgc() > 2 ) minLandFraction = parameter2double(cdoOperatorArgv(2));
char *longname = (char*) Malloc(strlen(GSL_LONGNAME) + 160);
sprintf(longname, GSL_LONGNAME, argN, argT, argN, argT);
std::vector<char> longname(strlen(GSL_LONGNAME) + 160);
sprintf(longname.data(), GSL_LONGNAME, argN, argT, argN, argT);
ECA_REQUEST_4 request;
request.name = GSL_NAME;
request.longname = longname;
request.longname = longname.data();
request.units = GSL_UNITS;
request.name2 = GSL_NAME2;
request.longname2 = GSL_LONGNAME2;
......@@ -686,8 +690,6 @@ void *EcaGsl(void *process)
request.consecutiveDays = argN;
eca4(&request);
Free(longname);
cdoFinish();
......@@ -699,9 +701,9 @@ void *EcaHd(void *process)
{
double argX = 17.0;
double argA = 17.0;
ECA_REQUEST_1 request;
cdoInitialize(process);
cdoOperatorAdd("eca_hd", 0, 31, nullptr);
if ( operatorArgc() > 0 )
......@@ -712,6 +714,8 @@ void *EcaHd(void *process)
if ( operatorArgc() > 1 )
argA = parameter2double(cdoOperatorArgv(1));
ECA_REQUEST_1 request;
request.var1.name = HD_NAME;
request.var1.longname = HD_LONGNAME;
request.var1.refdate = 19550101;
......@@ -738,7 +742,6 @@ void *EcaHwdi(void *process)
{
int argN = 6;
double argT = 5.0;
ECA_REQUEST_2 request;
cdoInitialize(process);
......@@ -757,11 +760,13 @@ void *EcaHwdi(void *process)
cdoOperatorAdd("eca_hwdi", 0, 31, nullptr);
}
char *longname = (char*) Malloc(strlen(HWDI_LONGNAME) + 80);
sprintf(longname, HWDI_LONGNAME, argN, argT);
std::vector<char> longname(strlen(HWDI_LONGNAME) + 80);
sprintf(longname.data(), HWDI_LONGNAME, argN, argT);
ECA_REQUEST_2 request;
request.var1.name = HWDI_NAME;
request.var1.longname = longname;
request.var1.longname = longname.data();
request.var1.refdate = 19550101;
request.var1.units = HWDI_UNITS;
request.var1.f1 = nullptr;
......@@ -780,8 +785,7 @@ void *EcaHwdi(void *process)
request.var2.h2 = vfarnum;
eca2(&request);
Free(longname);
cdoFinish();
return 0;
......@@ -791,7 +795,6 @@ void *EcaHwdi(void *process)
void *EcaHwfi(void *process)
{
int argN = 6;
ECA_REQUEST_2 request;
cdoInitialize(process);
......@@ -811,13 +814,15 @@ void *EcaHwfi(void *process)
OPID_ETC = cdoOperatorAdd("etccdi_wsdi", 0, 10, nullptr);
}
char *longname = (char*) Malloc(strlen(HWFI_LONGNAME) + 40);
sprintf(longname, HWFI_LONGNAME, argN);
std::vector<char> longname(strlen(HWFI_LONGNAME) + 40);
sprintf(longname.data(), HWFI_LONGNAME, argN);
if ( OPID_ECA == cdoOperatorID() )
ECA_REQUEST_2 request;
if (OPID_ECA == cdoOperatorID())
{
request.var1.name = HWFI_NAME;
request.var1.longname = longname;
request.var1.longname = longname.data();
request.var1.refdate = 19550101;
request.var1.units = HWFI_UNITS;
}
......@@ -844,8 +849,7 @@ void *EcaHwfi(void *process)
request.var2.h2 = vfarnum;
eca2(&request);
Free(longname);
cdoFinish();
return 0;
......@@ -853,9 +857,7 @@ void *EcaHwfi(void *process)
void *EcaId(void *process)
{
ECA_REQUEST_1 request;
{
cdoInitialize(process);
int OPID_ECA = 0, OPID_ETC = 0;
......@@ -871,6 +873,8 @@ void *EcaId(void *process)
OPID_ECA = cdoOperatorAdd("eca_id", 0, 31, nullptr);
}
ECA_REQUEST_1 request;
if ( OPID_ECA == cdoOperatorID() )
{
request.var1.name = ID_NAME;
......@@ -895,6 +899,7 @@ void *EcaId(void *process)
request.var2.h3 = nullptr;
eca1(&request);
cdoFinish();
return 0;
......@@ -904,7 +909,6 @@ void *EcaId(void *process)
void *EcaSu(void *process)
{
double argT = 25.0;
ECA_REQUEST_1 request;
cdoInitialize(process);
......@@ -923,13 +927,15 @@ void *EcaSu(void *process)
OPID_ECA = cdoOperatorAdd("eca_su", 0, 31, nullptr);
}
char *longname = (char*) Malloc(strlen(SU_LONGNAME) + 40);
sprintf(longname, SU_LONGNAME, argT);
std::vector<char> longname(strlen(SU_LONGNAME) + 40);
sprintf(longname.data(), SU_LONGNAME, argT);
ECA_REQUEST_1 request;
if ( OPID_ECA == cdoOperatorID() )
{
request.var1.name = SU_NAME;
request.var1.longname = longname;
request.var1.longname = longname.data();
request.var1.refdate = 19550101;
request.var1.units = nullptr;
}
......@@ -952,8 +958,7 @@ void *EcaSu(void *process)
request.var2.h3 = nullptr;
eca1(&request);
Free(longname);
cdoFinish();
return 0;
......@@ -961,12 +966,13 @@ void *EcaSu(void *process)
void *EcaTg10p(void *process)
{
ECA_REQUEST_2 request;
{
cdoInitialize(process);
cdoOperatorAdd("eca_tg10p", 0, 31, nullptr);
ECA_REQUEST_2 request;
request.var1.name = TG10P_NAME;
request.var1.longname = TG10P_LONGNAME;
request.var1.refdate = 19550101;
......@@ -980,6 +986,7 @@ void *EcaTg10p(void *process)
request.var2.h2 = nullptr;
eca2(&request);
cdoFinish();
return 0;
......@@ -987,12 +994,13 @@ void *EcaTg10p(void *process)
void *EcaTg90p(void *process)
{
ECA_REQUEST_2 request;
{
cdoInitialize(process);
cdoOperatorAdd("eca_tg90p", 0, 31, nullptr);
ECA_REQUEST_2 request;
request.var1.name = TG90P_NAME;
request.var1.longname = TG90P_LONGNAME;
request.var1.refdate = 19550101;
......@@ -1006,6 +1014,7 @@ void *EcaTg90p(void *process)
request.var2.h2 = nullptr;
eca2(&request);
cdoFinish();
return 0;
......@@ -1014,11 +1023,12 @@ void *EcaTg90p(void *process)
void *EcaTn10p(void *process)
{
ECA_REQUEST_2 request;
cdoInitialize(process);
cdoOperatorAdd("eca_tn10p", 0, 31, nullptr);
ECA_REQUEST_2 request;
request.var1.name = TN10P_NAME;
request.var1.longname = TN10P_LONGNAME;
request.var1.refdate = 19550101;
......@@ -1032,6 +1042,7 @@ void *EcaTn10p(void *process)
request.var2.h2 = nullptr;
eca2(&request);
cdoFinish();
return 0;
......@@ -1039,12 +1050,13 @@ void *EcaTn10p(void *process)
void *EcaTn90p(void *process)
{
ECA_REQUEST_2 request;
{
cdoInitialize(process);
cdoOperatorAdd("eca_tn90p", 0, 31, nullptr);
ECA_REQUEST_2 request;
request.var1.name = TN90P_NAME;
request.var1.longname = TN90P_LONGNAME;
request.var1.refdate = 19550101;
......@@ -1058,6 +1070,7 @@ void *EcaTn90p(void *process)
request.var2.h2 = nullptr;
eca2(&request);
cdoFinish();
return 0;
......@@ -1066,9 +1079,7 @@ void *EcaTn90p(void *process)
void *EcaTr(void *process)
{
ECA_REQUEST_1 request;
double argT = 20.0;
char tr_longname[1024];
cdoInitialize(process);
......@@ -1087,7 +1098,11 @@ void *EcaTr(void *process)
OPID_ECA = cdoOperatorAdd("eca_tr", 0, 31, nullptr);
}
char tr_longname[1024];
sprintf(tr_longname, TR_LONGNAME, argT);
ECA_REQUEST_1 request;
if ( OPID_ECA == cdoOperatorID() )
{
request.var1.name = TR_NAME;
......@@ -1122,12 +1137,13 @@ void *EcaTr(void *process)
void *EcaTx10p(void *process)
{
ECA_REQUEST_2 request;
{
cdoInitialize(process);
cdoOperatorAdd("eca_tx10p", 0, 31, nullptr);
ECA_REQUEST_2 request;
request.var1.name = TX10P_NAME;
request.var1.longname = TX10P_LONGNAME;
request.var1.refdate = 19550101;
......@@ -1141,6 +1157,7 @@ void *EcaTx10p(void *process)
request.var2.h2 = nullptr;
eca2(&request);
cdoFinish();
return 0;
......@@ -1148,9 +1165,7 @@ void *EcaTx10p(void *process)
void *EcaTx90p(void *process)
{
ECA_REQUEST_2 request;
{
cdoInitialize(process);
if (operatorArgc() > 0)
{
......@@ -1164,7 +1179,8 @@ void *EcaTx90p(void *process)
else
cdoOperatorAdd("eca_tx90p", 0, 31, nullptr);
ECA_REQUEST_2 request;
request.var1.name = TX90P_NAME;
request.var1.longname = TX90P_LONGNAME;
request.var1.refdate = 19550101;
......@@ -1178,23 +1194,20 @@ void *EcaTx90p(void *process)
request.var2.h2 = nullptr;
eca2(&request);
cdoFinish();
return 0;
}
/* ECA precipitation indices */
// ECA precipitation indices
void *EcaCdd(void *process)
{
ECA_REQUEST_1 request;
double threshold = 1;
int ndays = 5;
char cdd_longname[1024];
char cdd_longname2[1024];
char cdd_name2[1024];
cdoInitialize(process);
......@@ -1220,10 +1233,15 @@ void *EcaCdd(void *process)
OPID_ETC = cdoOperatorAdd("etccdi_cdd", 0, 10, nullptr);
}
char cdd_longname[1024];
char cdd_longname2[1024];
char cdd_name2[1024];
sprintf(cdd_longname, CDD_LONGNAME, threshold);
sprintf(cdd_longname2, CDD_LONGNAME2, ndays);
sprintf(cdd_name2, CDD_NAME2, ndays);
ECA_REQUEST_1 request;
if ( OPID_ECA == cdoOperatorID() )
{
request.var1.name = CDD_NAME;
......@@ -1264,14 +1282,11 @@ void *EcaCdd(void *process)
void *EcaCwd(void *process)
{
ECA_REQUEST_1 request;
double threshold = 1;
int ndays = 5;
char cwd_longname[1024];
char cwd_longname2[1024];
char cwd_name2[1024];
cdoInitialize(process);
int OPID_ECA = 0, OPID_ETC = 0;
if ( operatorArgc() > 3 ) cdoAbort("Too many arguments!");
......@@ -1295,10 +1310,15 @@ void *EcaCwd(void *process)
OPID_ETC = cdoOperatorAdd("etccdi_cwd", 0, 10, nullptr);
}
char cwd_longname[1024];
char cwd_longname2[1024];
char cwd_name2[1024];
sprintf(cwd_longname, CWD_LONGNAME, threshold);
sprintf(cwd_longname2, CWD_LONGNAME2, ndays);
sprintf(cwd_name2, CWD_NAME2, ndays);
ECA_REQUEST_1 request;
if ( OPID_ECA == cdoOperatorID() )
{
request.var1.name = CWD_NAME;
......@@ -1451,6 +1471,7 @@ void *EcaPd(void *process)
request.var2.h3 = nullptr;
eca1(&request);
cdoFinish();
return 0;
......@@ -1459,11 +1480,12 @@ void *EcaPd(void *process)
void *
EcaR75p(void *process)
{
ECA_REQUEST_2 request;
cdoInitialize(process);
cdoOperatorAdd("eca_r75p", 0, 31, nullptr);
ECA_REQUEST_2 request;
request.var1.name = R75P_NAME;
request.var1.longname = R75P_LONGNAME;
request.var1.refdate = 19550101;
......@@ -1478,6 +1500,7 @@ EcaR75p(void *process)
request.var2.h2 = nullptr;
eca2(&request);
cdoFinish();
return 0;