MapReduce.c 6.14 KB
Newer Older
Ralf Mueller's avatar
Ralf Mueller committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
   This file is part of CDO. CDO is a collection of Operators to
   manipulate and analyse Climate model Data.

   Copyright (C) 2003-2016 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.
   */

/*
   This module contains the following operators:

   Pack      reduce
22
*/
Ralf Mueller's avatar
Ralf Mueller committed
23
24
25
26
27
28
29
30
31
32

#if defined(_OPENMP)
#  include <omp.h>
#endif

#include <limits.h>

#include <cdi.h>
#include "cdo.h"
#include "cdo_int.h"
Ralf Mueller's avatar
Ralf Mueller committed
33
#include "grid.h"
Ralf Mueller's avatar
Ralf Mueller committed
34

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
void read_first_record(char *filename, int gridSize, double *field)
{
  int nmiss,varID,levelID;
  double *missval1, *missval2;
  int streamID = streamOpenRead(filename);
  int nrecs = streamInqTimestep(streamID,0);
  streamInqRecord(streamID,&varID,&levelID);
  streamReadRecord(streamID, field, &nmiss);
  /*  minmaxval(gridSize, field, NULL,&missval1, &missval2);
      cdoPrint("min: %g | max: %g ",missval1, missval2);
      */
  /*for (int l = 0; l < maskSize; l++) cdoPrint("maskIndexList[%d] = %d",l,maskIndexList[l]);
  */
  streamClose(streamID);
  /* }}} */
  }

#include "pstream.h"

Ralf Mueller's avatar
Ralf Mueller committed
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
85
86
87
88
89
90
91
int matrix2vector(int i, int j, int Ni, int Nj)
{
  return i*Nj + j;
}
int *vector2matrix(int k, int Ni, int Nj)
{
  int *retval;
  retval = (int *) malloc(2*sizeof(int));

  retval[0] = k/Nj; retval[1] = k%Nj;
  return retval;
}

/* count the number of locations, for which the mask is TRUE, i.e. has a
 * non-zero value */
int countMask(double *maskField, int gridSize, double falseVal)
{
  int counter;

  counter = 0;

  for (int i = 0; i < gridSize; i++)
  {
    if (!DBL_IS_EQUAL(maskField[i],falseVal)) counter += 1;
  }
  return counter;
}

/*
 * collect the indices of relevant location out of the given mask
 */
void collectLocations(double *maskField, int gridSize, double falseVal, int *maskIndexList)
{
  int k = 0;
  for (int i = 0; i < gridSize; i++)
  {
    if (!DBL_IS_EQUAL(maskField[i],falseVal))
    {
92
      printf("found at:%d -",i);
Ralf Mueller's avatar
Ralf Mueller committed
93
94
95
      k += 1;
    }
  }
96
  printf(" k =%d ",k);
Ralf Mueller's avatar
Ralf Mueller committed
97
98
99
100
101
102
}

/*
 * the operators argument has to be a single horizontal field,
 * non-zero values are used to mark the relevant locations
 */
