-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathcmor_CV.c
2493 lines (2264 loc) · 101 KB
/
cmor_CV.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
#define _XOPEN_SOURCE
#define _GNU_SOURCE
#include <string.h>
#include <time.h>
#include <regex.h>
#include "cmor.h"
#include <json-c/json.h>
#include <json-c/json_tokener.h>
#include <json-c/arraylist.h>
#include "libgen.h"
#include "math.h"
#ifndef REG_NOERROR
#define REG_NOERROR 0
#endif
extern void cdCompAdd(cdCompTime comptime,
double value, cdCalenType calendar, cdCompTime * result);
extern void cdCompAddMixed(cdCompTime ct, double value, cdCompTime * result);
/************************************************************************/
/* cmor_CV_set_att() */
/************************************************************************/
void cmor_CV_set_att(cmor_CV_def_t * CV, char *szKey, json_object * joValue)
{
json_bool bValue;
double dValue;
int nValue;
array_list *pArray;
int k, length;
strcpy(CV->key, szKey);
if (json_object_is_type(joValue, json_type_null)) {
printf("Will not save NULL JSON type from CV.json\n");
} else if (json_object_is_type(joValue, json_type_boolean)) {
bValue = json_object_get_boolean(joValue);
CV->nValue = (int)bValue;
CV->type = CV_integer;
} else if (json_object_is_type(joValue, json_type_double)) {
dValue = json_object_get_double(joValue);
CV->dValue = dValue;
CV->type = CV_double;
} else if (json_object_is_type(joValue, json_type_int)) {
nValue = json_object_get_int(joValue);
CV->nValue = nValue;
CV->type = CV_integer;
/* -------------------------------------------------------------------- */
/* if value is a JSON object, recursively call in this function */
/* -------------------------------------------------------------------- */
} else if (json_object_is_type(joValue, json_type_object)) {
int nCVId = -1;
int nbObject = 0;
cmor_CV_def_t *oValue;
int nTableID = CV->table_id; /* Save Table ID */
json_object_object_foreach(joValue, key, value) {
nbObject++;
oValue = (cmor_CV_def_t *) realloc(CV->oValue,
sizeof(cmor_CV_def_t) *
nbObject);
CV->oValue = oValue;
nCVId++;
cmor_CV_init(&CV->oValue[nCVId], nTableID);
cmor_CV_set_att(&CV->oValue[nCVId], key, value);
}
CV->nbObjects = nbObject;
CV->type = CV_object;
} else if (json_object_is_type(joValue, json_type_array)) {
int i;
pArray = json_object_get_array(joValue);
length = array_list_length(pArray);
CV->aszValue = (char **) malloc(length * sizeof(char**));
for(i=0; i < length; i++) {
CV->aszValue[i] = (char *) malloc(sizeof(char) * CMOR_MAX_STRING);
}
json_object *joItem;
CV->anElements = length;
for (k = 0; k < length; k++) {
joItem = (json_object *) array_list_get_idx(pArray, k);
strcpy(CV->aszValue[k], json_object_get_string(joItem));
}
CV->type = CV_stringarray;
} else if (json_object_is_type(joValue, json_type_string)) {
strcpy(CV->szValue, json_object_get_string(joValue));
CV->type = CV_string;
}
}
/************************************************************************/
/* cmor_CV_init() */
/************************************************************************/
void cmor_CV_init(cmor_CV_def_t * CV, int table_id)
{
cmor_is_setup();
cmor_add_traceback("_init_CV_def");
CV->table_id = table_id;
CV->type = CV_undef; //undefined
CV->nbObjects = -1;
CV->nValue = -1;
CV->key[0] = '\0';
CV->szValue[0] = '\0';
CV->dValue = -9999.9;
CV->oValue = NULL;
CV->aszValue = NULL;
CV->anElements = -1;
cmor_pop_traceback();
}
/************************************************************************/
/* cmor_CV_print() */
/************************************************************************/
void cmor_CV_print(cmor_CV_def_t * CV)
{
int k;
if (CV != NULL) {
if (CV->key[0] != '\0') {
printf("key: %s \n", CV->key);
} else {
return;
}
switch (CV->type) {
case CV_string:
printf("value: %s\n", CV->szValue);
break;
case CV_integer:
printf("value: %d\n", CV->nValue);
break;
case CV_stringarray:
printf("value: [\n");
for (k = 0; k < CV->anElements; k++) {
printf("value: %s\n", CV->aszValue[k]);
}
printf(" ]\n");
break;
case CV_double:
printf("value: %lf\n", CV->dValue);
break;
case CV_object:
printf("*** nbObjects=%d\n", CV->nbObjects);
for (k = 0; k < CV->nbObjects; k++) {
cmor_CV_print(&CV->oValue[k]);
}
break;
case CV_undef:
break;
}
}
}
/************************************************************************/
/* cmor_CV_printall() */
/************************************************************************/
void cmor_CV_printall()
{
int j, i;
cmor_CV_def_t *CV;
int nCVs;
/* -------------------------------------------------------------------- */
/* Parse tree and print key values. */
/* -------------------------------------------------------------------- */
for (i = 0; i < CMOR_MAX_TABLES; i++) {
if (cmor_tables[i].CV != NULL) {
printf("table %s\n", cmor_tables[i].szTable_id);
nCVs = cmor_tables[i].CV->nbObjects;
for (j = 0; j <= nCVs; j++) {
CV = &cmor_tables[i].CV[j];
cmor_CV_print(CV);
}
}
}
}
/************************************************************************/
/* cmor_CV_set_dataset_attr_from_key() */
/************************************************************************/
cmor_CV_def_t *cmor_CV_set_dataset_attr_from_key(cmor_CV_def_t * CV,
char *key)
{
int i;
cmor_CV_def_t *searchCV;
int nbCVs = -1;
cmor_add_traceback("_CV_search_child_key");
nbCVs = CV->nbObjects;
// Look at this objects
if (strcmp(CV->key, key) == 0) {
cmor_pop_traceback();
return (CV);
}
// Look at each of object key
for (i = 0; i < nbCVs; i++) {
// Is there a branch on that object?
if (&CV->oValue[i] != NULL) {
searchCV = cmor_CV_set_dataset_attr_from_key(&CV->oValue[i], key);
if (searchCV != NULL) {
cmor_pop_traceback();
return (searchCV);
}
}
}
cmor_pop_traceback();
return (NULL);
}
/************************************************************************/
/* cmor_CV_search_child_key() */
/************************************************************************/
cmor_CV_def_t *cmor_CV_search_child_key(cmor_CV_def_t * CV, char *key)
{
int i;
cmor_CV_def_t *searchCV;
int nbCVs = -1;
cmor_add_traceback("_CV_search_child_key");
nbCVs = CV->nbObjects;
// Look at this objects
if (strcmp(CV->key, key) == 0) {
cmor_pop_traceback();
return (CV);
}
// Look at each of object key
for (i = 0; i < nbCVs; i++) {
// Is there a branch on that object?
if (&CV->oValue[i] != NULL) {
searchCV = cmor_CV_search_child_key(&CV->oValue[i], key);
if (searchCV != NULL) {
cmor_pop_traceback();
return (searchCV);
}
}
}
cmor_pop_traceback();
return (NULL);
}
/************************************************************************/
/* cmor_CV_rootsearch() */
/************************************************************************/
cmor_CV_def_t *cmor_CV_rootsearch(cmor_CV_def_t * CV, char *key)
{
int i;
int nbCVs = -1;
cmor_add_traceback("_CV_rootsearch");
// Look at first objects
if (strcmp(CV->key, key) == 0) {
cmor_pop_traceback();
return (CV);
}
// Is there more than 1 object?
if (CV->nbObjects != -1) {
nbCVs = CV->nbObjects;
}
// Look at each of object key
for (i = 1; i < nbCVs; i++) {
if (strcmp(CV[i].key, key) == 0) {
cmor_pop_traceback();
return (&CV[i]);
}
}
cmor_pop_traceback();
return (NULL);
}
/************************************************************************/
/* cmor_CV_get_value() */
/************************************************************************/
char *cmor_CV_get_value(cmor_CV_def_t * CV, char *key)
{
switch (CV->type) {
case CV_string:
return (CV->szValue);
break;
case CV_integer:
sprintf(CV->szValue, "%d", CV->nValue);
break;
case CV_stringarray:
return (CV->aszValue[0]);
break;
case CV_double:
sprintf(CV->szValue, "%lf", CV->dValue);
break;
case CV_object:
return (NULL);
break;
case CV_undef:
CV->szValue[0] = '\0';
break;
}
return (CV->szValue);
}
/************************************************************************/
/* cmor_CV_free() */
/************************************************************************/
void cmor_CV_free(cmor_CV_def_t * CV)
{
int k;
int i;
int length;
/* -------------------------------------------------------------------- */
/* Free allocated double string array */
/* -------------------------------------------------------------------- */
length = CV->anElements;
if(length != 0) {
for(i=0; i < length; i++) {
free(CV->aszValue[i]);
}
free(CV->aszValue);
}
/* -------------------------------------------------------------------- */
/* Recursively go down the tree and free branch */
/* -------------------------------------------------------------------- */
if (CV->oValue != NULL) {
for (k = 0; k < CV->nbObjects; k++) {
cmor_CV_free(&CV->oValue[k]);
}
free(CV->oValue);
CV->oValue = NULL;
}
}
/************************************************************************/
/* cmor_CV_checkFurtherInfoURL() */
/************************************************************************/
int cmor_CV_checkFurtherInfoURL(int nVarRefTblID)
{
char szFurtherInfoURLTemplate[CMOR_MAX_STRING];
char szFurtherInfoURL[CMOR_MAX_STRING];
char copyURL[CMOR_MAX_STRING];
char szFurtherInfoBaseURL[CMOR_MAX_STRING];
char szFurtherInfoFileURL[CMOR_MAX_STRING];
char szValue[CMOR_MAX_STRING];
char msg[CMOR_MAX_STRING];
char CV_Filename[CMOR_MAX_STRING];
char *baseURL;
char *fileURL;
char *szToken;
szFurtherInfoURL[0] = '\0';
szFurtherInfoFileURL[0] = '\0';
szFurtherInfoBaseURL[0] = '\0';
cmor_is_setup();
cmor_add_traceback("_CV_checkFurtherInfoURL");
/* -------------------------------------------------------------------- */
/* Retrieve default Further URL info */
/* -------------------------------------------------------------------- */
strncpy(szFurtherInfoURLTemplate, cmor_current_dataset.furtherinfourl,
CMOR_MAX_STRING);
/* -------------------------------------------------------------------- */
/* If this is a string with no token we have nothing to do. */
/* -------------------------------------------------------------------- */
szToken = strtok(szFurtherInfoURLTemplate, "<>");
if (strcmp(szToken, cmor_current_dataset.furtherinfourl) == 0) {
return (0);
}
strncpy(szFurtherInfoURLTemplate, cmor_current_dataset.furtherinfourl,
CMOR_MAX_STRING);
/* -------------------------------------------------------------------- */
/* Separate path template from file template. */
/* -------------------------------------------------------------------- */
strcpy(copyURL, szFurtherInfoURLTemplate);
baseURL = dirname(copyURL);
cmor_CreateFromTemplate(nVarRefTblID, baseURL, szFurtherInfoBaseURL, "/");
strcpy(copyURL, szFurtherInfoURLTemplate);
fileURL = basename(copyURL);
cmor_CreateFromTemplate(nVarRefTblID, fileURL, szFurtherInfoFileURL, ".");
strncpy(szFurtherInfoURL, szFurtherInfoBaseURL, CMOR_MAX_STRING);
strcat(szFurtherInfoURL, "/");
strncat(szFurtherInfoURL, szFurtherInfoFileURL,
strlen(szFurtherInfoFileURL));
if (cmor_has_cur_dataset_attribute(GLOBAL_ATT_FURTHERINFOURL) == 0) {
cmor_get_cur_dataset_attribute(GLOBAL_ATT_FURTHERINFOURL, szValue);
if (strncmp(szFurtherInfoURL, szValue, CMOR_MAX_STRING) != 0) {
cmor_get_cur_dataset_attribute(CV_INPUTFILENAME, CV_Filename);
snprintf(msg, CMOR_MAX_STRING,
"The further info in attribute does not match "
"the one found in your Control Vocabulary(CV) File. \n! "
"We found \"%s\" and \n! "
"CV requires \"%s\" \n! "
"Check your Control Vocabulary file \"%s\".\n! ",
szValue, szFurtherInfoURL, CV_Filename);
cmor_handle_error(msg, CMOR_WARNING);
cmor_pop_traceback();
return (-1);
}
}
cmor_set_cur_dataset_attribute_internal(GLOBAL_ATT_FURTHERINFOURL,
szFurtherInfoURL, 0);
cmor_pop_traceback();
return (0);
}
/************************************************************************/
/* get_CV_Error() */
/************************************************************************/
int get_CV_Error()
{
return (CV_ERROR);
}
/************************************************************************/
/* cmor_CV_checkSourceType() */
/************************************************************************/
int cmor_CV_checkSourceType(cmor_CV_def_t * CV_exp, char *szExptID)
{
int nObjects;
cmor_CV_def_t *CV_exp_attr;
char szAddSourceType[CMOR_MAX_STRING];
char szReqSourceType[CMOR_MAX_STRING];
char szAddSourceTypeCpy[CMOR_MAX_STRING];
char szReqSourceTypeCpy[CMOR_MAX_STRING];
char szSourceType[CMOR_MAX_STRING];
char msg[CMOR_MAX_STRING];
char CV_Filename[CMOR_MAX_STRING];
int i, j;
char *szTokenRequired;
char *szTokenAdd;
int nbSourceType;
char *ptr;
int nbGoodType;
regex_t regex;
int reti;
cmor_add_traceback("_CV_checkSourceType");
szAddSourceType[0] = '\0';
szReqSourceType[0] = '\0';
szAddSourceTypeCpy[0] = '\0';
szReqSourceTypeCpy[0] = '\0';
szSourceType[0] = '\0';
nbGoodType = 0;
nbSourceType = -1;
cmor_get_cur_dataset_attribute(CV_INPUTFILENAME, CV_Filename);
szAddSourceType[0] = '\0';
nObjects = CV_exp->nbObjects;
// Parse all experiment attributes get source type related attr.
for (i = 0; i < nObjects; i++) {
CV_exp_attr = &CV_exp->oValue[i];
if (strcmp(CV_exp_attr->key, CV_EXP_ATTR_ADDSOURCETYPE) == 0) {
for (j = 0; j < CV_exp_attr->anElements; j++) {
strcat(szAddSourceType, CV_exp_attr->aszValue[j]);
strcat(szAddSourceType, " ");
strcat(szAddSourceTypeCpy, CV_exp_attr->aszValue[j]);
strcat(szAddSourceTypeCpy, " ");
}
continue;
}
if (strcmp(CV_exp_attr->key, CV_EXP_ATTR_REQSOURCETYPE) == 0) {
for (j = 0; j < CV_exp_attr->anElements; j++) {
strcat(szReqSourceType, CV_exp_attr->aszValue[j]);
strcat(szReqSourceType, " ");
strcat(szReqSourceTypeCpy, CV_exp_attr->aszValue[j]);
strcat(szReqSourceTypeCpy, " ");
}
continue;
}
}
if (cmor_has_cur_dataset_attribute(GLOBAL_ATT_SOURCE_TYPE) == 0) {
cmor_get_cur_dataset_attribute(GLOBAL_ATT_SOURCE_TYPE, szSourceType);
// Count how many are token we have.
ptr = szSourceType;
if (szSourceType[0] != '\0') {
nbSourceType = 1;
} else {
cmor_pop_traceback();
return (-1);
}
while ((ptr = strpbrk(ptr, " ")) != NULL) {
nbSourceType++;
ptr++;
}
}
szTokenRequired = strtok(szReqSourceType, " ");
while (szTokenRequired != NULL) {
reti = regcomp(®ex, szTokenRequired, REG_EXTENDED);
if (reti) {
snprintf(msg, CMOR_MAX_STRING,
"You regular expression \"%s\" is invalid. \n! "
"Please refer to the CMIP6 documentations.\n! ",
szTokenRequired);
regfree(®ex);
cmor_handle_error(msg, CMOR_NORMAL);
cmor_pop_traceback();
return (-1);
}
/* -------------------------------------------------------------------- */
/* Execute regular expression */
/* -------------------------------------------------------------------- */
reti = regexec(®ex, szSourceType, 0, NULL, 0);
if (reti == REG_NOMATCH) {
snprintf(msg, CMOR_MAX_STRING,
"The following source type(s) \"%s\" are required and\n! "
"some source type(s) could not be found in your "
"input file. \n! "
"Your file contains a source type of \"%s\".\n! "
"Check your Control Vocabulary file \"%s\".\n! ",
szReqSourceTypeCpy, szSourceType, CV_Filename);
regfree(®ex);
cmor_handle_error(msg, CMOR_NORMAL);
} else {
nbGoodType++;
}
regfree(®ex);
szTokenRequired = strtok(NULL, " ");
}
szTokenAdd = strtok(szAddSourceType, " ");
while (szTokenAdd != NULL) {
// Is the token "CHEM"?
if (strcmp(szTokenAdd, "CHEM") == 0) {
reti = regcomp(®ex, szTokenAdd, REG_EXTENDED);
if (regexec(®ex, szSourceType, 0, NULL, 0) == REG_NOERROR) {
nbGoodType++;
};
} else if (strcmp(szTokenAdd, "AER") == 0) {
regfree(®ex);
// Is the token "AER"?
reti = regcomp(®ex, szTokenAdd, REG_EXTENDED);
if (regexec(®ex, szSourceType, 0, NULL, 0) == REG_NOERROR) {
nbGoodType++;
};
} else {
regfree(®ex);
// Is the token in the list of AddSourceType?
reti = regcomp(®ex, szTokenAdd, REG_EXTENDED);
if (regexec(®ex, szSourceType, 0, NULL, 0) == REG_NOERROR) {
nbGoodType++;
};
}
szTokenAdd = strtok(NULL, " ");
regfree(®ex);
}
if (nbGoodType != nbSourceType) {
snprintf(msg, CMOR_MAX_STRING,
"You source_type attribute contains invalid source types\n! "
"Your source type is set to \"%s\". The required source types\n! "
"are \"%s\" and possible additional source types are \"%s\" \n! "
"Check your Control Vocabulary file \"%s\".\n! ",
szSourceType, szReqSourceTypeCpy, szAddSourceTypeCpy,
CV_Filename);
cmor_handle_error(msg, CMOR_NORMAL);
cmor_pop_traceback();
return (-1);
}
cmor_pop_traceback();
return (0);
}
/************************************************************************/
/* cmor_CV_checkSourceID() */
/************************************************************************/
int cmor_CV_checkSourceID(cmor_CV_def_t * CV)
{
cmor_CV_def_t *CV_source_ids;
cmor_CV_def_t *CV_source_id = NULL;
char szSource_ID[CMOR_MAX_STRING];
char szSource[CMOR_MAX_STRING];
char szSubstring[CMOR_MAX_STRING];
char *pos;
int nLen;
char msg[CMOR_MAX_STRING];
char CV_Filename[CMOR_MAX_STRING];
int rc;
int i;
int j = 0;
cmor_is_setup();
cmor_add_traceback("_CV_checkSourceID");
cmor_get_cur_dataset_attribute(CV_INPUTFILENAME, CV_Filename);
/* -------------------------------------------------------------------- */
/* Find experiment_ids dictionary in Control Vocabulary */
/* -------------------------------------------------------------------- */
CV_source_ids = cmor_CV_rootsearch(CV, CV_KEY_SOURCE_IDS);
if (CV_source_ids == NULL) {
snprintf(msg, CMOR_MAX_STRING,
"Your \"source_ids\" key could not be found in\n! "
"your Control Vocabulary file.(%s)\n! ", CV_Filename);
cmor_handle_error(msg, CMOR_NORMAL);
cmor_pop_traceback();
return (-1);
}
// retrieve source_id
rc = cmor_get_cur_dataset_attribute(GLOBAL_ATT_SOURCE_ID, szSource_ID);
if (rc != 0) {
snprintf(msg, CMOR_MAX_STRING,
"Your \"%s\" is not defined, check your required attributes\n! "
"See Control Vocabulary JSON file.(%s)\n! ",
GLOBAL_ATT_SOURCE_ID, CV_Filename);
cmor_handle_error(msg, CMOR_NORMAL);
cmor_pop_traceback();
return (-1);
}
// Validate source_id
for (i = 0; i < CV_source_ids->nbObjects; i++) {
CV_source_id = &CV_source_ids->oValue[i];
if (strncmp(CV_source_id->key, szSource_ID, CMOR_MAX_STRING) == 0) {
// Make sure that "source" exist.
if (cmor_has_cur_dataset_attribute(GLOBAL_ATT_SOURCE) != 0) {
cmor_set_cur_dataset_attribute_internal(GLOBAL_ATT_SOURCE,
CV_source_id->szValue, 1);
}
// Check source with experiment_id label.
rc = cmor_get_cur_dataset_attribute(GLOBAL_ATT_SOURCE, szSource);
if(CV_source_id->nbObjects == -1) {
snprintf(msg, CMOR_MAX_STRING,
"You did not define a %s section in your source_id %s.\n! \n! \n! "
"See Control Vocabulary JSON file. (%s)\n! ",
CV_KEY_SOURCE_LABEL, szSource_ID, CV_Filename);
cmor_handle_error(msg, CMOR_WARNING);
return(1);
break;
}
for (j = 0; j < CV_source_id->nbObjects; j++) {
if (strcmp(CV_source_id->oValue[j].key, CV_KEY_SOURCE_LABEL) ==
0) {
break;
}
}
if (j == CV_source_id->nbObjects) {
snprintf(msg, CMOR_MAX_STRING,
"Could not find %s string in source_id section.\n! \n! \n! "
"See Control Vocabulary JSON file. (%s)\n! ",
CV_KEY_SOURCE_LABEL, CV_Filename);
cmor_handle_error(msg, CMOR_WARNING);
break;
}
pos = strchr(CV_source_id->oValue[j].szValue, ')');
strncpy(szSubstring, CV_source_id->oValue[j].szValue,
CMOR_MAX_STRING);
nLen = strlen(CV_source_id->oValue[j].szValue);
if (pos != 0) {
nLen = pos - CV_source_id->oValue[j].szValue + 1;
}
szSubstring[nLen] = '\0';
if (strncmp(szSubstring, szSource, nLen) != 0) {
snprintf(msg, CMOR_MAX_STRING,
"Your input attribute \"%s\" with value \n! \"%s\" "
"will be replaced with "
"value \n! \"%s\".\n! \n! \n! "
"See Control Vocabulary JSON file.(%s)\n! ",
GLOBAL_ATT_SOURCE, szSource,
CV_source_id->oValue[j].szValue, CV_Filename);
cmor_handle_error(msg, CMOR_WARNING);
}
break;
}
}
// We could not found the Source ID in the CV file
if (i == CV_source_ids->nbObjects) {
snprintf(msg, CMOR_MAX_STRING,
"The source_id, \"%s\", which you specified in your \n! "
"input file could not be found in \n! "
"your Controlled Vocabulary file. (%s) \n! \n! "
"Please correct your input file or contact "
"cmor@listserv.llnl.gov to register\n! "
"a new source. ", szSource_ID, CV_Filename);
cmor_handle_error(msg, CMOR_NORMAL);
return (-1);
}
// Set/replace attribute.
cmor_set_cur_dataset_attribute_internal(GLOBAL_ATT_SOURCE_ID,
CV_source_id->key, 1);
cmor_set_cur_dataset_attribute_internal(GLOBAL_ATT_SOURCE,
CV_source_id->oValue[j].szValue, 1);
cmor_pop_traceback();
return (0);
}
/************************************************************************/
/* CV_VerifyNBElement() */
/************************************************************************/
int CV_VerifyNBElement(cmor_CV_def_t * CV)
{
char msg[CMOR_MAX_STRING];
char CV_Filename[CMOR_MAX_STRING];
cmor_get_cur_dataset_attribute(CV_INPUTFILENAME, CV_Filename);
cmor_add_traceback("_CV_VerifyNBElement");
// printf("**** CV->key: %s\n", CV->key);
// printf("**** CV->anElement: %d\n", CV->anElements);
// printf("**** CV->asValue: %s\n", CV->aszValue[0]);
// printf("**** CV->szValue: %s\n", CV->szValue);
if (CV->anElements > 1) {
snprintf(msg, CMOR_MAX_STRING,
"Your %s has more than 1 element\n! "
"only the first one will be used\n! "
"Check your Control Vocabulary file \"%s\".\n! ",
CV->key, CV_Filename);
cmor_handle_error(msg, CMOR_NORMAL);
cmor_pop_traceback();
return (0);
} else if (CV->anElements == -1) {
snprintf(msg, CMOR_MAX_STRING,
"Your %s has more than 0 element\n! "
"Check your Control Vocabulary file \"%s\".\n! ",
CV->key, CV_Filename);
cmor_handle_error(msg, CMOR_NORMAL);
cmor_pop_traceback();
return (0);
}
cmor_pop_traceback();
return (0);
}
/************************************************************************/
/* CV_CompareNoParent() */
/************************************************************************/
int CV_CompareNoParent(char *szKey)
{
char msg[CMOR_MAX_STRING];
char szValue[CMOR_MAX_STRING];
cmor_add_traceback("_CV_CompareNoParent");
if (cmor_has_cur_dataset_attribute(szKey) == 0) {
cmor_get_cur_dataset_attribute(szKey, szValue);
if (strcmp(szValue, NO_PARENT) != 0) {
snprintf(msg, CMOR_MAX_STRING,
"Your input attribute %s with value \"%s\" \n! "
"will be replaced with value \"%s\".\n! ", szKey,
szValue, NO_PARENT);
cmor_set_cur_dataset_attribute_internal(szKey, NO_PARENT, 1);
cmor_handle_error(msg, CMOR_WARNING);
cmor_pop_traceback();
return (-1);
}
}
cmor_pop_traceback();
return (0);
}
/************************************************************************/
/* InArray() */
/************************************************************************/
int CV_IsStringInArray(cmor_CV_def_t * CV, char *szValue)
{
int nElements;
int i;
int found = 0;
cmor_add_traceback("_CV_InArray");
nElements = CV->anElements;
for (i = 0; i < nElements; i++) {
if (strcmp(CV->aszValue[i], szValue) == 0) {
found = 1;
break;
}
}
cmor_pop_traceback();
return (found);
}
/************************************************************************/
/* cmor_CV_checkSubExpID() */
/************************************************************************/
int cmor_CV_checkSubExpID(cmor_CV_def_t * CV)
{
cmor_CV_def_t *CV_experiment_id;
cmor_CV_def_t *CV_experiment;
cmor_CV_def_t *CV_experiment_sub_exp_id;
cmor_CV_def_t *CV_sub_experiment_id;
cmor_CV_def_t *CV_sub_experiment_id_key;
char szExperiment_ID[CMOR_MAX_STRING];
char CV_Filename[CMOR_MAX_STRING];
char szSubExptID[CMOR_MAX_STRING];
char szValue[CMOR_MAX_STRING];
char szVariant[CMOR_MAX_STRING];
char msg[CMOR_MAX_STRING];
cmor_add_traceback("_CV_checkSubExperiment");
// Initialize variables
cmor_get_cur_dataset_attribute(CV_INPUTFILENAME, CV_Filename);
cmor_get_cur_dataset_attribute(GLOBAL_ATT_EXPERIMENTID, szExperiment_ID);
// Look for sub_experiment_id section
CV_sub_experiment_id = cmor_CV_rootsearch(CV, CV_KEY_SUB_EXPERIMENT_ID);
if (CV_sub_experiment_id == NULL) {
snprintf(msg, CMOR_MAX_STRING,
"Your \"sub_experiment_id\" key could not be found in\n! "
"your Control Vocabulary file.(%s)\n! ", CV_Filename);
cmor_handle_error(msg, CMOR_NORMAL);
cmor_pop_traceback();
return (-1);
}
// Look for experiment_id section
CV_experiment_id = cmor_CV_rootsearch(CV, CV_KEY_EXPERIMENT_ID);
if (CV_experiment_id == NULL) {
snprintf(msg, CMOR_MAX_STRING,
"Your \"experiment_id\" key could not be found in\n! "
"your Control Vocabulary file.(%s)\n! ", CV_Filename);
cmor_handle_error(msg, CMOR_NORMAL);
cmor_pop_traceback();
return (-1);
}
// Get specified experiment
CV_experiment = cmor_CV_search_child_key(CV_experiment_id, szExperiment_ID);
if (CV_experiment == NULL) {
snprintf(msg, CMOR_MAX_STRING,
"Your experiment_id \"%s\" defined in your input file\n! "
"could not be found in your Control Vocabulary file.(%s)\n! ",
szExperiment_ID, CV_Filename);
cmor_handle_error(msg, CMOR_NORMAL);
cmor_pop_traceback();
return (-1);
}
// sub_experiment_id
CV_experiment_sub_exp_id = cmor_CV_search_child_key(CV_experiment,
GLOBAL_ATT_SUB_EXPT_ID);
if (CV_experiment_sub_exp_id == NULL) {
snprintf(msg, CMOR_MAX_STRING,
"Your \"%s\" defined in your input file\n! "
"could not be found in your Control Vocabulary file.(%s)\n! ",
GLOBAL_ATT_SUB_EXPT_ID, CV_Filename);
cmor_handle_error(msg, CMOR_NORMAL);
cmor_pop_traceback();
return (-1);
}
// Check sub_experiment_id value
if (cmor_has_cur_dataset_attribute(GLOBAL_ATT_SUB_EXPT_ID) != 0) {
// sub_experiment_id not found and set to "none"
if (CV_IsStringInArray(CV_experiment_sub_exp_id, NONE)) {
snprintf(msg, CMOR_MAX_STRING,
"Your input attribute \"%s\" was not defined and \n! "
"will be set to \"%s\"\n! "
"as defined in your Control Vocabulary file \"%s\".\n! ",
GLOBAL_ATT_SUB_EXPT_ID, NONE, CV_Filename);
cmor_handle_error(msg, CMOR_WARNING);
cmor_set_cur_dataset_attribute_internal(GLOBAL_ATT_SUB_EXPT_ID,
NONE, 1);
} else {
// can't be "none".
snprintf(msg, CMOR_MAX_STRING,
"Your input attribute \"%s\" is not defined properly \n! "
"for your experiment \"%s\" \n! \n! "
"See Control Vocabulary JSON file.(%s)\n! ",
GLOBAL_ATT_SUB_EXPT_ID, szExperiment_ID, CV_Filename);
cmor_handle_error(msg, CMOR_NORMAL);
cmor_pop_traceback();
return (-1);
}
} else {
// sub_experiment_id has been defined!
cmor_get_cur_dataset_attribute(GLOBAL_ATT_SUB_EXPT_ID, szSubExptID);
// cannot be found in CV
if (!CV_IsStringInArray(CV_experiment_sub_exp_id, szSubExptID)) {
// only 1 element in list set it!
if (CV_experiment_sub_exp_id->anElements == 1) {
snprintf(msg, CMOR_MAX_STRING,
"Your input attribute \"%s\" defined as \"%s\" "
"will be replaced with \n! "
"\"%s\" as defined in your Control Vocabulary file.\n! ",
GLOBAL_ATT_SUB_EXPT_ID, szSubExptID,
CV_experiment_sub_exp_id->aszValue[0]);
cmor_handle_error(msg, CMOR_WARNING);
cmor_set_cur_dataset_attribute_internal(GLOBAL_ATT_SUB_EXPT_ID,
CV_experiment_sub_exp_id->aszValue
[0], 1);
} else {
// too many options.
snprintf(msg, CMOR_MAX_STRING,
"Your input attribute \"%s\" is not defined properly \n! "
"for your experiment \"%s\"\n! "
"There is more than 1 option for this sub_experiment.\n! "
"See Control Vocabulary JSON file.(%s)\n! ",
GLOBAL_ATT_SUB_EXPT_ID, szExperiment_ID, CV_Filename);
cmor_handle_error(msg, CMOR_NORMAL);
cmor_pop_traceback();
return (-1);
}
}
}
// sub_experiment has not been defined!
if (cmor_has_cur_dataset_attribute(GLOBAL_ATT_SUB_EXPT) != 0) {
snprintf(msg, CMOR_MAX_STRING,
"Your input attribute \"%s\" was not defined and \n! "
"will be set to \"%s\" \n! "
"as defined in your Control Vocabulary file \"%s\".\n! ",
GLOBAL_ATT_SUB_EXPT, NONE, CV_Filename);
cmor_handle_error(msg, CMOR_WARNING);
cmor_set_cur_dataset_attribute_internal(GLOBAL_ATT_SUB_EXPT, NONE, 1);
} else {
cmor_get_cur_dataset_attribute(GLOBAL_ATT_SUB_EXPT, szValue);
CV_sub_experiment_id_key =
cmor_CV_search_child_key(CV_sub_experiment_id, szSubExptID);
if (CV_sub_experiment_id_key == NULL) {
snprintf(msg, CMOR_MAX_STRING,
"Your \"sub_experiment\" text describing \n! "
"sub_experiment_id \"%s\" could not be found in \n! "
"your Control Vocabulary file.(%s)\n! ", szSubExptID,
CV_Filename);
cmor_handle_error(msg, CMOR_NORMAL);
cmor_pop_traceback();
return (-1);
}
if (strcmp(szValue, CV_sub_experiment_id_key->szValue) != 0) {
snprintf(msg, CMOR_MAX_STRING,
"Your input attribute \"%s\" defined as \"%s\" "
"will be replaced with \n! "
"\"%s\" as defined in your Control Vocabulary file.\n! ",
GLOBAL_ATT_SUB_EXPT, szValue,
CV_sub_experiment_id_key->szValue);
cmor_handle_error(msg, CMOR_WARNING);
cmor_set_cur_dataset_attribute_internal(GLOBAL_ATT_SUB_EXPT,
CV_sub_experiment_id_key->szValue,
1);
}
}
// append sub-experiment_id
if (cmor_has_cur_dataset_attribute(GLOBAL_ATT_SUB_EXPT_ID) == 0) {
cmor_get_cur_dataset_attribute(GLOBAL_ATT_SUB_EXPT_ID, szValue);
cmor_get_cur_dataset_attribute(GLOBAL_ATT_MEMBER_ID, szVariant);
if (strcmp(szValue, NONE) != 0) {
// not already in variant
if (strstr(szVariant, szValue) == NULL) {
strcat(szValue, "-");
strcat(szValue, szVariant);
cmor_set_cur_dataset_attribute_internal(GLOBAL_ATT_MEMBER_ID,
szValue, 1);
}
}
}
cmor_pop_traceback();
return (0);
}
/************************************************************************/
/* cmor_CV_checkParentExpID() */
/************************************************************************/
int cmor_CV_checkParentExpID(cmor_CV_def_t * CV)
{
cmor_CV_def_t *CV_experiment_ids;
cmor_CV_def_t *CV_experiment;
cmor_CV_def_t *CV_parent_exp_id;
cmor_CV_def_t *CV_parent_activity_id;
cmor_CV_def_t *CV_source_id;
cmor_CV_def_t *CV_source;
char szValue[CMOR_MAX_STRING];
char szParentExpValue[CMOR_MAX_STRING];