2
2
This file is part of CDO. CDO is a collection of Operators to
3
3
manipulate and analyse Climate model Data.
5
Copyright (C) 2003-2009 Uwe Schulzweida, Uwe.Schulzweida@zmaw.de
5
Copyright (C) 2003-2010 Uwe Schulzweida, Uwe.Schulzweida@zmaw.de
6
6
See COPYING file for copying and redistribution conditions.
8
8
This program is free software; you can redistribute it and/or modify
32
33
void *Gridcell(void *argument)
34
static char func[] = "Gridcell";
35
int GRIDAREA, GRIDWGTS;
35
static const char *func = "Gridcell";
36
int GRIDAREA, GRIDWGTS, GRIDMASK;
37
38
int streamID1, streamID2;
38
39
int vlistID1, vlistID2;
39
40
int gridID, zaxisID;
40
int gridsize, gridtype;
43
44
int tsID, varID, levelID, taxisID;
44
double *grid_area = NULL;
45
double *grid_wgts = NULL;
48
48
double EarthRadius = 6371000; /* default radius of the earth in m */
49
49
double PlanetRadius = EarthRadius;
51
51
cdoInitialize(argument);
53
envstr = getenv("PLANET_RADIUS");
62
cdoPrint("Set PlanetRadius to %g", PlanetRadius);
66
53
GRIDAREA = cdoOperatorAdd("gridarea", 0, 0, NULL);
67
54
GRIDWGTS = cdoOperatorAdd("gridweights", 0, 0, NULL);
55
GRIDMASK = cdoOperatorAdd("gridmask", 0, 0, NULL);
69
57
operatorID = cdoOperatorID();
59
if ( operatorID == GRIDAREA || operatorID == GRIDWGTS )
61
envstr = getenv("PLANET_RADIUS");
70
cdoPrint("Set PlanetRadius to %g", PlanetRadius);
71
75
streamID1 = streamOpenRead(cdoStreamName(0));
72
76
if ( streamID1 < 0 ) cdiError(streamID1, "Open failed on %s", cdoStreamName(0));
93
97
vlistDefVarUnits(vlistID2, varID, "m2");
95
99
else if ( operatorID == GRIDWGTS )
96
vlistDefVarName(vlistID2, varID, "cell_weights");
101
vlistDefVarName(vlistID2, varID, "cell_weights");
103
else if ( operatorID == GRIDMASK )
105
vlistDefVarName(vlistID2, varID, "grid_mask");
106
vlistDefVarDatatype(vlistID2, varID, DATATYPE_UINT8);
98
109
taxisID = taxisCreate(TAXIS_ABSOLUTE);
99
110
vlistDefTaxis(vlistID2, taxisID);
120
130
if ( operatorID == GRIDAREA )
122
132
gridtype = gridInqType(gridID);
123
if ( gridtype != GRID_LONLAT &&
124
gridtype != GRID_GAUSSIAN &&
125
gridtype != GRID_LCC &&
126
gridtype != GRID_GME &&
127
gridtype != GRID_CURVILINEAR &&
128
gridtype != GRID_CELL )
130
if ( gridInqType(gridID) == GRID_GAUSSIAN_REDUCED )
131
cdoAbort("Unsupported grid type: %s, use CDO option -R to convert reduced to regular grid!",
132
gridNamePtr(gridtype));
134
cdoAbort("Unsupported grid type: %s", gridNamePtr(gridtype));
133
if ( gridtype == GRID_LONLAT ||
134
gridtype == GRID_GAUSSIAN ||
135
gridtype == GRID_LCC ||
136
gridtype == GRID_GME ||
137
gridtype == GRID_CURVILINEAR ||
138
gridtype == GRID_CELL )
138
140
if ( gridHasArea(gridID) )
140
142
if ( cdoVerbose ) cdoPrint("Using existing grid cell area!");
141
gridInqArea(gridID, grid_area);
143
gridInqArea(gridID, array);
147
status = gridGenArea(gridID, grid_area);
147
status = gridGenArea(gridID, array);
148
148
if ( status == 1 )
149
149
cdoAbort("Grid corner missing!");
150
150
else if ( status == 2 )
151
151
cdoAbort("Can't compute grid cell areas for this grid!");
153
153
for ( i = 0; i < gridsize; ++i )
154
grid_area[i] *= PlanetRadius*PlanetRadius;
154
array[i] *= PlanetRadius*PlanetRadius;
159
if ( gridtype == GRID_GAUSSIAN_REDUCED )
160
cdoAbort("Unsupported grid type: %s, use CDO option -R to convert reduced to regular grid!",
161
gridNamePtr(gridtype));
163
cdoAbort("Unsupported grid type: %s", gridNamePtr(gridtype));
166
else if ( operatorID == GRIDWGTS )
162
status = gridWeights(gridID, grid_wgts);
168
status = gridWeights(gridID, array);
163
169
if ( status != 0 )
164
170
cdoWarning("Using constant grid cell area weights!");
169
streamWriteRecord(streamID2, pdata, 0);
172
else if ( operatorID == GRIDMASK )
175
mask = (int *) malloc(gridsize*sizeof(int));
176
if ( gridInqMask(gridID, NULL) )
178
gridInqMask(gridID, mask);
182
for ( i = 0; i < gridsize; ++i ) mask[i] = 1;
185
for ( i = 0; i < gridsize; ++i ) array[i] = mask[i];
189
streamWriteRecord(streamID2, array, 0);
172
192
streamClose(streamID2);
173
193
streamClose(streamID1);
175
if ( grid_area ) free(grid_area);
176
if ( grid_wgts ) free(grid_wgts);
195
if ( array ) free(array);