Ralf Mueller's avatar
Ralf Mueller committed
103
104
105
106
void *MapReduce(void *argument)
{
  int gridsize;
  int nrecs;
Ralf Mueller's avatar
Ralf Mueller committed
107
  int tsID;
108
  int varID, levelID, recID;
Ralf Mueller's avatar
Ralf Mueller committed
109
110
111
112
113
114
115
116
117
118
119
  int i;
  int nts;
  int nalloc = 0;
  int nmiss;
  int nlevel;
  int datatype = DATATYPE_INT16;
  dtlist_type *dtlist = dtlist_new();
  double missval1, missval2;

  cdoInitialize(argument);

Ralf Mueller's avatar
Ralf Mueller committed
120
121
122
123
124
125
126
127
128

  /* check input grid type and size */
  int inputGridID = cdoDefineGrid(operatorArgv()[0]);
  int inputGridSize = gridInqSize(inputGridID);
  if ( cdoVerbose ) cdoPrint("input gridSize:%d", inputGridSize);

  /* search the number of relevant locations */
  tsID = 0; nrecs = 0;
  double *inputMaskField = (double*) Malloc(inputGridSize*sizeof(double));
129
  read_first_record(operatorArgv()[0],inputGridSize, inputMaskField);
130
  /* count points {{{*/
Ralf Mueller's avatar
Ralf Mueller committed
131
  int maskSize = countMask(inputMaskField, inputGridSize, 0.0);
132
  cdoPrint("maskSize = %d",maskSize); /* }}} */
Ralf Mueller's avatar
Ralf Mueller committed
133
134

  /* collect the original coordinates */
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
  int *maskIndexList = (int *) Malloc(maskSize*sizeof(int));
  for (int m = 0; m < maskSize; m++) maskIndexList[m] = -1;

  /* create an index list of relevant points {{{ */
  int k = 0;
  for (int i = 0; i < inputGridSize; i++)
  {
    if (!DBL_IS_EQUAL(inputMaskField[i],0.0))
    {
      if (cdoDebug) printf("found at:%d -",i);
      maskIndexList[k] = i;
      k += 1;
    }
  }

150

Ralf Mueller's avatar
Ralf Mueller committed
151
  /* create unstructured output grid */
152
  int outputGridID = gridToUnstructuredSelecton(inputGridID,TRUE, maskSize, maskIndexList);
Ralf Mueller's avatar
Ralf Mueller committed
153
154

  int inputGridType = gridInqType(inputGridID);
Ralf Mueller's avatar
Ralf Mueller committed
155

Ralf Mueller's avatar
Ralf Mueller committed
156
  /* copy time axis */
157
158
  int streamID1 = streamOpenRead(cdoStreamName(0));
  int vlistID1  = streamInqVlist(streamID1);
159
160
161
  int nvars = vlistNvars(vlistID1);
  int *vars = (int*) Malloc(nvars*sizeof(int));

162
163
  int taxisID1  = vlistInqTaxis(vlistID1);

164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
  vlistClearFlag(vlistID1);
  for ( varID = 0; varID < nvars; varID++ )
  {
    vars[varID] = FALSE;

    int gridID = vlistInqVarGrid(vlistID1, varID);
    if (inputGridType == gridInqType(gridID) && inputGridSize == gridInqSize(gridID))
    {
      vars[varID] = TRUE;
      int zaxisID  = vlistInqVarZaxis(vlistID1, varID);
      int nlevs    = zaxisInqSize(zaxisID);
      for ( int levID = 0; levID < nlevs; levID++ )
      {
        vlistDefFlag(vlistID1, varID, levID, TRUE);
      }
    }
  }
  int vlistID2 = vlistCreate();
  vlistCopyFlag(vlistID2, vlistID1);

184
  int taxisID2  = taxisDuplicate(taxisID1);
Ralf Mueller's avatar
Ralf Mueller committed
185
186
187
  vlistDefTaxis(vlistID2, taxisID2);


188
  /* copy the mask to target grid */
189
  int ngrids = vlistNgrids(vlistID2);
190
  for ( int index = 0; index < ngrids; index++ ) vlistChangeGridIndex(vlistID2, index, outputGridID);
191

192
  int streamID2 = streamOpenWrite(cdoStreamName(1), cdoFiletype());
193
  streamDefVlist(streamID2, vlistID2);
194
  /* loop over all data fields */
195
  double *arrayIn = (double *)Malloc(inputGridSize*sizeof(double));
196
  double *arrayOut = (double *)Malloc(maskSize*sizeof(double));
197
  tsID = 0; 
198
199
200
  while ( (nrecs = streamInqTimestep(streamID1, tsID)) )
  {
    taxisCopyTimestep(taxisID2, taxisID1);
201
    streamDefTimestep(streamID2, tsID);
202

203
204
205
    for ( recID = 0; recID < nrecs; recID++ )
    {
      streamInqRecord(streamID1, &varID, &levelID);
206
207
208
209
      if (TRUE == vars[varID])
      {
        int varID2   = vlistFindVar(vlistID2, varID);
        int levelID2 = vlistFindLevel(vlistID2, varID, levelID);
210

211
212
213
        cdoPrint("aaaaaa");
        streamReadRecord(streamID1, arrayIn, &nmiss);
        cdoPrint("bbbbbb");
214
215
216
217

        for (int i = 0; i < maskSize;  i++)
          arrayOut[i] = arrayIn[maskIndexList[i]];

218
219

        streamDefRecord(streamID2, varID2, levelID2);
220
        streamWriteRecord(streamID2, arrayOut, 0);
221

222
223
224
225
226
227
228
      }
    }

    tsID++;
  }


Ralf Mueller's avatar
Ralf Mueller committed
229
230
231
  streamClose(streamID2);
  streamClose(streamID1);

232
233
234
  Free(inputMaskField);
  Free(maskIndexList);

Ralf Mueller's avatar
Ralf Mueller committed
235
236
237
238
  cdoFinish();

  return 0;
}