Commit 6cf35503 authored by Fabian Wachsmann's avatar Fabian Wachsmann
Browse files

Added operators showatts, showattsglob, showattsvar, showattribute

parent dabdcc00
......@@ -332,6 +332,7 @@ cdo_SOURCES += Adisit.cc \
Setzaxis.cc \
Shiftxy.cc \
Showinfo.cc \
Showattribute.cc \
Sinfo.cc \
Smooth.cc \
Sort.cc \
......
/*
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"
void *printAtts(int vlistID, int varOrGlobal, int natts, char *argument)
{
char stdname[CDI_MAX_NAME], longname[CDI_MAX_NAME], units[CDI_MAX_NAME];
int found = 0;
if ( varOrGlobal != CDI_GLOBAL )
{
vlistInqVarStdname(vlistID, varOrGlobal, stdname);
vlistInqVarLongname(vlistID, varOrGlobal, longname);
vlistInqVarUnits(vlistID, varOrGlobal, units);
double misval = vlistInqVarMissval(vlistID, varOrGlobal);
if ( argument )
{
if ( STR_IS_EQ("standard_name", argument) && stdname[0] )
{
fprintf(stdout, " standard_name = \"%s\"\n", stdname);
found++;
}
else if ( STR_IS_EQ("long_name", argument) && longname[0] )
{
fprintf(stdout, " long_name = \"%s\"\n", longname);
found++;
}
else if ( STR_IS_EQ("units", argument) && units[0] )
{
fprintf(stdout, " units = \"%s\"\n", units);
found++;
}
else if ( STR_IS_EQ("missing_value", argument) )
{
fprintf(stdout, " missing_value = \"%e\"\n", misval);
found++;
}
}
else
{
if ( stdname[0] )
fprintf(stdout, " standard_name = \"%s\"\n", stdname);
if ( longname[0] )
fprintf(stdout, " long_name = \"%s\"\n", longname);
if ( units[0] )
fprintf(stdout, " units = \"%s\"\n", units);
fprintf(stdout, " missing_value = \"%e\"\n", misval);
}
}
if ( found == 0 )
{
for ( int i = 0; i < natts; i++ )
{
char name[CDI_MAX_NAME];
char *value = (char *)Malloc(4*CDI_MAX_NAME * sizeof(char));
int type, len;
cdiInqAtt(vlistID, varOrGlobal, i, name, &type, &len);
if ( argument )
{
if ( !STR_IS_EQ(argument, name) )
continue;
else
found++;
}
switch ( type )
{
case CDI_DATATYPE_TXT:
cdiInqAttTxt(vlistID, varOrGlobal, name, len, value);
value[len] = '\0';
fprintf(stdout, " %s = \"%s\"\n", name, value);
break;
case CDI_DATATYPE_INT32:
cdiInqAttInt(vlistID, varOrGlobal, name, len, (int *)value);
fprintf(stdout, " %s = %i\n", name, *(int *)value);
break;
case CDI_DATATYPE_FLT64:
cdiInqAttFlt(vlistID, varOrGlobal, name, len, (double *)value);
fprintf(stdout, " %s = %e\n", name, *(double *)value);
break;
default:
cdoWarning("Unsupported type %i name %s\n", type, name);
}
Free(value);
}
}
if ( argument && found == 0 )
{
if ( varOrGlobal != CDI_GLOBAL )
cdoAbort("Could not find variable attribute %s in infile.", argument);
else
cdoAbort("Could not find global attribute %s in infile.", argument);
}
}
void *Showattribute(void *argument)
{
const int delim = '@';
cdoInitialize(argument);
int SHOWATTRIBUTE = cdoOperatorAdd("showattribute", 0, 0, NULL);
int SHOWATTSVAR = cdoOperatorAdd("showattsvar", 0, 0, NULL);
int operatorID = cdoOperatorID();
int streamID = pstreamOpenRead(cdoStreamName(0));
int vlistID = pstreamInqVlist(streamID);
int nvars = vlistNvars(vlistID);
int natts = operatorArgc();
if ( natts == 0 ) cdoAbort("Parameter missing!");
char **params = operatorArgv();
char buffer[CDI_MAX_NAME];
for ( int i = 0; i < natts; i++)
{
strcpy(buffer, params[i]);
char *varname = NULL, *attname = NULL;
char *result = strrchr(buffer, delim);
if ( result == NULL )
{
attname = buffer;
if ( operatorID == SHOWATTRIBUTE )
{
fprintf(stdout, "Global:\n");
int nfileatts;
cdiInqNatts(vlistID, CDI_GLOBAL, &nfileatts);
printAtts(vlistID, CDI_GLOBAL, nfileatts, attname);
}
else if (operatorID == SHOWATTSVAR )
{
int varID = 0;
for ( varID = 0; varID < nvars; varID++ )
{
char filevarname[CDI_MAX_NAME];
vlistInqVarName(vlistID, varID, filevarname);
if ( STR_IS_EQ(attname, filevarname) )
{
fprintf(stdout, "%s:\n", attname);
int nfileattsvar;
cdiInqNatts(vlistID, varID, &nfileattsvar);
printAtts(vlistID, varID, nfileattsvar, NULL);
break;
}
}
if ( nvars == varID )
cdoAbort("Could not find variable %s in infile.", varname);
}
}
else
{
attname = result+1;
*result = 0;
varname = buffer;
if ( operatorID == SHOWATTRIBUTE )
{
int varID = 0;
for ( varID = 0; varID < nvars; varID++ )
{
char filevarname[CDI_MAX_NAME];
vlistInqVarName(vlistID, varID, filevarname);
if ( STR_IS_EQ(varname, filevarname) )
{
fprintf(stdout, "%s:\n", varname);
int nfileattsvar;
cdiInqNatts(vlistID, varID, &nfileattsvar);
printAtts(vlistID, varID, nfileattsvar, attname);
break;
}
}
if ( nvars == varID )
cdoAbort("Could not find variable %s in infile.", varname);
}
}
}
pstreamClose(streamID);
cdoFinish();
return 0;
}
void *printAtts(int vlistID, int varOrGlobal, int natts, char *argument);
......@@ -37,7 +37,7 @@
#include "cdo.h"
#include "cdo_int.h"
#include "pstream.h"
#include "Showattribute.h"
void *Showinfo(void *argument)
{
......@@ -62,6 +62,8 @@ void *Showinfo(void *argument)
int SHOWLTYPE = cdoOperatorAdd("showltype", 0, 0, NULL);
int SHOWFORMAT = cdoOperatorAdd("showformat", 0, 0, NULL);
int SHOWGRID = cdoOperatorAdd("showgrid", 0, 0, NULL);
int SHOWATTS = cdoOperatorAdd("showatts", 0, 0, NULL);
int SHOWATTSGLOB = cdoOperatorAdd("showattsglob", 0, 0, NULL);
// clang-format on
int operatorID = cdoOperatorID();
......@@ -258,7 +260,38 @@ void *Showinfo(void *argument)
{
printFiletype(streamID, vlistID);
}
else if ( operatorID == SHOWSTDNAME )
{
char stdname[CDI_MAX_NAME];
for ( int varID = 0; varID < nvars; varID++ )
{
vlistInqVarStdname(vlistID, varID, stdname);
fprintf(stdout, " %s", stdname[0] != 0 ? stdname : "unknown");
}
fprintf(stdout, "\n");
}
else if ( operatorID == SHOWATTS || operatorID == SHOWATTSGLOB )
{
if ( operatorID == SHOWATTS )
{
int vlistID = pstreamInqVlist(streamID);
int nvars = vlistNvars(vlistID);
for ( int varID = 0; varID < nvars; varID++ )
{
char varname[CDI_MAX_NAME];
vlistInqVarName(vlistID, varID, varname);
fprintf(stdout, "%s:\n", varname);
int nattsvar;
cdiInqNatts(vlistID, varID, &nattsvar);
printAtts(vlistID, varID, nattsvar, NULL);
}
}
fprintf(stdout, "Global:\n");
int natts;
cdiInqNatts(vlistID, CDI_GLOBAL, &natts);
printAtts(vlistID, CDI_GLOBAL, natts, NULL);
}
pstreamClose(streamID);
cdoFinish();
......
......@@ -164,6 +164,7 @@ void *Settime(void *argument);
void *Setzaxis(void *argument);
void *Shiftxy(void *argument);
void *Showinfo(void *argument);
void *Showattribute(void *argument);
void *Sinfo(void *argument);
void *Smooth(void *argument);
void *Sort(void *argument);
......@@ -433,7 +434,8 @@ void *Samplegrid(void *argument); // "samplegrid", "subgrid"
#define SetzaxisOperators {"setzaxis", "genlevelbounds"}
#define ShiftxyOperators {"shiftx", "shifty"}
#define ShowinfoOperators {"showyear", "showmon", "showdate", "showtime", "showtimestamp", "showcode", "showunit", \
"showparam", "showname", "showstdname", "showlevel", "showltype", "showformat", "showgrid"}
"showparam", "showname", "showstdname", "showlevel", "showltype", "showformat", "showgrid", "showatts", "showattsglob"}
#define ShowattributeOperators {"showattribute", "showattsvar"}
#define SinfoOperators {"sinfo", "sinfop", "sinfon", "sinfoc", "seinfo", "seinfop", "seinfon", "seinfoc"}
#define SmoothOperators {"smooth", "smooth9"}
#define SortOperators {"sortcode", "sortparam", "sortname", "sortlevel"}
......@@ -944,6 +946,7 @@ void init_modules()
add_module("Setzaxis" , {Setzaxis , SetzaxisHelp , SetzaxisOperators , 1 , CDI_BOTH , 1 , 1 });
add_module("Shiftxy" , {Shiftxy , {} , ShiftxyOperators , 1 , CDI_REAL , 1 , 1 });
add_module("Showinfo" , {Showinfo , ShowinfoHelp , ShowinfoOperators , 1 , CDI_BOTH , 1 , 0 });
add_module("Showattribute" , {Showattribute , {} , ShowattributeOperators , 1 , CDI_REAL , 1 , 0 });
add_module("Sinfo" , {Sinfo , SinfoHelp , SinfoOperators , 1 , CDI_BOTH , -1 , 0 });
add_module("Smooth" , {Smooth , SmoothHelp , SmoothOperators , 1 , CDI_REAL , 1 , 1 });
add_module("Sort" , {Sort , {} , SortOperators , 1 , CDI_REAL , 1 , 1 });
......
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