Deltime.cc 3.75 KB
Newer Older
1
2
3
4
/*
  This file is part of CDO. CDO is a collection of Operators to
  manipulate and analyse Climate model Data.

Uwe Schulzweida's avatar
Uwe Schulzweida committed
5
  Copyright (C) 2003-2017 Uwe Schulzweida, <uwe.schulzweida AT mpimet.mpg.de>
6
7
8
9
10
11
12
13
14
15
16
17
18
  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.
*/


Ralf Mueller's avatar
Ralf Mueller committed
19
#include <cdi.h>
20
#include "cdo.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
21
#include "cdo_int.h"
22
23
24
#include "pstream.h"


25
void *Deltime(void *argument)
26
{
27
  int nrecs;
28
  int varID, levelID;
29
  int vdate /*, vtime */;
30
31
32
33
  int copytimestep;
  int gridsize;
  int nmiss;
  int year, month, day;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
34
  int dday, dmon;
35
  double *array = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
36
  const char *cmons[]={"", "jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"};
37
38
39

  cdoInitialize(argument);

40
41
42
43
  bool lcopy = UNCHANGED_RECORD;

  int DELDAY   = cdoOperatorAdd("delday",   0, 0, NULL);
  int DEL29FEB = cdoOperatorAdd("del29feb", 0, 0, NULL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
44

45
46
  UNUSED(DELDAY);

47
  int operatorID = cdoOperatorID();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
48
49
50
51
52
53
54
55
56
57
58
59

  if ( operatorID == DEL29FEB )
    {
      dday = 29;
      dmon = 2;
    }
  else
    {
      int im;
      int nsel;
      char *sarg;
      nsel = operatorArgc();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
60
      if ( nsel < 1 ) cdoAbort("Too few arguments!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
61
62
63
64
      if ( nsel > 1 ) cdoAbort("Too many arguments!");
      sarg = operatorArgv()[0];
      dday = atoi(sarg);
      dmon = 0;
65
      while ( isdigit(*sarg) ) sarg++;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
66
67
68
      if ( isalpha(*sarg) )
	{
	  char smon[32];
69
70
	  strncpy(smon, sarg, sizeof(smon)-1);
	  smon[sizeof(smon)-1] = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
71
72
73
74
75
76
77
78
79
80
	  strtolower(smon);
	  for ( im = 0; im < 12; ++im )
	    if ( memcmp(smon, cmons[im+1], 3) == 0 ) break;

	  if ( im < 12 ) dmon = im + 1;
	}
    }

  if ( cdoVerbose ) cdoPrint("delete day %d%s", dday, cmons[dmon]);

81
  int streamID1 = streamOpenRead(cdoStreamName(0));
82

83
84
  int vlistID1 = streamInqVlist(streamID1);
  int vlistID2 = vlistDuplicate(vlistID1);
85

86
87
  int taxisID1 = vlistInqTaxis(vlistID1);
  int taxisID2 = taxisDuplicate(taxisID1);
88
89
90
  taxisDefCalendar(taxisID2, CALENDAR_365DAYS);
  vlistDefTaxis(vlistID2, taxisID2);

91
  int streamID2 = streamOpenWrite(cdoStreamName(1), cdoFiletype());
92
93
94
95
96
97

  streamDefVlist(streamID2, vlistID2);

  if ( ! lcopy )
    {
      gridsize = vlistGridsizeMax(vlistID1);
98
      array = (double*) Malloc(gridsize*sizeof(double));
99
100
    }
      
101
102
103
  int nfound = 0;
  int tsID  = 0;
  int tsID2 = 0;
104
105
106
  while ( (nrecs = streamInqTimestep(streamID1, tsID)) )
    {
      vdate = taxisInqVdate(taxisID1);
107
      // vtime = taxisInqVtime(taxisID1);
108

Uwe Schulzweida's avatar
Uwe Schulzweida committed
109
      cdiDecodeDate(vdate, &year, &month, &day);
110

Uwe Schulzweida's avatar
Uwe Schulzweida committed
111
      if ( day == dday && (month == dmon || dmon == 0) )
112
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
113
	  nfound++;
114
115
116
117
118
119
120
121
122
123
124
125
126
	  copytimestep = FALSE;
	  if ( cdoVerbose )
	    cdoPrint("Delete %4.4d-%2.2d-%2.2d at timestep %d", year, month, day, tsID+1);
	}
      else
	copytimestep = TRUE;

      if ( copytimestep )
	{
	  taxisCopyTimestep(taxisID2, taxisID1);

	  streamDefTimestep(streamID2, tsID2++);

127
	  for ( int recID = 0; recID < nrecs; recID++ )
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
	    {
	      streamInqRecord(streamID1, &varID, &levelID);
	      streamDefRecord(streamID2, varID, levelID);
	      if ( lcopy )
		{
		  streamCopyRecord(streamID2, streamID1);
		}
	      else
		{
		  streamReadRecord(streamID1, array, &nmiss);
		  streamWriteRecord(streamID2, array, nmiss);
		}
	    }
	}
       
      tsID++;
    }

  streamClose(streamID2);
  streamClose(streamID1);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
149
150
151
  if ( nfound == 0 )
    cdoWarning("Day %d%s not found!", dday, cmons[dmon]);

152
  if ( ! lcopy )
153
    if ( array ) Free(array);
154
155
156
157
158

  vlistDestroy(vlistID2);

  cdoFinish();

159
  return 0;
160
}