Arithlat.c 4.11 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-2014 Uwe Schulzweida, Uwe.Schulzweida@zmaw.de
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  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:

      Arithlat   mulcoslat       Multiply with cos(lat)
      Arithlat   divcoslat       Divide by cos(lat)
*/


Ralf Mueller's avatar
Ralf Mueller committed
26
#include <cdi.h>
27
28
29
#include "cdo.h"
#include "cdo_int.h"
#include "pstream.h"
30
#include "grid.h"
31
32
33
34
35
36
37


void *Arithlat(void *argument)
{
  int operatorID;
  int operfunc;
  int streamID1, streamID2;
38
  int gridtype;
39
40
41
42
43
44
45
  int gridID, gridID0 = -1;
  int nrecs, recID;
  int tsID;
  int varID, levelID;
  int vlistID1, vlistID2;
  int taxisID1, taxisID2;
  int nmiss;
46
  long gridsize, i;
47
  char units[CDI_MAX_NAME];
48
49
50
51
52
53
54
55
56
  double *scale = NULL;
  double *array = NULL;

  cdoInitialize(argument);

  cdoOperatorAdd("mulcoslat", func_mul, 0, NULL);
  cdoOperatorAdd("divcoslat", func_div, 0, NULL);

  operatorID = cdoOperatorID();
57
  operfunc = cdoOperatorF1(operatorID);
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

  streamID1 = streamOpenRead(cdoStreamName(0));

  vlistID1 = streamInqVlist(streamID1);
  vlistID2 = vlistDuplicate(vlistID1);

  taxisID1 = vlistInqTaxis(vlistID1);
  taxisID2 = taxisDuplicate(taxisID1);
  vlistDefTaxis(vlistID2, taxisID2);

  streamID2 = streamOpenWrite(cdoStreamName(1), cdoFiletype());

  streamDefVlist(streamID2, vlistID2);

  gridsize = vlistGridsizeMax(vlistID1);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
74
  array = (double*) malloc(gridsize*sizeof(double));
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91

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

      streamDefTimestep(streamID2, tsID);

      for ( recID = 0; recID < nrecs; recID++ )
	{
	  streamInqRecord(streamID1, &varID, &levelID);
	  streamReadRecord(streamID1, array, &nmiss);
	  
	  gridID = vlistInqVarGrid(vlistID1, varID);

	  if ( gridID != gridID0 )
	    {
92
93
	      gridID0 = gridID;

94
	      gridtype = gridInqType(gridID);
95
96
97
	      if ( gridtype == GRID_LONLAT      ||
		   gridtype == GRID_GAUSSIAN    ||
		   gridtype == GRID_LCC )
98
		{
99
		  gridID = gridToCurvilinear(gridID, 0);
100
101
		}
	      else if ( gridtype == GRID_CURVILINEAR ||
102
			gridtype == GRID_UNSTRUCTURED )
103
104
105
106
107
		{
		  /* No conversion necessary */
		}
	      else if ( gridtype == GRID_GME )
		{
108
		  gridID = gridToUnstructured(gridID, 0);
109
110
111
112
113
114
		}
	      else
		{
		  if ( gridtype == GRID_GAUSSIAN_REDUCED )
		    cdoAbort("Unsupported grid type: %s, use CDO option -R to convert reduced to regular grid!",
			     gridNamePtr(gridtype));
115
		  else
116
		    cdoAbort("Unsupported grid type: %s", gridNamePtr(gridtype));
117
118
119
120
		}

	      gridsize = gridInqSize(gridID);

121
	      scale = realloc(scale, gridsize*sizeof(double));
122
123
	      gridInqYvals(gridID, scale);

124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
	      /* Convert lat/lon units if required */
	      
	      gridInqXunits(gridID, units);

	      if ( memcmp(units, "degree", 6) == 0 )
		{
		  for ( i = 0; i < gridsize; ++i ) scale[i] *= DEG2RAD;
		}
	      else if ( memcmp(units, "radian", 6) == 0 )
		{
		  /* No conversion necessary */
		}
	      else
		{
		  cdoWarning("Unknown units supplied for grid1 center lat/lon: proceeding assuming radians");
		}

141
	      if ( operfunc == func_mul )
142
		for ( i = 0; i < gridsize; ++i ) scale[i] = cos(scale[i]);
143
	      else
144
		for ( i = 0; i < gridsize; ++i ) scale[i] = 1./cos(scale[i]);
145

146
	      if ( cdoVerbose ) for ( i = 0; i < 10; ++i ) cdoPrint("coslat  %3d  %g", i+1, scale[i]);
147
148
	    }

149
	  for ( i = 0; i < gridsize; ++i ) array[i] *= scale[i];
150
151
152
153

	  streamDefRecord(streamID2, varID, levelID);
	  streamWriteRecord(streamID2, array, nmiss);
	}
154

155
156
157
158
159
160
161
162
163
164
165
166
167
      tsID++;
    }

  streamClose(streamID2);
  streamClose(streamID1);

  if ( array ) free(array);
  if ( scale ) free(scale);

  cdoFinish();

  return (0);
}