-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadnetcdf.c
More file actions
530 lines (440 loc) · 15.6 KB
/
readnetcdf.c
File metadata and controls
530 lines (440 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/**********************************************************************
File reads in model geometry
by: Beth Fulton
date: 21/10/2003
comments: This file is the code that reads in the model geometry
Changes:
14-09-2009 Bec Gorton
Changed the code to work out which face each vertices belongs to - this is
required for the new ROMS data. The new ROMS input file has data for all faces - regardless
of if they are into the model or out of the model.
01-10-2009 Bec Gorton
Added code to free up the geometry structure.
*************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//#include <malloc.h>
#include <sjwlib.h>
#include <string.h>
#include "mainexchange.h"
/**//**
/brief Free the memory associated with the geometry.
****/
void freeGeom(Stuff *st) {
if (st->nbgmface > 0) {
free(st->faces);
i_free1d(st->faceVertIndex);
}
/* Get inside points */
free(st->boxinsidey);
free(st->boxinsidex);
free(st->boxnconn);
/* Allocate memory for face and neighbour box lists */
i_free2d(st->ibox);
i_free2d(st->iface);
i_free2d(st->boxns);
i_free2d(st->boxwe);
/** Get box areas and layer volumes **/
/* Create area and depth arrays */
if (st->nbox > 0) {
free(st->boxarea);
free(st->boxtotdepth);
}
/* Get box heights and widths */
free(st->boxht);
free(st->boxwd);
free(st->boxnscoefft);
free(st->boxwecoefft);
}
/************************************************************************************************
code is of SJW's readBoxModelGeom in geomIO.c
This routine reads a box model geometry from an ascii file. It does not return if an error is encountered.
Input file format for this part is shown below, where N is an integer and X and Y are floating point
numbers.
# Number of boxes in horizontal plane
nbox N
# Number of faces in horizontal plane
nface N
# Box Model polyline (should be closed)
bnd_vert X Y
bnd_vert X Y
bnd_vert X Y
.
.
.
The boxes and faces are read using readBoxGeom() and
readFaceGeom() below. This routine assumes that the
boxes and faces are each in order in the input file
(ie, data for box0 comes before data for box1 etc). This
minimises the number of times which the file has to be rewound
and re-read.
Arguments: name input file name
bm box model pointer
***************************************************************************************************/
void readBoxGeom(char *name, Stuff *st) {
char key[STSLEN];
FILE *fp;
int i, nconn, nndest, b, faceID, vertIndex;
double barea, botzz, maxx, maxy, minx, miny,
px, py;
int buflen = 2000;
char buf[2000];
double prevPx = 0.0, prevPy = 0.0;
printf("Reading in bgm file %s\n", name);
if (verbose)
printf("Read basic box geometry\n");
/* open the file */
if ((fp = fopen(name, "r")) == NULL)
quit("readBoxGeom: Can't open %s\n", name);
/* rewind file */
fseek(fp, 0L, SEEK_SET);
/* Read number of boxes and faces */
skipToKeyEnd(fp, "nbox");
if (fscanf(fp, "%d", &st->nbox) != 1 || st->nbox < 1 || st->nbox > 10000)
quit("readBoxModelGeom: Can't read nbox\n");
skipToKeyEnd(fp, "nface");
if (fscanf(fp, "%d", &st->nbgmface) != 1 || st->nbgmface < 0
|| st->nbgmface > 20000)
quit("readBoxGeom: Can't read nface\n");
else
printf("Found %d faces\n", st->nbgmface);
st->nface = st->nbgmface;
/* allocate face arrays */
if (st->nbgmface > 0) {
st->faces = (Face *) malloc(st->nbgmface * sizeof(Face));
st->faceVertIndex = i_alloc1d(st->nbgmface);
if (st->faces == NULL)
quit("readBoxGeom: No memory for face array\n");
} else
st->faces = NULL;
/* Set up faces */
if (verbose)
printf("Read face definitions\n");
fseek(fp, 0L, SEEK_SET);
for (i = 0; i < st->nbgmface; i++) {
st->faces[i].n = i;
st->faceVertIndex[i] = -1;
readFaceGeom(fp, &st->faces[i]);
}
/* Get inside points */
st->boxinsidey = (double *) malloc(st->nbox * sizeof(double));
st->boxinsidex = (double *) malloc(st->nbox * sizeof(double));
/* Read station location */
for (i = 0; i < st->nbox; i++) {
sprintf(key, "box%d.inside", i);
skipToKeyEnd(fp, key);
if (fscanf(fp, "%lf %lf", &st->boxinsidex[i], &st->boxinsidey[i]) != 2){
warn("readBoxGeom: Can't read %s\n", key);
st->boxinsidex[i] = 0;
st->boxinsidey[i] = 0;
}
}
/* Determine ndest */
st->boxnconn = (int *) malloc(st->nbox * sizeof(int));
nndest = 0;
for (i = 0; i < st->nbox; i++) {
/* Read number of connections */
sprintf(key, "box%d.nconn", i);
skipToKeyEnd(fp, key);
if (fscanf(fp, "%d", &nconn) != 1 || nconn < 0)
quit("readBoxGeom: Can't read %s\n", key);
st->boxnconn[i] = nconn;
/* Compare against ndest so that save the largest value of connections as the
number of destinations to worry about*/
if (nconn > nndest)
nndest = nconn;
}
if ((nndest > st->ndest) || (!st->ndest))
st->ndest = nndest;
/* Allocate memory for face and neighbour box lists */
st->ibox = (int **) i_alloc2d(nndest, st->nbox);
st->iface = (int **) i_alloc2d(nndest, st->nbox);
st->boxns = (int **) i_alloc2d(nndest, st->nbox);
st->boxwe = (int **) i_alloc2d(nndest, st->nbox);
for (b = 0; b < st->nbox; b++) {
for (i = 0; i < nndest; i++) {
/* Initialise box index list */
st->ibox[b][i] = -1;
/* Initialise boxns and boxwe arrays */
st->boxns[b][i] = -1;
st->boxwe[b][i] = -1;
}
}
/*Read the face index list */
for (b = 0; b < st->nbox; b++) {
sprintf(key, "box%d.iface", b);
skipToKeyEnd(fp, key);
for (i = 0; i < st->boxnconn[b]; i++)
if (fscanf(fp, "%d", &st->iface[b][i]) != 1)
quit("readBoxGeom: Can't read %s\n", key);
}
for (b = 0; b < st->nbox; b++) {
/* Read the box index list */
sprintf(key, "box%d.ibox", b);
skipToKeyEnd(fp, key);
for (i = 0; i < st->boxnconn[b]; i++){
if (fscanf(fp, "%d", &st->ibox[b][i]) != 1)
quit("readBoxGeom: Can't read %s\n", key);
}
}
/** Get box areas and layer volumes **/
/* Create area and depth arrays */
if (st->nbox > 0) {
st->boxarea = (double *) malloc(st->nbox * sizeof(double));
if (st->boxarea == NULL)
quit("readBoxGeom: No memory for box area\n");
} else
st->boxarea = NULL;
if (st->nbox > 0) {
st->boxtotdepth = (double *) malloc(st->nbox * sizeof(double));
if (st->boxtotdepth == NULL)
quit("readBoxGeom: No memory for total depth of the box\n");
} else
st->boxtotdepth = NULL;
for (i = 0; i < st->nbox; i++) {
/* Read Area */
sprintf(key, "box%d.area", i);
skipToKeyEnd(fp, key);
if (fscanf(fp, "%lf", &barea) != 1 || barea < 0)
quit("readBoxGeom: Can't read valid area for %s (area = %d)\n",
key, barea);
st->boxarea[i] = barea;
/* Read tot water column volume in the cell */
sprintf(key, "box%d.botz", i);
skipToKeyEnd(fp, key);
if (fscanf(fp, "%lf", &botzz) != 1)
quit(
"readBoxGeom: Can't read valid total depth for %s (depth = %d)\n",
key, botzz);
st->boxtotdepth[i] = -botzz;
}
/* Get box heights and widths */
st->boxht = (double *) malloc(st->nbox * sizeof(double));
st->boxwd = (double *) malloc(st->nbox * sizeof(double));
st->boxnscoefft = (double *) malloc(st->nbox * sizeof(double));
st->boxwecoefft = (double *) malloc(st->nbox * sizeof(double));
for (i = 0; i < st->nbox; i++) {
//printf("box = %d\n", i);
maxx = -MAXDOUBLE;
maxy = -MAXDOUBLE;
minx = MAXDOUBLE;
miny = MAXDOUBLE;
sprintf(key, "box%d.vert ", i);
skipToKeyStart(fp, key);
sprintf(key, "box%d.vert %%lf %%lf ", i);
// sprintf(key,"%%lf %%lf ");
vertIndex = 0;
//printf("fscanf(fp,key,&px,&py) = %d\n", fscanf(fp,key,&px,&py));
prevPx = -1;
while (fgets(buf, buflen, fp) != NULL) {
// printf("buf = %s\n", buf);
if (strstr(buf, "# Data") != NULL)
break;
if (sscanf(buf, key, &px, &py) != 2)
break;
//while( fscanf(fp,key,&px,&py) == 2 ){
if (py > maxy)
maxy = py;
if (py < miny)
miny = py;
if (px > maxx)
maxx = px;
if (px < minx)
minx = px;
/* Find which face this corresponds to */
if (vertIndex > 0) {
//printf("vertIndex = %d, px = %e, py = %e, prevPx = %e, prevPy = %e\n", vertIndex - 1, px, py, prevPx, prevPy);
for (faceID = 0; faceID < st->nbgmface; faceID++) {
//printf("st->faces[%d].p2.x = %e, st->faces[%d].p2.y = %e\n", faceID, st->faces[faceID].p2.x, faceID, st->faces[faceID].p2.y);
if ((st->faces[faceID].p2.x == px && st->faces[faceID].p2.y
== py && st->faces[faceID].p1.x == prevPx
&& st->faces[faceID].p1.y == prevPy)
|| (st->faces[faceID].p1.x == px
&& st->faces[faceID].p1.y == py
&& st->faces[faceID].p2.x == prevPx
&& st->faces[faceID].p2.y == prevPy)) {
/* Check to see that this face is to do with this box */
if (st->faces[faceID].ibl == i || st->faces[faceID].ibr
== i) {
st->faceVertIndex[faceID] = vertIndex - 1;
//printf("FOUND!!. faceID = %d, vertIndex = %d\n", faceID, vertIndex - 1);
break;
}
}
}
}
prevPx = px;
prevPy = py;
vertIndex++;
}
/* Assign width and height of the box - note that for diagonal boxes these will be overstated */
st->boxht[i] = maxy - miny;
st->boxwd[i] = maxx - minx;
/* Initialise coefficients */
st->boxnscoefft[i] = 1;
st->boxwecoefft[i] = 1;
/* Rescale coefficents based on shape */
if (st->boxht[i] < st->boxwd[i]) // Short and fat
st->boxwecoefft[i] = st->boxht[i] / (st->boxwd[i] + tiny);
if (st->boxwd[i] < st->boxht[i]) // Tall and thin
st->boxnscoefft[i] = st->boxwd[i] / (st->boxht[i] + tiny);
/* Check for null or negative sized boxes */
if (st->boxnscoefft[i] <= 0) {
warn("Had to reset box %d height\n", i);
st->boxnscoefft[i] = tiny;
}
if (st->boxwecoefft[i] <= 0) {
warn("Had to reset box %d width\n", i);
st->boxwecoefft[i] = tiny;
}
}
/* Close the file */
fclose(fp);
if (verbose)
printf("Geometry file closed\n");
}
/*************************************************************************************************
Code is of SJW's readFaceGeom in geomIO.c
This routine reads a face geometry from an ascii file. It assumes that the face already contains
valid values for n. It also assumes that the file is positioned at or before the data for the face
requested. No file positioning is done here, apart from reading data. This means that my parameter
library (in sjwlib) should not be used, as its routines do explicit file positioning.
Input file format for data for 1 face is shown below. Here X, Y, L, C and S are all floating point values.
# Geometry for face nn
# Start point of face
facenn.p1 X Y
# End point of face
facenn.p2 X Y
# Length of face
facenn.length L
# Cos and Sin of angle from +ve x axis to normal to face
facenn.cs C S
# Indices of boxes to left and right of this face
facenn.lr N N
Arguments: fp input file pointer
f face pointer
***************************************************************************************************/
void readFaceGeom(FILE *fp, Face *f) {
char key[STSLEN];
/* Sanity checks */
if (f == NULL)
quit("readFaceGeom: NULL face pointer!\n");
if (f->n < 0)
quit("readFaceGeom: Face number < 0!\n");
/* Read face endpoints */
sprintf(key, "face%d.p1", f->n);
skipToKeyEnd(fp, key);
if (fscanf(fp, "%lf %lf", &f->p1.x, &f->p1.y) != 2)
quit("readFaceGeom: Can't read %s\n", key);
sprintf(key, "face%d.p2", f->n);
skipToKeyEnd(fp, key);
if (fscanf(fp, "%lf %lf", &f->p2.x, &f->p2.y) != 2)
quit("readFaceGeom: Can't read %s\n", key);
/* Read face length */
sprintf(key, "face%d.length", f->n);
skipToKeyEnd(fp, key);
if (fscanf(fp, "%lf", &f->len) != 1 || f->len <= 0.0)
quit("readFaceGeom: Can't read %s\n", key);
/* Read face orientation */
sprintf(key, "face%d.cs", f->n);
skipToKeyEnd(fp, key);
if (fscanf(fp, "%lf %lf", &f->cos, &f->sin) != 2)
quit("readFaceGeom: Can't read %s\n", key);
/* Read indices of boxes to left and right */
sprintf(key, "face%d.lr", f->n);
skipToKeyEnd(fp, key);
if (fscanf(fp, "%d %d", &f->ibl, &f->ibr) != 2 || f->ibl < 0 || f->ibr < 0)
quit("readFaceGeom: Can't read %s\n", key);
}
/************************************************************************************************
This routine reads only the face info in lat-long to supplement existing info in x-y from basic
readBoxGeom() routine -
This routine reads in the face geometry in lat-long coords from an ascii file (to supplement
x-y coordinate info from basic bgm file). This is so that code can check Jeff Dunn style
netcdf input files are mapped to correct box and face ID numbers in bgm file
It assumes that the face already contains valid values for n. It also assumes that the
file is positioned at or before the data for the face requested. No file positioning is
done here, apart from reading data. Input file format for data for 1 face is shown below.
# Geometry for face nn
# Start point of face
facenn.p1 X Y
# End point of face
facenn.p2 X Y
Arguments: name input file name
bm box model pointer
***************************************************************************************************/
void readBoxGeomll(char *name, Stuff *st) {
char key[STSLEN];
FILE *fp;
int i;
if (verbose)
printf("Read lat-long face geometry\n");
/* open the file */
if ((fp = fopen(name, "r")) == NULL)
quit("readBoxGeomll: Can't open %s\n", name);
/* rewind file */
fseek(fp, 0L, SEEK_SET);
for (i = 0; i < st->nbgmface; i++) {
/* Sanity checks */
if (st->faces[i].n < 0)
quit("readllFaceGeom: Face number < 0!\n");
/* Read face endpoints in latitude and longitude */
sprintf(key, "face%d.p1", st->faces[i].n);
skipToKeyEnd(fp, key);
if (fscanf(fp, "%lf %lf", &st->faces[i].pll1.x, &st->faces[i].pll1.y)
!= 2)
quit("read11FaceGeom: Can't read %s in latlong faces file\n", key);
sprintf(key, "face%d.p2", st->faces[i].n);
skipToKeyEnd(fp, key);
if (fscanf(fp, "%lf %lf", &st->faces[i].pll2.x, &st->faces[i].pll2.y)
!= 2)
quit("read11FaceGeom: Can't read %s in latlong faces file\n", key);
if (verbose > 1)
printf("bgmfcell: %d, p1(%e, %e) p2(%e, %e)\n", i,
st->faces[i].pll1.x, st->faces[i].pll1.y,
st->faces[i].pll2.x, st->faces[i].pll2.y);
}
/* Close the file */
fclose(fp);
if (verbose)
printf("Geometry file closed\n");
return;
}
/**************************************************************************************
Routine to identify which surrounding boxes are north-south and east-west for each box,
so can do u-v decomposition of currents and scale to account for hyperdiffusion
differently based on whether flow is in x or y - so can cope with long thin boxes, rather
than almost square boxes (i.e. wide as tall).
**************************************************************************************/
void setup_NorthSouth(Stuff *st) {
int b, ij, checkbox;
double currentx, currenty, checkx, checky;
for (b = 0; b < st->nbox; b++) {
currentx = st->boxinsidex[b];
currenty = st->boxinsidey[b];
for (ij = 0; ij < st->boxnconn[b]; ij++) {
checkbox = st->ibox[b][ij];
if (checkbox >= 0) {
/* Only check valid boxes */
checkx = st->boxinsidex[checkbox];
checky = st->boxinsidey[checkbox];
/* Check if north or south */
if ((checky - currenty) > (st->boxht[b] * 0.5))
st->boxns[b][ij] = to_north; // Box to north
else if ((currenty - checky) > (st->boxht[b] * 0.5))
st->boxns[b][ij] = to_south; // Box to south
else
st->boxns[b][ij] = same_lat; // In same latitudinal band
/* Check of east or west */
if (checkx < currentx)
st->boxwe[b][ij] = to_west; // Box to west
else if (checkx > currentx)
st->boxwe[b][ij] = to_east; // Box to east
else
st->boxwe[b][ij] = same_long; // In same longitudinal band
}
}
}
return;
}