Commit d83552e5 authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

Replaced Malloc() by std::vector.

parent fc1f2b5d
......@@ -32,47 +32,23 @@
#include <cdi.h>
#ifdef CDO
#include "cdo_int.h"
#include "cdo_task.h"
#include "pstream_int.h"
#define streamOpenWrite cdoStreamOpenWrite
#define streamDefVlist pstreamDefVlist
#define streamDefTimestep pstreamDefTimestep
#endif
#ifdef AFTERBURNER
#include "afterdoc.h"
#endif
#include "afterburner.h"
#include "constants.h"
#include "compare.h"
#include "cdoOptions.h"
#if defined(_OPENMP)
#ifdef _OPENMP
#include <omp.h>
#endif
#if !defined(VERSION)
#define VERSION "0.0.1"
#endif
#ifndef CLOCKS_PER_SEC
#define CLOCKS_PER_SEC 1000000
#endif
#ifdef AFTERBURNER
static double starttime = 0.0;
#endif
#ifdef AFTERBURNER
void afterInqHistory(int fileID);
void afterDefHistory(int fileID, char *histstring);
long get_nfft(void);
#endif
int scan_par_obsolate(char *namelist, const char *name, int def);
void scan_code(char *namelist, struct Variable *vars, int maxCodes, int *numCodes);
......@@ -89,11 +65,6 @@ struct RARG
struct Control *globs;
};
#ifdef AFTERBURNER
int stdin_is_tty = 0; /* true if stdin is character device */
int stdout_is_tty = 0; /* true if stdout is character device */
#endif
static bool lstdout = true;
static int Source = 0;
......@@ -106,9 +77,6 @@ static char *filename;
static const char **ifiles;
static char *ifile = NULL;
static const char *ofile = NULL;
#ifdef AFTERBURNER
static char *ofile2 = NULL;
#endif
static int ofileidx = 0;
......@@ -514,9 +482,9 @@ after_readTimestep(void *arg)
}
TsID++;
/*
printf("%3d date = %d time = %04d\n", TsID, vdate, vtime);
*/
// printf("%3d date = %d time = %04d\n", TsID, vdate, vtime);
num_recs = after_setNextDate(globs);
return (void *) &num_recs;
......@@ -544,19 +512,6 @@ after_defineNextTimestep(struct Control *globs)
streamDefTimestep(globs->ostreamID, otsID);
}
#ifdef AFTERBURNER
if (globs->Mean >= 2)
{
if (otsID == 0)
{
vlistDefTaxis(globs->ovlistID2, globs->taxisID2);
streamDefVlist(globs->ostreamID2, globs->ovlistID2);
}
taxisDefNumavg(globs->taxisID2, globs->MeanCount + 1);
streamDefTimestep(globs->ostreamID2, otsID);
}
#endif
otsID++;
}
......@@ -1237,37 +1192,6 @@ after_checkNamelist(struct Control *globs)
}
}
#ifdef AFTERBURNER
static void
after_usage(void)
{
fprintf(stderr, "\nafter [options] <InputFiles> <OutputFile> <VarianceFile>\n");
#if defined(_OPENMP)
fprintf(stderr, " option -P <nthreads> : Set number of OpenMP threads\n");
#endif
fprintf(stderr, " option -a : Forces analysis data process\n");
fprintf(stderr, " option -c : Print available codes and names\n");
fprintf(stderr, " option -d : Debug mode\n");
fprintf(stderr, " option -v <vctfile> : Read vct from vctfile\n");
/* fprintf(stderr, " option -h : help (this output)\n"); */
/* fprintf(stderr, " option -p : parallel read on\n"); */
fprintf(stderr, " <InputFiles> : ECHAM or ECMWF Ana or ReAna files\n");
fprintf(stderr, " <OutputFile> : GRIB, NetCDF or SERVICE format file\n");
fprintf(stderr, "<VarianceFile> : GRIB, NetCDF or SERVICE format file\n");
fprintf(stderr, " namelist is read from <stdin>\n");
fprintf(stderr, " output is written to <stdout>\n\n");
fprintf(stderr, " default Namelist: \n");
fprintf(stderr, " &SELECT\n");
fprintf(stderr, " TYPE = 0, CODE = -1, LEVEL = -1, MULTI = 0, DAYIN = 30,\n");
fprintf(stderr, " MEAN = 0, TIMESEL = -1, UNITSEL = 0,\n");
fprintf(stderr, " FORMAT = 0, PRECISION = 0, SZIP = 0\n");
fprintf(stderr, " &END\n");
exit(1);
}
#endif
static void
after_parini(struct Control *globs, struct Variable *vars)
{
......@@ -1275,23 +1199,15 @@ after_parini(struct Control *globs, struct Variable *vars)
if (stdin_is_tty)
{
#ifdef CDO
fprintf(stderr, "Default namelist: \n");
fprintf(stderr, " TYPE=0, CODE=-1, LEVEL=-1, INTERVAL=0, MEAN=0, EXTRAPOLATE=0\n");
#endif
fprintf(stdout, "Enter namelist parameter:\n");
}
else
{
fseek(stdin, 0L, SEEK_END);
long length = ftell(stdin);
if (length == 0L)
{
fprintf(stderr, "\n stdin not connected\n");
#ifdef AFTERBURNER
after_usage();
#endif
}
if (length == 0L) fprintf(stderr, "\n stdin not connected\n");
fseek(stdin, 0L, SEEK_SET);
}
......@@ -1334,9 +1250,7 @@ after_parini(struct Control *globs, struct Variable *vars)
globs->Mean = scan_par(globs->Verbose, namelist, "mean", 0);
globs->OutputInterval = scan_par(globs->Verbose, namelist, "interval", MONTHLY_INTERVAL);
#ifdef CDO
if (globs->Mean >= 2) cdoAbort("Namelist parameter MEAN=%d out of bounds (0:1)", globs->Mean);
#endif
int fileFormat = scan_par(globs->Verbose, namelist, "format", -1);
int gribFormat = scan_par_obsolate(namelist, "grib", 0);
......@@ -1346,11 +1260,7 @@ after_parini(struct Control *globs, struct Variable *vars)
switch (fileFormat)
{
#ifdef CDO
case -1: ofiletype = -1; break;
#else
case -1: ofiletype = CDI_FILETYPE_SRV; break;
#endif
case 0: ofiletype = CDI_FILETYPE_SRV; break;
case 1: ofiletype = CDI_FILETYPE_GRB; break;
case 2: ofiletype = CDI_FILETYPE_NC; break;
......@@ -1686,17 +1596,8 @@ after_postcntl(struct Control *globs, struct Variable *vars)
int ovarID2;
int ivarID, instID, modelID, tableID;
char name[CDI_MAX_NAME], longname[CDI_MAX_NAME], units[CDI_MAX_NAME];
char histstring[99];
int datatype;
snprintf(histstring, sizeof(histstring), "afterburner version %s type = %d", VERSION, globs->Type);
#ifdef AFTERBURNER
afterInqHistory(globs->istreamID);
if (globs->Mean != 2) afterDefHistory(globs->ostreamID, histstring);
if (globs->Mean >= 2) afterDefHistory(globs->ostreamID2, histstring);
#endif
if (globs->Debug) lprintf(stdout);
if (globs->Debug)
for (code = 0; code < MaxCodes; code++)
......@@ -1848,34 +1749,6 @@ after_readVct(struct Control *globs, const char *vctfile)
fclose(fp);
}
#ifdef AFTERBURNER
static void
after_version(void)
{
#if defined(COMPILER)
fprintf(stderr, "Compiler: %s\n", COMPILER);
#endif
#if defined(COMP_VERSION)
fprintf(stderr, " version: %s\n", COMP_VERSION);
#endif
#if defined(HAVE_LIBSZ) || defined(_OPENMP)
fprintf(stderr, " with:");
#if defined(HAVE_LIBSZ)
fprintf(stderr, " libsz");
#endif
#if defined(_OPENMP)
fprintf(stderr, " OpenMP");
#endif
fprintf(stderr, "\n");
#endif
#ifdef SYSTEM_TYPE
fprintf(stderr, "System: %s\n", SYSTEM_TYPE);
#endif
cdiPrintVersion();
fprintf(stderr, "\n");
}
#endif
static void
after_control_init(struct Control *globs)
{
......@@ -1912,125 +1785,6 @@ after_variable_init(struct Variable *vars)
vars->tableID = -1;
}
#ifdef AFTERBURNER
static void
after_printCodes(void)
{
int tableID = tableInq(-1, 128, "echam4");
int codes[] = { 34, 35, 36, 131, 132, 135, 148, 149, 151, 156, 157, 259, 260, 261, 262, 263, 264, 268, 269, 270, 271, 275 };
int ncodes = sizeof(codes) / sizeof(codes[0]);
lprintf(stdout);
fprintf(stdout, " Code Name Longname\n");
fprintf(stdout, " ---- ---- --------\n");
for (int i = 0; i < ncodes; i++)
{
int code = codes[i];
char name[CDI_MAX_NAME];
name[0] = 0;
char longname[CDI_MAX_NAME];
longname[0] = 0;
tableInqEntry(tableID, code, -1, name, longname, NULL);
fprintf(stdout, " %4d", code);
if (name[0])
{
fprintf(stdout, " %-16s", name);
if (longname[0]) fprintf(stdout, " %s", longname);
}
else
fprintf(stdout, " var%d", code);
fprintf(stdout, "\n");
}
lprintf(stdout);
}
#endif
/* =============================================== */
/* procstat - appends info about memory usage */
/* and time consumption */
/* =============================================== */
#ifdef AFTERBURNER
static void
after_procstat(char *procpath, int truncation)
{
time_t tp;
char mtype[12];
char stat_file[128];
double CPUTime = ((double) clock() - starttime) / CLOCKS_PER_SEC;
(void) time(&tp);
long yy = gmtime(&tp)->tm_year + 1900;
long mm = gmtime(&tp)->tm_mon + 1;
long dd = gmtime(&tp)->tm_mday;
long hh = gmtime(&tp)->tm_hour;
long mi = gmtime(&tp)->tm_min;
char *name = getpwuid(getuid())->pw_name;
char *proc = strrchr(procpath, '/');
if (proc == 0)
proc = procpath;
else
proc++;
strcpy(stat_file, "/pf/m/m214003/local/log/after.log");
double MaxMBytes = (double) memTotal() / 1048576.;
FILE *sf = fopen(stat_file, "a");
if (sf)
{
char unknown[] = "";
char *hostname;
if ((hostname = getenv("HOST")) == NULL) hostname = unknown;
setvbuf(sf, (char *) NULL, _IONBF, 0);
fprintf(sf, "%.7s %4.4ld.%2.2ld.%2.2ld %2.2ld:%2.2ld %s %-9.9s %7.1f %7.1f T%3.3d %s\n", name, yy, mm, dd, hh, mi, VERSION,
proc, MaxMBytes, CPUTime, truncation, hostname);
fclose(sf);
}
#if defined(CRAY)
#if defined(_CRAYMPP)
strcpy(mtype, " CRAYMPP --");
#elif (_MAXVL == 64)
strcpy(mtype, " CRAYVL64 -");
#elif (_MAXVL == 128)
strcpy(mtype, " CRAYVL128 ");
#else
strcpy(mtype, " CRAY -----");
#endif
#elif defined(SX)
strcpy(mtype, " NECSX ----");
#elif defined(__uxp__)
strcpy(mtype, " FUJI -----");
#elif defined(sun)
strcpy(mtype, " SUN ------");
#elif defined(i386)
strcpy(mtype, " i386 -----");
#elif defined(sgi)
strcpy(mtype, " sgi ------");
#else
strcpy(mtype, "-----------");
#endif
fprintf(stdout, " NORMAL EXIT\n");
fprintf(stdout, " ------ End after -%-11.11s- %7.1f sec", mtype, CPUTime);
if (MaxMBytes > 0)
fprintf(stdout, " --- %7.1f MB ---\n", MaxMBytes);
else
fprintf(stdout, " ----------------\n");
}
#endif
static void
after_processing(struct Control *globs, struct Variable *vars)
{
......@@ -2050,28 +1804,12 @@ after_processing(struct Control *globs, struct Variable *vars)
if (globs->Mean != 2)
{
#ifdef CDO
globs->ostreamID = cdoStreamOpenWrite(cdoStreamName(ofileidx), ofiletype);
#else
globs->ostreamID = streamOpenWrite(ofile, ofiletype);
if (globs->ostreamID < 0) cdiError(globs->ostreamID, "Open failed on %s", ofile);
#endif
if (globs->Szip) streamDefCompType(globs->ostreamID, CDI_COMPRESS_SZIP);
globs->ovlistID = vlistCreate();
}
#ifdef AFTERBURNER
if (globs->Mean >= 2)
{
globs->ostreamID2 = streamOpenWrite(ofile2, ofiletype);
if (globs->ostreamID2 < 0) cdiError(globs->ostreamID2, "Open failed on %s", ofile2);
if (globs->Szip) streamDefCompType(globs->ostreamID, CDI_COMPRESS_SZIP);
globs->ovlistID2 = vlistCreate();
}
#endif
/* ---------------- */
/* pre-processing */
......@@ -2191,16 +1929,11 @@ after_processing(struct Control *globs, struct Variable *vars)
after_control(globs, vars);
#ifdef CDO
if (globs->ostreamID != CDI_UNDEFID) pstreamClose(globs->ostreamID);
#else
if (globs->ostreamID2 != CDI_UNDEFID) streamClose(globs->ostreamID2);
if (globs->ostreamID != CDI_UNDEFID) streamClose(globs->ostreamID);
#endif
#ifdef CDO
processDefVarNum(vlistNvars(globs->ivlistID));
processSelf().addNvals(streamNvals(globs->istreamID));
#endif
streamClose(globs->istreamID);
if (globs->rcoslat) Free(globs->rcoslat);
......@@ -2219,182 +1952,6 @@ after_processing(struct Control *globs, struct Variable *vars)
extern char *optarg;
extern int optind, opterr, optopt;
#ifdef AFTERBURNER
static int
afterburner(int argc, char *argv[])
{
int i, code;
char *proc = argv[0];
char Line[132];
int c;
int fargc0, fargcn;
FILE *fp;
int numThreads = 0;
char *Vctfile = NULL;
extern int dmemory_ExitOnError;
dmemory_ExitOnError = 1;
starttime = (double) clock();
#ifdef AFTERBURNER
{ /* check character device on stdin and stdout */
struct stat statbuf;
fstat(0, &statbuf);
if (S_ISCHR(statbuf.st_mode)) stdin_is_tty = 1;
fstat(1, &statbuf);
if (S_ISCHR(statbuf.st_mode)) stdout_is_tty = 1;
}
#endif
/* ------------------- */
/* print information */
/* ------------------- */
lprintf(stdout);
fprintf(stdout, " afterburner version %s\n", VERSION);
fprintf(stdout, " ECHAM & analyses postprocessor\n");
if (sizeof(double) != 8 || sizeof(int) < 4)
{
fprintf(stderr, "byte size of type double %d\n", (int) sizeof(double));
fprintf(stderr, "byte size of type int %d\n", (int) sizeof(int));
fprintf(stderr, "byte size of type size_t %d\n", (int) sizeof(size_t));
return 1;
}
fp = fopen("/pf/m/m214003/doc/afterburner.doc", "r");
if (fp)
{
do
{
fgets(Line, 130, fp);
fprintf(stdout, "%s", &Line[1]);
}
while (!feof(fp) && Line[0] == '#');
fclose(fp);
}
struct Control *globs = (struct Control *) Malloc(sizeof(struct Control));
after_control_init(globs);
globs->Verbose = 1;
/* --------------------- */
/* options & filenames */
/* --------------------- */
extern int labort_after;
while ((c = getopt(argc, argv, "P:b:v:acdgpVw")) != EOF) switch (c)
{
case 'a': globs->AnalysisData = 1; break;
case 'b': Message("option -b not longer needed!"); break;
case 'c': after_printCodes(); break;
case 'd': globs->Debug = 1; break;
case 'p': lparallelread = true; break;
case 'P': numThreads = atoi(optarg); break;
case 'V': after_version(); break;
case 'v': Vctfile = optarg; break;
case 'w': labort_after = FALSE; break;
default: Message("option -%c unsupported!", optopt); after_usage();
}
#if defined(_OPENMP)
lprintf(stdout);
if (numThreads <= 0) numThreads = 1;
omp_set_num_threads(numThreads);
if (omp_get_max_threads() > omp_get_num_procs())
fprintf(stdout, " Number of threads is greater than number of Cores=%d!\n", omp_get_num_procs());
fprintf(stdout, " OpenMP: num_procs=%d max_threads=%d\n", omp_get_num_procs(), omp_get_max_threads());
#else
if (numThreads > 0)
{
fprintf(stderr, "Option -P failed, OpenMP support not compiled in!\n");
return -1;
}
#endif
if (lparallelread) fprintf(stdout, " Parallel read enabled\n");
fargc0 = optind;
fargcn = argc;
if (optind < argc) ifile = argv[optind++];
if (!ifile)
{
Message("*** Missing input file ***");
after_usage();
}
struct Variable vars[MaxCodes + 5];
for (code = 0; code < MaxCodes + 5; code++) after_variable_init(&vars[code]);
after_parini(globs, vars); /* read namelist parameter */
fprintf(stdout, " Input File: %-25s\n", ifile);
if (globs->Mean >= 2)
{
if (fargcn - fargc0 >= 3) ofile2 = argv[--fargcn];
if (!ofile2)
{
Message("*** Missing variance file ***");
after_usage();
}
}
if (globs->Mean != 2)
{
if (optind < argc) ofile = argv[optind++];
if (fargcn - fargc0 >= 2) ofile = argv[--fargcn];
if (!ofile)
{
Message("*** Missing output file ***");
after_usage();
}
fprintf(stdout, " Output File: %-25s\n", ofile);
}
globs->Nfiles = fargcn - fargc0 - 1;
if (globs->Nfiles > 0)
{
if (globs->Multi > 0) Error("Namelist parameter MULTI works only with one inputfile");
ifiles = (const char **) Malloc(globs->Nfiles * sizeof(char *));
for (i = 0; i < globs->Nfiles; i++) ifiles[i] = argv[--fargcn];
}
if (ofile2) fprintf(stdout, "Variance File: %-25s\n", ofile2);
if (globs->Debug)
{
extern int afterDebug;
afterDebug = globs->Debug;
fprintf(stderr, "* Debug on! *\n");
fprintf(stderr, " Maximum ffts to run in parallel: %ld\n", get_nfft());
}
/* read optional VCT */
if (Vctfile) after_readVct(globs, Vctfile);
/* --------------------- */
/* open in/output file */
/* --------------------- */
cdiDefGlobal("REGULARGRID", 1);
after_processing(globs, vars);
after_procstat(proc, globs->Truncation);
FreeMean(vars);
Free(globs);
return 0;
}
#endif
#ifdef CDO
void *
Afterburner(void *process)
{
......@@ -2451,10 +2008,3 @@ Afterburner(void *process)
return 0;
}
#else
int
main(int argc, char *argv[])
{
return afterburner(argc, argv);
}
#endif
......@@ -233,7 +233,7 @@ sign(double x)
}
static bool
is_simple_polygon_convex(double cell_corners[], int number_corners)
is_simple_polygon_convex(const std::vector<double> &cell_corners, int number_corners)
{
/* Tests in which direction the polygon winds when walking along its edges. Does so for all edges of the polygon. */
......@@ -340,7 +340,7 @@ verify_grid(int gridtype, size_t gridsize, int gridno, int ngrids, int ncorner,
/* For performing the first test, an array of all center point coordinates is built. */
double *center_point_array = (double *) Malloc(gridsize * 2 * sizeof(double));
std::vector<double> center_point_array(gridsize * 2);
for (size_t cell_no = 0; cell_no < gridsize; cell_no++)
{
......@@ -350,7 +350,7 @@ verify_grid(int gridtype, size_t gridsize, int gridno, int ngrids, int ncorner,
/* The cell center points are sorted by their first coordinate (lon) with quicksort. */
quick_sort_by_lon(center_point_array, gridsize * 2);
quick_sort_by_lon(center_point_array.data(), gridsize * 2);