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

select: added search key timestepmask to select timesteps by a mask.

parent 1fadaa09
......@@ -58,6 +58,38 @@ void write_const_vars(int streamID2, int vlistID2, int nvars, double **vardata2)
}
}
static
void eval_timestepmask(const char *maskfile, list_t *kvlist)
{
int n = 0;
bool *imask = cdo_read_timestepmask(maskfile, &n);
int nvals = 0;
for ( int i = 0; i < n; ++i ) if ( imask[i] ) nvals++;
if ( nvals == 0 ) cdoPrint("timestepmask has no values!");
else
{
char **values = (char**) Malloc(nvals*sizeof(char*));
int j = 0;
for ( int i = 0; i < n; ++i )
{
if ( imask[i] )
{
size_t length = (size_t)log10(j+1)+2;
values[j] = (char*) Malloc(length*sizeof(char));
sprintf(values[j++], "%d", i+1);
}
}
kvlist_append(kvlist, "timestep", (const char **)values, nvals);
for ( int i = 0; i < nvals; ++i ) Free(values[i]);
Free(values);
}
Free(imask);
}
void *Select(void *argument)
{
......@@ -102,6 +134,13 @@ void *Select(void *argument)
if ( kvlist_parse_cmdline(kvlist, nsel, argnames) != 0 ) cdoAbort("Parse error!");
if ( cdoVerbose ) kvlist_print(kvlist);
keyValues_t *kv = kvlist_search(kvlist, "timestepmask");
if ( kv && kv->nvalues > 0 )
{
if ( kvlist_search(kvlist, "timestep") ) cdoAbort("Parameter timestep and timestepmask can't be combined!");
eval_timestepmask(kv->values[0], kvlist);
}
sellist_t *sellist = sellist_create(kvlist);
SELLIST_ADD_INT(timestep_of_year, "Timestep of year");
......@@ -130,19 +169,11 @@ void *Select(void *argument)
if ( cdoVerbose ) sellist_print(sellist);
if ( SELLIST_NVAL(timestepmask) )
{
if ( SELLIST_NVAL(timestep) ) cdoAbort("Parameter timestep and timestepmask can't be combined!");
SELLIST_GET_VAL(timestepmask, 0, &timestepmask);
printf("mask: %s\n", timestepmask);
int n = 0;
bool *imask = cdo_read_timestepmask(timestepmask, &n);
for ( int i = 0; i < n; ++i ) printf("%d %d\n", i+1, imask[i]);
Free(imask);
}
sellist_verify(sellist);
if ( SELLIST_NVAL(timestepmask) > 1 ) cdoAbort("Key timestepmask has too many values!");
UNUSED(timestepmask);
int streamCnt = cdoStreamCnt();
int nfiles = streamCnt - 1;
......
......@@ -333,6 +333,17 @@ void sellist_def_val(sellist_t *sellist, int idx, int vindex, void *val)
}
}
static
void sellist_print_val(int type, cvalues_t *cvalues, int i)
{
switch (type)
{
case SELLIST_INT: printf(" %d", ((int*)cvalues)[i]); break;
case SELLIST_FLT: printf(" %g", ((double*)cvalues)[i]); break;
case SELLIST_WORD: printf(" %s", ((char**)cvalues)[i]); break;
}
}
void sellist_print(sellist_t *sellist)
{
......@@ -345,15 +356,10 @@ void sellist_print(sellist_t *sellist)
selentry_t *e = &(sellist->entry[idx]);
printf("%3d %-16s %4d %4d ", idx+1, e->key, e->type, e->nvalues);
int nvalues = e->nvalues;
if ( nvalues > 12 ) nvalues = 12;
for ( int i = 0; i < nvalues; ++i )
switch (e->type)
{
case SELLIST_INT: printf(" %d", ((int*)e->cvalues)[i]); break;
case SELLIST_FLT: printf(" %g", ((double*)e->cvalues)[i]); break;
case SELLIST_WORD: printf(" %s", ((char**)e->cvalues)[i]); break;
}
if ( nvalues > 12 ) nvalues = 11;
for ( int i = 0; i < nvalues; ++i ) sellist_print_val(e->type, e->cvalues, i);
if ( nvalues < e->nvalues ) printf(" ...");
sellist_print_val(e->type, e->cvalues, e->nvalues-1);
printf("\n");
}
}
......
Supports Markdown
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