-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetcdfinputdata.c
More file actions
901 lines (754 loc) · 28.8 KB
/
getcdfinputdata.c
File metadata and controls
901 lines (754 loc) · 28.8 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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
/**********************************************************************
getinputdata.c
by: Beth Fulton
date: 2/3/2006
comments: This file is the code that reads in netcdf file version of
raw hydrodynamics flows, and temperature and salinity profiles
assumptions: assumes raw input is a series of data entries in netcdf file
Changes:
12-05-2009 Bec Gorton
The new netcdf files form Jeff contain the bgm face id's so there is no need to
use the face geometry to match the input file faces with the bgm faces.
So all of the facepx facepy code has been removed.
02-06-2009 Bec Gorton
Got rid of some commented out code thats not used anymore.
03-06-2009 Bec Gorton
Added the load_bgm_mappings function to load the mappings between the hydro box numbers
and the bgm box numbers - this functionality was added to allow users to change the box
numbers after the hydro files have been generated.
07-06-2009 Bec Gorton
Changed the code that reads in the input files to always allocate floats for the exchange array.
The values in the input file are always floats - the valtime array is still allocated based on the current
definition of FPTYPE.
*************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <math.h>
#include <string.h>
#include <netcdf.h>
#include <sjwlib.h>
#include "mainexchange.h"
#include "unixUtils.h"
/***************************************************************************************************
Routine to get the exchange data out of the raw data file - Jeff Dunn style netcdf input (not netcdf files)
Involves reading data from netcdf file to buffer
****************************************************************************************************/
void get_cdf_hydro(Stuff *st) {
int i, k, lastk, entry_i, nz, ij;
double time_of_year, totnum, this_tofy, mintime;
float ***valexchange;
FPTYPE *valtime;
int *valboxs;
int *valboxd;
int *valfaceid;
long start[3];
long count[3];
long startt[1];
long countt[1];
long startfce[1];
long countfce[1];
nc_type datatype;
long nt, fce, lvl;
ncopts = NC_VERBOSE | NC_FATAL;
if (verbose)
printf("Loading raw data from netcdf files\n");
/* Give code warning */
if (st->hd.nfiles > 1)
warn(
"%d raw hydro files listed, code may not cope properly with more than one - in particular the face mapping may be wrong\n",
st->hd.nfiles);
/* Loop through the exchange files */
lastk = 0;
for (i = 0; i < st->hd.nfiles; i++) {
if (verbose)
printf("Reading raw exchange file %s\n", st->hd.fname[i]);
/* Open the exchange file */
open_hydro(st, st->hd.fname[i], i);
if (verbose > 1)
printf("Checking for dimensions\n");
/* Check for dimensions in file */
ncdiminq(st->hd.fid, ncdimid(st->hd.fid, "time"), NULL, &nt);
ncdiminq(st->hd.fid, ncdimid(st->hd.fid, "faces"), NULL, &fce);
ncdiminq(st->hd.fid, ncdimid(st->hd.fid, "level"), NULL, &lvl);
if (fce > st->nbgmface) {
warn(
"More faces in netcdf file %s (nface = %d) than in bgm file %s (nface = %d) - reconcile and retry\n",
st->hd.fname[i], fce, st->geomIfname, st->nface);
} else if (fce < st->nbgmface) {
warn(
"Less faces in netcdf file %s (nface = %d) than in bgm file %s (nface = %d)\n",
st->hd.fname[i], fce, st->geomIfname, st->nface);
}
/* Allocate temporary storage for one tracer */
nc_inq_vartype(st->hd.fid, st->hd.fcex1_vid, &datatype);
if (datatype != NC_FLOAT)
quit("The data type in the netcdf must be NC_FLOAT\n");
valexchange = (float ***) f_alloc3d(lvl, fce, nt);
switch (sizeof(FPTYPE)) {
case sizeof(float):
valtime = (FPTYPE *) f_alloc1d(nt);
break;
case sizeof(double):
valtime = (FPTYPE *) d_alloc1d(nt);
break;
default:
quit("get_cdf_hydro: Unknown size for FPTYPE\n");
}
valboxs = (int *) i_alloc1d(fce);
valboxd = (int *) i_alloc1d(fce);
valfaceid = (int *) i_alloc1d(fce);
/* Set indices for reading tracers */
start[0] = 0;
start[1] = 0;
start[2] = 0;
count[0] = nt;
count[1] = fce;
count[2] = lvl;
startfce[0] = 0;
countfce[0] = fce;
startt[0] = 0;
countt[0] = nt;
/* Read data - exchanges, dates and box-ids associated with the faces */
ncvarget(st->hd.fid, st->hd.t_vid, startt, countt, valtime);
ncvarget(st->hd.fid, st->hd.fce_vid, startfce, countfce, valfaceid);
ncvarget(st->hd.fid, st->hd.srcb_vid, startfce, countfce, valboxs);
ncvarget(st->hd.fid, st->hd.destb_vid, startfce, countfce, valboxd);
ncvarget(st->hd.fid, st->hd.e_vid, start, count, valexchange[0][0]);
printf("st->dt = %e\n", st->dt);
/* Transfer to rawdata arrays */
/* Transfer timestamps - while says seconds in input file actually in days since reference date */
if (st->reset_time) {
mintime = MAXDOUBLE;
for (k = 0; k < nt; k++) {
if (valtime[k] < mintime)
mintime = valtime[k];
}
//st->time_reset = mintime * st->dt;
st->time_reset = mintime * 86400;
}
printf("st->time_reset = %e\n", st->time_reset);
for (k = 0; k < nt; k++) {
if (st->verbose_pad)
printf(
"k = %d, valtime[k] = %e, st->dt = %e, st->time_reset = %e\n",
k, valtime[k], st->dt, st->time_reset);
//exchangedate[k + lastk][time_id] = valtime[k] * st->dt - st->time_reset;
exchangedate[k + lastk][time_id] = valtime[k] * 86400
- st->time_reset;
/* Multiple by 86400 / dt as may be tidal entries so must expand
list to fit these extra entries in */
if(st->dt < 86400){
time_of_year = valtime[k] * (86400 / st->dt);
totnum = 365.0 * (86400 / st->dt);
if (time_of_year / totnum < 1.0)
this_tofy = floor(time_of_year / (86400 / st->dt) + 0.5);
else
this_tofy = floor((time_of_year / totnum - floor(time_of_year
/ totnum)) * totnum + 0.5);
}else{
time_of_year = valtime[k];// * (st->dt / 86400.0);
totnum = 365.0 * (st->dt/86400.0);
if ((time_of_year / totnum) < 1.0)
this_tofy = floor(time_of_year / (st->dt/86400.0) + 0.5);
else
this_tofy = floor((time_of_year / totnum - floor(time_of_year
/ totnum)) * totnum + 0.5);
}
exchangedate[k + lastk][TofY_id] = this_tofy;
if (st->verbose_pad)
printf("k: %d, time: %e, tofy: %e valtime: %e\n", k,
exchangedate[k + lastk][time_id], exchangedate[k
+ lastk][TofY_id], valtime[k]);
}
/* Transfer boxes */
for (k = 0; k < fce; k++) {
/* Swap the box values to the bgm box values using the preloaded boxLookup array.
* These values have been loaded in the load_bgm_mappings function.
* The boxLookup array simply stores the bgm box id in the hydro box value index.
* */
faceinfo[i][k][lbox_id] = boxLookup[valboxs[k]];
faceinfo[i][k][rbox_id] = boxLookup[valboxd[k]];
faceinfo[i][k][fceid_id] = valfaceid[k];
facemap[k] = valfaceid[k];
//facemap[k] = k;
}
/* Transfer exchanges */
entry_i = 0;
for (k = 0; k < nt; k++) {
for (nz = 0; nz < lvl; nz++) {
for (ij = 0; ij < fce; ij++) {
if(valexchange[k][ij][nz] > 1e15){
printf("horizontal exchange value is too large. valexchange[%d][%d][%d] = %e\n", k, ij, nz, valexchange[k][ij][nz]);
quit("");
}
if (isnan(valexchange[k][ij][nz]) || !_finite(valexchange[k][ij][nz]))
rawexchanges[k + lastk][nz][ij][entry_i] = 0.0;
else
rawexchanges[k + lastk][nz][ij][entry_i]
= valexchange[k][ij][nz];
if(ij == 113){
printf("rawexchanges[k + lastk][nz][ij][entry_i] = %e\n",rawexchanges[k + lastk][nz][ij][entry_i]);
}
}
}
}
/* Set lastk */
lastk += nt;
/* Free temporary storage */
switch (sizeof(FPTYPE)) {
case sizeof(float):
f_free1d((float *) valtime);
break;
case sizeof(double):
d_free1d((double *) valtime);
break;
default:
quit("get_cdf_hydro: Unknown size for FPTYPE\n");
}
f_free3d((float ***) valexchange);
i_free1d((int *) valboxs);
i_free1d((int *) valboxd);
i_free1d((int *) valfaceid);
/* Close hydro file */
close_hydro(st);
}
if (verbose)
printf("Finished reading raw exchange files\n");
return;
}
/****************************************************************************************************
Routine to map box and face ids from netcdf file to matching ones in bgm file
*****************************************************************************************************/
void map_bgm_netcdf_faces(Stuff *st, int i) {
int k, fce, actfce, bgmlbox, bgmrbox, cdflbox,
cdfrbox, p1forp1, p1forp2, p2forp2, p2forp1,
positive_equiv;
//, matchscrew = 0;
//, foundmatch = 0;
/* Do face assignment */
for (fce = 0; fce < st->nface; fce++) {
facemap[fce] = -1;
//foundmatch = 0;
//matchscrew = 0;
positive_equiv = -1;
for (k = 0; k < st->nbgmface; k++) {
/* Initialise markers */
p1forp1 = 0;
p1forp2 = 0;
p2forp1 = 0;
p2forp2 = 0;
if (verbose > 1) {
printf("fce (%e, %e)(%e, %e) vs bgmfce (%e, %e)(%e, %e)\n",
faceinfo[i][fce][px1_id], faceinfo[i][fce][py1_id],
faceinfo[i][fce][px2_id], faceinfo[i][fce][py2_id],
st->faces[k].pll1.x, st->faces[k].pll1.y,
st->faces[k].pll2.x, st->faces[k].pll2.y);
}
/* As ordering not always identical first check against one pair then the other
then decide if ordering needs to be reversed or not */
/* Check p1 vs pll1 */
if (fabs(faceinfo[i][fce][px1_id] - st->faces[k].pll1.x)
< buffer_rounding) {
if (fabs(faceinfo[i][fce][py1_id] - st->faces[k].pll1.y)
< buffer_rounding) {
p1forp1 = 1;
//if(fce == 13)
printf("p1p1 with %d\n", k);
}
}
/* Check p1 vs pll2 */
if (fabs(faceinfo[i][fce][px1_id] - st->faces[k].pll2.x)
< buffer_rounding) {
if (fabs(faceinfo[i][fce][py1_id] - st->faces[k].pll2.y)
< buffer_rounding) {
p1forp2 = 1;
//if(fce == 13)
printf("p1p2 with %d\n", k);
}
}
/* Check p2 vs pll1 */
if (fabs(faceinfo[i][fce][px2_id] - st->faces[k].pll1.x)
< buffer_rounding) {
if (fabs(faceinfo[i][fce][py2_id] - st->faces[k].pll1.y)
< buffer_rounding) {
p2forp1 = 1;
//if(fce == 13)
printf("p2p1 with %d\n", k);
}
}
/* Check p2 vs pll2 */
if (fabs(faceinfo[i][fce][px2_id] - st->faces[k].pll2.x)
< buffer_rounding) {
if (fabs(faceinfo[i][fce][py2_id] - st->faces[k].pll2.y)
< buffer_rounding) {
p2forp2 = 1;
//if(fce == 13)
printf("p2p2 with %d\n", k);
}
}
if (p1forp1 && p2forp2) {
/* All ordinates match so assign positive equivalency */
if (facemap[fce] > -1) {
printf(
"Already assigned face %d to bgmface: %d now reassigning to %d - is this right?\n",
fce, facemap[fce], k);
//matchscrew = 1;
}
facemap[fce] = k;
positive_equiv = 1;
//foundmatch = 1;
printf("Assigning face %d to bgmface: %d\n", fce, k);
} else if (p1forp2 && p2forp1) {
/* All ordinates match but points back to front from one file to the other (p1 in one
file is p2 in the other) then assign negative equivalency */
if (facemap[fce] > -1) {
printf(
"Already assigned face %d to bgmface: %d now reassigning to %d - is this right?\n",
fce, facemap[fce], k);
//matchscrew = 1;
}
facemap[fce] = k;
positive_equiv = 0;
//foundmatch = 1;
printf("Assigning face %d to bgmface: %d\n", fce, k);
}
/**
if(foundmatch or matchscrew){
if(verbose > 1){
printf("for file %d, checking fce %d p1(%e,%e) and p2(%e,%e) vs bgmfce %d pll1(%e,%e) and pll2(%e,%e)\n",
i, fce, faceinfo[i][fce][px1_id], faceinfo[i][fce][py1_id], faceinfo[i][fce][px2_id], faceinfo[i][fce][py2_id], k, st->faces[k].pll1.x, st->faces[k].pll1.y, st->faces[k].pll2.x, st->faces[k].pll2.y);
printf("do p1-p1 xcheck: %e, ycheck: %e (vs %e)\n", fabs(faceinfo[i][fce][px1_id] - st->faces[k].pll1.x), fabs(faceinfo[i][fce][py1_id] - st->faces[k].pll1.y), buffer_rounding);
printf("do p1-p2 xcheck: %e, ycheck: %e (vs %e)\n", fabs(faceinfo[i][fce][px1_id] - st->faces[k].pll2.x), fabs(faceinfo[i][fce][py1_id] - st->faces[k].pll2.y), buffer_rounding);
printf("do p2-p1 xcheck: %e, ycheck: %e (vs %e)\n", fabs(faceinfo[i][fce][px2_id] - st->faces[k].pll1.x), fabs(faceinfo[i][fce][py2_id] - st->faces[k].pll1.y), buffer_rounding);
printf("do p2-p2 xcheck: %e, ycheck: %e (vs %e)\n", fabs(faceinfo[i][fce][px2_id] - st->faces[k].pll2.x), fabs(faceinfo[i][fce][py2_id] - st->faces[k].pll2.y), buffer_rounding);
}
}
**/
}
if (facemap[fce] < 0) {
warn("Face %d does not have a cdf <-> bgm mapping\n", fce);
if (verbose > 1) {
for (k = 0; k < st->nbgmface; k++) {
printf(
"for file %d, checking fce %d p1(%e,%e) and p2(%e,%e) vs bgmfce %d pll1(%e,%e) and pll2(%e,%e)\n",
i, fce, faceinfo[i][fce][px1_id],
faceinfo[i][fce][py1_id], faceinfo[i][fce][px2_id],
faceinfo[i][fce][py2_id], k, st->faces[k].pll1.x,
st->faces[k].pll1.y, st->faces[k].pll2.x,
st->faces[k].pll2.y);
printf(
"do p1-p1 xcheck: %e, ycheck: %e (vs %e)\n",
fabs(faceinfo[i][fce][px1_id] - st->faces[k].pll1.x),
fabs(faceinfo[i][fce][py1_id] - st->faces[k].pll1.y),
buffer_rounding);
printf(
"do p1-p2 xcheck: %e, ycheck: %e (vs %e)\n",
fabs(faceinfo[i][fce][px1_id] - st->faces[k].pll2.x),
fabs(faceinfo[i][fce][py1_id] - st->faces[k].pll2.y),
buffer_rounding);
printf(
"do p2-p1 xcheck: %e, ycheck: %e (vs %e)\n",
fabs(faceinfo[i][fce][px2_id] - st->faces[k].pll1.x),
fabs(faceinfo[i][fce][py2_id] - st->faces[k].pll1.y),
buffer_rounding);
printf(
"do p2-p2 xcheck: %e, ycheck: %e (vs %e)\n",
fabs(faceinfo[i][fce][px2_id] - st->faces[k].pll2.x),
fabs(faceinfo[i][fce][py2_id] - st->faces[k].pll2.y),
buffer_rounding);
}
}
//quit("");
}
if (verbose > 1) {
printf("Map fce %d <-> bgmfce: %d\n", fce, facemap[fce]);
}
/* Do box assignment */
actfce = facemap[fce];
if (actfce < 0)
/* If not a real face nothing to do so skip ahead */
continue;
/* Match faces */
bgmlbox = st->faces[actfce].ibl;
bgmrbox = st->faces[actfce].ibr;
printf("bgmlbox = %d\n", bgmlbox);
printf("bgmrbox = %d\n", bgmrbox);
cdflbox = (int) (faceinfo[i][fce][lbox_id]);
cdfrbox = (int) (faceinfo[i][fce][rbox_id]);
printf("cdflbox = %d\n", cdflbox);
printf("cdfrbox = %d\n", cdfrbox);
if (verbose > 1) {
if (!positive_equiv) {
printf(
"+ve equiv: %d fce%d (l-%d, r-%d) vs bgmfce%d (l-%d, r-%d)\n",
positive_equiv, fce, cdflbox, cdfrbox, actfce, bgmlbox,
bgmrbox);
} else {
printf(
"-ve equiv: %d fce%d (l-%d, r-%d) vs bgmfce%d (l-%d, r-%d)\n",
positive_equiv, fce, cdflbox, cdfrbox, actfce, bgmlbox,
bgmrbox);
}
//printf("--> fp(%e, %e | %e, %e) vs bp(%e, %e | %e, %e)\n",
// faceinfo[i][fce][px1_id], faceinfo[i][fce][py1_id], faceinfo[i][fce][px2_id], faceinfo[i][fce][py2_id], st->faces[actfce].pll1.x, st->faces[actfce].pll1.y, st->faces[actfce].pll2.x, st->faces[actfce].pll2.y);
}
/* Left side box */
if (positive_equiv && boxmap[cdflbox] < 0) {
/* Positive equivalency and previously unassigned */
printf("Assigning lbox %d to lbgmbox: %d\n", cdflbox, bgmlbox);
boxmap[cdflbox] = bgmlbox;
} else if (positive_equiv && boxmap[cdflbox] != bgmlbox) {
/* Positive equivalence, but previously assigned */
printf(
"Already assigned lbox %d to lbgmbox: %d now reassigning to %d - is this right?\n",
cdflbox, boxmap[cdflbox], bgmlbox);
printf(
"+ve_equiv: %d, fce%d p(%e, %e | %e, %e) vs bgmfce%d p (%e, %e | %e, %e)\n",
positive_equiv, fce, faceinfo[i][fce][px1_id],
faceinfo[i][fce][py1_id], faceinfo[i][fce][px2_id],
faceinfo[i][fce][py2_id], actfce, st->faces[actfce].pll1.x,
st->faces[actfce].pll1.y, st->faces[actfce].pll2.x,
st->faces[actfce].pll2.y);
boxmap[cdflbox] = bgmlbox;
} else if (!positive_equiv && boxmap[cdfrbox] < 0) {
/* Positive equivalency and previously unassigned */
printf("Assigning rbox %d to lbgmbox: %d\n", cdfrbox, bgmlbox);
boxmap[cdfrbox] = bgmlbox;
} else if (!positive_equiv && boxmap[cdfrbox] != bgmlbox) {
/* Positive equivalence, but previously assigned */
printf(
"Already assigned rbox %d to lbgmbox: %d now reassigning to %d - is this right?\n",
cdfrbox, boxmap[cdfrbox], bgmlbox);
printf(
"+ve_equiv: %d, fce%d p(%e, %e | %e, %e) vs bgmfce%d p (%e, %e | %e, %e)\n",
positive_equiv, fce, faceinfo[i][fce][px1_id],
faceinfo[i][fce][py1_id], faceinfo[i][fce][px2_id],
faceinfo[i][fce][py2_id], actfce, st->faces[actfce].pll1.x,
st->faces[actfce].pll1.y, st->faces[actfce].pll2.x,
st->faces[actfce].pll2.y);
boxmap[cdfrbox] = bgmlbox;
}
/* Right side box */
if (positive_equiv && boxmap[cdfrbox] < 0) {
/* Positive equivalency and previously unassigned */
//printf("Assigning rbox %d to rbgmbox: %d\n", cdfrbox, bgmrbox);
boxmap[cdfrbox] = bgmrbox;
} else if (positive_equiv && boxmap[cdfrbox] != bgmrbox) {
/* Positive equivalence, but previously assigned */
//printf("Already assigned rbox %d to rbgmbox: %d now reassigning to %d - is this right?\n", cdfrbox, boxmap[cdfrbox], bgmrbox);
//printf("+ve_equiv: %d, fce%d p(%e, %e | %e, %e) vs bgmfce%d p (%e, %e | %e, %e)\n",
// positive_equiv, fce, faceinfo[i][fce][px1_id], faceinfo[i][fce][py1_id], faceinfo[i][fce][px2_id], faceinfo[i][fce][py2_id], actfce, st->faces[actfce].pll1.x, st->faces[actfce].pll1.y, st->faces[actfce].pll2.x, st->faces[actfce].pll2.y);
boxmap[cdfrbox] = bgmrbox;
} else if (!positive_equiv && boxmap[cdflbox] < 0) {
/* Positive equivalency and previously unassigned */
//printf("Assigning lbox %d to rbgmbox: %d\n", cdflbox, bgmrbox);
boxmap[cdflbox] = bgmrbox;
} else if (!positive_equiv && boxmap[cdflbox] != bgmrbox) {
/* Positive equivalence, but previously assigned */
//printf("Already assigned lbox %d to rbgmbox: %d now reassigning to %d - is this right?\n", cdflbox, boxmap[cdflbox], bgmrbox);
//printf("+ve_equiv: %d, fce%d p(%e, %e | %e, %e) vs bgmfce%d p (%e, %e | %e, %e)\n",
// positive_equiv, fce, faceinfo[i][fce][px1_id], faceinfo[i][fce][py1_id], faceinfo[i][fce][px2_id], faceinfo[i][fce][py2_id], actfce, st->faces[actfce].pll1.x, st->faces[actfce].pll1.y, st->faces[actfce].pll2.x, st->faces[actfce].pll2.y);
boxmap[cdflbox] = bgmrbox;
}
printf("boxmap[%d] = %d\n", cdflbox, boxmap[cdflbox]);
printf("boxmap[%d] = %d\n", cdfrbox, boxmap[cdfrbox]);
//quit("");
}
/* Check box assignments */
for (k = 0; k < st->nbox; k++) {
if (boxmap[k] < 0)
warn(
"Box %d does not have a cdf <-> bgm mapping, boxmap[k] = %d\n",
k, boxmap[k]);
}
return;
}
/****************************************************************************************************
Routine to open netcdf file containing raw hydrodynamic fluxes
*****************************************************************************************************/
void open_hydro(Stuff *st, char *name, int filenum) {
int ndims;
int nvars;
int natts;
int recdim;
int len;
long n;
nc_type daty;
int dims[MAX_NC_DIMS];
char hdu[STSLEN];
char stu[STSLEN];
/* Set netCDF library error handling */
ncopts = NC_VERBOSE;
if (verbose > 1)
printf("open_hydro: opening the file\n");
/* Open the file */
if ((st->hd.fid = ncopen(name, NC_NOWRITE)) < 0)
quit("open_hydro: Can't open hydrodynamic model input data file %s\n",
name);
/* Inquire about this file */
ncopts = NC_VERBOSE | NC_FATAL;
ncinquire(st->hd.fid, &ndims, &nvars, &natts, &recdim);
if (ndims < 3)
quit("open_hydro: not enough dimensions in %s\n", name);
if (verbose > 1)
printf("open_hydro: check dimensions\n");
/* Check dimensions are as expected */
printf("check for time in standard CSIRO cdf\n");
if ((st->hd.t_did = ncdimid(st->hd.fid, "time")) == -1)
quit("open_hydro: no t dimension in %s\n", name);
if (st->hd.t_did != recdim)
quit("open_hydro: t dimension not unlimited in %s\n", name);
printf("check for faces\n");
if ((st->hd.b_did = ncdimid(st->hd.fid, "faces")) == -1) {
quit("open_hydro: no faces dimension in %s\n", name);
}
printf("check for level\n");
if ((st->hd.z_did = ncdimid(st->hd.fid, "level")) == -1)
quit("open_hydro: no levels dimension in %s\n", name);
/* Get dimension sizes. Note check against geometry now happens in get_cdf_hydro() */
if (verbose > 1)
printf("open_hydro: get dimnensionn\n");
ncdiminq(st->hd.fid, st->hd.b_did, NULL, &n);
ncdiminq(st->hd.fid, st->hd.z_did, NULL, &n);
/* Check that time units and steps match this model */
if (verbose > 1)
printf("open_hydro: get units\n");
st->hd.t_vid = ncvarid(st->hd.fid, "time");
if (st->hd.t_vid < 0)
quit("open_hydro: no t variable in %s\n", name);
memset(st->hd.t_units, 0, STSLEN);
ncattget(st->hd.fid, st->hd.t_vid, "units", st->hd.t_units);
sscanf(st->hd.t_units, "%s", hdu);
if (!filenum)
sprintf(st->t_units, "%s", st->hd.t_units);
sscanf(st->t_units, "%s", stu);
if (strcmp(hdu, stu) != 0) {
quit(
"open_hydro: Time units (%s) in %s don't match model time units (%s) loaded from %s\n",
hdu, name, stu, st->hd.fname[0]);
}
/* Check for timestep size */
if (verbose > 1)
printf("open_hydro: get timestep\n");
ncattinq(st->hd.fid, st->hd.t_vid, "dt", &daty, &len);
if (nctypelen(daty) != sizeof(st->hd.dt))
quit("open_hydro: dt attribute wrong type\n");
ncattget(st->hd.fid, st->hd.t_vid, "dt", &st->hd.dt);
if (st->hd.dt <= 0.0)
quit("open_hydro: dt attribute must have positive value\n");
/* Find out how many time steps are in the file */
ncdiminq(st->hd.fid, st->hd.t_did, NULL, &st->hd.nstep);
/* Get other variable ids */
if (verbose > 1)
printf("open_hydro: get variable ids\n");
st->hd.srcb_vid = ncvarid(st->hd.fid, "source_boxid");
if (st->hd.srcb_vid < 0)
quit("open_hydro: no source_boxid variable in %s\n", name);
st->hd.destb_vid = ncvarid(st->hd.fid, "dest_boxid");
if (st->hd.destb_vid < 0)
quit("open_hydro: no dest_boxid variable in %s\n", name);
st->hd.fce_vid = ncvarid(st->hd.fid, "faces");
if (st->hd.fce_vid < 0)
quit("open_hydro: no faces variable in %s\n", name);
st->hd.fcex1_vid = ncvarid(st->hd.fid, "pt1_x");
if (st->hd.fcex1_vid < 0)
quit("open_hydro: no pt1_x variable in %s\n", name);
st->hd.fcey1_vid = ncvarid(st->hd.fid, "pt1_y");
if (st->hd.fcey1_vid < 0)
quit("open_hydro: no pt1_y variable in %s\n", name);
st->hd.fcex2_vid = ncvarid(st->hd.fid, "pt2_x");
if (st->hd.fcex2_vid < 0)
quit("open_hydro: no pt2_x variable in %s\n", name);
st->hd.fcey2_vid = ncvarid(st->hd.fid, "pt2_y");
if (st->hd.fcey2_vid < 0)
quit("open_hydro: no pt2_y variable in %s\n", name);
st->hd.e_vid = ncvarid(st->hd.fid, "transport");
if (st->hd.e_vid < 0)
quit("open_hydro: no transport variable in %s\n", name);
/* Check variable types and dimensions for the transport data */
if (verbose > 1)
printf("open_hydro: get exchange data ids\n");
ncvarinq(st->hd.fid, st->hd.e_vid, NULL, &daty, &ndims, dims, &natts);
if (nctypelen(daty) != sizeof(FPTYPE))
warn(
"open_hydro: Type of exchange variable doesn't match model (ok if float in file and double FPTYPE)\n");
if (ndims != 3 || dims[0] != st->hd.t_did || dims[1] != st->hd.b_did
|| dims[2] != st->hd.z_did)
quit("open_hydro: transport variable has incorrect dimensions\n");
ncattget(st->hd.fid, st->hd.e_vid, "units", st->hd.e_units);
if (verbose > 1)
printf("open_hydro: get exchange units\n");
sscanf(st->hd.e_units, "%s", stu);
if (strstr("Sverdrup", stu) != NULL)
st->unit_type = 0;
else
st->unit_type = 1;
/* Reset netCDF error handling */
ncopts = NC_VERBOSE | NC_FATAL;
return;
}
/****************************************************************************************************
Routine to close netcdf file containing raw hydrodynamic fluxes
*****************************************************************************************************/
void close_hydro(Stuff *st) {
/* Close file */
if (st->hd.fid >= 0)
ncclose(st->hd.fid);
st->hd.fid = -1;
return;
}
/***************************************************************************************************
Routine to calculate number of data points, faces and levels to be loaded from netcdf file
****************************************************************************************************/
void get_netcdf_dimensions(Stuff *st) {
int i, totndata, maxfaces, minlevel, maxlevel, totnvdata, maxboxes;
long nt, fce, lvl, nvt, vlvl, nvb;
/* Initialise counters */
totndata = 0;
maxfaces = 0;
minlevel = MAXINT;
maxlevel = 0;
for (i = 0; i < st->hd.nfiles; i++) {
if (verbose)
printf("Reading raw exchange file to get dimensions %s\n",
st->hd.fname[i]);
/* Open the exchange file */
open_hydro(st, st->hd.fname[i], i);
/* Check for dimensions in file */
ncdiminq(st->hd.fid, ncdimid(st->hd.fid, "time"), NULL, &nt);
ncdiminq(st->hd.fid, ncdimid(st->hd.fid, "faces"), NULL, &fce);
ncdiminq(st->hd.fid, ncdimid(st->hd.fid, "level"), NULL, &lvl);
/* Check against counters */
totndata += nt;
if (fce > maxfaces)
maxfaces = fce;
if (lvl < minlevel)
minlevel = lvl;
if (lvl > maxlevel)
maxlevel = lvl;
/* Close hydro file */
close_hydro(st);
}
/* Check aganist stored values */
st->ndata = totndata;
st->nhdsteps = st->ndata;
printf("ndata: %d, nface: %d vs maxface: %d\n", st->ndata, st->nface,
maxfaces);
if (st->nface < maxfaces) {
warn(
"One of the raw hydro files contains more faces than the bgm file - reset nface\n");
st->nface = maxfaces;
}
if (st->wcnz > minlevel) {
warn(
"One of the raw hydro files contains less water column layers (%d) than the parameter file specifies (%d)\n",
minlevel, st->wcnz);
}
if (st->wcnz < maxlevel) {
warn(
"One of the raw hydro files contains less water column layers (%d) than the parameter file specifies (%d)\n",
maxlevel, st->wcnz);
}
/* Get vertical exchange dimensions */
totnvdata = 0;
minlevel = MAXINT;
maxlevel = 0;
maxboxes = 0;
for (i = 0; i < st->ts.ntsfiles; i++) {
if (verbose)
printf("Reading raw exchange file to get dimensions %s\n",
st->ts.tsfname[i]);
/* Open the exchange file */
open_tsfile(st, st->ts.tsfname[i], i);
/* Check for dimensions in file */
ncdiminq(st->ts.tsfid, ncdimid(st->ts.tsfid, "time"), NULL, &nvt);
ncdiminq(st->ts.tsfid, ncdimid(st->ts.tsfid, "level"), NULL, &vlvl);
ncdiminq(st->ts.tsfid, ncdimid(st->ts.tsfid, "boxes"), NULL, &nvb);
/* Check against counters */
totnvdata += nvt;
if (nvb > maxboxes)
maxboxes = nvb;
if (vlvl < minlevel)
minlevel = vlvl;
if (vlvl > maxlevel)
maxlevel = vlvl;
/* Close hydro file */
close_tsfile(st);
}
/* Check aganist stored values */
st->nvhdsteps = totnvdata;
//st->nvhdsteps = 0;
printf("set nhd: %d, nvhd: %d\n", st->nhdsteps, st->nvhdsteps);
if (st->nbox < maxboxes) {
warn(
"One of the raw vert hydro files contains more boxes than the bgm file - reset nbox\n");
st->nbox = maxboxes;
}
if (st->wcnz > minlevel) {
warn(
"One of the raw vert hydro files contains less water column layers (%d) than the parameter file specifies (%d)\n",
minlevel, st->wcnz);
}
if (st->wcnz < maxlevel) {
warn(
"One of the raw vert hydro files contains less water column layers (%d) than the parameter file specifies (%d)\n",
maxlevel, st->wcnz);
}
return;
}
/**
*
* \brief Read in the mappings between the input hydro file boxes and the bgm file.
*
* This allows the user to swap box numbers.
*
*
*
*/
void load_bgm_mappings(Stuff *st) {
FILE *fp;
int hydroBoxNumber, bgmBoxNumber;
int buflen = 500;
char buf[500];
/* Read and load lookup table */
if (verbose)
printf("Reading lookup table file %s\n", st->lookupIfname);
/* Initialise look-up box array */
boxLookup = (int *) i_alloc1d(st->nbox);
/* Open the lookup table file */
if ((fp = fopen(st->lookupIfname, "r")) == NULL){
warn("load_bgm_mappings - Lookup table file: Can't open %s. Values loaded from the input file will be used.\n",
st->lookupIfname);
for(hydroBoxNumber = 0; hydroBoxNumber < st->nbox; hydroBoxNumber++)
boxLookup[hydroBoxNumber] = hydroBoxNumber;
return;
}
/* read in each line */
while (fgets(buf, buflen, fp) != NULL) {
//printf("buf = %s\n",buf);
/* Ignore the first line */
if (strstr(buf, "BGMBox") != NULL) {
continue;
}
/* Read data line */
if (sscanf(buf, "%d,%d", &hydroBoxNumber, &bgmBoxNumber) != 2)
quit("not enough entries for %s - with %d, %d\n", buf,
hydroBoxNumber, bgmBoxNumber);
if (verbose > 3)
printf("readin %s, hydroBoxNumber-%d, bgmBoxNumber-%d\n", buf,
hydroBoxNumber, bgmBoxNumber);
if (hydroBoxNumber >= st->nbox)
quit(
"There are only %d boxes in the model - your box_conversion input has a hydro box number of %d?\n",
st->nbox, hydroBoxNumber);
if (bgmBoxNumber >= st->nbox)
quit(
"There are only %d boxes in the model - your box_conversion input has a bgm box number of %d?\n",
st->nbox, bgmBoxNumber);
/* All is ok - store the numbers */
boxLookup[hydroBoxNumber] = bgmBoxNumber;
}
/* Close the conversion file */
fclose(fp);
}