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

added pmlist.c and pmlist.h

parent f892036f
......@@ -660,6 +660,8 @@ src/percentiles_hist.c -text
src/percentiles_hist.h -text
src/pipe.c -text
src/pipe.h -text
src/pmlist.c -text
src/pmlist.h -text
src/pragma_omp_atomic_update.h -text
src/printinfo.h -text
src/process.c -text
......
......@@ -100,6 +100,8 @@ libcdo_la_SOURCES = \
percentiles.h \
pipe.c \
pipe.h \
pmlist.c \
pmlist.h \
pragma_omp_atomic_update.h \
printinfo.h \
process.c \
......
......@@ -127,11 +127,11 @@ am_libcdo_la_OBJECTS = libcdo_la-cdo_pthread.lo libcdo_la-cdo_vlist.lo \
libcdo_la-namelist.lo libcdo_la-normal.lo \
libcdo_la-nth_element.lo libcdo_la-par_io.lo \
libcdo_la-percentiles_hist.lo libcdo_la-percentiles.lo \
libcdo_la-pipe.lo libcdo_la-process.lo libcdo_la-pstream.lo \
libcdo_la-pthread_debug.lo libcdo_la-readline.lo \
libcdo_la-realtime.lo libcdo_la-remaplib.lo \
libcdo_la-remapsort.lo libcdo_la-remap_scrip_io.lo \
libcdo_la-remap_search_reg2d.lo \
libcdo_la-pipe.lo libcdo_la-pmlist.lo libcdo_la-process.lo \
libcdo_la-pstream.lo libcdo_la-pthread_debug.lo \
libcdo_la-readline.lo libcdo_la-realtime.lo \
libcdo_la-remaplib.lo libcdo_la-remapsort.lo \
libcdo_la-remap_scrip_io.lo libcdo_la-remap_search_reg2d.lo \
libcdo_la-remap_search_latbins.lo \
libcdo_la-remap_store_link.lo \
libcdo_la-remap_store_link_cnsrv.lo libcdo_la-remap_conserv.lo \
......@@ -516,25 +516,25 @@ libcdo_la_SOURCES = cdo_int.h compare.h cdo_pthread.c cdo_vlist.c \
modules.c modules.h namelist.c namelist.h normal.c \
nth_element.c nth_element.h operator_help.h par_io.c par_io.h \
percentiles_hist.c percentiles_hist.h percentiles.c \
percentiles.h pipe.c pipe.h pragma_omp_atomic_update.h \
printinfo.h process.c process.h pstream.c pstream.h \
pstream_write.h pstream_int.h pthread_debug.c pthread_debug.h \
readline.c realtime.c remap.h remaplib.c remapsort.c \
remap_scrip_io.c remap_search_reg2d.c remap_search_latbins.c \
remap_store_link.c remap_store_link.h remap_store_link_cnsrv.c \
remap_store_link_cnsrv.h remap_conserv.c remap_conserv_scrip.c \
remap_distwgt.c remap_bicubic_scrip.c remap_bilinear_scrip.c \
stdnametable.c stdnametable.h specspace.c specspace.h \
statistic.c statistic.h table.c text.c text.h timebase.h \
timer.c userlog.c uthash.h util.c util.h zaxis.c \
kdtreelib/kdtree.h kdtreelib/kdtree_cartesian.c \
kdtreelib/kdtree_common.c kdtreelib/kdtree_spherical.c \
kdtreelib/pmergesort.c kdtreelib/pqueue.c kdtreelib/pqueue.h \
clipping/clipping.c clipping/clipping.h clipping/area.c \
clipping/area.h clipping/ensure_array_size.c \
clipping/ensure_array_size.h clipping/geometry.h \
clipping/grid.h clipping/points.h clipping/dep_list.h \
clipping/grid_cell.c clipping/grid_cell.h \
percentiles.h pipe.c pipe.h pmlist.c pmlist.h \
pragma_omp_atomic_update.h printinfo.h process.c process.h \
pstream.c pstream.h pstream_write.h pstream_int.h \
pthread_debug.c pthread_debug.h readline.c realtime.c remap.h \
remaplib.c remapsort.c remap_scrip_io.c remap_search_reg2d.c \
remap_search_latbins.c remap_store_link.c remap_store_link.h \
remap_store_link_cnsrv.c remap_store_link_cnsrv.h \
remap_conserv.c remap_conserv_scrip.c remap_distwgt.c \
remap_bicubic_scrip.c remap_bilinear_scrip.c stdnametable.c \
stdnametable.h specspace.c specspace.h statistic.c statistic.h \
table.c text.c text.h timebase.h timer.c userlog.c uthash.h \
util.c util.h zaxis.c kdtreelib/kdtree.h \
kdtreelib/kdtree_cartesian.c kdtreelib/kdtree_common.c \
kdtreelib/kdtree_spherical.c kdtreelib/pmergesort.c \
kdtreelib/pqueue.c kdtreelib/pqueue.h clipping/clipping.c \
clipping/clipping.h clipping/area.c clipping/area.h \
clipping/ensure_array_size.c clipping/ensure_array_size.h \
clipping/geometry.h clipping/grid.h clipping/points.h \
clipping/dep_list.h clipping/grid_cell.c clipping/grid_cell.h \
clipping/intersection.c clipping/utils.c clipping/utils.h
#
#if ENABLE_NEARPT3
......@@ -1027,6 +1027,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libcdo_la-percentiles.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libcdo_la-percentiles_hist.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libcdo_la-pipe.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libcdo_la-pmlist.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libcdo_la-process.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libcdo_la-pstream.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libcdo_la-pthread_debug.Plo@am__quote@
......@@ -1481,6 +1482,13 @@ libcdo_la-pipe.lo: pipe.c
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libcdo_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o libcdo_la-pipe.lo `test -f 'pipe.c' || echo '$(srcdir)/'`pipe.c
libcdo_la-pmlist.lo: pmlist.c
@am__fastdepCC_TRUE@ $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libcdo_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT libcdo_la-pmlist.lo -MD -MP -MF $(DEPDIR)/libcdo_la-pmlist.Tpo -c -o libcdo_la-pmlist.lo `test -f 'pmlist.c' || echo '$(srcdir)/'`pmlist.c
@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libcdo_la-pmlist.Tpo $(DEPDIR)/libcdo_la-pmlist.Plo
@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='pmlist.c' object='libcdo_la-pmlist.lo' libtool=yes @AMDEPBACKSLASH@
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libcdo_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o libcdo_la-pmlist.lo `test -f 'pmlist.c' || echo '$(srcdir)/'`pmlist.c
libcdo_la-process.lo: process.c
@am__fastdepCC_TRUE@ $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libcdo_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT libcdo_la-process.lo -MD -MP -MF $(DEPDIR)/libcdo_la-process.Tpo -c -o libcdo_la-process.lo `test -f 'process.c' || echo '$(srcdir)/'`process.c
@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libcdo_la-process.Tpo $(DEPDIR)/libcdo_la-process.Plo
......
This diff is collapsed.
#include "cdo_int.h"
#include "pmlist.h"
static
void pml_init(pml_t *pml, const char *name)
{
pml->size = 0;
pml->name = strdup(name);
}
pml_t *pml_create(const char *name)
{
pml_t *pml = (pml_t*) Malloc(sizeof(pml_t));
pml_init(pml, name);
return pml;
}
void pml_destroy(pml_t *pml)
{
if ( pml == NULL ) return;
for ( int i = 0; i < pml->size; ++i )
{
if ( pml->entry[i]->txt ) free(pml->entry[i]->txt);
if ( pml->entry[i]->name ) free(pml->entry[i]->name);
if ( pml->entry[i] ) Free(pml->entry[i]);
}
if ( pml->name ) free(pml->name);
Free(pml);
}
void pml_print(pml_t *pml)
{
if ( pml == NULL ) return;
fprintf(stdout, "Parameter list: %s\n", pml->name);
fprintf(stdout, " Num Name Type Size Occ Entries\n");
for ( int i = 0; i < pml->size; ++i )
{
pml_entry_t *entry = pml->entry[i];
fprintf(stdout, "%4d %-16s %4d %4d %4d ",
i+1, pml->entry[i]->name, pml->entry[i]->type, (int)pml->entry[i]->size, pml->entry[i]->occ);
int nout = pml->entry[i]->occ;
if ( nout > 8 ) nout = 8;
for ( int j = 0; j < nout; j++ )
{
switch (entry->type)
{
case PML_WORD: fprintf(stdout, " %s", ((char **)entry->ptr)[j]); break;
case PML_INT: fprintf(stdout, " %d", ((int *)entry->ptr)[j]); break;
case PML_FLT: fprintf(stdout, " %g", ((double *)entry->ptr)[j]); break;
}
}
fprintf(stdout, "\n");
}
}
int pml_add(pml_t *pml, const char *txt, const char *name, int type, void *ptr, size_t size)
{
if ( pml->size >= MAX_PML_ENTRY )
{
fprintf(stderr, "Too many entries in parameter list %s! (Max = %d)\n", pml->name, MAX_PML_ENTRY);
return -1;
}
pml_entry_t *pml_entry = (pml_entry_t*) Malloc(sizeof(pml_entry_t));
pml_entry->txt = strdup(txt);
pml_entry->name = strdup(name);
pml_entry->len = strlen(name);
pml_entry->type = type;
pml_entry->ptr = ptr;
pml_entry->size = size;
pml_entry->occ = 0;
int entry = pml->size;
pml->entry[pml->size++] = pml_entry;
return entry;
}
int pml_num_par(pml_t *pml, int pid)
{
int nocc = 0;
if ( pml && pid >= 0 && pid < pml->size ) nocc = pml->entry[pid]->occ;
return nocc;
}
int pml_num(pml_t *pml, const char *name)
{
pml_entry_t *entry;
int i, nocc = 0;
if ( pml == NULL ) return nocc;
for ( i = 0; i < pml->size; i++ )
{
entry = pml->entry[i];
if ( strcmp(name, entry->name) == 0 )
{
nocc = entry->occ;
break;
}
}
if ( i == pml->size )
fprintf(stderr, "Parameter list entry %s not found in %s\n", name, pml->name);
return nocc;
}
void split_intstring(const char *intstr, int *first, int *last, int *inc);
int pml_add_entry(pml_entry_t *entry, char *arg)
{
int status = 0;
if ( entry->type == PML_INT )
{
int ival, first, last, inc;
split_intstring(arg, &first, &last, &inc);
if ( inc >= 0 )
{
for ( ival = first; ival <= last; ival += inc )
if ( entry->occ < (int) entry->size )
((int *) entry->ptr)[entry->occ++] = ival;
}
else
{
for ( ival = first; ival >= last; ival += inc )
if ( entry->occ < (int) entry->size )
((int *) entry->ptr)[entry->occ++] = ival;
}
}
else if ( entry->type == PML_FLT )
{
if ( entry->occ < (int) entry->size )
((double *) entry->ptr)[entry->occ++] = atof(arg);
}
else if ( entry->type == PML_WORD )
{
if ( entry->occ < (int) entry->size )
((char **) entry->ptr)[entry->occ++] = strdupx(arg);
}
else
{
fprintf(stderr, "Unsupported type!\n");
}
return status;
}
static
void pml_process(pml_entry_t *entry, int argc, char **argv)
{
for ( int i = 0; i < argc; ++i )
{
char *parg = argv[i];
if ( i == 0 )
{
char *epos = strchr(parg, '=');
if ( epos == NULL ) fprintf(stderr, "Internal problem, keyword not found!\n");
parg += epos-parg+1;
}
pml_add_entry(entry, parg);
}
}
int pml_read(pml_t *pml, int argc, char **argv)
{
pml_entry_t *entry = NULL;
pml_entry_t *pentry[MAX_PML_ENTRY];
int params[MAX_PML_ENTRY];
int num_par[MAX_PML_ENTRY];
int nparams = 0;
int i;
char *epos;
size_t len;
int bufsize = 0;
int status = 0;
for ( i = 0; i < argc; ++i )
{
len = strlen(argv[i]);
bufsize += len+1;
}
char *parbuf = (char*) Malloc(bufsize*sizeof(char));
memset(parbuf, 0, bufsize*sizeof(char));
int istart = 0;
while ( istart < argc )
{
epos = strchr(argv[istart], '=');
if ( epos == NULL )
{
fprintf(stderr, "Parameter >%s< has no keyword!\n", argv[istart]);
status = 1;
goto END_LABEL;
}
len = epos - argv[istart];
for ( i = 0; i < pml->size; ++i )
{
entry = pml->entry[i];
if ( entry->len == len )
if ( memcmp(entry->name, argv[istart], len) == 0 ) break;
}
if ( i == pml->size )
{
fprintf(stderr, "Parameter >%s< has an invalid keyword!\n", argv[istart]);
status = 2;
goto END_LABEL;
}
num_par[nparams] = 0;
pentry[nparams] = entry;
params[nparams] = istart;
num_par[nparams] = 1;
istart++;
for ( i = istart; i < argc; ++i )
{
if ( *argv[i] == 0 ) { i++; break;}
epos = strchr(argv[i], '=');
if ( epos != NULL ) break;
num_par[nparams]++;
}
istart = i;
nparams++;
}
for ( i = 0; i < nparams; ++i )
{
pml_process(pentry[i], num_par[i], &argv[params[i]]);
}
END_LABEL:
Free(parbuf);
return status;
}
bool pml_check_int(pml_t *pml, int pid, int *parlist, bool *flaglist, int par)
{
bool found = false;
int npar = pml_num_par(pml, pid);
for ( int i = 0; i < npar; i++ )
if ( par == parlist[i] ) { found = true; flaglist[i] = true;/* break;*/}
return found;
}
bool pml_check_flt(pml_t *pml, int pid, double *parlist, bool *flaglist, double par)
{
bool found = false;
int npar = pml_num_par(pml, pid);
for ( int i = 0; i < npar; i++ )
if ( fabs(par - parlist[i]) < 1.e-4 ) { found = true; flaglist[i] = true;/* break;*/}
return found;
}
bool pml_check_word(pml_t *pml, int pid, char **parlist, bool *flaglist, const char *par)
{
bool found = false;
int npar = pml_num_par(pml, pid);
for ( int i = 0; i < npar; i++ )
if ( wildcardmatch(parlist[i], par) == 0 ) { found = true; flaglist[i] = true;/* break;*/}
return found;
}
bool pml_check_date(pml_t *pml, int pid, char **parlist, bool *flaglist, const char *par)
{
bool found = false;
int npar = pml_num_par(pml, pid);
char wcdate[512];
if ( *par == ' ' ) ++par;
for ( int i = 0; i < npar; i++ )
{
strcpy(wcdate, parlist[i]);
strcat(wcdate, "*");
if ( wildcardmatch(wcdate, par) == 0 ) { found = true; flaglist[i] = true;/* break;*/}
}
return found;
}
void season_to_months(const char *season, int *imonths);
bool pml_check_season(pml_t *pml, int pid, char **parlist, bool *flaglist, int month)
{
assert(month>=1&&month<=12);
bool found = false;
int npar = pml_num_par(pml, pid);
int imon[13]; /* 1-12 ! */
for ( int i = 0; i < npar; i++ )
{
for ( int m = 0; m < 13; ++m ) imon[m] = 0;
season_to_months(parlist[i], imon);
if ( imon[month] ) { found = true; flaglist[i] = true;/* break;*/}
}
return found;
}
void pml_check_int_flag(pml_t *pml, int pid, int *parlist, bool *flaglist)
{
int npar = pml_num_par(pml, pid);
for ( int i = 0; i < npar; ++i )
if ( flaglist[i] == false )
cdoWarning("%s >%d< not found!", pml->entry[pid]->txt, parlist[i]);
}
void pml_check_flt_flag(pml_t *pml, int pid, double *parlist, bool *flaglist)
{
int npar = pml_num_par(pml, pid);
for ( int i = 0; i < npar; ++i )
if ( flaglist[i] == false )
cdoWarning("%s >%g< not found!", pml->entry[pid]->txt, parlist[i]);
}
void pml_check_word_flag(pml_t *pml, int pid, char **parlist, bool *flaglist)
{
int npar = pml_num_par(pml, pid);
for ( int i = 0; i < npar; ++i )
if ( flaglist[i] == false )
cdoWarning("%s >%s< not found!", pml->entry[pid]->txt, parlist[i]);
}
/*
This file is part of CDO. CDO is a collection of Operators to
manipulate and analyse Climate model Data.
Copyright (C) 2003-2016 Uwe Schulzweida, <uwe.schulzweida AT mpimet.mpg.de>
See COPYING file for copying and redistribution conditions.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#ifndef _PMLIST_H
#define _PMLIST_H
#define PML_INT 1
#define PML_FLT 2
#define PML_WORD 3
#define PML_DATE 4
#define PML_TIME 4
#define PML_INIT(name, size) memset(flag_##name, 0, size * sizeof(bool))
#define PML_DEF(name, size) bool flag_##name[size]
#define PML_DEF_INT(name, size) int par_##name[size]; int name = 0; PML_DEF(name, size); PML_INIT(name, size)
#define PML_DEF_FLT(name, size) double par_##name[size]; double name = 0; PML_DEF(name, size); PML_INIT(name, size)
#define PML_DEF_WORD(name, size) char *par_##name[size]; const char *name = 0; PML_DEF(name, size); PML_INIT(name, size)
#define PML_ADD_INT(pml, name, size, txt) PML_DEF_INT(name, size); int pid_##name = pml_add(pml, txt, #name, PML_INT, par_##name, sizeof(par_##name)/sizeof(int))
#define PML_ADD_FLT(pml, name, size, txt) PML_DEF_FLT(name, size); int pid_##name = pml_add(pml, txt, #name, PML_FLT, par_##name, sizeof(par_##name)/sizeof(double))
#define PML_ADD_WORD(pml, name, size, txt) PML_DEF_WORD(name, size); int pid_##name = pml_add(pml, txt, #name, PML_WORD, par_##name, sizeof(par_##name)/sizeof(char *))
#define PML_NOCC(pml, name) pml_num_par(pml, pid_##name)
//#define PML_NUM(pml, name) npar_##name = pml_num(pml, #name)
#define PML_CHECK_INT_FLAG(pml, name) pml_check_int_flag(pml, pid_##name, par_##name, flag_##name)
#define PML_CHECK_FLT_FLAG(pml, name) pml_check_flt_flag(pml, pid_##name, par_##name, flag_##name)
#define PML_CHECK_WORD_FLAG(pml, name) pml_check_word_flag(pml, pid_##name, par_##name, flag_##name)
#define PML_CHECK_INT(pml, name) pml_check_int(pml, pid_##name, par_##name, flag_##name, name)
#define PML_CHECK_FLT(pml, name) pml_check_flt(pml, pid_##name, par_##name, flag_##name, name)
#define PML_CHECK_WORD(pml, name) pml_check_word(pml, pid_##name, par_##name, flag_##name, name)
#define PML_CHECK_DATE(pml, name) pml_check_date(pml, pid_##name, par_##name, flag_##name, name)
#define PML_CHECK_SEASON(pml, name, month) pml_check_season(pml, pid_##name, par_##name, flag_##name, month)
#define MAX_PML_ENTRY 256
typedef struct
{
char *txt;
char *name;
size_t len;
void *ptr;
int type;
int occ;
size_t size;
} pml_entry_t;
typedef struct
{
int size;
char *name;
pml_entry_t *entry[MAX_PML_ENTRY];
} pml_t;
pml_t *pml_create(const char *name);
void pml_destroy(pml_t *pml);
void pml_print(pml_t *pml);
int pml_add(pml_t *pml, const char *txt, const char *name, int type, void *ptr, size_t size);
int pml_num_par(pml_t *pml, int pid);
int pml_add_entry(pml_entry_t *entry, char *arg);
int pml_read(pml_t *pml, int argc, char **argv);
bool pml_check_int(pml_t *pml, int pid, int *parlist, bool *flaglist, int par);
bool pml_check_flt(pml_t *pml, int pid, double *parlist, bool *flaglist, double par);
bool pml_check_word(pml_t *pml, int pid, char **parlist, bool *flaglist, const char *par);
bool pml_check_date(pml_t *pml, int pid, char **parlist, bool *flaglist, const char *par);
bool pml_check_season(pml_t *pml, int pid, char **parlist, bool *flaglist, int month);
void pml_check_int_flag(pml_t *pml, int pid, int *parlist, bool *flaglist);
void pml_check_flt_flag(pml_t *pml, int pid, double *parlist, bool *flaglist);
void pml_check_word_flag(pml_t *pml, int pid, char **parlist, bool *flaglist);
#endif /* _PMLIST_H */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment