-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimcdf_low_level.c
More file actions
1107 lines (898 loc) · 34.1 KB
/
imcdf_low_level.c
File metadata and controls
1107 lines (898 loc) · 34.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
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
/*****************************************************************************
* imcdf_low_level.c - basic interactions with a CDF file - all the calls to
* the CDF library are in this source code file - there is
* no other linkage to the CDF library outside this file
*
* THE IMCDF ROUTINES SHOULD NOT HAVE DEPENDENCIES ON OTHER LIBRARY ROUTINES -
* IT MUST BE POSSIBLE TO DISTRIBUTE THE IMCDF SOURCE CODE
*
* This code only used Extended Standard Interface functions
*
* access to an open CDF is controlled by a CDF handle, which is an index into
* an array of CDFid elements
*
* Simon Flower, 19/12/2012
* Updates to version 1.1 of ImagCDF. Simon Flower, 19/02/2015
* Updates to version 1.3 of ImagCDF. Simon Flower, 09/09/2025
*****************************************************************************/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cdf.h"
#include "imcdf.h"
/* private global variables: */
/* an array of CDF ids - this allows for more than one CDF file to be kept open
* at a time, though in reality it's unlikely that more than one will ever be
* open simultaneously */
#define MAX_OPEN_CDF_FILES 10
static CDFid cdf_ids [MAX_OPEN_CDF_FILES];
static int cdf_index = -1;
/* the status of the last call to the CDF library */
CDFstatus cdf_status;
/* private forward declarations */
static void initialise_cdf_ids ();
static int sanity_check_handles (int cdf_handle);
static long find_global_attribute (int cdf_handle, char *name);
static long find_variable_attribute (int cdf_handle, char *name);
/** ------------------------------------------------------------------------
* --------------------- Opening and closing CDF files --------------------
* ------------------------------------------------------------------------*/
/*****************************************************************************
* imcdf_open
*
* Description: open a CDF for reading or writing
*
* Input parameters: filename - the CDF file to open
* open_type - how to open the file
* compress_type - how to compress the file (only used
* on file that is being created)
* Output parameters:
* Returns: a handle (greater than or equal to zero) on success
* a negative number on failure
*
*****************************************************************************/
int imcdf_open (char *filename, enum IMCDFOpenType open_type,
enum IMCDFCompressionType compress_type)
{
long cparams [1];
CDFid id;
initialise_cdf_ids ();
/* check there is space to open another file */
if (cdf_index >= MAX_OPEN_CDF_FILES) return -1;
/* open the file */
switch (open_type)
{
case IMCDF_FORCE_CREATE:
if (! access (filename, 0)) remove (filename);
cdf_status = CDFcreateCDF (filename, &id);
if (cdf_status < CDF_WARN) return -1;
break;
case IMCDF_CREATE:
cdf_status = CDFcreateCDF (filename, &id);
if (cdf_status < CDF_WARN) return -1;
break;
case IMCDF_OPEN:
cdf_status = CDFopenCDF (filename, &id);
if (cdf_status < CDF_WARN) return -1;
compress_type = IMCDF_COMPRESS_NONE;
break;
default:
return -1;
}
/* set the compression */
switch (compress_type)
{
case IMCDF_COMPRESS_RLE:
cparams [0] = RLE_OF_ZEROs;
cdf_status = CDFsetCompression (id, RLE_COMPRESSION, cparams);
break;
case IMCDF_COMPRESS_HUFF:
cparams [0] = OPTIMAL_ENCODING_TREES;
cdf_status = CDFsetCompression (id, HUFF_COMPRESSION, cparams);
break;
case IMCDF_COMPRESS_AHUFF:
cparams [0] = OPTIMAL_ENCODING_TREES;
cdf_status = CDFsetCompression (id, AHUFF_COMPRESSION, cparams);
break;
case IMCDF_COMPRESS_GZIP1:
cparams [0] = 1l;
cdf_status = CDFsetCompression (id, GZIP_COMPRESSION, cparams);
break;
case IMCDF_COMPRESS_GZIP2:
cparams [0] = 2l;
cdf_status = CDFsetCompression (id, GZIP_COMPRESSION, cparams);
break;
case IMCDF_COMPRESS_GZIP3:
cparams [0] = 3l;
cdf_status = CDFsetCompression (id, GZIP_COMPRESSION, cparams);
break;
case IMCDF_COMPRESS_GZIP4:
cparams [0] = 4l;
cdf_status = CDFsetCompression (id, GZIP_COMPRESSION, cparams);
break;
case IMCDF_COMPRESS_GZIP5:
cparams [0] = 5l;
cdf_status = CDFsetCompression (id, GZIP_COMPRESSION, cparams);
break;
case IMCDF_COMPRESS_GZIP6:
cparams [0] = 6l;
cdf_status = CDFsetCompression (id, GZIP_COMPRESSION, cparams);
break;
case IMCDF_COMPRESS_GZIP7:
cparams [0] = 7l;
cdf_status = CDFsetCompression (id, GZIP_COMPRESSION, cparams);
break;
case IMCDF_COMPRESS_GZIP8:
cparams [0] = 8l;
cdf_status = CDFsetCompression (id, GZIP_COMPRESSION, cparams);
break;
case IMCDF_COMPRESS_GZIP9:
cparams [0] = 9l;
cdf_status = CDFsetCompression (id, GZIP_COMPRESSION, cparams);
break;
default:
break;
}
if (cdf_status < CDF_WARN) return -1;
/* insert the ID */
cdf_ids [cdf_index] = id;
return cdf_index ++;
}
/*****************************************************************************
* imcdf_close
*
* Description: close a CDF - you MUST call this after writing to the CDF
* otherwise it will be corrupt
*
* Input parameters: cdf_handle - the CDF to close
* Output parameters: none
* Returns: 0 for success, -1 for failure
*
*****************************************************************************/
int imcdf_close (int cdf_handle)
{
int count;
if (sanity_check_handles (cdf_handle)) return -1;
/* close the CDF */
cdf_status = CDFcloseCDF (cdf_ids [cdf_handle]);
if (cdf_status < CDF_WARN) return -1;
/* sort out the array of CDF ids */
for (count=cdf_handle +1; count<cdf_index; count++)
cdf_ids [count -1] = cdf_ids [count];
cdf_index --;
return 0;
}
/** ------------------------------------------------------------------------
* ------------------------- Writing to CDF files -------------------------
* ------------------------------------------------------------------------*/
/****************************************************************************
* imcdf_add_global_attr_string
* imcdf_add_global_attr_double
* imcdf_add_global_attr_tt2000
*
* Description: add a global attribute of the specified type to the CDF file
* and make an entry in it
*
* Input parameters: cdf_handle - handle to the CDF file
* attr_name - the attribute name (must be unique)
* entry_no - the entry number required, 0..n_entries-1
* value - the contents of the entry
* Output parameters:
* Returns: 0 for success, -1 for failure
*
****************************************************************************/
int imcdf_add_global_attr_string (int cdf_handle, char *attr_name, int entry_no, char *value)
{
long attr_num;
if (sanity_check_handles (cdf_handle)) return -1;
if (value)
{
attr_num = find_global_attribute (cdf_handle, attr_name);
if (attr_num < 0l) return -1;
cdf_status = CDFputAttrgEntry (cdf_ids [cdf_handle], attr_num, entry_no, CDF_CHAR, (long) strlen (value), value);
if (cdf_status < CDF_WARN) return -1;
}
return 0;
}
int imcdf_add_global_attr_double (int cdf_handle, char *attr_name, int entry_no, double value)
{
long attr_num;
double values [1];
if (sanity_check_handles (cdf_handle)) return -1;
attr_num = find_global_attribute (cdf_handle, attr_name);
if (attr_num < 0l) return -1;
values [0] = value;
cdf_status = CDFputAttrgEntry (cdf_ids [cdf_handle], attr_num, entry_no, CDF_DOUBLE, 1l, values);
if (cdf_status < CDF_WARN) return -1;
return 0;
}
int imcdf_add_global_attr_tt2000 (int cdf_handle, char *attr_name, int entry_no, long long value)
{
long attr_num;
long long values [1];
if (sanity_check_handles (cdf_handle)) return -1;
attr_num = find_global_attribute (cdf_handle, attr_name);
if (attr_num < 0l) return -1;
values [0] = value;
cdf_status = CDFputAttrgEntry (cdf_ids [cdf_handle], attr_num, entry_no, CDF_TIME_TT2000, 1l, values);
if (cdf_status < CDF_WARN) return -1;
return 0;
}
/****************************************************************************
* imcdf_add_variable_attr_string
* imcdf_add_variable_attr_double
* imcdf_add_variable_attr_tt2000
*
* Description: add a variable attribute of the specified type to the CDF file
* and make an entry in it
*
* Input parameters: cdf_handle - handle to the CDF file
* attr_name - the attribute name (must be unique)
* var_name - the name of the variable to add the entry to
* value - the contents of the entry
* Output parameters:
* Returns: 0 for success, -1 for failure
*
****************************************************************************/
int imcdf_add_variable_attr_string (int cdf_handle, char *attr_name,
char *var_name, char *value)
{
long attr_num, var_num;
if (sanity_check_handles (cdf_handle)) return -1;
attr_num = find_variable_attribute (cdf_handle, attr_name);
if (attr_num < 0l) return -1;
var_num = CDFgetVarNum (cdf_ids [ cdf_handle], var_name);
if (var_num < 0l)
{
cdf_status = (CDFstatus) var_num;
return -1;
}
cdf_status = CDFputAttrzEntry (cdf_ids [cdf_handle], attr_num, var_num, CDF_CHAR, (long) strlen (value), value);
if (cdf_status < CDF_WARN) return -1;
return 0;
}
int imcdf_add_variable_attr_double (int cdf_handle, char *attr_name,
char *var_name, double value)
{
long attr_num, var_num;
double values [1];
if (sanity_check_handles (cdf_handle)) return -1;
attr_num = find_variable_attribute (cdf_handle, attr_name);
if (attr_num < 0l) return -1;
var_num = CDFgetVarNum (cdf_ids [ cdf_handle], var_name);
if (var_num < 0l)
{
cdf_status = (CDFstatus) var_num;
return -1;
}
values [0] = value;
cdf_status = CDFputAttrzEntry (cdf_ids [cdf_handle], attr_num, var_num,
CDF_DOUBLE, 1l, values);
if (cdf_status < CDF_WARN) return -1;
return 0;
}
int imcdf_add_variable_attr_tt2000 (int cdf_handle, char *attr_name,
char *var_name, long long value)
{
long attr_num, var_num;
long long values [1];
if (sanity_check_handles (cdf_handle)) return -1;
attr_num = find_variable_attribute (cdf_handle, attr_name);
if (attr_num < 0l) return -1;
var_num = CDFgetVarNum (cdf_ids [ cdf_handle], var_name);
if (var_num < 0l)
{
cdf_status = (CDFstatus) var_num;
return -1;
}
values [0] = value;
cdf_status = CDFputAttrzEntry (cdf_ids [cdf_handle], attr_num, var_num,
CDF_TIME_TT2000, 1l, values);
if (cdf_status < CDF_WARN) return -1;
return 0;
}
/****************************************************************************
* imcdf_create_data_array
* imcdf_create_time_stamp_array
* imcdf_append_data_array
* imcdf_append_time_stamp_array
*
* create a data array or a time stamp array in the CDF file
* append data to a data array or a time stamp aray in the CDF file
*
* Input parameters: cdf_handle - handle to the CDF file
* name - the variable name
* data - the data to write into the variable
* data_length - the number of elements of data to write
* Output parameters:
* Returns: 0 for success, -1 for failure
*
****************************************************************************/
int imcdf_create_data_array (int cdf_handle, char *name, double *data,
int data_length)
{
long var_num, dim_size [1], dim_var [1];
if (sanity_check_handles (cdf_handle)) return -1;
dim_size [0] = 1;
dim_var [0] = VARY;
cdf_status = CDFcreatezVar (cdf_ids [cdf_handle], name, CDF_DOUBLE,
1l, 0l, dim_size, VARY, dim_var, &var_num);
if (cdf_status < CDF_WARN) return -1;
return imcdf_append_data_array (cdf_handle, name, data, data_length);
}
int imcdf_create_time_stamp_array (int cdf_handle, char *name, long long *data,
int data_length)
{
long var_num, dim_size [1], dim_var [1];
if (sanity_check_handles (cdf_handle)) return -1;
dim_size [0] = 1;
dim_var [0] = VARY;
cdf_status = CDFcreatezVar (cdf_ids [cdf_handle], name, CDF_TIME_TT2000,
1l, 0l, dim_size, VARY, dim_var, &var_num);
if (cdf_status < CDF_WARN) return -1;
return imcdf_append_time_stamp_array (cdf_handle, name, data, data_length);
}
int imcdf_append_data_array (int cdf_handle, char *name, double *data,
int data_length)
{
long var_num, n_recs, count;
if (sanity_check_handles (cdf_handle)) return -1;
var_num = CDFgetVarNum (cdf_ids [cdf_handle], name);
if (var_num < 0)
{
cdf_status = var_num;
return -1;
}
cdf_status = CDFgetzVarMaxWrittenRecNum (cdf_ids [cdf_handle], var_num, &n_recs);
if (cdf_status != CDF_OK) return -1;
if (n_recs < 0) n_recs = 0;
for (count=0; count<data_length; count++)
{
cdf_status = CDFputzVarRecordData (cdf_ids [cdf_handle], var_num, count + n_recs, data + count);
if (cdf_status < CDF_WARN) return -1;
}
return 0;
}
int imcdf_append_time_stamp_array (int cdf_handle, char *name, long long *data,
int data_length)
{
long var_num, n_recs, count;
if (sanity_check_handles (cdf_handle)) return -1;
var_num = CDFgetVarNum (cdf_ids [cdf_handle], name);
if (var_num < 0)
{
cdf_status = var_num;
return -1;
}
cdf_status = CDFgetzVarMaxWrittenRecNum (cdf_ids [cdf_handle], var_num, &n_recs);
if (cdf_status != CDF_OK) return -1;
if (n_recs < 0) n_recs = 0;
for (count=0; count<data_length; count++)
{
cdf_status = CDFputzVarRecordData (cdf_ids [cdf_handle], var_num, count + n_recs, data + count);
if (cdf_status < CDF_WARN) return -1;
}
return 0;
}
/** ------------------------------------------------------------------------
* ----------------------- Reading from CDF files -------------------------
* ------------------------------------------------------------------------*/
/****************************************************************************
* imcdf_get_global_attribute_string
* imcdf_get_global_attribute_double
* imcdf_get_global_attribute_tt2000
*
* Description: get the contents of a global attribute
*
* Input parameters: cdf_handle - handle to the CDF file
* name - the name of the attribute
* entry_no - the entry number required, 0..n_entries-1
* Output parameters: value - the value of the attribute - for strings
* this will point to dynamically allocated memory
* Returns: 0 for success, -1 for failure
*
****************************************************************************/
int imcdf_get_global_attribute_string (int cdf_handle, char *name, int entry_no, char **value)
{
long attr_num, data_type, num_elements;
if (sanity_check_handles (cdf_handle)) return -1;
attr_num = CDFattrNum (cdf_ids [cdf_handle], name);
if (attr_num < 0)
{
cdf_status = attr_num;
return -1;
}
cdf_status = CDFinquireAttrgEntry (cdf_ids [cdf_handle], attr_num, entry_no,
&data_type, &num_elements);
if (cdf_status < 0) return -1;
if (data_type != CDF_CHAR) return -1;
*value = malloc (num_elements +1);
if (! *value)
{
cdf_status = BAD_MALLOC;
return -1;
}
cdf_status = CDFgetAttrgEntry (cdf_ids [cdf_handle], attr_num, entry_no, *value);
if (cdf_status < 0) return -1;
*((*value) + num_elements) = '\0';
return 0;
}
int imcdf_get_global_attribute_double (int cdf_handle, char *name, int entry_no, double *value)
{
long attr_num, data_type, num_elements;
if (sanity_check_handles (cdf_handle)) return -1;
attr_num = CDFattrNum (cdf_ids [cdf_handle], name);
if (attr_num < 0)
{
cdf_status = attr_num;
return -1;
}
cdf_status = CDFinquireAttrgEntry (cdf_ids [cdf_handle], attr_num, entry_no,
&data_type, &num_elements);
if (cdf_status < 0) return -1;
if (data_type != CDF_DOUBLE) return -1;
cdf_status = CDFgetAttrgEntry (cdf_ids [cdf_handle], attr_num, entry_no, value);
if (cdf_status < 0) return -1;
return 0;
}
int imcdf_get_global_attribute_tt2000 (int cdf_handle, char *name, int entry_no, long long *value)
{
long attr_num, data_type, num_elements;
if (sanity_check_handles (cdf_handle)) return -1;
attr_num = CDFattrNum (cdf_ids [cdf_handle], name);
if (attr_num < 0)
{
cdf_status = attr_num;
return -1;
}
cdf_status = CDFinquireAttrgEntry (cdf_ids [cdf_handle], attr_num, entry_no,
&data_type, &num_elements);
if (cdf_status < 0) return -1;
if (data_type != CDF_TIME_TT2000) return -1;
cdf_status = CDFgetAttrgEntry (cdf_ids [cdf_handle], attr_num, entry_no, value);
if (cdf_status < 0) return -1;
return 0;
}
/****************************************************************************
* imcdf_get_variable_attribute_string
* imcdf_get_variable_attribute_double
* imcdf_get_variable_attribute_tt2000
*
* Description: get the contents of a variable attribute
*
* Input parameters: cdf_handle - handle to the CDF file
* attr_name - the attribute name
* var_name - the name of the variable
* Output parameters: value - the value of the attribute - for strings
* this will point to dynamically allocated memory
* Returns: 0 for success, -1 for failure
*
****************************************************************************/
int imcdf_get_variable_attribute_string (int cdf_handle, char *attr_name,
char *var_name, char **value)
{
long var_num, attr_num, data_type, num_elements;
if (sanity_check_handles (cdf_handle)) return -1;
var_num = CDFgetVarNum (cdf_ids [ cdf_handle], var_name);
if (var_num < 0l)
{
cdf_status = (CDFstatus) cdf_status;
return -1;
}
attr_num = CDFattrNum (cdf_ids [cdf_handle], attr_name);
if (attr_num < 0)
{
cdf_status = attr_num;
return -1;
}
cdf_status = CDFinquireAttrzEntry (cdf_ids [cdf_handle], attr_num, var_num,
&data_type, &num_elements);
if (cdf_status < 0) return -1;
if (data_type != CDF_CHAR) return -1;
*value = malloc (num_elements +1);
if (! *value)
{
cdf_status = BAD_MALLOC;
return -1;
}
cdf_status = CDFgetAttrzEntry (cdf_ids [cdf_handle], attr_num, var_num, *value);
if (cdf_status < 0) return -1;
*((*value) + num_elements) = '\0';
return 0;
}
int imcdf_get_variable_attribute_double (int cdf_handle, char *attr_name,
char *var_name, double *value)
{
long var_num, attr_num, data_type, num_elements;
if (sanity_check_handles (cdf_handle)) return -1;
var_num = CDFgetVarNum (cdf_ids [ cdf_handle], var_name);
if (var_num < 0l)
{
cdf_status = (CDFstatus) cdf_status;
return -1;
}
attr_num = CDFattrNum (cdf_ids [cdf_handle], attr_name);
if (attr_num < 0)
{
cdf_status = attr_num;
return -1;
}
cdf_status = CDFinquireAttrzEntry (cdf_ids [cdf_handle], attr_num, var_num,
&data_type, &num_elements);
if (cdf_status < 0) return -1;
if (data_type != CDF_DOUBLE) return -1;
cdf_status = CDFgetAttrzEntry (cdf_ids [cdf_handle], attr_num, var_num, value);
if (cdf_status < 0) return -1;
return 0;
}
int imcdf_get_variable_attribute_tt2000 (int cdf_handle, char *attr_name,
char *var_name, long long *value)
{
long var_num, attr_num, data_type, num_elements;
if (sanity_check_handles (cdf_handle)) return -1;
var_num = CDFgetVarNum (cdf_ids [ cdf_handle], var_name);
if (var_num < 0l)
{
cdf_status = (CDFstatus) cdf_status;
return -1;
}
attr_num = CDFattrNum (cdf_ids [cdf_handle], attr_name);
if (attr_num < 0)
{
cdf_status = attr_num;
return -1;
}
cdf_status = CDFinquireAttrzEntry (cdf_ids [cdf_handle], attr_num, var_num,
&data_type, &num_elements);
if (cdf_status < 0) return -1;
if (data_type != CDF_TIME_TT2000) return -1;
cdf_status = CDFgetAttrzEntry (cdf_ids [cdf_handle], attr_num, var_num, value);
if (cdf_status < 0) return -1;
return 0;
}
/***************************************************************************
* imcdf_get_var_data
* imcdf_get_var_time_stamp
*
* Description: get data from a data variable or a timestamp variable
*
* Input parameters: cdf_handle - handle to the CDF file
* name - the name of the variable
* Output parameters: data_len - the length of the retrieved data
* Returns: the data in a newly allocated memory space or NULL if there
* is a failure
*
****************************************************************************/
double *imcdf_get_var_data (int cdf_handle, char *var_name, int *data_len)
{
long var_num, data_type, num_elements, num_dims, dim_sizes [CDF_MAX_DIMS];
long rec_variance, dim_variance [CDF_MAX_DIMS], count;
double *data;
char local_var_name [CDF_VAR_NAME_LEN256 +1];
if (sanity_check_handles (cdf_handle)) return 0;
var_num = CDFgetVarNum (cdf_ids [cdf_handle], var_name);
if (var_num < 0l)
{
cdf_status = (CDFstatus) cdf_status;
return 0;
}
cdf_status = CDFinquirezVar (cdf_ids [cdf_handle], var_num, local_var_name,
&data_type, &num_elements, &num_dims, dim_sizes,
&rec_variance, dim_variance);
if (cdf_status < 0) return 0;
if (data_type != CDF_DOUBLE) return 0;
if (num_dims != 0) return 0;
cdf_status = CDFgetzVarNumRecsWritten (cdf_ids [cdf_handle], var_num, data_len);
if (cdf_status != CDF_OK) return 0;
data = malloc (*data_len * sizeof (double));
if (! data)
{
cdf_status = BAD_MALLOC;
return 0;
}
for (count=0; count<*data_len; count++)
{
cdf_status = CDFgetzVarRecordData (cdf_ids [cdf_handle], var_num, count, data + count);
if (cdf_status < 0)
{
free (data);
return 0;
}
}
return data;
}
long long *imcdf_get_var_time_stamps (int cdf_handle, char *var_name, int *data_len)
{
long var_num, data_type, num_elements, num_dims, dim_sizes [CDF_MAX_DIMS];
long rec_variance, dim_variance [CDF_MAX_DIMS], count;
long long *data;
char local_var_name [CDF_VAR_NAME_LEN256 +1];
if (sanity_check_handles (cdf_handle)) return 0;
var_num = CDFgetVarNum (cdf_ids [cdf_handle], var_name);
if (var_num < 0l)
{
cdf_status = (CDFstatus) cdf_status;
return 0;
}
cdf_status = CDFinquirezVar (cdf_ids [cdf_handle], var_num, local_var_name,
&data_type, &num_elements, &num_dims, dim_sizes,
&rec_variance, dim_variance);
if (cdf_status < 0) return 0;
if (data_type != CDF_TIME_TT2000) return 0;
if (num_dims != 0) return 0;
cdf_status = CDFgetzVarNumRecsWritten (cdf_ids [cdf_handle], var_num, data_len);
if (cdf_status != CDF_OK) return 0;
data = malloc (*data_len * sizeof (long long));
if (! data)
{
cdf_status = BAD_MALLOC;
return 0;
}
for (count=0; count<*data_len; count++)
{
cdf_status = CDFgetzVarRecordData (cdf_ids [cdf_handle], var_num, count, data + count);
if (cdf_status < 0)
{
free (data);
return 0;
}
}
return data;
}
/***************************************************************************
* imcdf_is_var_exist
*
* Description: test if the given variable exists
*
* Input parameters: cdf_handle - handle to the CDF file
* name - the name of the variable
* Output parameters:
* Returns: 0 if variable is found, -1 otherwise
*
****************************************************************************/
int imcdf_is_var_exist (int cdf_handle, char *name)
{
long var_num;
var_num = CDFgetVarNum (cdf_ids [ cdf_handle], name);
if (var_num < 0l)
{
cdf_status = (CDFstatus) var_num;
return -1;
}
return 0;
}
/** ------------------------------------------------------------------------
* ---------------------- TT2000 data manipulation ------------------------
* ------------------------------------------------------------------------*/
/****************************************************************************
* imcdf_date_time_to_tt2000
* imcdf_tt2000_to_date_time
*
* Description: convert date/time to / from TT2000
*
* Input parameters: year, month, day - the date - month and day start at 1
* hour, min, sec - the time (all 0 based)
* Output parameters: tt2000 - the output tt2000 value
* Returns: 0 if conversion was completed OK, -1 otherwise
*
****************************************************************************/
int imcdf_date_time_to_tt2000 (int year, int month, int day, int hour,
int min, int sec, long long *tt2000)
{
*tt2000 = computeTT2000 ((double) year, (double) month, (double) day,
(double) hour, (double) min, (double) sec,
0.0, 0.0, 0.0);
if (*tt2000 == ILLEGAL_TT2000_VALUE)
{
cdf_status = BAD_ARGUMENT;
return -1;
}
cdf_status = CDF_OK;
return 0;
}
int imcdf_tt2000_to_date_time (long long tt2000,
int *year, int *month, int *day,
int *hour, int *min, int *sec)
{
double d_year, d_month, d_day, d_hour, d_min, d_sec;
breakdownTT2000 (tt2000, &d_year, &d_month, &d_day,
&d_hour, &d_min, &d_sec, TT2000NULL);
*year = (int) d_year;
*month = (int) d_month;
*day = (int) d_day;
*hour = (int) d_hour;
*min = (int) d_min;
*sec = (int) (d_sec + 0.5);
cdf_status = CDF_OK;
return 0;
}
/****************************************************************************
* imcdf_tt2000_inc
*
* Description: increment (or decrement) a TT2000 date/time
*
* Input parameters: tt2000 - the date / time to increment or decrement
* inc - the amount to increment or decrement by (in seconds)
* Output parameters:
* Returns: the new TT2000 value
*
****************************************************************************/
long long imcdf_tt2000_inc (long long tt2000, int inc)
{
return tt2000 + ((long long) inc * 1000000000ll);
}
/****************************************************************************
* imcdf_make_tt2000_array
*
* Description: create an array of TT2000 time stamps
*
* Input parameters: year, month, day - the date - month and day start at 1
* hour, min, sec - the time (all 0 based)
* increment - the time increment between samples, in seconds
* n_samples - the number of samples
* Output parameters:
* Returns: an array of TT2000 time stamps, allocated dynamically (so will need
* to be freed when done with) OR null if there was a problem.
* If this routine returns null, call imcdf_get_last_status_code()
* to see what happened - if it returns CDF_OK there was a memory
* allocation fault.
*
****************************************************************************/
long long *imcdf_make_tt2000_array (int year, int month, int day, int hour, int min, int sec,
int increment, int n_samples)
{
int count;
long long *tt2000_array, tt2000;
tt2000_array = malloc (sizeof (long long) * n_samples);
if (! tt2000_array) return 0;
if (imcdf_date_time_to_tt2000 (year, month, day, hour, min, sec, &tt2000))
{
free (tt2000_array);
return 0;
}
for (count=0; count<n_samples; count++)
{
*(tt2000_array + count) = tt2000;
tt2000 = imcdf_tt2000_inc (tt2000, increment);
}
return tt2000_array;
}
/****************************************************************************
* imcdf_calc_samp_per_from_tt2000
*
* Description: calculate the sample period of a TT2000 array
*
* Input parameters: tt2000_array - the array to calculate the period for
* minimum length 2 samples
* Output parameters: none
* Returns: the sample period, in seconds
****************************************************************************/
int imcdf_calc_samp_per_from_tt2000 (long long *tt2000_array)
{
long diff;
diff = (long) (*(tt2000_array +1) - *tt2000_array);
return (int) (diff / 1000000000l);
}
/****************************************************************************
* imcdf_tt2000_tostring
*
* Description: format a TT2000 object as a string
*
* Input parameters: tt2000 - the date/time object to format
* Output parameters: none
* Returns: an ISO format date/time string
*
* The return points to a static buffer which will be overwritten on each
* call to this function
****************************************************************************/
char *imcdf_tt2000_tostring (long long tt2000)
{
static char buffer [30];
encodeTT2000 (tt2000, buffer, 3);
buffer [19] = '\0';
return buffer;
}
/** ------------------------------------------------------------------------
* --------------------------- Error notification -------------------------
* ------------------------------------------------------------------------*/
/*****************************************************************************
* imcdf_get_last_status_code
*
* Description: get the status code (which may have been successful) from
* the last call to the CDF library
*
* Input parameters:
* Output parameters:
* Returns: the status code
*