Deltime.c 4.02 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.

5
  Copyright (C) 2003-2010 Uwe Schulzweida, Uwe.Schulzweida@zmaw.de
6
7
8
9
10
11
12
13
14
15
16
17
  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.
*/

Uwe Schulzweida's avatar
Uwe Schulzweida committed
18
#include <ctype.h>
19
20
21

#include "cdi.h"
#include "cdo.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
22
#include "cdo_int.h"
23
24
25
#include "pstream.h"


26
void *Deltime(void *argument)
27
{
28
  const char func[] = "Deltime";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
29
30
  int DELDAY, DEL29FEB;
  int operatorID;
31
32
33
  int streamID1, streamID2;
  int tsID, tsID2, nrecs;
  int recID, varID, levelID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
34
  int vlistID1, vlistID2;
35
36
37
38
39
40
  int taxisID1, taxisID2;
  int vdate, vtime;
  int copytimestep;
  int lcopy = FALSE;
  int gridsize;
  int nmiss;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
41
  int nfound;
42
  int year, month, day;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
43
  int dday, dmon;
44
  double *array = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
45
  const char *cmons[]={"", "jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"};
46
47
48

  cdoInitialize(argument);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  DELDAY   = cdoOperatorAdd("delday",   0, 0, NULL);
  DEL29FEB = cdoOperatorAdd("del29feb", 0, 0, NULL);

  operatorID = cdoOperatorID();

  if ( operatorID == DEL29FEB )
    {
      dday = 29;
      dmon = 2;
    }
  else
    {
      int im;
      int nsel;
      char *sarg;
      nsel = operatorArgc();
      if ( nsel < 1 ) cdoAbort("Not enough arguments!");
      if ( nsel > 1 ) cdoAbort("Too many arguments!");
      sarg = operatorArgv()[0];
      dday = atoi(sarg);
      dmon = 0;
      while ( isdigit(*sarg) ) *sarg++;
      if ( isalpha(*sarg) )
	{
	  char smon[32];
	  strncpy(smon, sarg, 32);
	  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]);

85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
  if ( UNCHANGED_RECORD ) lcopy = TRUE;

  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);
  taxisDefCalendar(taxisID2, CALENDAR_365DAYS);
  vlistDefTaxis(vlistID2, taxisID2);

  streamID2 = streamOpenWrite(cdoStreamName(1), cdoFiletype());
  if ( streamID2 < 0 ) cdiError(streamID2, "Open failed on %s", cdoStreamName(1));

  streamDefVlist(streamID2, vlistID2);

  if ( ! lcopy )
    {
      gridsize = vlistGridsizeMax(vlistID1);
      array = (double *) malloc(gridsize*sizeof(double));
    }
      
Uwe Schulzweida's avatar
Uwe Schulzweida committed
109
  nfound = 0;
110
111
112
113
114
115
116
  tsID  = 0;
  tsID2 = 0;
  while ( (nrecs = streamInqTimestep(streamID1, tsID)) )
    {
      vdate = taxisInqVdate(taxisID1);
      vtime = taxisInqVtime(taxisID1);

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
119
      if ( day == dday && (month == dmon || dmon == 0) )
120
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
121
	  nfound++;
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
	  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++);

	  for ( recID = 0; recID < nrecs; recID++ )
	    {
	      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
157
158
159
  if ( nfound == 0 )
    cdoWarning("Day %d%s not found!", dday, cmons[dmon]);

160
161
162
163
164
165
166
167
168
  if ( ! lcopy )
    if ( array ) free(array);

  vlistDestroy(vlistID2);

  cdoFinish();

  return (NULL);
}