-
Notifications
You must be signed in to change notification settings - Fork 15
/
write_output.cu
1357 lines (1078 loc) · 39.5 KB
/
write_output.cu
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
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
void handle_error(int status) {
if (status != NC_NOERR) {
fprintf(stderr, "Netcdf %s\n", nc_strerror(status));
std::ostringstream stringStream;
stringStream << nc_strerror(status);
std::string copyOfStr = stringStream.str();
write_text_to_log_file("Netcdf error:" + copyOfStr);
//fprintf(logfile, "Netcdf: %s\n", nc_strerror(status));
exit(2);
}
}
Param creatncfileUD(Param XParam)
{
int status;
int nx = ceil(XParam.nx / 16.0) * 16.0; // should be (xmax-xo)/dx but xmax is not known yet
int ny = ceil(XParam.ny / 16.0) * 16.0;
//double dx = XParam.dx;
size_t nxx, nyy;
int ncid, xx_dim, yy_dim, time_dim;
float * xval, *yval;
static size_t xcount[] = { nx };
static size_t ycount[] = { ny };
int time_id, xx_id, yy_id;
static size_t tst[] = { 0 };
static size_t xstart[] = { 0 }; // start at first value
static size_t ystart[] = { 0 }; // start at first value
static size_t thstart[] = { 0 }; // start at first value
nxx = nx;
nyy = ny;
//Recreat the x, y and theta array
xval = (float *)malloc(nx*sizeof(float));
yval = (float *)malloc(ny*sizeof(float));
for (int i = 0; i<nx; i++)
{
xval[i] = (float)(XParam.xo + i*XParam.dx);
}
for (int i = 0; i<ny; i++)
{
yval[i] = (float)(XParam.yo + i*XParam.dx);
}
//create the netcdf datasetXParam.outfile.c_str()
status = nc_create(XParam.outfile.c_str(), NC_NOCLOBBER, &ncid);
if (status != NC_NOERR)
{
if (status == NC_EEXIST) // File already axist so automatically rename the output file
{
printf("Warning! Outut file name already exist ");
write_text_to_log_file("Warning! Outut file name already exist ");
int fileinc = 1;
std::vector<std::string> extvec = split(XParam.outfile, '.');
std::string bathyext = extvec.back();
std::string newname;
while (status == NC_EEXIST)
{
newname = extvec[0];
for (int nstin = 1; nstin < extvec.size() - 1; nstin++)
{
// This is in case there are "." in the file name that do not relate to the file extension"
newname = newname + "." + extvec[nstin];
}
newname = newname + "_" + std::to_string(fileinc) + "." + bathyext;
XParam.outfile = newname;
status = nc_create(XParam.outfile.c_str(), NC_NOCLOBBER, &ncid);
fileinc++;
}
printf("New file name: %s ", XParam.outfile.c_str());
write_text_to_log_file("New file name: " + XParam.outfile);
}
else
{
// Other error
handle_error(status);
}
}
// status could be a new error after renaming the file
if (status != NC_NOERR) handle_error(status);
//Define dimensions: Name and length
status = nc_def_dim(ncid, "xx", nxx, &xx_dim);
if (status != NC_NOERR) handle_error(status);
status = nc_def_dim(ncid, "yy", nyy, &yy_dim);
if (status != NC_NOERR) handle_error(status);
//status = nc_def_dim(ncid, "npart",nnpart,&p_dim);
status = nc_def_dim(ncid, "time", NC_UNLIMITED, &time_dim);
if (status != NC_NOERR) handle_error(status);
int tdim[] = { time_dim };
int xdim[] = { xx_dim };
int ydim[] = { yy_dim };
status = nc_def_var(ncid, "time", NC_FLOAT, 1, tdim, &time_id);
if (status != NC_NOERR) handle_error(status);
status = nc_def_var(ncid, "xx", NC_FLOAT, 1, xdim, &xx_id);
if (status != NC_NOERR) handle_error(status);
status = nc_def_var(ncid, "yy", NC_FLOAT, 1, ydim, &yy_id);
if (status != NC_NOERR) handle_error(status);
//End definitions: leave define mode
status = nc_enddef(ncid);
if (status != NC_NOERR) handle_error(status);
//Provide values for variables
status = nc_put_var1_double(ncid, time_id, tst, &XParam.totaltime);
if (status != NC_NOERR) handle_error(status);
status = nc_put_vara_float(ncid, xx_id, xstart, xcount, xval);
if (status != NC_NOERR) handle_error(status);
status = nc_put_vara_float(ncid, yy_id, ystart, ycount, yval);
if (status != NC_NOERR) handle_error(status);
//close and save new file
status = nc_close(ncid);
if (status != NC_NOERR) handle_error(status);
free(xval);
free(yval);
return XParam;
}
template <class T> void defncvar(Param XParam, double * blockxo, double *blockyo, std::string varst, int vdim, T * var)
{
std::string outfile = XParam.outfile;
int smallnc = XParam.smallnc;
float scalefactor = XParam.scalefactor;
float addoffset = XParam.addoffset;
//int nx = ceil(XParam.nx / 16.0) * 16.0;
//int ny = ceil(XParam.ny / 16.0) * 16.0;
int status;
int ncid, var_id;
int var_dimid2D[2];
int var_dimid3D[3];
//int var_dimid4D[4];
short * var_s, *varblk_s;
float * varblk;
int recid, xid, yid;
//size_t ntheta;// nx and ny are stored in XParam not yet for ntheta
status = nc_open(outfile.c_str(), NC_WRITE, &ncid);
if (status != NC_NOERR) handle_error(status);
status = nc_redef(ncid);
if (status != NC_NOERR) handle_error(status);
//Inquire dimensions ids
status = nc_inq_unlimdim(ncid, &recid);//time
if (status != NC_NOERR) handle_error(status);
status = nc_inq_dimid(ncid, "xx", &xid);
if (status != NC_NOERR) handle_error(status);
status = nc_inq_dimid(ncid, "yy", &yid);
if (status != NC_NOERR) handle_error(status);
var_dimid2D[0] = yid;
var_dimid2D[1] = xid;
var_dimid3D[0] = recid;
var_dimid3D[1] = yid;
var_dimid3D[2] = xid;
float fillval = 9.9692e+36f;
short Sfillval = 32767;
//short fillval = 32767
static size_t start2D[] = { 0, 0 }; // start at first value
//static size_t count2D[] = { ny, nx };
static size_t count2D[] = { 16, 16 };
static size_t start3D[] = { 0, 0, 0 }; // start at first value
//static size_t count3D[] = { 1, ny, nx };
static size_t count3D[] = { 1, 16, 16 };
varblk = (float *)malloc(XParam.blksize * sizeof(float));
if (smallnc > 0)
{
//If saving as short than we first need to scale and shift the data
var_s = (short *)malloc(XParam.nblk*XParam.blksize *sizeof(short));
varblk_s = (short *)malloc(XParam.blksize * sizeof(short));
for (int bl = 0; bl < XParam.nblk; bl++)
{
for (int j = 0; j < 16; j++)
{
for (int i = 0; i < 16; i++)
{
int n = i + j * 16 + bl * XParam.blksize;
// packed_data_value = nint((unpacked_data_value - add_offset) / scale_factor)
var_s[n] = (short)round((var[n] - addoffset) / scalefactor);
}
}
}
}
if (vdim == 2)
{
if (smallnc > 0)
{
status = nc_def_var(ncid, varst.c_str(), NC_SHORT, vdim, var_dimid2D, &var_id);
if (status != NC_NOERR) handle_error(status);
status = nc_put_att_float(ncid, var_id, "scale_factor", NC_FLOAT, 1, &scalefactor);
if (status != NC_NOERR) handle_error(status);
status = nc_put_att_float(ncid, var_id, "add_offset", NC_FLOAT, 1, &addoffset);
if (status != NC_NOERR) handle_error(status);
status = nc_put_att_short(ncid, var_id, "_FillValue", NC_SHORT, 1, &Sfillval);
if (status != NC_NOERR) handle_error(status);
status = nc_put_att_short(ncid, var_id, "missingvalue", NC_SHORT, 1, &Sfillval);
if (status != NC_NOERR) handle_error(status);
status = nc_enddef(ncid);
if (status != NC_NOERR) handle_error(status);
for (int bl = 0; bl < XParam.nblk; bl++)
{
for (int j = 0; j < 16; j++)
{
for (int i = 0; i < 16; i++)
{
int n = i + j * 16 + bl * XParam.blksize;
varblk_s[i + j * 16] = var_s[n];
}
}
//jstart
start2D[0] = (size_t)round((blockyo[bl] - XParam.yo) / XParam.dx);
start2D[1] = (size_t)round((blockxo[bl] - XParam.xo) / XParam.dx);
status = nc_put_vara_short(ncid, var_id, start2D, count2D, varblk_s);
if (status != NC_NOERR) handle_error(status);
}
}
else
{
status = nc_def_var(ncid, varst.c_str(), NC_FLOAT, vdim, var_dimid2D, &var_id);
if (status != NC_NOERR) handle_error(status);
status = nc_put_att_float(ncid, var_id, "_FillValue", NC_FLOAT, 1, &fillval);
if (status != NC_NOERR) handle_error(status);
status = nc_put_att_float(ncid, var_id, "missingvalue", NC_FLOAT, 1, &fillval);
if (status != NC_NOERR) handle_error(status);
status = nc_enddef(ncid);
if (status != NC_NOERR) handle_error(status);
for (int bl = 0; bl < XParam.nblk; bl++)
{
for (int j = 0; j < 16; j++)
{
for (int i = 0; i < 16; i++)
{
int n = i + j * 16 + bl * XParam.blksize;
varblk[i + j * 16] = var[n];
}
}
start2D[0] = (size_t)round((blockyo[bl] - XParam.yo) / XParam.dx);
start2D[1] = (size_t)round((blockxo[bl] - XParam.xo) / XParam.dx);
status = nc_put_vara_float(ncid, var_id, start2D, count2D, varblk);
if (status != NC_NOERR) handle_error(status);
}
}
}
if (vdim == 3)
{
if (smallnc > 0)
{
status = nc_def_var(ncid, varst.c_str(), NC_SHORT, vdim, var_dimid3D, &var_id);
if (status != NC_NOERR) handle_error(status);
status = nc_put_att_float(ncid, var_id, "scale_factor", NC_FLOAT, 1, &scalefactor);
if (status != NC_NOERR) handle_error(status);
status = nc_put_att_float(ncid, var_id, "add_offset", NC_FLOAT, 1, &addoffset);
if (status != NC_NOERR) handle_error(status);
status = nc_put_att_short(ncid, var_id, "_FillValue", NC_SHORT, 1, &Sfillval);
if (status != NC_NOERR) handle_error(status);
status = nc_put_att_short(ncid, var_id, "missingvalue", NC_SHORT, 1, &Sfillval);
if (status != NC_NOERR) handle_error(status);
status = nc_enddef(ncid);
if (status != NC_NOERR) handle_error(status);
for (int bl = 0; bl < XParam.nblk; bl++)
{
for (int j = 0; j < 16; j++)
{
for (int i = 0; i < 16; i++)
{
int n = i + j * 16 + bl * XParam.blksize;
varblk_s[i + j * 16] = var_s[n];
}
}
start3D[1] = (size_t)round((blockyo[bl] - XParam.yo) / XParam.dx);
start3D[2] = (size_t)round((blockxo[bl] - XParam.xo) / XParam.dx);
status = nc_put_vara_short(ncid, var_id, start3D, count3D, varblk_s);
if (status != NC_NOERR) handle_error(status);
}
}
else
{
status = nc_def_var(ncid, varst.c_str(), NC_FLOAT, vdim, var_dimid3D, &var_id);
if (status != NC_NOERR) handle_error(status);
status = nc_put_att_float(ncid, var_id, "_FillValue", NC_FLOAT, 1, &fillval);
if (status != NC_NOERR) handle_error(status);
status = nc_put_att_float(ncid, var_id, "missingvalue", NC_FLOAT, 1, &fillval);
if (status != NC_NOERR) handle_error(status);
status = nc_enddef(ncid);
if (status != NC_NOERR) handle_error(status);
for (int bl = 0; bl < XParam.nblk; bl++)
{
for (int j = 0; j < 16; j++)
{
for (int i = 0; i < 16; i++)
{
int n = i + j * 16 + bl * XParam.blksize;
varblk[i + j * 16] = var[n];
}
}
start3D[1] = (size_t)round((blockyo[bl] - XParam.yo) / XParam.dx);
start3D[2] = (size_t)round((blockxo[bl] - XParam.xo) / XParam.dx);
status = nc_put_vara_float(ncid, var_id, start3D, count3D, varblk);
if (status != NC_NOERR) handle_error(status);
}
}
}
if (smallnc > 0)
{
free(var_s);
free(varblk_s);
}
free(varblk);
//close and save new file
status = nc_close(ncid);
if (status != NC_NOERR) handle_error(status);
}
Param creatncfileBUQ(Param XParam)
{
int status;
int nx, ny;
//double dx = XParam.dx;
size_t nxx, nyy;
int ncid, xx_dim, yy_dim, time_dim, blockid_dim;
float * xval, *yval;
// create the netcdf datasetXParam.outfile.c_str()
status = nc_create(XParam.outfile.c_str(), NC_NOCLOBBER, &ncid);
if (status != NC_NOERR)
{
if (status == NC_EEXIST) // File already axist so automatically rename the output file
{
printf("Warning! Outut file name already exist ");
write_text_to_log_file("Warning! Outut file name already exist ");
int fileinc = 1;
std::vector<std::string> extvec = split(XParam.outfile, '.');
std::string bathyext = extvec.back();
std::string newname;
while (status == NC_EEXIST)
{
newname = extvec[0];
for (int nstin = 1; nstin < extvec.size() - 1; nstin++)
{
// This is in case there are "." in the file name that do not relate to the file extension"
newname = newname + "." + extvec[nstin];
}
newname = newname + "_" + std::to_string(fileinc) + "." + bathyext;
XParam.outfile = newname;
status = nc_create(XParam.outfile.c_str(), NC_NOCLOBBER, &ncid);
fileinc++;
}
printf("New file name: %s ", XParam.outfile.c_str());
write_text_to_log_file("New file name: " + XParam.outfile);
}
else
{
// Other error
handle_error(status);
}
}
// status could be a new error after renaming the file
if (status != NC_NOERR) handle_error(status);
double initdx = calcres(XParam.dx, XParam.initlevel);
double xmin, xmax, ymin, ymax;
xmin = XParam.xo - initdx / 2.0;
xmax = XParam.xmax + initdx / 2.0;
ymin = XParam.yo - initdx / 2.0;
ymax = XParam.ymax + initdx / 2.0;
// Define global attributes
status = nc_put_att_int(ncid, NC_GLOBAL, "maxlevel", NC_INT, 1, &XParam.maxlevel);
if (status != NC_NOERR) handle_error(status);
status = nc_put_att_int(ncid, NC_GLOBAL, "minlevel", NC_INT, 1, &XParam.minlevel);
if (status != NC_NOERR) handle_error(status);
status = nc_put_att_double(ncid, NC_GLOBAL, "xmin", NC_DOUBLE, 1, &xmin);
if (status != NC_NOERR) handle_error(status);
status = nc_put_att_double(ncid, NC_GLOBAL, "xmax", NC_DOUBLE, 1, &xmax);
if (status != NC_NOERR) handle_error(status);
status = nc_put_att_double(ncid, NC_GLOBAL, "ymin", NC_DOUBLE, 1, &ymin);
if (status != NC_NOERR) handle_error(status);
status = nc_put_att_double(ncid, NC_GLOBAL, "ymax", NC_DOUBLE, 1, &ymax);
if (status != NC_NOERR) handle_error(status);
// Define time variable
status = nc_def_dim(ncid, "time", NC_UNLIMITED, &time_dim);
if (status != NC_NOERR) handle_error(status);
int time_id, xx_id, yy_id;
int tdim[] = { time_dim };
static size_t tst[] = { 0 };
size_t blkstart[] = { 0 };
size_t blkcount[] = { XParam.nblk };
size_t xcount[] = { 0 };
size_t ycount[] = { 0 };
static size_t xstart[] = { 0 }; // start at first value
static size_t ystart[] = { 0 };
status = nc_def_var(ncid, "time", NC_FLOAT, 1, tdim, &time_id);
if (status != NC_NOERR) handle_error(status);
// Define dimensions and variables to store block id,, status, level xo, yo
status = nc_def_dim(ncid, "blockid", XParam.nblk, &blockid_dim);
if (status != NC_NOERR) handle_error(status);
int biddim[] = { blockid_dim };
int blkid_id, blkxo_id, blkyo_id, blklevel_id, blkwidth_id, blkstatus_id;
status = nc_def_var(ncid, "blockid", NC_INT, 1, biddim, &blkid_id);
if (status != NC_NOERR) handle_error(status);
status = nc_def_var(ncid, "blockxo", NC_FLOAT, 1, biddim, &blkxo_id);
if (status != NC_NOERR) handle_error(status);
status = nc_def_var(ncid, "blockyo", NC_FLOAT, 1, biddim, &blkyo_id);
if (status != NC_NOERR) handle_error(status);
status = nc_def_var(ncid, "blockwidth", NC_FLOAT, 1, biddim, &blkwidth_id);
if (status != NC_NOERR) handle_error(status);
status = nc_def_var(ncid, "blocklevel", NC_INT, 1, biddim, &blklevel_id);
if (status != NC_NOERR) handle_error(status);
status = nc_def_var(ncid, "blockstatus", NC_INT, 1, biddim, &blkstatus_id);
if (status != NC_NOERR) handle_error(status);
// For each level Define xx yy
for (int lev = XParam.minlevel; lev <= XParam.maxlevel; lev++)
{
double ddx = calcres(XParam.dx, lev);
double initdx= calcres(XParam.dx, XParam.initlevel);
double xxmax, xxmin, yymax, yymin;
xxmax = XParam.xmax + initdx / 2.0 - calcres(XParam.dx, lev + 1);
yymax = XParam.ymax + initdx / 2.0 - calcres(XParam.dx, lev + 1);
xxmin = XParam.xo - initdx / 2.0 + calcres(XParam.dx, lev + 1);
yymin = XParam.yo - initdx / 2.0 + calcres(XParam.dx, lev + 1);
nx = (xxmax - xxmin) / ddx + 1;
ny = (yymax - yymin) / ddx + 1;
//printf("lev=%d; xxmax=%f; xxmin=%f; nx=%d\n", lev, xxmax, xxmin,nx);
//printf("lev=%d; yymax=%f; yymin=%f; ny=%d\n", lev, yymax, yymin, ny);
nxx = nx;
nyy = ny;
//Define dimensions: Name and length
std::string xxname, yyname, sign;
lev < 0?sign="N":sign = "P";
xxname = "xx_" + sign + std::to_string(abs(lev));
yyname = "yy_" + sign + std::to_string(abs(lev));
//printf("lev=%d; xxname=%s; yyname=%s;\n", lev, xxname.c_str(), yyname.c_str());
//printf("ddx=%f; nxx=%d;\n", ddx, nxx);
status = nc_def_dim(ncid, xxname.c_str(), nxx, &xx_dim);
if (status != NC_NOERR) handle_error(status);
status = nc_def_dim(ncid, yyname.c_str(), nyy, &yy_dim);
if (status != NC_NOERR) handle_error(status);
//status = nc_def_dim(ncid, "npart",nnpart,&p_dim);
int xdim[] = { xx_dim };
int ydim[] = { yy_dim };
status = nc_def_var(ncid, xxname.c_str(), NC_FLOAT, 1, xdim, &xx_id);
if (status != NC_NOERR) handle_error(status);
status = nc_def_var(ncid, yyname.c_str(), NC_FLOAT, 1, ydim, &yy_id);
if (status != NC_NOERR) handle_error(status);
//End definitions: leave define mode
}
status = nc_enddef(ncid);
if (status != NC_NOERR) handle_error(status);
//status = nc_close(ncid);
//if (status != NC_NOERR) handle_error(status);
float* blkwidth;
int* blkid;
Allocate1CPU(1, XParam.nblk, blkwidth);
Allocate1CPU(1, XParam.nblk, blkid);
for (int ib = 0; ib < XParam.nblk; ib++)
{
int ibl = activeblk[ib];
blkwidth[ib] = (float)calcres(XParam.dx, level[ibl]);
blkid[ib] = ibl;
}
status = nc_put_vara_int(ncid, blkid_id, blkstart, blkcount, blkid);
//status = nc_put_vara_int(ncid, blkstatus_id, blkstart, blkcount, activeblk);
status = nc_put_vara_float(ncid, blkwidth_id, blkstart, blkcount, blkwidth);
// Reusing blkwidth for other array
// This is needed because the blockxo array may be shuffled to memory block beyond nblk
for (int ib = 0; ib < XParam.nblk; ib++)
{
int ibl = activeblk[ib];
blkwidth[ib] = blockxo[ibl];
blkid[ib] = level[ibl];
}
status = nc_put_vara_float(ncid, blkxo_id, blkstart, blkcount, blkwidth);
status = nc_put_vara_int(ncid, blklevel_id, blkstart, blkcount, blkid);
for (int ib = 0; ib < XParam.nblk; ib++)
{
int ibl = activeblk[ib];
blkwidth[ib] = blockyo[ibl];
}
status = nc_put_vara_float(ncid, blkyo_id, blkstart, blkcount, blkwidth);
free(blkid);
free(blkwidth);
if (status != NC_NOERR) handle_error(status);
std::string xxname, yyname, sign;
for (int lev = XParam.minlevel; lev <= XParam.maxlevel; lev++)
{
double ddx = calcres(XParam.dx, lev);
double initdx = calcres(XParam.dx, XParam.initlevel);
double xxmax, xxmin, yymax, yymin;
xxmax = XParam.xmax + initdx / 2.0 - calcres(XParam.dx, lev + 1);
yymax = XParam.ymax + initdx / 2.0 - calcres(XParam.dx, lev + 1);
xxmin = XParam.xo - initdx / 2.0 + calcres(XParam.dx, lev + 1);
yymin = XParam.yo - initdx / 2.0 + calcres(XParam.dx, lev + 1);
nx = (xxmax - xxmin) / ddx + 1;
ny = (yymax - yymin) / ddx + 1;
//printf("lev=%d; xxmax=%f; xxmin=%f; nx=%d\n", lev, xxmax, xxmin, nx);
//printf("lev=%d; yymax=%f; yymin=%f; ny=%d\n", lev, yymax, yymin, ny);
// start at first value
//static size_t thstart[] = { 0 };
xcount[0] = nx;
ycount[0] = ny;
//Recreat the x, y
xval = (float *)malloc(nx*sizeof(float));
yval = (float *)malloc(ny*sizeof(float));
for (int i = 0; i < nx; i++)
{
xval[i] = (float)(xxmin + i*ddx);
}
for (int i = 0; i < ny; i++)
{
yval[i] = (float)(yymin + i*ddx);
}
//std::string xxname, yyname, sign;
lev < 0 ? sign = "N" : sign = "P";
xxname = "xx_" + sign + std::to_string(abs(lev));
yyname = "yy_" + sign + std::to_string(abs(lev));
//printf("lev=%d; xxname=%s; yyname=%s;\n", lev, xxname.c_str(), yyname.c_str());
status = nc_inq_varid(ncid, xxname.c_str(), &xx_id);
if (status != NC_NOERR) handle_error(status);
status = nc_inq_varid(ncid, yyname.c_str(), &yy_id);
if (status != NC_NOERR) handle_error(status);
//Provide values for variables
status = nc_put_vara_float(ncid, xx_id, xstart, xcount, xval);
if (status != NC_NOERR) handle_error(status);
status = nc_put_vara_float(ncid, yy_id, ystart, ycount, yval);
if (status != NC_NOERR) handle_error(status);
free(xval);
free(yval);
}
//close and save new file
status = nc_close(ncid);
if (status != NC_NOERR) handle_error(status);
return XParam;
//return XParam;
}
Param creatncfileBUQO(Param XParam)
{
int status;
int nx, ny;
//double dx = XParam.dx;
size_t nxx, nyy;
int ncid, xx_dim, yy_dim, time_dim;
float * xval, *yval;
// create the netcdf datasetXParam.outfile.c_str()
status = nc_create(XParam.outfile.c_str(), NC_NOCLOBBER, &ncid);
if (status != NC_NOERR)
{
if (status == NC_EEXIST) // File already axist so automatically rename the output file
{
printf("Warning! Outut file name already exist ");
write_text_to_log_file("Warning! Outut file name already exist ");
int fileinc = 1;
std::vector<std::string> extvec = split(XParam.outfile, '.');
std::string bathyext = extvec.back();
std::string newname;
while (status == NC_EEXIST)
{
newname = extvec[0];
for (int nstin = 1; nstin < extvec.size() - 1; nstin++)
{
// This is in case there are "." in the file name that do not relate to the file extension"
newname = newname + "." + extvec[nstin];
}
newname = newname + "_" + std::to_string(fileinc) + "." + bathyext;
XParam.outfile = newname;
status = nc_create(XParam.outfile.c_str(), NC_NOCLOBBER, &ncid);
fileinc++;
}
printf("New file name: %s ", XParam.outfile.c_str());
write_text_to_log_file("New file name: " + XParam.outfile);
}
else
{
// Other error
handle_error(status);
}
}
// status could be a new error after renaming the file
if (status != NC_NOERR) handle_error(status);
// Define time variable
//status = nc_def_dim(ncid, "time", NC_UNLIMITED, &time_dim);
//if (status != NC_NOERR) handle_error(status);
double initdx = calcres(XParam.dx, XParam.initlevel);
double xmin, xmax, ymin, ymax;
xmin = XParam.xo - initdx / 2.0;
xmax = XParam.xmax + initdx / 2.0;
ymin = XParam.yo - initdx / 2.0;
ymax = XParam.ymax + initdx / 2.0;
// Define global attributes
status = nc_put_att_int(ncid, NC_GLOBAL, "maxlevel", NC_INT, 1, &XParam.maxlevel);
if (status != NC_NOERR) handle_error(status);
status = nc_put_att_int(ncid, NC_GLOBAL, "minlevel", NC_INT, 1, &XParam.minlevel);
if (status != NC_NOERR) handle_error(status);
status = nc_put_att_double(ncid, NC_GLOBAL, "xmin", NC_DOUBLE, 1, &xmin);
if (status != NC_NOERR) handle_error(status);
status = nc_put_att_double(ncid, NC_GLOBAL, "xmax", NC_DOUBLE, 1, &xmax);
if (status != NC_NOERR) handle_error(status);
status = nc_put_att_double(ncid, NC_GLOBAL, "ymin", NC_DOUBLE, 1, &ymin);
if (status != NC_NOERR) handle_error(status);
status = nc_put_att_double(ncid, NC_GLOBAL, "ymax", NC_DOUBLE, 1, &ymax);
if (status != NC_NOERR) handle_error(status);
int time_id, xx_id, yy_id;
int tdim[] = { time_dim };
static size_t tst[] = { 0 };
//status = nc_def_var(ncid, "time", NC_FLOAT, 1, tdim, &time_id);
//if (status != NC_NOERR) handle_error(status);
// For each level Define xx yy
for (int lev = XParam.minlevel; lev <= XParam.maxlevel; lev++)
{
double ddx = calcres(XParam.dx, lev);
nx = (XParam.xmax - XParam.xo) / ddx;
ny = (XParam.ymax - XParam.yo) / ddx;
nxx = nx;
nyy = ny;
//Define dimensions: Name and length
std::string xxname, yyname;
xxname = "xx_" + std::to_string(lev);
yyname = "yy_" + std::to_string(lev);
//printf("lev=%d; xxname=%s; yynam e=%s;\n",lev, xxname.c_str(), yyname.c_str());
//printf("ddx=%f; nxx=%d;\n", ddx, nxx);
//status = nc_def_dim(ncid, xxname.c_str(), nxx, &xx_dim);
//if (status != NC_NOERR) handle_error(status);
//status = nc_def_dim(ncid, yyname.c_str(), nyy, &yy_dim);
//if (status != NC_NOERR) handle_error(status);
//status = nc_def_dim(ncid, "npart",nnpart,&p_dim);
int xdim[] = { xx_dim };
int ydim[] = { yy_dim };
//status = nc_def_var(ncid, xxname.c_str(), NC_FLOAT, 1, xdim, &xx_id);
//if (status != NC_NOERR) handle_error(status);
//status = nc_def_var(ncid, yyname.c_str(), NC_FLOAT, 1, ydim, &yy_id);
//if (status != NC_NOERR) handle_error(status);
//End definitions: leave define mode
}
status = nc_enddef(ncid);
if (status != NC_NOERR) handle_error(status);
//status = nc_close(ncid);
//if (status != NC_NOERR) handle_error(status);
//status = nc_put_var1_double(ncid, time_id, tst, &XParam.totaltime);
//if (status != NC_NOERR) handle_error(status);
/*
std::string xxname, yyname;
for (int lev = XParam.minlevel; lev <= XParam.maxlevel; lev++)
{
double ddx = calcres(XParam.dx, lev);
nx = (XParam.xmax - XParam.xo) / ddx;
ny = (XParam.ymax - XParam.yo) / ddx;
static size_t xcount[] = { nx };
static size_t ycount[] = { ny };
static size_t xstart[] = { 0 }; // start at first value
static size_t ystart[] = { 0 }; // start at first value
static size_t thstart[] = { 0 };
//Recreat the x, y
xval = (float *)malloc(nx*sizeof(float));
yval = (float *)malloc(ny*sizeof(float));
for (int i = 0; i < nx; i++)
{
xval[i] = (float)(XParam.xo + i*ddx);
}
for (int i = 0; i < ny; i++)
{
yval[i] = (float)(XParam.yo + i*ddx);
}
xxname = "xx_" + std::to_string(lev);
yyname = "yy_" + std::to_string(lev);
printf("lev=%d; xxname=%s; yyname=%s;\n", lev, xxname.c_str(), yyname.c_str());
//status = nc_inq_varid(ncid, xxname.c_str(), &xx_id);
//if (status != NC_NOERR) handle_error(status);
//status = nc_inq_varid(ncid, yyname.c_str(), &yy_id);
//if (status != NC_NOERR) handle_error(status);
//Provide values for variables
//status = nc_put_vara_float(ncid, xx_id, xstart, xcount, xval);
//if (status != NC_NOERR) handle_error(status);
//status = nc_put_vara_float(ncid, yy_id, ystart, ycount, yval);
//if (status != NC_NOERR) handle_error(status);
free(xval);
free(yval);
}
*/
//close and save new file
status = nc_close(ncid);
if (status != NC_NOERR) handle_error(status);
return XParam;
}
template <class T> void defncvarBUQ(Param XParam, int * activeblk, int * level, double * blockxo, double *blockyo, std::string varst, int vdim, T * var)
{
std::string outfile = XParam.outfile;
int smallnc = XParam.smallnc;
float scalefactor = XParam.scalefactor;
float addoffset = XParam.addoffset;
//int nx = ceil(XParam.nx / 16.0) * 16.0;
//int ny = ceil(XParam.ny / 16.0) * 16.0;
int status;
int ncid, var_id;
int var_dimid2D[2];
int var_dimid3D[3];
//int var_dimid4D[4];
short *varblk_s;
float * varblk;
int recid, xid, yid;
//size_t ntheta;// nx and ny are stored in XParam not yet for ntheta
float fillval = 9.9692e+36f;
short Sfillval = 32767;
//short fillval = 32767
static size_t start2D[] = { 0, 0 }; // start at first value
//static size_t count2D[] = { ny, nx };
static size_t count2D[] = { 16, 16 };
static size_t start3D[] = { 0, 0, 0 }; // start at first value
//static size_t count3D[] = { 1, ny, nx };
static size_t count3D[] = { 1, 16, 16 };
nc_type VarTYPE;
if (smallnc > 0)
{
VarTYPE = NC_SHORT;
}
else
{
VarTYPE = NC_FLOAT;
}
status = nc_open(outfile.c_str(), NC_WRITE, &ncid);
if (status != NC_NOERR) handle_error(status);
status = nc_redef(ncid);
if (status != NC_NOERR) handle_error(status);
//Inquire dimensions ids
status = nc_inq_unlimdim(ncid, &recid);//time
if (status != NC_NOERR) handle_error(status);
varblk = (float *)malloc(XParam.blksize * sizeof(float));
if (smallnc > 0)
{
varblk_s = (short *)malloc(XParam.blksize * sizeof(short));
}
std::string xxname, yyname, varname,sign;
//generate a different variable name for each level and add attribute as necessary
for (int lev = XParam.minlevel; lev <= XParam.maxlevel; lev++)
{
//std::string xxname, yyname, sign;
lev < 0 ? sign = "N" : sign = "P";
xxname = "xx_" + sign + std::to_string(abs(lev));
yyname = "yy_" + sign + std::to_string(abs(lev));
varname = varst + "_" + sign + std::to_string(abs(lev));
//printf("lev=%d; xxname=%s; yyname=%s;\n", lev, xxname.c_str(), yyname.c_str());
status = nc_inq_dimid(ncid, xxname.c_str(), &xid);
if (status != NC_NOERR) handle_error(status);
status = nc_inq_dimid(ncid, yyname.c_str(), &yid);
if (status != NC_NOERR) handle_error(status);
var_dimid2D[0] = yid;
var_dimid2D[1] = xid;
var_dimid3D[0] = recid;
var_dimid3D[1] = yid;
var_dimid3D[2] = xid;
if (vdim == 2)
{
status = nc_def_var(ncid, varname.c_str(), VarTYPE, vdim, var_dimid2D, &var_id);
if (status != NC_NOERR) handle_error(status);
}
else if (vdim == 3)
{
status = nc_def_var(ncid, varname.c_str(), VarTYPE, vdim, var_dimid3D, &var_id);
if (status != NC_NOERR) handle_error(status);
}
status = nc_put_att_short(ncid, var_id, "_FillValue", VarTYPE, 1, &Sfillval);
if (status != NC_NOERR) handle_error(status);
status = nc_put_att_short(ncid, var_id, "missingvalue", VarTYPE, 1, &Sfillval);
if (status != NC_NOERR) handle_error(status);
if (smallnc > 0)
{
status = nc_put_att_float(ncid, var_id, "scale_factor", NC_FLOAT, 1, &scalefactor);
if (status != NC_NOERR) handle_error(status);
status = nc_put_att_float(ncid, var_id, "add_offset", NC_FLOAT, 1, &addoffset);
if (status != NC_NOERR) handle_error(status);
}
}
// End definition
status = nc_enddef(ncid);
if (status != NC_NOERR) handle_error(status);
// Now write the initial value of the Variable out
int lev, bl;
for (int ibl = 0; ibl < XParam.nblk; ibl++)
{
bl = activeblk[ibl];
lev = level[bl];
double xxmax, xxmin, yymax, yymin;
double initdx = calcres(XParam.dx, XParam.initlevel);
xxmax = XParam.xmax + initdx / 2.0 - calcres(XParam.dx, lev )/2.0;
yymax = XParam.ymax + initdx / 2.0 - calcres(XParam.dx, lev )/2.0;
xxmin = XParam.xo - initdx / 2.0 + calcres(XParam.dx, lev )/2.0;
yymin = XParam.yo - initdx / 2.0 + calcres(XParam.dx, lev )/2.0;
//std::string xxname, yyname, sign;
lev < 0 ? sign = "N" : sign = "P";
xxname = "xx_" + sign + std::to_string(abs(lev));
yyname = "yy_" + sign + std::to_string(abs(lev));
varname = varst + "_" + sign + std::to_string(abs(lev));
status = nc_inq_dimid(ncid, xxname.c_str(), &xid);
if (status != NC_NOERR) handle_error(status);
status = nc_inq_dimid(ncid, yyname.c_str(), &yid);
if (status != NC_NOERR) handle_error(status);