Fldrms.cc 4.56 KB
Newer Older
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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.
*/

18
19
20
21
22
23
/*
   This module contains the following operators:

*/


Ralf Mueller's avatar
Ralf Mueller committed
24
#include <cdi.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
25
26
27
#include "cdo.h"
#include "cdo_int.h"
#include "pstream.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
28
#include "grid.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
29
30
31
32


void *Fldrms(void *argument)
{
33
34
35
36
  int lastgrid = -1;
  int index;
  int nrecs;
  int varID, levelID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
37
  int nmiss;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
38
39
40
41
  double sglval;

  cdoInitialize(argument);

42
  bool needWeights = true;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
43

44
45
  int streamID1 = pstreamOpenRead(cdoStreamName(0));
  int streamID2 = pstreamOpenRead(cdoStreamName(1));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
46

47
48
  int vlistID1 = pstreamInqVlist(streamID1);
  int vlistID2 = pstreamInqVlist(streamID2);
49
  int vlistID3 = vlistDuplicate(vlistID1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
50

51
52
  int taxisID1 = vlistInqTaxis(vlistID1);
  int taxisID3 = taxisDuplicate(taxisID1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
53
54
  vlistDefTaxis(vlistID3, taxisID3);

55
56
57
  double slon = 0;
  double slat = 0;
  int gridID3 = gridCreate(GRID_LONLAT, 1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
58
59
60
61
62
  gridDefXsize(gridID3, 1);
  gridDefYsize(gridID3, 1);
  gridDefXvals(gridID3, &slon);
  gridDefYvals(gridID3, &slat);

63
64
  int ngrids = vlistNgrids(vlistID1);
  int ndiffgrids = 0;
65
66
67
68
  for ( index = 1; index < ngrids; index++ )
    if ( vlistGrid(vlistID1, 0) != vlistGrid(vlistID1, index))
      ndiffgrids++;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
69
  index = 0;
70
71
  int gridID1 = vlistGrid(vlistID1, index);
  int gridID2 = vlistGrid(vlistID2, index);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
72
73

  if ( gridInqSize(gridID1) != gridInqSize(gridID2) )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
74
    cdoAbort("Fields have different grid size!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
75
76
77
78
79
80
  
  if ( needWeights &&
       gridInqType(gridID1) != GRID_LONLAT &&
       gridInqType(gridID1) != GRID_GAUSSIAN )
    cdoAbort("Unsupported gridtype: %s", gridNamePtr(gridInqType(gridID1)));

81
82
83
84
  for ( index = 0; index < ngrids; index++ )
    vlistChangeGridIndex(vlistID3, index, gridID3);

  if ( ndiffgrids > 0 ) cdoAbort("Too many different grids!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
85

86
87
  int streamID3 = pstreamOpenWrite(cdoStreamName(2), cdoFiletype());
  pstreamDefVlist(streamID3, vlistID3);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
88

89
  field_type field1, field2, field3;
90
91
92
93
  field_init(&field1);
  field_init(&field2);
  field_init(&field3);

94
  int lim = vlistGridsizeMax(vlistID1);
95
  field1.ptr    = (double*) Malloc(lim*sizeof(double));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
96
97
  field1.weight = NULL;
  if ( needWeights )
98
    field1.weight = (double*) Malloc(lim*sizeof(double));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
99

100
  field2.ptr    = (double*) Malloc(lim*sizeof(double));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
101
102
103
104
105
  field2.weight = NULL;

  field3.ptr  = &sglval;
  field3.grid = gridID3;

106
  int tsID = 0;
107
  while ( (nrecs = pstreamInqTimestep(streamID1, tsID)) )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
108
    {
109
      nrecs = pstreamInqTimestep(streamID2, tsID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
110
111
112

      taxisCopyTimestep(taxisID3, taxisID1);

113
      pstreamDefTimestep(streamID3, tsID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
114

115
      for ( int recID = 0; recID < nrecs; recID++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
116
	{
117
118
	  pstreamInqRecord(streamID1, &varID, &levelID);
	  pstreamReadRecord(streamID1, field1.ptr, &nmiss);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
119
          field1.nmiss = (size_t) nmiss;
120
121
	  pstreamInqRecord(streamID2, &varID, &levelID);
	  pstreamReadRecord(streamID2, field2.ptr, &nmiss);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
122
          field2.nmiss = (size_t) nmiss;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
123
124
125

	  field1.grid    = vlistInqVarGrid(vlistID1, varID);
	  field2.grid    = vlistInqVarGrid(vlistID2, varID);
126
127

          if ( needWeights && field1.grid != lastgrid )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
128
129
	    {
	      lastgrid = field1.grid;
130
131
132
133
134
135
136
137
138
139
140
	      field1.weight[0] = 1;
	      if ( field1.size > 1 )
		{
		  int wstatus = gridWeights(field1.grid, field1.weight);
		  if ( wstatus != 0 && tsID == 0 && levelID == 0 )
		    {
		      char varname[CDI_MAX_NAME];
		      vlistInqVarName(vlistID1, varID, varname);
		      cdoWarning("Grid cell bounds not available, using constant grid cell area weights for variable %s!", varname);
		    }
		}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
141
	    }
142

Uwe Schulzweida's avatar
Uwe Schulzweida committed
143
144
145
146
147
148
	  field1.missval = vlistInqVarMissval(vlistID1, varID);
	  field2.missval = vlistInqVarMissval(vlistID1, varID);
	  field3.missval = vlistInqVarMissval(vlistID1, varID);

	  fldrms(field1, field2, &field3);

149
150
	  pstreamDefRecord(streamID3, varID,  levelID);
	  pstreamWriteRecord(streamID3, &sglval, (int)field3.nmiss);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
151
152
153
154
	}
      tsID++;
    }

155
156
157
  pstreamClose(streamID3);
  pstreamClose(streamID2);
  pstreamClose(streamID1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
158

159
160
161
  if ( field1.ptr )    Free(field1.ptr);
  if ( field1.weight ) Free(field1.weight);
  if ( field2.ptr )    Free(field2.ptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
162
163
164

  cdoFinish();

Uwe Schulzweida's avatar
Uwe Schulzweida committed
165
  return 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
166
}