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

Add pctl source code

parent 69618c26
......@@ -193,11 +193,14 @@ src/Pinfo.c -text
src/Remap.c -text
src/Replace.c -text
src/Rotuv.c -text
src/Runpctl.c -text
src/Runstat.c -text
src/Seaspctl.c -text
src/Seasstat.c -text
src/Selbox.c -text
src/Select.c -text
src/Seloperator.c -text
src/Selpctl.c -text
src/Selrec.c -text
src/Selstat.c -text
src/Seltime.c -text
......@@ -222,6 +225,7 @@ src/Splityear.c -text
src/Subtrend.c -text
src/Templates.c -text
src/Test.c -text
src/Timpctl.c -text
src/Timsort.c -text
src/Timstat.c -text
src/Trend.c -text
......@@ -234,9 +238,14 @@ src/Vertstat.c -text
src/Wind.c -text
src/Writegrid.c -text
src/Writerandom.c -text
src/Ydaypctl.c -text
src/Ydaystat.c -text
src/Ydrunpctl.c -text
src/Ydrunstat.c -text
src/Ymonarith.c -text
src/Ymonpctl.c -text
src/Ymonstat.c -text
src/Yseaspctl.c -text
src/Yseasstat.c -text
src/Zonstat.c -text
src/cdi.h -text
......@@ -285,7 +294,11 @@ src/modules.h -text
src/namelist.c -text
src/namelist.h -text
src/normal.c -text
src/nth_element.c -text
src/nth_element.h -text
src/operator_help.h -text
src/percentiles.c -text
src/percentiles.h -text
src/pipe.c -text
src/pipe.h -text
src/process.c -text
......
/*
This file is part of CDO. CDO is a collection of Operators to
manipulate and analyse Climate model Data.
Copyright (C) 2003-2006 Brockmann Consult
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.
*/
/*
This module contains the following operators:
Runpctl runpctl Running percentiles
*/
#include <stdio.h>
#include <math.h>
#include "cdi.h"
#include "cdo.h"
#include "cdo_int.h"
#include "pstream.h"
#include "dtypes.h"
#include "functs.h"
#include "field.h"
#include "dmemory.h"
#include "nth_element.h"
typedef struct {
int date;
int time;
int julval;
}
DATETIME;
void *Runpctl(void *argument)
{
static char func[] = "Runpctl";
int gridsize;
int varID;
int recID;
int gridID;
int nrecs, nrecords;
int levelID;
int tsID;
int otsID;
int i, j, inp, its, ndates = 0;
int streamID1, streamID2;
int vlistID1, vlistID2;
int nmiss;
int nvars, nlevels;
int *recVarID, *recLevelID;
double missval, val;
FIELD ***vars1 = NULL;
DATETIME *datetime;
int taxisID1, taxisID2;
int calendar, dpy;
int pn;
double *array;
cdoInitialize(argument);
cdoOperatorAdd("runpctl", func_pctl, 0, NULL);
operatorInputArg("percentile number, number of timesteps");
operatorCheckArgc(2);
pn = atoi(operatorArgv()[0]);
ndates = atoi(operatorArgv()[1]);
if ( pn < 1 || pn > 99 )
cdoAbort("Illegal argument: percentile number %d is not in the range 1..99!", pn);
streamID1 = streamOpenRead(cdoStreamName(0));
if ( streamID1 < 0 ) cdiError(streamID1, "Open failed on %s", cdoStreamName(0));
vlistID1 = streamInqVlist(streamID1);
vlistID2 = vlistDuplicate(vlistID1);
taxisID1 = vlistInqTaxis(vlistID1);
taxisID2 = taxisDuplicate(taxisID1);
vlistDefTaxis(vlistID2, taxisID2);
calendar = taxisInqCalendar(taxisID1);
dpy = calendar_dpy(calendar);
streamID2 = streamOpenWrite(cdoStreamName(1), cdoFiletype());
if ( streamID2 < 0 ) cdiError(streamID2, "Open failed on %s", cdoStreamName(1));
streamDefVlist(streamID2, vlistID2);
nvars = vlistNvars(vlistID1);
nrecords = vlistNrecs(vlistID1);
recVarID = (int *) malloc(nrecords*sizeof(int));
recLevelID = (int *) malloc(nrecords*sizeof(int));
datetime = (DATETIME *) malloc((ndates+1)*sizeof(DATETIME));
vars1 = (FIELD ***) malloc((ndates+1)*sizeof(FIELD **));
array = (double *) malloc(ndates*sizeof(double));
for ( its = 0; its < ndates; its++ )
{
vars1[its] = (FIELD **) malloc(nvars*sizeof(FIELD *));
for ( varID = 0; varID < nvars; varID++ )
{
gridID = vlistInqVarGrid(vlistID1, varID);
gridsize = gridInqSize(gridID);
nlevels = zaxisInqSize(vlistInqVarZaxis(vlistID1, varID));
missval = vlistInqVarMissval(vlistID1, varID);
vars1[its][varID] = (FIELD *) malloc(nlevels*sizeof(FIELD));
for ( levelID = 0; levelID < nlevels; levelID++ )
{
vars1[its][varID][levelID].grid = gridID;
vars1[its][varID][levelID].nmiss = 0;
vars1[its][varID][levelID].missval = missval;
vars1[its][varID][levelID].ptr = (double *) malloc(gridsize*sizeof(double));
}
}
}
for ( tsID = 0; tsID < ndates; tsID++ )
{
nrecs = streamInqTimestep(streamID1, tsID);
if ( nrecs == 0 )
cdoAbort("File has less than %d timesteps!", ndates);
datetime[tsID].date = taxisInqVdate(taxisID1);
datetime[tsID].time = taxisInqVtime(taxisID1);
for ( recID = 0; recID < nrecs; recID++ )
{
streamInqRecord(streamID1, &varID, &levelID);
if ( tsID == 0 )
{
recVarID[recID] = varID;
recLevelID[recID] = levelID;
}
streamReadRecord(streamID1, vars1[tsID][varID][levelID].ptr, &nmiss);
vars1[tsID][varID][levelID].nmiss = nmiss;
}
}
otsID = 0;
while ( TRUE )
{
for ( varID = 0; varID < nvars; varID++ )
{
if ( vlistInqVarTime(vlistID1, varID) == TIME_CONSTANT ) continue;
gridsize = gridInqSize(vlistInqVarGrid(vlistID1, varID));
missval = vlistInqVarMissval(vlistID1, varID);
nlevels = zaxisInqSize(vlistInqVarZaxis(vlistID1, varID));
for ( levelID = 0; levelID < nlevels; levelID++ )
{
nmiss = 0;
for ( i = 0; i < gridsize; i++ )
{
for ( inp = 0, j = 0; inp < ndates; inp++ )
{
val = vars1[inp][varID][levelID].ptr[i];
if ( !DBL_IS_EQUAL(val, missval) )
array[j++] = val;
}
if ( j > 0 )
{
vars1[0][varID][levelID].ptr[i] = nth_element(array, j, ceil(j*(pn/100.0))-1);
}
else
{
vars1[0][varID][levelID].ptr[i] = missval;
nmiss++;
}
}
vars1[0][varID][levelID].nmiss = nmiss;
}
}
datetime_avg(dpy, ndates, datetime);
taxisDefVdate(taxisID2, datetime[ndates].date);
taxisDefVtime(taxisID2, datetime[ndates].time);
streamDefTimestep(streamID2, otsID++);
for ( recID = 0; recID < nrecords; recID++ )
{
varID = recVarID[recID];
levelID = recLevelID[recID];
if ( otsID == 1 || vlistInqVarTime(vlistID1, varID) == TIME_VARIABLE )
{
streamDefRecord(streamID2, varID, levelID);
streamWriteRecord(streamID2, vars1[0][varID][levelID].ptr, vars1[0][varID][levelID].nmiss);
}
}
datetime[ndates] = datetime[0];
vars1[ndates] = vars1[0];
for ( inp = 0; inp < ndates; inp++ )
{
datetime[inp] = datetime[inp+1];
vars1[inp] = vars1[inp+1];
}
nrecs = streamInqTimestep(streamID1, tsID);
if ( nrecs == 0 ) break;
datetime[ndates-1].date = taxisInqVdate(taxisID1);
datetime[ndates-1].time = taxisInqVtime(taxisID1);
for ( recID = 0; recID < nrecs; recID++ )
{
streamInqRecord(streamID1, &varID, &levelID);
streamReadRecord(streamID1, vars1[ndates-1][varID][levelID].ptr, &nmiss);
vars1[ndates-1][varID][levelID].nmiss = nmiss;
}
tsID++;
}
for ( its = 0; its < ndates; its++ )
{
for ( varID = 0; varID < nvars; varID++ )
{
nlevels = zaxisInqSize(vlistInqVarZaxis(vlistID1, varID));
for ( levelID = 0; levelID < nlevels; levelID++ )
free(vars1[its][varID][levelID].ptr);
free(vars1[its][varID]);
}
free(vars1[its]);
}
free(vars1);
free(array);
if ( recVarID ) free(recVarID);
if ( recLevelID ) free(recLevelID);
streamClose(streamID2);
streamClose(streamID1);
cdoFinish();
return (0);
}
/*
This file is part of CDO. CDO is a collection of Operators to
manipulate and analyse Climate model Data.
Copyright (C) 2003-2006 Brockmann Consult
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.
*/
/*
This module contains the following operators:
Seaspctl seaspctl Seasonally percentiles
*/
#include "cdi.h"
#include "cdo.h"
#include "cdo_int.h"
#include "pstream.h"
#include "dtypes.h"
#include "percentiles.h"
void *Seaspctl(void *argument)
{
static char func[] = "Seaspctl";
int gridsize;
int vdate1 = 0, vtime1 = 0;
int vdate2 = 0, vtime2 = 0;
int vdate3 = 0, vtime3 = 0;
int vdate4 = 0, vtime4 = 0;
int nrecs, nrecords;
int gridID, varID, levelID, recID;
int tsID;
int otsID;
long nsets;
int i;
int year, month, seas, seas0 = 0;
int streamID1, streamID2, streamID3, streamID4;
int vlistID1, vlistID2, vlistID3, vlistID4, taxisID1, taxisID2, taxisID3, taxisID4;
int nmiss;
int nvars, nlevels;
int *recVarID, *recLevelID;
int newseas, oldmon = 0, newmon;
double missval;
FIELD **vars1 = NULL;
FIELD field;
int pn;
HISTOGRAM_SET *hset = NULL;
cdoInitialize(argument);
cdoOperatorAdd("seaspctl", func_pctl, 0, NULL);
operatorInputArg("percentile number");
pn = atoi(operatorArgv()[0]);
if ( pn < 1 || pn > 99 )
cdoAbort("Illegal argument: percentile number %d is not in the range 1..99!", pn);
streamID1 = streamOpenRead(cdoStreamName(0));
if ( streamID1 < 0 ) cdiError(streamID1, "Open failed on %s", cdoStreamName(0));
streamID2 = streamOpenRead(cdoStreamName(1));
if ( streamID2 < 0 ) cdiError(streamID2, "Open failed on %s", cdoStreamName(1));
streamID3 = streamOpenRead(cdoStreamName(2));
if ( streamID3 < 0 ) cdiError(streamID3, "Open failed on %s", cdoStreamName(2));
vlistID1 = streamInqVlist(streamID1);
vlistID2 = streamInqVlist(streamID2);
vlistID3 = streamInqVlist(streamID3);
vlistID4 = vlistDuplicate(vlistID1);
vlistCompare(vlistID1, vlistID2, func_hrd);
vlistCompare(vlistID1, vlistID3, func_hrd);
taxisID1 = vlistInqTaxis(vlistID1);
taxisID2 = vlistInqTaxis(vlistID2);
taxisID3 = vlistInqTaxis(vlistID3);
/* TODO - check that time axes 2 and 3 are equal */
taxisID4 = taxisCreate(TAXIS_ABSOLUTE);
vlistDefTaxis(vlistID4, taxisID4);
streamID4 = streamOpenWrite(cdoStreamName(3), cdoFiletype());
if ( streamID4 < 0 ) cdiError(streamID4, "Open failed on %s", cdoStreamName(3));
streamDefVlist(streamID4, vlistID4);
nvars = vlistNvars(vlistID1);
nrecords = vlistNrecs(vlistID1);
recVarID = (int *) malloc(nrecords * sizeof(int));
recLevelID = (int *) malloc(nrecords * sizeof(int));
gridsize = vlistGridsizeMax(vlistID1);
field.ptr = (double *) malloc(gridsize*sizeof(double));
vars1 = (FIELD **) malloc(nvars * sizeof(FIELD *));
hset = hsetCreate(nvars);
for ( varID = 0; varID < nvars; varID++ )
{
gridID = vlistInqVarGrid(vlistID1, varID);
gridsize = gridInqSize(gridID);
nlevels = zaxisInqSize(vlistInqVarZaxis(vlistID1, varID));
missval = vlistInqVarMissval(vlistID1, varID);
vars1[varID] = (FIELD *) malloc(nlevels * sizeof(FIELD));
hsetCreateVarLevels(hset, varID, nlevels, gridID);
for ( levelID = 0; levelID < nlevels; levelID++ )
{
vars1[varID][levelID].grid = gridID;
vars1[varID][levelID].nmiss = 0;
vars1[varID][levelID].missval = missval;
vars1[varID][levelID].ptr = (double *) malloc(gridsize * sizeof(double));
}
}
tsID = 0;
otsID = 0;
while ( TRUE )
{
nsets = 0;
newseas = FALSE;
nrecs = streamInqTimestep(streamID2, otsID);
if ( nrecs != streamInqTimestep(streamID3, otsID) )
cdoAbort("Number of records in time step %d of %s and %s are different!", otsID+1, cdoStreamName(1), cdoStreamName(2));
vdate2 = taxisInqVdate(taxisID2);
vtime2 = taxisInqVtime(taxisID2);
vdate3 = taxisInqVdate(taxisID3);
vtime3 = taxisInqVtime(taxisID3);
if ( vdate2 != vdate3 || vtime2 != vtime3 )
cdoAbort("Verification dates for time step %d of %s and %s are different!", otsID+1, cdoStreamName(1), cdoStreamName(2));
for ( recID = 0; recID < nrecs; recID++ )
{
streamInqRecord(streamID2, &varID, &levelID);
streamReadRecord(streamID2, vars1[varID][levelID].ptr, &nmiss);
vars1[varID][levelID].nmiss = nmiss;
}
for ( recID = 0; recID < nrecs; recID++ )
{
streamInqRecord(streamID3, &varID, &levelID);
streamReadRecord(streamID3, field.ptr, &nmiss);
field.nmiss = nmiss;
field.grid = vars1[varID][levelID].grid;
field.missval = vars1[varID][levelID].missval;
hsetDefVarLevelBounds(hset, varID, levelID, &vars1[varID][levelID], &field);
}
while ( nrecs && (nrecs = streamInqTimestep(streamID1, tsID)) )
{
vdate1 = taxisInqVdate(taxisID1);
vtime1 = taxisInqVtime(taxisID1);
year = vdate1 / 10000;
month = (vdate1 - year * 10000) / 100;
if ( month < 0 || month > 16 )
cdoAbort("Month %d out of range!", month);
if ( month <= 12 )
seas = (month % 12) / 3;
else
seas = month - 13;
if ( seas < 0 || seas > 3 )
cdoAbort("Season %d out of range!", seas + 1);
newmon = month;
if ( newmon == 12 ) newmon = 0;
if ( nsets == 0 )
{
seas0 = seas;
oldmon = newmon;
}
if ( newmon < oldmon ) newseas = TRUE;
if ( (seas != seas0) || newseas ) break;
oldmon = newmon;
for ( recID = 0; recID < nrecs; recID++ )
{
streamInqRecord(streamID1, &varID, &levelID);
if ( tsID == 0 )
{
recVarID[recID] = varID;
recLevelID[recID] = levelID;
}
streamReadRecord(streamID1, vars1[varID][levelID].ptr, &nmiss);
vars1[varID][levelID].nmiss = nmiss;
hsetAddVarLevelValues(hset, varID, levelID, &vars1[varID][levelID]);
}
vdate4 = vdate1;
vtime4 = vtime1;
nsets++;
tsID++;
}
if ( nrecs == 0 && nsets == 0 ) break;
if ( vdate2 != vdate4 )
cdoAbort("Verification dates for time step %d of %s, %s and %s are different!", otsID+1, cdoStreamName(1), cdoStreamName(2), cdoStreamName(3));
if ( vtime2 != vtime4 )
cdoAbort("Verification times for time step %d of %s, %s and %s are different!", otsID+1, cdoStreamName(1), cdoStreamName(2), cdoStreamName(3));
for ( varID = 0; varID < nvars; varID++ )
{
if ( vlistInqVarTime(vlistID1, varID) == TIME_CONSTANT ) continue;
nlevels = zaxisInqSize(vlistInqVarZaxis(vlistID1, varID));
for ( levelID = 0; levelID < nlevels; levelID++ )
hsetGetVarLevelPercentiles(&vars1[varID][levelID], hset, varID, levelID, pn);
}
taxisDefVdate(taxisID4, vdate4);
taxisDefVtime(taxisID4, vtime4);
streamDefTimestep(streamID4, otsID++);
for ( recID = 0; recID < nrecords; recID++ )
{
varID = recVarID[recID];
levelID = recLevelID[recID];
if ( otsID == 1 || vlistInqVarTime(vlistID1, varID) == TIME_VARIABLE )
{
streamDefRecord(streamID4, varID, levelID);
streamWriteRecord(streamID4, vars1[varID][levelID].ptr, vars1[varID][levelID].nmiss);
}
}
if ( nrecs == 0 ) break;
}
for ( varID = 0; varID < nvars; varID++ )
{
nlevels = zaxisInqSize(vlistInqVarZaxis(vlistID1, varID));
for ( levelID = 0; levelID < nlevels; levelID++ )
free(vars1[varID][levelID].ptr);
free(vars1[varID]);
}
free(vars1);
hsetDestroy(hset);
if ( field.ptr ) free(field.ptr);
if ( recVarID ) free(recVarID);
if ( recLevelID ) free(recLevelID);
streamClose(streamID4);
streamClose(streamID3);
streamClose(streamID2);
streamClose(streamID1);
cdoFinish();
return (0);
}
/*
This file is part of CDO. CDO is a collection of Operators to
manipulate and analyse Climate model Data.
Copyright (C) 2003-2006 Brockmann Consult
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.
*/
/*
This module contains the following operators:
Selpctl selpctl Time range percentiles
*/
#include "cdi.h"
#include "cdo.h"
#include "cdo_int.h"
#include "pstream.h"
#include "dtypes.h"
#include "percentiles.h"
void *Selpctl(void *argument)
{
static char func[] = "Selpctl";
int gridsize;
int vdate1 = 0, vtime1 = 0;
int vdate2 = 0, vtime2 = 0;