Commit 4c432359 authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

Added new operator setattribute.

parent cb37868f
......@@ -3,6 +3,10 @@
* Using CDI library version 1.8.0
* Version 1.8.0 release
2017-02-07 Uwe Schulzweida
* New operator setattribute: set attributes
2017-02-03 Uwe Schulzweida
* using CDI library version 1.8.0rc7
......
......@@ -309,6 +309,7 @@ cdo_SOURCES += Adisit.c \
Seltime.c \
Selvar.c \
Set.c \
Setattribute.c \
Setbox.c \
Setgatt.c \
Setgrid.c \
......
......@@ -219,7 +219,8 @@ am_cdo_OBJECTS = cdo-cdo.$(OBJEXT) cdo-Adisit.$(OBJEXT) \
cdo-Selbox.$(OBJEXT) cdo-Selindex.$(OBJEXT) \
cdo-Select.$(OBJEXT) cdo-Seloperator.$(OBJEXT) \
cdo-Selrec.$(OBJEXT) cdo-Seltime.$(OBJEXT) \
cdo-Selvar.$(OBJEXT) cdo-Set.$(OBJEXT) cdo-Setbox.$(OBJEXT) \
cdo-Selvar.$(OBJEXT) cdo-Set.$(OBJEXT) \
cdo-Setattribute.$(OBJEXT) cdo-Setbox.$(OBJEXT) \
cdo-Setgatt.$(OBJEXT) cdo-Setgrid.$(OBJEXT) \
cdo-Sethalo.$(OBJEXT) cdo-Setmiss.$(OBJEXT) \
cdo-Setpartab.$(OBJEXT) cdo-Setrcaname.$(OBJEXT) \
......@@ -577,8 +578,8 @@ cdo_SOURCES = cdo.c Adisit.c Afterburner.c Arith.c Arithc.c \
Pack.c Pardup.c Pinfo.c Pressure.c Regres.c Remap.c Remapeta.c \
Replace.c Replacevalues.c Rhopot.c Rotuv.c Runpctl.c Runstat.c \
Seascount.c Seaspctl.c Seasstat.c Selbox.c Selindex.c Select.c \
Seloperator.c Selrec.c Seltime.c Selvar.c Set.c Setbox.c \
Setgatt.c Setgrid.c Sethalo.c Setmiss.c Setpartab.c \
Seloperator.c Selrec.c Seltime.c Selvar.c Set.c Setattribute.c \
Setbox.c Setgatt.c Setgrid.c Sethalo.c Setmiss.c Setpartab.c \
Setrcaname.c Settime.c Setzaxis.c Shiftxy.c Showinfo.c Sinfo.c \
Smooth.c Sort.c Sorttimestamp.c Specinfo.c Spectral.c \
Spectrum.c Split.c Splitrec.c Splitsel.c Splittime.c \
......@@ -922,6 +923,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cdo-Seltime.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cdo-Selvar.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cdo-Set.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cdo-Setattribute.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cdo-Setbox.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cdo-Setgatt.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cdo-Setgrid.Po@am__quote@
......@@ -3532,6 +3534,20 @@ cdo-Set.obj: Set.c
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(cdo_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o cdo-Set.obj `if test -f 'Set.c'; then $(CYGPATH_W) 'Set.c'; else $(CYGPATH_W) '$(srcdir)/Set.c'; fi`
cdo-Setattribute.o: Setattribute.c
@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(cdo_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT cdo-Setattribute.o -MD -MP -MF $(DEPDIR)/cdo-Setattribute.Tpo -c -o cdo-Setattribute.o `test -f 'Setattribute.c' || echo '$(srcdir)/'`Setattribute.c
@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/cdo-Setattribute.Tpo $(DEPDIR)/cdo-Setattribute.Po
@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='Setattribute.c' object='cdo-Setattribute.o' libtool=no @AMDEPBACKSLASH@
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(cdo_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o cdo-Setattribute.o `test -f 'Setattribute.c' || echo '$(srcdir)/'`Setattribute.c
cdo-Setattribute.obj: Setattribute.c
@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(cdo_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT cdo-Setattribute.obj -MD -MP -MF $(DEPDIR)/cdo-Setattribute.Tpo -c -o cdo-Setattribute.obj `if test -f 'Setattribute.c'; then $(CYGPATH_W) 'Setattribute.c'; else $(CYGPATH_W) '$(srcdir)/Setattribute.c'; fi`
@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/cdo-Setattribute.Tpo $(DEPDIR)/cdo-Setattribute.Po
@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='Setattribute.c' object='cdo-Setattribute.obj' libtool=no @AMDEPBACKSLASH@
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(cdo_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o cdo-Setattribute.obj `if test -f 'Setattribute.c'; then $(CYGPATH_W) 'Setattribute.c'; else $(CYGPATH_W) '$(srcdir)/Setattribute.c'; fi`
cdo-Setbox.o: Setbox.c
@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(cdo_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT cdo-Setbox.o -MD -MP -MF $(DEPDIR)/cdo-Setbox.Tpo -c -o cdo-Setbox.o `test -f 'Setbox.c' || echo '$(srcdir)/'`Setbox.c
@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/cdo-Setbox.Tpo $(DEPDIR)/cdo-Setbox.Po
......
/*
This file is part of CDO. CDO is a collection of Operators to
manipulate and analyse Climate model Data.
Copyright (C) 2003-2017 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.
*/
#include <cdi.h>
#include "cdo.h"
#include "cdo_int.h"
#include "pstream.h"
static
void set_attributes(list_t *kvlist, int vlistID)
{
int nvars = vlistNvars(vlistID);
const int delim = '@';
char name[CDI_MAX_NAME];
char buffer[CDI_MAX_NAME];
for ( listNode_t *kvnode = kvlist->head; kvnode; kvnode = kvnode->next )
{
char *varname = NULL, *attname = NULL;
keyValues_t *kv = *(keyValues_t **)kvnode->data;
strcpy(buffer, kv->key);
char *result = strchr(buffer, delim);
if ( result == NULL )
{
attname = buffer;
}
else
{
attname = result+1;
*result = 0;
varname = buffer;
}
if ( *attname == 0 ) cdoAbort("Attribute name missing in >%s<!", kv->key);
int cdiID = -2;
if ( varname && *varname )
{
for ( int varID = 0; varID < nvars; varID++ )
{
vlistInqVarName(vlistID, varID, name);
if ( wildcardmatch(varname, name) == 0 )
{
cdiID = varID;
break;
}
}
if ( cdiID < 0 ) cdoWarning("Variable >%s< not found!");
}
else
{
cdiID = CDI_GLOBAL;
}
if ( cdiID != -2 )
{
const char *value = (kv->nvalues > 0) ? kv->values[0] : NULL;
int nvalues = kv->nvalues;
if ( nvalues == 1 && !*value ) nvalues = 0;
int dtype = literals_find_datatype(nvalues, kv->values);
if ( dtype == CDI_DATATYPE_INT8 || dtype == CDI_DATATYPE_INT16 || dtype == CDI_DATATYPE_INT32 )
{
int *ivals = (int*) Malloc(nvalues*sizeof(int));
for ( int i = 0; i < nvalues; ++i ) ivals[i] = literal_to_int(kv->values[i]);
cdiDefAttInt(vlistID, cdiID, attname, dtype, nvalues, ivals);
Free(ivals);
}
else if ( dtype == CDI_DATATYPE_FLT32 || dtype == CDI_DATATYPE_FLT64 )
{
double *dvals = (double*) Malloc(nvalues*sizeof(double));
for ( int i = 0; i < nvalues; ++i ) dvals[i] = literal_to_double(kv->values[i]);
cdiDefAttFlt(vlistID, cdiID, attname, dtype, nvalues, dvals);
Free(dvals);
}
else
{
int len = (value && *value) ? (int) strlen(value) : 0;
cdiDefAttTxt(vlistID, cdiID, attname, len, value);
}
}
}
}
void *Setattribute(void *argument)
{
int nrecs;
int varID, levelID;
cdoInitialize(argument);
cdoOperatorAdd("setattribute", 0, 0, "attributes");
bool lcopy = UNCHANGED_RECORD;
int operatorID = cdoOperatorID();
operatorInputArg(cdoOperatorEnter(operatorID));
int natts = operatorArgc();
if ( natts == 0 ) cdoAbort("Parameter missing!");
list_t *pmlist = NULL;
list_t *kvlist = kvlist_new("SETATTRIBUTES");
if ( kvlist_parse_cmdline(kvlist, natts, operatorArgv()) != 0 ) cdoAbort("Parse error!");
if ( cdoVerbose ) kvlist_print(kvlist);
if ( natts == 1 )
{
keyValues_t *kv = *(keyValues_t **)kvlist->head->data;
if ( STR_IS_EQ(kv->key, "file") )
{
const char *filename = parameter2word(kv->values[0]);
FILE *fp = fopen(filename, "r");
if ( fp == NULL ) cdoAbort("Open failed on: %s\n", filename);
pmlist = namelist_to_pmlist(fp, filename);
if ( pmlist == NULL ) cdoAbort("Parse error!");
list_destroy(kvlist);
kvlist = *(list_t **)pmlist->head->data;
if ( kvlist == NULL ) cdoAbort("Parse error!");;
fclose(fp);
if ( cdoVerbose ) kvlist_print(kvlist);
}
}
int streamID1 = streamOpenRead(cdoStreamName(0));
int vlistID1 = streamInqVlist(streamID1);
int vlistID2 = vlistDuplicate(vlistID1);
set_attributes(kvlist, vlistID2);
if ( pmlist ) list_destroy(pmlist);
else list_destroy(kvlist);
int taxisID1 = vlistInqTaxis(vlistID1);
int taxisID2 = taxisDuplicate(taxisID1);
vlistDefTaxis(vlistID2, taxisID2);
int streamID2 = streamOpenWrite(cdoStreamName(1), cdoFiletype());
streamDefVlist(streamID2, vlistID2);
double *array = NULL;
if ( ! lcopy )
{
size_t gridsize = (size_t) vlistGridsizeMax(vlistID1);
if ( vlistNumber(vlistID1) != CDI_REAL ) gridsize *= 2;
array = (double*) Malloc(gridsize*sizeof(double));
}
int tsID = 0;
while ( (nrecs = streamInqTimestep(streamID1, tsID)) )
{
taxisCopyTimestep(taxisID2, taxisID1);
streamDefTimestep(streamID2, tsID);
for ( int recID = 0; recID < nrecs; recID++ )
{
streamInqRecord(streamID1, &varID, &levelID);
streamDefRecord(streamID2, varID, levelID);
if ( lcopy )
{
streamCopyRecord(streamID2, streamID1);
}
else
{
int nmiss;
streamReadRecord(streamID1, array, &nmiss);
streamWriteRecord(streamID2, array, nmiss);
}
}
tsID++;
}
streamClose(streamID1);
streamClose(streamID2);
if ( array ) Free(array);
cdoFinish();
return 0;
}
......@@ -22,8 +22,6 @@
Setgatt setgatts Set global attributes
*/
#include <ctype.h>
#include <cdi.h>
#include "cdo.h"
#include "cdo_int.h"
......
......@@ -155,6 +155,7 @@ void *Seloperator(void *argument);
void *Selrec(void *argument);
void *Seltime(void *argument);
void *Set(void *argument);
void *Setattribute(void *argument);
void *Setbox(void *argument);
void *Setgatt(void *argument);
void *Setgrid(void *argument);
......@@ -412,6 +413,7 @@ void *Maggraph(void *argument);
#define SeltimeOperators {"seltimestep", "selyear", "selseason", "selmonth", "selday", "selhour", "seldate", \
"seltime", "selsmon"}
#define SetOperators {"setcode", "setparam", "setname", "setunit", "setlevel", "setltype", "settabnum"}
#define SetattributeOperators {"setattribute"}
#define SetboxOperators {"setclonlatbox", "setcindexbox"}
#define SetgattOperators {"setgatt", "setgatts"}
#define SetgridOperators {"setgrid", "setgridtype", "setgridarea", "setgridmask", "unsetgridmask", "setgridnumber", "setgriduri"}
......@@ -695,6 +697,7 @@ static modules_t Modules[] =
{ Seloperator, NULL, SeloperatorOperators, 1, CDI_REAL, 1, 1 },
{ Seltime, SeltimeHelp, SeltimeOperators, 1, CDI_BOTH, 1, 1 },
{ Set, SetHelp, SetOperators, 1, CDI_BOTH, 1, 1 },
{ Setattribute, NULL, SetattributeOperators, 1, CDI_BOTH, 1, 1 },
{ Setbox, SetboxHelp, SetboxOperators, 1, CDI_REAL, 1, 1 },
{ Setgatt, SetgattHelp, SetgattOperators, 1, CDI_BOTH, 1, 1 },
{ Setgrid, SetgridHelp, SetgridOperators, 1, CDI_BOTH, 1, 1 },
......
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