-
Notifications
You must be signed in to change notification settings - Fork 32
/
cmor.c
6753 lines (6109 loc) · 289 KB
/
cmor.c
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
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <uuid/uuid.h>
#include <unistd.h>
#include <string.h>
#include <json-c/json.h>
#include <json-c/json_tokener.h>
#include "cmor.h"
#include "cmor_locale.h"
#include <netcdf.h>
#include <udunits2.h>
#include <time.h>
#include <errno.h>
#include <math.h>
#include <limits.h>
#include <regex.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
/* ==================================================================== */
/* this is defining NETCDF4 variable if we are */
/* using NETCDF3 not used anywhere else */
/* ==================================================================== */
#ifndef NC_NETCDF4
#define NC_NETCDF4 0
#define NC_CLASSIC_MODEL 0
int nc_def_var_deflate(int i, int j, int k, int l, int m)
{
return (0);
};
int nc_def_var_chunking(int i, int j, int k, size_t * l)
{
return (0);
};
#endif
/* -------------------------------------------------------------------- */
/* function declaration */
/* -------------------------------------------------------------------- */
extern int cmor_set_cur_dataset_attribute_internal(char *name,
char *value, int optional);
extern int cmor_set_variable_attribute_internal(int id,
char *attribute_name,
char type, void *value);
extern int cmor_history_contains(int var_id, char *add);
extern void cdCompAdd(cdCompTime comptime,
double value, cdCalenType calendar, cdCompTime * result);
extern void cdCompAddMixed(cdCompTime ct, double value, cdCompTime * result);
/* -------------------------------------------------------------------- */
/* Global Variable */
/* -------------------------------------------------------------------- */
int USE_NETCDF_4;
int cleanup_varid = -1;
const char CMOR_VALID_CALENDARS[CMOR_N_VALID_CALS][CMOR_MAX_STRING] =
{ "gregorian",
"standard",
"proleptic_gregorian",
"noleap",
"365_day",
"360_day",
"julian",
"none"
};
int CMOR_HAS_BEEN_SETUP = 0;
int CMOR_TERMINATE_SIGNAL = -999; /* not set by default */
int CV_ERROR = 0;
ut_system *ut_read = NULL;
FILE *output_logfile;
/* -------------------------------------------------------------------- */
/* Variable related to cmor */
/* -------------------------------------------------------------------- */
cmor_dataset_def cmor_current_dataset;
cmor_table_t cmor_tables[CMOR_MAX_TABLES];
cmor_var_t cmor_vars[CMOR_MAX_VARIABLES];
cmor_axis_t cmor_axes[CMOR_MAX_AXES];
cmor_grid_t cmor_grids[CMOR_MAX_GRIDS];
int CMOR_MODE;
int CMOR_TABLE;
int CMOR_VERBOSITY;
int CMOR_NETCDF_MODE;
int cmor_naxes;
int cmor_nvars;
int cmor_ntables;
int cmor_ngrids;
int cmor_nerrors;
int cmor_nwarnings;
int did_history = 0;
int CMOR_CREATE_SUBDIRECTORIES = 1;
char cmor_input_path[CMOR_MAX_STRING];
char cmor_traceback_info[CMOR_MAX_STRING];
int bAppendMode = 0;
volatile sig_atomic_t stop = 0;
/**************************************************************************/
/* reset signal code
/**************************************************************************/
int cmor_get_terminate_signal() {
return CMOR_TERMINATE_SIGNAL;
}
void cmor_set_terminate_signal_to_sigint() {
CMOR_TERMINATE_SIGNAL = SIGINT;
}
void cmor_set_terminate_signal_to_sigterm() {
CMOR_TERMINATE_SIGNAL = SIGTERM;
}
void cmor_set_terminate_signal( int code) {
CMOR_TERMINATE_SIGNAL = code;
}
/**************************************************************************/
/* cmor_mkdir() */
/**************************************************************************/
void terminate(int signal)
{
if (signal == CMOR_TERMINATE_SIGNAL) {
stop = 1;
}
}
/**************************************************************************/
/* cmor_mkdir() */
/**************************************************************************/
int cmor_mkdir(const char *dir)
{
char tmp[PATH_MAX];
char *p = NULL;
size_t len;
int ierr;
cmor_add_traceback("cmor_mkdir");
snprintf(tmp, sizeof(tmp), "%s", dir);
len = strlen(tmp);
if (tmp[len - 1] == '/')
tmp[len - 1] = 0;
for (p = tmp + 1; *p; p++) {
if (*p == '/') {
*p = 0;
mkdir(tmp, (S_IRWXU | S_IRWXG | S_IRWXO));
*p = '/';
}
}
ierr = mkdir(tmp, (S_IRWXU | S_IRWXG | S_IRWXO));
cmor_pop_traceback();
return (ierr);
}
/**************************************************************************/
/* */
/* cmorstringstring() */
/* */
/**************************************************************************/
int cmor_stringinstring(char *dest, char *src)
{
/* -------------------------------------------------------------------- */
/* returns 1 if dest contains the words of src. */
/* The end of a word is either a space, a period or null. */
/* */
/* This will not give the desired results if */
/* period is used internal to a word. */
/* -------------------------------------------------------------------- */
char *pstr = dest;
cmor_add_traceback("cmor_stringinstring");
while ((pstr = strstr(pstr, src))) {
/* -------------------------------------------------------------------- */
/* if this word is at the beginning of msg or preceeded by a */
/* space */
/* -------------------------------------------------------------------- */
if (pstr == dest || pstr[-1] == ' ') {
/* -------------------------------------------------------------------- */
/* if it isn't a substring match */
/* -------------------------------------------------------------------- */
if ((pstr[strlen(src)] == ' ') ||
(pstr[strlen(src)] == 0) || (pstr[strlen(src)] == '.')) {
/* -------------------------------------------------------------------- */
/* then return( 1 ) to indicate string found */
/* -------------------------------------------------------------------- */
return (1);
}
}
pstr++; /* In which case, skip to the next char */
}
/* -------------------------------------------------------------------- */
/* nothing like src is in dest, and so return the location of the end*/
/* of string */
/* -------------------------------------------------------------------- */
cmor_pop_traceback();
return (0);
}
/************************************************************************/
/* */
/* cmor_cat_unique_string() */
/* */
/************************************************************************/
void cmor_cat_unique_string(char *dest, char *src)
{
int offset;
int spare_space;
cmor_add_traceback("cmor_cat_unique_string");
/* -------------------------------------------------------------------- */
/* if this string is in msg */
/* -------------------------------------------------------------------- */
if (!cmor_stringinstring(dest, src)) {
if ((offset = strlen(dest))) {
strcat(dest + offset, " ");
offset++;
spare_space = CMOR_MAX_STRING - offset - 1;
strncat(dest + offset, src, spare_space);
} else {
strncpy(dest, src, CMOR_MAX_STRING);
}
}
cmor_pop_traceback();
}
/**************************************************************************/
/* */
/* cmor_check_forcing_validity() */
/* */
/**************************************************************************/
int cmor_check_forcing_validity(int table_id, char *value)
{
int i, j, n, found = 0;
char msg[CMOR_MAX_STRING];
char astr[CMOR_MAX_STRING];
char **bstr;
cmor_add_traceback("cmor_check_forcing_validity");
if (cmor_tables[table_id].nforcings == 0) {
cmor_pop_traceback();
return (0);
}
strcpy(astr, value);
found = 0;
for (i = 0; i < strlen(astr); i++) {
if (astr[i] == ',')
astr[i] = ' ';
/* -------------------------------------------------------------------- */
/* removes everything after first parenthesis */
/* -------------------------------------------------------------------- */
if (astr[i] == '(')
astr[i] = '\0';
}
cmor_convert_string_to_list(astr, 'c', (void **)&bstr, &n);
if (n == 0) {
cmor_pop_traceback();
return (0);
}
for (i = 0; i < n; i++) {
found = 0;
for (j = 0; j < cmor_tables[table_id].nforcings; j++) {
if (strcmp(bstr[i], cmor_tables[table_id].forcings[j]) == 0) {
found = 1;
break;
}
}
if (found == 0) {
sprintf(msg, "forcing attribute elt %i (%s) is not valid for\n! "
"table %s, valid values are:", i, bstr[i],
cmor_tables[table_id].szTable_id);
for (j = 0; j < cmor_tables[table_id].nforcings; j++) {
strncat(msg, " ", CMOR_MAX_STRING - strlen(msg));
strncat(msg, cmor_tables[table_id].forcings[j],
CMOR_MAX_STRING - strlen(msg));
strncat(msg, ",", CMOR_MAX_STRING - strlen(msg));
}
msg[strlen(msg) - 1] = '\0';
cmor_handle_error(msg, CMOR_NORMAL);
cmor_pop_traceback();
return (-1);
}
}
/* -------------------------------------------------------------------- */
/* Ok now we need to clean up the memory allocations.... */
/* -------------------------------------------------------------------- */
for (i = 0; i < n; i++) {
free(bstr[i]);
}
free(bstr);
cmor_pop_traceback();
return (0);
}
/**************************************************************************/
/* cmor_check_expt_id() */
/**************************************************************************/
int cmor_check_expt_id(char *szExptID, int nTableID,
char *szGblAttLong, char *szGblAttShort)
{
int i, j;
char szTableExptID[CMOR_MAX_STRING];
char szTableShtExptID[CMOR_MAX_STRING];
cmor_add_traceback("cmor_check_expt_id");
j = 1;
for (i = 0; i <= cmor_tables[nTableID].nexps; i++) {
strncpy(szTableExptID,
cmor_tables[nTableID].expt_ids[i], CMOR_MAX_STRING);
strncpy(szTableShtExptID,
cmor_tables[nTableID].sht_expt_ids[i], CMOR_MAX_STRING);
if ((strncmp(szTableExptID, szExptID, CMOR_MAX_STRING) == 0) ||
(strncmp(szTableShtExptID, szExptID, CMOR_MAX_STRING) == 0)) {
j = 0;
cmor_set_cur_dataset_attribute_internal(szGblAttLong,
szTableExptID, 0);
cmor_set_cur_dataset_attribute_internal(szGblAttShort,
szTableShtExptID, 1);
strncpy(szExptID, szTableShtExptID, CMOR_MAX_STRING);
break;
}
}
cmor_pop_traceback();
return (j);
}
/************************************************************************/
/* strncpytrim() */
/************************************************************************/
int strncpytrim(char *out, char *in, int max)
{
int i, n, j, k;
cmor_add_traceback("strncpytrim");
j = 0;
n = strlen(in);
if (n > max) {
n = max;
}
/* -------------------------------------------------------------------- */
/* Look for first space position and last space position and */
/* copy interval characters in out. */
/* -------------------------------------------------------------------- */
while ((in[j] == ' ') && (j < n)) {
j++;
}
k = n - 1;
while ((in[k] == ' ') && (k > 0)) {
k--;
}
for (i = j; i <= k; i++) {
out[i - j] = in[i];
}
out[i - j] = '\0';
cmor_pop_traceback();
return (0);
}
/************************************************************************/
/* cmor_is_setup() */
/************************************************************************/
void cmor_is_setup(void)
{
extern int CMOR_HAS_BEEN_SETUP;
char msg[CMOR_MAX_STRING];
extern void cmor_handle_error(char error_msg[CMOR_MAX_STRING], int level);
stop = 0;
cmor_add_traceback("cmor_is_setup");
if (CMOR_HAS_BEEN_SETUP == 0) {
snprintf(msg, CMOR_MAX_STRING,
"You need to run cmor_setup before calling any cmor_function");
cmor_handle_error(msg, CMOR_NOT_SETUP);
}
cmor_pop_traceback();
return;
}
/************************************************************************/
/* cmor_add_traceback() */
/************************************************************************/
void cmor_add_traceback(char *name)
{
char tmp[CMOR_MAX_STRING];
if (strlen(cmor_traceback_info) == 0) {
sprintf(cmor_traceback_info, "%s\n! ", name);
} else {
sprintf(tmp, "%s\n! called from: %s", name, cmor_traceback_info);
strncpy(cmor_traceback_info, tmp, CMOR_MAX_STRING);
}
return;
}
/************************************************************************/
/* cmor_pop_traceback() */
/************************************************************************/
void cmor_pop_traceback(void)
{
int i;
char tmp[CMOR_MAX_STRING];
strcpy(tmp, "");
for (i = 0; i < strlen(cmor_traceback_info); i++) {
if (strncmp(&cmor_traceback_info[i], "called from: ", 13) == 0) {
strcpy(tmp, &cmor_traceback_info[i + 13]);
break;
}
}
strcpy(cmor_traceback_info, tmp);
return;
}
/************************************************************************/
/* cmor_have_NetCDF4() */
/************************************************************************/
int cmor_have_NetCDF4(void)
{
char version[50];
int major;
cmor_pop_traceback();
strncpy(version, nc_inq_libvers(), 50);
sscanf(version, "%1d%*s", &major);
if (major != 4) {
cmor_pop_traceback();
return (1);
}
cmor_pop_traceback();
return (0);
}
/************************************************************************/
/* cmor_prep_units() */
/************************************************************************/
int cmor_prep_units(char *uunits,
char *cunits,
ut_unit ** user_units,
ut_unit ** cmor_units, cv_converter ** ut_cmor_converter)
{
extern ut_system *ut_read;
char local_unit[CMOR_MAX_STRING];
char msg[CMOR_MAX_STRING];
extern void cmor_handle_error(char error_msg[CMOR_MAX_STRING], int level);
cmor_add_traceback("cmor_prep_units");
cmor_is_setup();
*cmor_units = ut_parse(ut_read, cunits, UT_ASCII);
if (ut_get_status() != UT_SUCCESS) {
snprintf(msg, CMOR_MAX_STRING,
"Udunits: analyzing units from cmor (%s)", cunits);
cmor_handle_error(msg, CMOR_CRITICAL);
cmor_pop_traceback();
return (1);
}
strncpy(local_unit, uunits, CMOR_MAX_STRING);
ut_trim(local_unit, UT_ASCII);
*user_units = ut_parse(ut_read, local_unit, UT_ASCII);
if (ut_get_status() != UT_SUCCESS) {
snprintf(msg, CMOR_MAX_STRING,
"Udunits: analyzing units from user (%s)", local_unit);
cmor_handle_error(msg, CMOR_CRITICAL);
cmor_pop_traceback();
return (1);
}
if (ut_are_convertible(*cmor_units, *user_units) == 0) {
snprintf(msg, CMOR_MAX_STRING,
"Udunits: cmor and user units are incompatible: %s and %s",
cunits, uunits);
cmor_handle_error(msg, CMOR_CRITICAL);
cmor_pop_traceback();
return (1);
}
*ut_cmor_converter = ut_get_converter(*user_units, *cmor_units);
if (*ut_cmor_converter == NULL) {
}
if (ut_get_status() != UT_SUCCESS) {
snprintf(msg, CMOR_MAX_STRING,
"Udunits: Error getting converter from %s to %s", cunits,
local_unit);
cmor_handle_error(msg, CMOR_CRITICAL);
cmor_pop_traceback();
return (1);
}
cmor_pop_traceback();
return (0);
}
/************************************************************************/
/* cmor_have_NetCDF41min() */
/************************************************************************/
int cmor_have_NetCDF41min(void)
{
char version[50];
int major, minor;
cmor_add_traceback("cmor_have_NetCDF41min");
strncpy(version, nc_inq_libvers(), 50);
sscanf(version, "%1d%*c%1d%*s", &major, &minor);
if (major > 4) {
cmor_pop_traceback();
return (0);
}
if (major < 4) {
cmor_pop_traceback();
return (1);
}
if (minor < 1) {
cmor_pop_traceback();
return (1);
}
cmor_pop_traceback();
return (0);
}
/************************************************************************/
/* cmor_handle_error() */
/************************************************************************/
void cmor_handle_error(char error_msg[CMOR_MAX_STRING], int level)
{
int i;
char msg[CMOR_MAX_STRING];
extern FILE *output_logfile;
if (output_logfile == NULL)
output_logfile = stderr;
msg[0] = '\0';
if (CMOR_VERBOSITY != CMOR_QUIET) {
fprintf(output_logfile, "\n");
}
if (level == CMOR_WARNING) {
cmor_nwarnings++;
if (CMOR_VERBOSITY != CMOR_QUIET) {
#ifdef COLOREDOUTPUT
fprintf(output_logfile, "%c[%d;%d;%dm", 0X1B, 2, 34, 47);
#endif
fprintf(output_logfile, "C Traceback:\nIn function: %s",
cmor_traceback_info);
#ifdef COLOREDOUTPUT
fprintf(output_logfile, "%c[%dm", 0X1B, 0);
#endif
fprintf(output_logfile, "\n\n");
#ifdef COLOREDOUTPUT
fprintf(output_logfile, "%c[%d;%d;%dm", 0X1B, 1, 34, 47);
#endif
snprintf(msg, CMOR_MAX_STRING, "! Warning: %s", error_msg);
}
} else {
cmor_nerrors++;
#ifdef COLOREDOUTPUT
fprintf(output_logfile, "%c[%d;%d;%dm", 0X1B, 2, 31, 47);
#endif
fprintf(output_logfile, "C Traceback:\n! In function: %s",
cmor_traceback_info);
#ifdef COLOREDOUTPUT
fprintf(output_logfile, "%c[%dm", 0X1B, 0);
#endif
fprintf(output_logfile, "\n\n");
#ifdef COLOREDOUTPUT
fprintf(output_logfile, "%c[%d;%d;%dm", 0X1B, 1, 31, 47);
#endif
snprintf(msg, CMOR_MAX_STRING, "! Error: %s", error_msg);
}
// fprintf(stderr, "%s ERROR LEVEL %d\n", error_msg, level);
if (CMOR_VERBOSITY != CMOR_QUIET || level != CMOR_WARNING) {
for (i = 0; i < 25; i++) {
fprintf(output_logfile, "!");
}
fprintf(output_logfile, "\n");
fprintf(output_logfile, "!\n");
fprintf(output_logfile, "%s\n", msg);
fprintf(output_logfile, "!\n");
for (i = 0; i < 25; i++)
fprintf(output_logfile, "!");
#ifdef COLOREDOUTPUT
fprintf(output_logfile, "%c[%dm", 0X1B, 0);
#endif
fprintf(output_logfile, "\n\n");
}
CV_ERROR = 1;
if (level == CMOR_NOT_SETUP) {
exit(1);
}
if ((CMOR_MODE == CMOR_EXIT_ON_WARNING) || (level == CMOR_CRITICAL)) {
fflush(stdout);
fflush(output_logfile);
kill(getpid(), SIGTERM);
}
fflush(output_logfile);
}
void cmor_handle_error_var(char error_msg[CMOR_MAX_STRING], int level,
int var_id)
{
cmor_vars[var_id].error = 1;
cmor_handle_error(error_msg, level);
}
/************************************************************************/
/* cmor_reset_variable() */
/************************************************************************/
void cmor_reset_variable(int var_id)
{
extern cmor_var_t cmor_vars[];
int j;
cmor_vars[var_id].self = -1;
cmor_vars[var_id].grid_id = -1;
cmor_vars[var_id].sign = 1;
cmor_vars[var_id].zfactor = -1;
cmor_vars[var_id].ref_table_id = -1;
cmor_vars[var_id].ref_var_id = -1;
cmor_vars[var_id].initialized = -1;
cmor_vars[var_id].error = 0;
cmor_vars[var_id].closed = 0;
cmor_vars[var_id].nc_var_id = -999;
for (j = 0; j < CMOR_MAX_VARIABLES; j++) {
cmor_vars[var_id].nc_zfactors[j] = -999;
}
cmor_vars[var_id].nzfactor = 0;
cmor_vars[var_id].ntimes_written = 0;
for (j = 0; j < 10; j++) {
cmor_vars[var_id].ntimes_written_coords[j] = -1;
cmor_vars[var_id].associated_ids[j] = -1;
cmor_vars[var_id].ntimes_written_associated[j] = 0;
}
cmor_vars[var_id].time_nc_id = -999;
cmor_vars[var_id].time_bnds_nc_id = -999;
cmor_vars[var_id].id[0] = '\0';
cmor_vars[var_id].ndims = 0;
for (j = 0; j < CMOR_MAX_DIMENSIONS; j++) {
/* -------------------------------------------------------------------- */
/* place holder for singleton axes ids */
/* -------------------------------------------------------------------- */
cmor_vars[var_id].singleton_ids[j] = -1;
cmor_vars[var_id].axes_ids[j] = -1;
cmor_vars[var_id].original_order[j] = -1;
}
for (j = 0; j < CMOR_MAX_ATTRIBUTES; j++) {
cmor_vars[var_id].attributes_values_char[j][0] = '\0';
cmor_vars[var_id].attributes_values_num[j] = -999.;
cmor_vars[var_id].attributes_type[j] = '\0';
cmor_vars[var_id].attributes[j][0] = '\0';
}
cmor_vars[var_id].nattributes = 0;
cmor_vars[var_id].type = '\0';
cmor_vars[var_id].itype = 'N';
cmor_vars[var_id].missing = 1.e20;
cmor_vars[var_id].omissing = 1.e20;
cmor_vars[var_id].tolerance = 1.e-4;
cmor_vars[var_id].valid_min = 1.e20;
cmor_vars[var_id].valid_max = 1.e20;
cmor_vars[var_id].ok_min_mean_abs = 1.e20;
cmor_vars[var_id].ok_max_mean_abs = 1.e20;
cmor_vars[var_id].shuffle = 0;
cmor_vars[var_id].deflate = 1;
cmor_vars[var_id].deflate_level = 1;
cmor_vars[var_id].nomissing = 1;
cmor_vars[var_id].iunits[0] = '\0';
cmor_vars[var_id].ounits[0] = '\0';
cmor_vars[var_id].isbounds = 0;
cmor_vars[var_id].needsinit = 1;
cmor_vars[var_id].zaxis = -1;
if (cmor_vars[var_id].values != NULL) {
free(cmor_vars[var_id].values);
}
cmor_vars[var_id].values = NULL;
cmor_vars[var_id].first_time = -999.;
cmor_vars[var_id].last_time = -999.;
cmor_vars[var_id].first_bound = 1.e20;
cmor_vars[var_id].last_bound = 1.e20;
cmor_vars[var_id].base_path[0] = '\0';
cmor_vars[var_id].current_path[0] = '\0';
cmor_vars[var_id].suffix[0] = '\0';
cmor_vars[var_id].suffix_has_date = 0;
cmor_vars[var_id].frequency[0] = '\0';
}
/************************************************************************/
/* cmor_setup() */
/************************************************************************/
int cmor_setup(char *path,
int *netcdf,
int *verbosity,
int *mode, char *logfile, int *create_subdirectories)
{
extern cmor_axis_t cmor_axes[];
extern int CMOR_TABLE, cmor_ntables;
extern ut_system *ut_read;
extern cmor_dataset_def cmor_current_dataset;
ut_unit *dimlessunit = NULL, *perunit = NULL, *newequnit = NULL;
ut_status myutstatus;
ut_unit *PracticalSSunit = NULL;
ut_unit *pss78unit = NULL;
ut_unit *psuunit = NULL;
int i, j;
char msg[CMOR_MAX_STRING];
char msg2[CMOR_MAX_STRING];
char tmplogfile[CMOR_MAX_STRING];
time_t lt;
struct stat buf;
struct tm *ptr;
extern FILE *output_logfile;
extern int did_history;
struct sigaction action;
/*********************************************************/
/* set the default signal */
/*********************************************************/
if (CMOR_TERMINATE_SIGNAL == -999) {
CMOR_TERMINATE_SIGNAL = SIGTERM;
}
memset(&action, 0, sizeof(struct sigaction));
action.sa_handler = terminate;
sigaction(CMOR_TERMINATE_SIGNAL, &action, NULL);
strcpy(cmor_traceback_info, "");
cmor_add_traceback("cmor_setup");
/* -------------------------------------------------------------------- */
/* ok we need to know if we are using NC3 or 4 */
/* -------------------------------------------------------------------- */
USE_NETCDF_4 = -1;
if (cmor_have_NetCDF4() == 0) {
USE_NETCDF_4 = 1;
if (cmor_have_NetCDF41min() != 0) {
sprintf(msg,
"You are using a wrong version of NetCDF4 (%s), \n! "
"you need 4.1", nc_inq_libvers());
cmor_handle_error(msg, CMOR_CRITICAL);
}
} else {
sprintf(msg,
"You are using a wrong version of NetCDF (%s), you need \n! "
"4.1 or an earlier netCDF release.", nc_inq_libvers());
cmor_handle_error(msg, CMOR_CRITICAL);
}
did_history = 0;
CMOR_HAS_BEEN_SETUP = 1;
CMOR_TABLE = -1;
cmor_ngrids = -1;
cmor_nvars = -1;
cmor_naxes = -1;
cmor_ntables = -1;
cmor_nerrors = 0;
cmor_nwarnings = 0;
// Define mode
if (mode == NULL) {
CMOR_MODE = CMOR_NORMAL;
} else {
if (*mode != CMOR_EXIT_ON_WARNING && *mode != CMOR_EXIT_ON_MAJOR
&& *mode != CMOR_NORMAL) {
snprintf(msg, CMOR_MAX_STRING,
"exit mode can be either CMOR_EXIT_ON_WARNING CMOR_NORMAL "
"or CMOR_EXIT_ON_MAJOR");
cmor_handle_error(msg, CMOR_CRITICAL);
}
CMOR_MODE = *mode;
}
// Define verbosity
if (verbosity == NULL) {
CMOR_VERBOSITY = CMOR_NORMAL;
} else {
if (*verbosity != CMOR_QUIET && *verbosity != CMOR_NORMAL) {
snprintf(msg, CMOR_MAX_STRING,
"verbosity mode can be either CMOR_QUIET or CMOR_NORMAL");
cmor_handle_error(msg, CMOR_NORMAL);
}
CMOR_VERBOSITY = *verbosity;
}
if (logfile == NULL) {
output_logfile = NULL;
} else {
#ifdef COLOREDOUTPUT
#undef COLOREDOUTPUT
#endif
cmor_trim_string(logfile, tmplogfile);
output_logfile = NULL;
output_logfile = fopen(tmplogfile, "r");
if (output_logfile != NULL) {
/* -------------------------------------------------------------------- */
/* logfile already exists need to rename it */
/* Figure out the time */
/* -------------------------------------------------------------------- */
stat(tmplogfile, &buf);
lt = buf.st_ctime;
ptr = localtime(<);
snprintf(msg, CMOR_MAX_STRING, "%s_%.4i-%.2i-%.2iT%.2i:%.2i:%.2i",
tmplogfile, ptr->tm_year + 1900, ptr->tm_mon + 1,
ptr->tm_mday, ptr->tm_hour, ptr->tm_min, ptr->tm_sec);
fclose(output_logfile);
rename(tmplogfile, msg);
snprintf(msg2, CMOR_MAX_STRING,
"Logfile %s already exist.\n! Renamed to: %s", tmplogfile,
msg);
output_logfile = NULL;
output_logfile = fopen(tmplogfile, "w");
if (output_logfile == NULL) {
snprintf(msg2, CMOR_MAX_STRING,
"Could not open logfile %s for writing", tmplogfile);
cmor_handle_error(msg2, CMOR_CRITICAL);
}
cmor_handle_error(msg2, CMOR_WARNING);
} else {
output_logfile = fopen(tmplogfile, "w");
if (output_logfile == NULL) {
snprintf(msg2, CMOR_MAX_STRING,
"Could not open logfile %s for writing", tmplogfile);
cmor_handle_error(msg2, CMOR_CRITICAL);
}
}
}
if (netcdf == NULL) {
CMOR_NETCDF_MODE = CMOR_PRESERVE;
} else {
if (*netcdf != CMOR_PRESERVE_4 && *netcdf != CMOR_APPEND_4
&& *netcdf != CMOR_REPLACE_4 && *netcdf != CMOR_PRESERVE_3
&& *netcdf != CMOR_APPEND_3 && *netcdf != CMOR_REPLACE_3) {
snprintf(msg, CMOR_MAX_STRING,
"file mode can be either CMOR_PRESERVE, CMOR_APPEND, "
"CMOR_REPLACE, CMOR_PRESERVE_4, CMOR_APPEND_4, "
"CMOR_REPLACE_4, CMOR_PRESERVE_3, CMOR_APPEND_3 or "
"CMOR_REPLACE_3");
cmor_handle_error(msg, CMOR_CRITICAL);
}
CMOR_NETCDF_MODE = *netcdf;
}
/* Make sure we are not trying to use NETCDF4 mode while linked against NetCDF3 */
if (((CMOR_NETCDF_MODE == CMOR_PRESERVE_4)
|| (CMOR_NETCDF_MODE == CMOR_REPLACE_4)
|| (CMOR_NETCDF_MODE == CMOR_APPEND_4)) && (USE_NETCDF_4 == 0)) {
sprintf(msg,
"You are trying to use a NetCDF4 mode but linked against "
"NetCDF3 libraries");
cmor_handle_error(msg, CMOR_CRITICAL);
}
if ((path == NULL) || (strcmp(path, "") == 0)) {
strncpy(cmor_input_path, ".", CMOR_MAX_STRING);
} else {
strncpytrim(cmor_input_path, path, CMOR_MAX_STRING);
}
for (i = 0; i < CMOR_MAX_VARIABLES; i++) {
cmor_reset_variable(i);
}
for (i = 0; i < CMOR_MAX_AXES; i++) {
cmor_axes[i].ref_table_id = -1;
cmor_axes[i].ref_axis_id = -1;
cmor_axes[i].isgridaxis = -1;
cmor_axes[i].axis = '\0';
cmor_axes[i].iunits[0] = '\0';
cmor_axes[i].id[0] = '\0';
if (cmor_axes[i].values != NULL)
free(cmor_axes[i].values);
cmor_axes[i].values = NULL;
if (cmor_axes[i].bounds != NULL)
free(cmor_axes[i].bounds);
cmor_axes[i].bounds = NULL;
if (cmor_axes[i].cvalues != NULL) {
for (j = 0; j < cmor_axes[i].length; j++) {
if (cmor_axes[i].cvalues[j] != NULL) {
free(cmor_axes[i].cvalues[j]);
cmor_axes[i].cvalues[j] = NULL;
}
}
free(cmor_axes[i].cvalues);
}
cmor_axes[i].cvalues = NULL;
cmor_axes[i].length = 0;
cmor_axes[i].revert = 1; /* 1 means no reverse -1 means reverse */
cmor_axes[i].offset = 0;
cmor_axes[i].type = '\0';
for (j = 0; j < CMOR_MAX_ATTRIBUTES; j++) {
cmor_axes[i].attributes_values_char[j][0] = '\0';
cmor_axes[i].attributes_values_num[j] = -999.;
cmor_axes[i].attributes_type[j] = '\0';
cmor_axes[i].attributes[j][0] = '\0';
}
cmor_axes[i].nattributes = 0;
cmor_axes[i].hybrid_in = 0;
cmor_axes[i].hybrid_out = 0;
cmor_axes[i].store_in_netcdf = 1;
cmor_axes[i].wrapping = NULL;
cmor_set_axis_attribute(i, "units", 'c', "");
cmor_set_axis_attribute(i, "interval", 'c', "");
}
if (create_subdirectories != NULL) {
CMOR_CREATE_SUBDIRECTORIES = *create_subdirectories;
}
if ((CMOR_CREATE_SUBDIRECTORIES != 1)
&& (CMOR_CREATE_SUBDIRECTORIES != 0)) {
snprintf(msg, CMOR_MAX_STRING,
"cmor_setup: create_subdirectories must be 0 or 1");
cmor_handle_error(msg, CMOR_CRITICAL);