Enlarge.c 3.95 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-2007 Uwe Schulzweida, Uwe.Schulzweida@zmaw.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
24
/*
   This module contains the following operators:

      Enlarge    enlarge         Enlarge fields
*/


25
26
#include <string.h>

Uwe Schulzweida's avatar
Uwe Schulzweida committed
27
28
29
30
31
32
33
34
35
36
37
38
#include "cdi.h"
#include "cdo.h"
#include "cdo_int.h"
#include "pstream.h"
#include "functs.h"


void *Enlarge(void *argument)
{
  static char func[] = "Enlarge";
  int streamID1, streamID2;
  int vlistID1, vlistID2;
39
  int gridID1, gridID2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
40
41
42
43
44
45
  int i, index, ngrids;
  int recID, nrecs;
  int tsID, varID, levelID;
  int taxisID1, taxisID2;
  int nmiss;
  int gridsize1, gridsize2;
46
47
48
49
  int xsize1, ysize1, xsize2, ysize2;
  int ix, iy;
  int linfo = TRUE;
  double *array1, *array2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
50
51
52
53
54
55

  cdoInitialize(argument);

  operatorCheckArgc(1);

  gridID2 = cdoDefineGrid(operatorArgv()[0]);
56
57
  xsize2 = gridInqXsize(gridID2);
  ysize2 = gridInqYsize(gridID2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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

  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);

  gridsize2 = gridInqSize(gridID2);
  if ( gridsize2 < vlistGridsizeMax(vlistID1) )
    cdoAbort("Gridsize of input stream is greater than new gridsize!");

  ngrids = vlistNgrids(vlistID1);
  for ( index = 0; index < ngrids; index++ )
    {
      vlistChangeGridIndex(vlistID2, index, gridID2);
    }

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

  streamDefVlist(streamID2, vlistID2);

84
85
  array1 = (double *) malloc(gridsize2*sizeof(double));
  array2 = (double *) malloc(gridsize2*sizeof(double));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
86
87
88
89
90
91
92
93
94
95
96

  tsID = 0;
  while ( (nrecs = streamInqTimestep(streamID1, tsID)) )
    {
      taxisCopyTimestep(taxisID2, taxisID1);

      streamDefTimestep(streamID2, tsID);

      for ( recID = 0; recID < nrecs; recID++ )
	{
	  streamInqRecord(streamID1, &varID, &levelID);
97
	  streamReadRecord(streamID1, array1, &nmiss);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
98
99
100

	  if ( nmiss > 0 ) cdoAbort("missing values unsupported for this operator!");

101
102
103
104
105
	  gridID1 = vlistInqVarGrid(vlistID1, varID);
	  xsize1 = gridInqXsize(gridID1);
	  ysize1 = gridInqYsize(gridID1);
	  gridsize1 = gridInqSize(gridID1);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
106
	  /* printf("%d %d %d %d\n", xsize1, ysize1, xsize2, ysize2); */
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
	  if ( xsize1 == 1 && ysize1 == ysize2 && xsize1*ysize1 == gridsize1 )
	    {
	      if ( linfo )
		{
		  cdoPrint("Enlarge zonal");
		  linfo = FALSE;
		}
	      
	      for ( iy = 0; iy < ysize2; iy++ )
		for ( ix = 0; ix < xsize2; ix++ )
		  array2[ix+iy*xsize2] = array1[iy];
	    }
	  else if ( ysize1 == 1 && xsize1 == xsize2 && xsize1*ysize1 == gridsize1 )
	    {
	      if ( linfo )
		{
		  cdoPrint("Enlarge meridional");
		  linfo = FALSE;
		}
	      
	      for ( iy = 0; iy < ysize2; iy++ )
		for ( ix = 0; ix < xsize2; ix++ )
		  array2[ix+iy*xsize2] = array1[ix];
	    }
	  else
	    {
	      memcpy(array2, array1, gridsize1*sizeof(double));
	      for ( i = gridsize1; i < gridsize2; i++ )
		{
		  array2[i] = array1[gridsize1-1];
		}
	    }
	    
Uwe Schulzweida's avatar
Uwe Schulzweida committed
140
	  streamDefRecord(streamID2, varID,  levelID);
141
	  streamWriteRecord(streamID2, array2, nmiss);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
142
143
144
145
146
147
148
	}
      tsID++;
    }

  streamClose(streamID2);
  streamClose(streamID1);

149
150
  if ( array1 ) free(array1);
  if ( array2 ) free(array2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
151
152
153
154
155

  cdoFinish();

  return (0);
}