-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathhecdss.c
More file actions
961 lines (785 loc) · 31.7 KB
/
Copy pathhecdss.c
File metadata and controls
961 lines (785 loc) · 31.7 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
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "hecdss.h"
#include "heclib.h"
HECDSS_API const char* hec_dss_api_version() {
return "0.3.0";
}
#if defined(__GNUC__) || defined(__sun__)
#define MIN(a, b) ({ \
__typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a < _b ? _a : _b; \
})
#define MAX(a, b) ({ \
__typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; \
})
#else
#define MIN(a, b) min(a,b)
#define MAX(a, b) max(a,b)
#endif
enum tsRetrieveDataType { RETRIEVE_AS_STORED, RETRIEVE_FLOATS, RETRIEVE_DOUBLES };
enum qualAndNoteFlag { NO_RETRIEVE_QUAL_AND_NOTES, RETRIEVE_QUAL_AND_NOTES };
enum regTsRetrFlag { TRIM_NO_TIME_ARR = -3, NO_TRIM_NO_TIME_ARR, TRIM_INCL_TIME_ARR, NO_TRIM_INCL_TIME_ARR };
enum irrTsRetrFlag { TIME_WINDOW_ONLY, TIME_WINDOW_WITH_NEXT, TIME_WINDOW_WITH_PREV_AND_NEXT };
enum regTsStorFlag { REPLACE_ALL, REPLACE_MISSING_ONLY, CREATE_MISSING_RECS, NO_CREATE_MISSING_RECS, REPLACE_WITH_NON_MISSING };
enum irrTsStorFlag { MERGE, DELETE_INSERT };
enum pdStorFlag { PD_STORE_AUTOMATIC, PD_STORE_FLOAT, PD_STORE_DOUBLE};
enum dssCatalog {UNSORTED, SORTED};
// private definition
struct dss_file {
long long ifltab[250];
};
HECDSS_API int hec_dss_open_log_file(const char* filename) {
return zopenLog(filename);
}
HECDSS_API void hec_dss_close_log_file() {
zcloseLog();
}
HECDSS_API int hec_dss_flush_log_file() {
if (zdssVals.messageHandle <= 0) {
return STATUS_NOT_OKAY;
}
return flushFile(zdssVals.messageHandle);
}
HECDSS_API int hec_dss_log_message(const char* message) {
if (message == NULL) {
return STATUS_NOT_OKAY;
}
long long ifltab[250] = {0};
zmessageLen(ifltab, message, strlen(message));
return STATUS_OKAY;
}
HECDSS_API int hec_dss_CONSTANT_MAX_PATH_SIZE() {
return MAX_PATHNAME_SIZE;
}
float* hec_dss_double_array_to_float(double* values,const int size) {
if (size <= 0 || values == NULL)
return NULL;
float* rval = (float*)malloc((size_t)size * 4);
if (rval != NULL) {
for (int i = 0; i < size; i++)
{
rval[i] = (float)values[i];
}
}
return rval;
}
void hec_dss_array_copy_double(double* destination, const long destinationSize,
double* source, const size_t sourceSize) {
if (destination == NULL || destinationSize <= 0 || source == NULL || sourceSize <= 0) {
return;
}
size_t numberToCopy = sourceSize < destinationSize ? sourceSize: destinationSize;
for (size_t i = 0; i < numberToCopy; i++)
{
destination[i] = source[i];
}
}
void hec_dss_array_copy_float(float* destination, const long destinationSize,
float* source, const size_t sourceSize) {
if (destination == NULL || destinationSize <= 0 || source == NULL || sourceSize <= 0) {
return;
}
size_t numberToCopy = sourceSize < destinationSize ? sourceSize : destinationSize;
for (size_t i = 0; i < numberToCopy; i++)
{
destination[i] = source[i];
}
}
void hec_dss_array_copy_int(int* destination, const long destinationSize,
int* source, const size_t sourceSize) {
if (destination == NULL || destinationSize <= 0 || source == NULL || sourceSize <= 0) {
return;
}
size_t numberToCopy = sourceSize < destinationSize ? sourceSize : destinationSize;
for (size_t i = 0; i < numberToCopy; i++)
{
destination[i] = source[i];
}
}
HECDSS_API int hec_dss_open(const char* filename, dss_file** dss)
{
dss_file* f = (dss_file*)malloc(sizeof(dss_file));
if (f == NULL)
return -1;
int status = hec_dss_zopen(f->ifltab,filename);
if (status != 0) {
free(f);
return status;
}
int version = zgetVersion(f->ifltab);
if (version != 7) {
hec_dss_log_message("version is not supported.\nOnly version 7 DSS files are supported");
zclose(f->ifltab);
free(f);
return -700;
}
*dss = f;
return status;
}
HECDSS_API int hec_dss_close(dss_file *dss){
int status = zclose(dss->ifltab);
free(dss);
dss = NULL;
return status;
}
HECDSS_API int hec_dss_getVersion(dss_file* dss) {
if (!dss)
return 0;
return zgetVersion(dss->ifltab);
}
HECDSS_API int hec_dss_getFileVersion(const char* filename) {
return zgetFileVersion(filename);
}
HECDSS_API int hec_dss_set_value(const char* name, const int value) {
return zset(name, "", value);
}
HECDSS_API int hec_dss_set_string(const char* name, const char* value) {
return zset(name, value, 0);
}
HECDSS_API int hec_dss_record_count(dss_file* dss) {
if (!dss)
return 0;
// Use "npri" for primary only or "nali" for aliases only.
long long nrec = zinquire(dss->ifltab, "nrec");// includes aliases
return (int)nrec;
}
HECDSS_API int hec_dss_catalog(dss_file* dss, char* pathBuffer, int* recordTypes, const char* pathFilter,
const int count, const int pathBufferItemSize) {
zStructCatalog* catStruct = zstructCatalogNew();
int status = zcatalog(dss->ifltab,pathFilter, catStruct, UNSORTED);
if (status < 0) {
printf("Error during catalog. Error code %d\n", status);
return status;
}
int maxPaths = catStruct->numberPathnames > count ? count : catStruct->numberPathnames;
for (int i = 0; i < maxPaths; i++)
{
recordTypes[i] = catStruct->recordType[i];
char* s = pathBuffer + i * pathBufferItemSize;
if (catStruct->pathnameList[i] != NULL) {
stringCopy(s, pathBufferItemSize, catStruct->pathnameList[i], strlen(catStruct->pathnameList[i]));
}
}
zstructFree(catStruct);
return maxPaths;
}
HECDSS_API int hec_dss_tsGetDateTimeRange(dss_file* dss, const char* pathname, const int boolFullSet,
int* firstValidJulian, int* firstSeconds,
int* lastValidJulian, int* lastSeconds) {
int status = 0;
if (isTsPattern(pathname)) {
zStructTimeSeries* tss = zstructTsNewTimes(pathname, "", "", "", "");
tss->boolPattern = isTsPattern(pathname);
status = ztsRetrieve(dss->ifltab, tss, NO_TRIM_INCL_TIME_ARR, RETRIEVE_DOUBLES, RETRIEVE_QUAL_AND_NOTES);
*firstValidJulian = tss->startJulianDate;
*firstSeconds = tss->startTimeSeconds;
*lastValidJulian = tss->endJulianDate;
*lastSeconds = tss->endTimeSeconds;
zstructFree(tss);
}
else {
status = ztsGetDateTimeRange(dss->ifltab, pathname, boolFullSet,
firstValidJulian, firstSeconds,
lastValidJulian, lastSeconds);
}
return status;
}
HECDSS_API int hec_dss_numberPeriods(const int intervalSeconds, const int julianStart, const int startSeconds,
const int julianEnd, const int endSeconds) {
int num = numberPeriods(intervalSeconds, julianStart, startSeconds, julianEnd, endSeconds);
return num;
}
HECDSS_API int hec_dss_tsGetSizes(dss_file* dss, const char* pathname,
const char* startDate, const char* startTime,
const char* endDate, const char* endTime,
int* numberValues, int* qualityElementSize) {
zStructRecordSize* recordSize = zstructRecordSizeNew(pathname);
zStructTimeSeries* tss = zstructTsNew(pathname);
tss->startJulianDate = dateToJulian(startDate);
tss->startTimeSeconds = timeStringToSeconds(startTime);
tss->endJulianDate = dateToJulian(endDate);
tss->endTimeSeconds = timeStringToSeconds(endTime);
ztsProcessTimes(dss->ifltab, tss, 0);
int status = ztsGetSizes(dss->ifltab, tss, recordSize);
if (status == 0) {
*numberValues = recordSize->logicalNumberValues;
*qualityElementSize = recordSize->tsQualityElementSize;
}
if( tss)
zstructFree(tss);
if( recordSize)
zstructFree(recordSize);
return status;
}
HECDSS_API int hec_dss_tsRetrieveInfo(dss_file* pdss, const char* pathname,char* units,
const int unitsLength, char* type, const int typeLength) {
zStructTransfer* transfer = zstructTransferNew(pathname, 0);
zStructTimeSeries* tss = zstructTsNew(pathname);
transfer->internalHeaderMode = 1;
int status = zread(pdss->ifltab, transfer);
if (status == 0)
{
int intervalType = ztsProcessTimes(pdss->ifltab, tss, 0);
status = ztsInternalHeaderUnpack(tss, transfer->internalHeader, transfer->internalHeaderNumber);
if (tss->units != NULL) {
stringCopy(units, unitsLength, tss->units, strlen(tss->units));
}
if (tss->type != NULL) {
stringCopy(type, typeLength, tss->type, strlen(tss->type));
}
}
zstructFree(tss);
zstructFree(transfer);
return status;
}
HECDSS_API int hec_dss_tsRetrieve(dss_file* dss, const char *pathname,
const char *startDate, const char *startTime,
const char* endDate, const char *endTime,
int *timeArray, double *valueArray, const int arraySize,
int *numberValuesRead, int *quality, const int qualityWidth,
int* julianBaseDate,int* timeGranularitySeconds,
char* units,const int unitsLength,
char* type,const int typeLength,
char* timeZoneName, const int timeZoneNameLength)
{
*numberValuesRead = 0;
zStructTimeSeries* tss = zstructTsNewTimes(pathname, startDate, startTime, endDate, endTime);
tss->boolPattern = isTsPattern(pathname);
// if no dates/times are given retrieve all data.
if (
(startDate == NULL || startDate == "\0")
&& (startTime == NULL || startTime == "\0")
&& (endDate == NULL || endDate == "\0")
&& (endTime == NULL || endTime == "\0")
&& ! tss->boolPattern
)
{
if (!isDpartEmpty(pathname)) {
hec_dss_log_message("The D-part of the path will be ignored.");
hec_dss_log_message("Since a time-window was not provided requesting all time.");
}
tss->boolRetrieveAllTimes = 1;
}
int status = ztsRetrieve(dss->ifltab, tss, NO_TRIM_INCL_TIME_ARR, RETRIEVE_DOUBLES, RETRIEVE_QUAL_AND_NOTES);
if (status == 0) {
*julianBaseDate = tss->julianBaseDate;
*timeGranularitySeconds = tss->timeGranularitySeconds;
if (tss->units != NULL) {
stringCopy(units, unitsLength, tss->units, strlen(tss->units));
}
if (tss->type != NULL) {
stringCopy(type, typeLength, tss->type, strlen(tss->type));
}
if (tss->timeZoneName != NULL) {
stringCopy(timeZoneName, timeZoneNameLength, tss->timeZoneName, strlen(tss->timeZoneName));
}
int size = MIN(tss->numberValues, arraySize);
size = MAX(0, size);
*numberValuesRead = size;
for (int i = 0; i < size; i++) {
if (tss->times) {
timeArray[i] = tss->times[i];
}
valueArray[i] = tss->doubleValues[i];
if (qualityWidth > 0) // TO DO.. quality can have multiple columns
quality[i] = tss->quality[i];
}
}
zstructFree(tss);
return status;
}
HECDSS_API int hec_dss_tsStoreRegular(dss_file* dss, const char* pathname,
const char* startDate, const char* startTime,
double* valueArray, const int valueArraySize,
int* qualityArray, const int qualityArraySize,
const int saveAsFloat,
const char* units, const char* type, const char* timeZoneName, int storageFlag)
{
zStructTimeSeries* tss = 0;
if (saveAsFloat) {
float* values = hec_dss_double_array_to_float(valueArray, valueArraySize);
if (values == NULL)
{
hec_dss_log_message("Error allocating memory in hec_dss_tsStoreRegular ");
return -1;
}
tss = zstructTsNewRegFloats(pathname, values, valueArraySize, startDate, startTime, units, type);
tss->allocated[zSTRUCT_TS_floatValues];// zstructFree will free float array
if (qualityArraySize > 0 && qualityArraySize == valueArraySize)
tss->quality = qualityArray;
}
else {
tss = zstructTsNewRegDoubles(pathname, valueArray, valueArraySize, startDate, startTime, units, type);
}
tss->boolPattern = isTsPattern(pathname);
tss->timeZoneName = mallocAndCopy(timeZoneName);
tss->allocated[zSTRUCT_timeZoneName] = 1;
int status = ztsStore(dss->ifltab, tss, storageFlag);
zstructFree(tss);
return status;
}
HECDSS_API int hec_dss_tsStoreIregular(dss_file* dss, const char* pathname,
const char* startDateBase,
int* times,const int timeGranularitySeconds,
double* valueArray, const int valueArraySize,
int* qualityArray, const int qualityArraySize,
const int saveAsFloat,
const char* units, const char* type, const char* timeZoneName, int storageFlag)
{
zStructTimeSeries* tss = NULL;
if (saveAsFloat) {
float* values = hec_dss_double_array_to_float(valueArray, valueArraySize);
if (values == NULL) {
hec_dss_log_message("Error allocating memory in hec_dss_tsStoreIregular ");
return -1;
}
tss = zstructTsNewIrregFloats(pathname,values, valueArraySize,times,
timeGranularitySeconds, startDateBase,units, type);
tss->allocated[zSTRUCT_TS_floatValues];
}
else {
tss = zstructTsNewIrregDoubles(pathname, valueArray, valueArraySize, times,
timeGranularitySeconds, startDateBase, units, type);
}
if (qualityArraySize > 0 && qualityArraySize == valueArraySize) {
// TO DO.. quality can be multi-dimensional (Is that used?)
tss->quality = qualityArray;
tss->qualityArraySize = qualityArraySize;
}
tss->boolPattern = isTsPattern(pathname);
tss->timeZoneName = mallocAndCopy(timeZoneName);
tss->allocated[zSTRUCT_timeZoneName] = 1;
int status = ztsStore(dss->ifltab, tss, storageFlag);
zstructFree(tss);
return status;
}
HECDSS_API int hec_dss_locationRetrieve(dss_file* dss, const char* fullPath,
double* x,double* y, double* z,
int* coordinateSystem, int* coordinateID,
int* horizontalUnits,int* horizontalDatum,
int* verticalUnits, int* verticalDatum,
char* timeZoneName, const int timeZoneNameLength,
char* supplemental, const int supplementalLength){
zStructLocation* loc = zstructLocationNew(fullPath);
int status = zlocationRetrieve(dss->ifltab, loc);
if (status == 0) {
*x = loc->xOrdinate;
*y = loc->yOrdinate;
*z = loc->zOrdinate;
*coordinateSystem = loc->coordinateSystem;
*coordinateID = loc->coordinateID;
*verticalUnits = loc->verticalUnits;
*verticalDatum = loc->verticalDatum;
*horizontalUnits = loc->horizontalUnits;
*horizontalDatum = loc->horizontalDatum;
if (loc->timeZoneName != NULL) {
stringCopy(timeZoneName, timeZoneNameLength, loc->timeZoneName, strlen(loc->timeZoneName));
}
if (loc->supplemental != NULL) {
stringCopy(supplemental, supplementalLength, loc->supplemental, strlen(loc->supplemental));
}
}
zstructFree(loc);
return status;
}
HECDSS_API int hec_dss_locationStore(dss_file* dss, const char* fullPath,
const double x, const double y, const double z,
const int coordinateSystem, const int coordinateID,
const int horizontalUnits, const int horizontalDatum,
const int verticalUnits, const int verticalDatum,
const char* timeZoneName,
const char* supplemental,
const int replace) {
zStructLocation* loc = zstructLocationNew(fullPath);
loc->xOrdinate = x;
loc->yOrdinate = y;
loc->zOrdinate = z;
loc->coordinateSystem = coordinateSystem;
loc->coordinateID = coordinateID;
loc->verticalUnits = verticalUnits;
loc->verticalDatum = verticalDatum;
loc->horizontalDatum = horizontalDatum;
loc->horizontalUnits = horizontalUnits;
loc->timeZoneName = mallocAndCopy(timeZoneName);
loc->allocated[zSTRUCT_timeZoneName] = 1;
loc->supplemental = mallocAndCopy(supplemental);
loc->allocated[zSTRUCT_otherInformation] = 1;
int status = zlocationStore(dss->ifltab, loc, replace);
zstructFree(loc);
return status;
}
/// <summary>
/// hec_dss_pdRetrieveInfo is used by client app to determine
/// required array sizes for calling hec_dss_pdRetrieve.
///
/// </summary>
/// <param name="dss">pointer to dss_file</param>
/// <param name="pathname">path of paired data</param>
/// <param name="numberOrdinates">number of rows for ordinates, and curves</param>
/// <param name="numberCurves">number of columns in the curve dataset</param>
/// <returns></returns>
HECDSS_API int hec_dss_pdRetrieveInfo(dss_file* dss, const char* pathname,
int* numberOrdinates, int* numberCurves,
char* unitsIndependent, const int unitsIndependentLength,
char* unitsDependent, const int unitsDependentLength,
char* typeIndependent, const int typeIndependentLength,
char* typeDependent, const int typeDependentLength,
int* labelsLength) {
zStructPairedData* pds = zstructPdNew(pathname);
int status = zpdRetrieve(dss->ifltab, pds, RETRIEVE_DOUBLES);
*numberOrdinates = pds->numberOrdinates;
*numberCurves = pds->numberCurves;
if (pds->unitsIndependent != NULL) {
stringCopy(unitsIndependent, unitsIndependentLength, pds->unitsIndependent, strlen(pds->unitsIndependent));
}
if (pds->unitsDependent != NULL) {
stringCopy(unitsDependent, unitsDependentLength, pds->unitsDependent, strlen(pds->unitsDependent));
}
if (pds->typeIndependent != NULL) {
stringCopy(typeIndependent, typeIndependentLength, pds->typeIndependent, strlen(pds->typeIndependent));
}
if (pds->typeDependent != NULL) {
stringCopy(typeDependent, typeDependentLength, pds->typeDependent, strlen(pds->typeDependent));
}
if (pds->labels)
*labelsLength = pds->labelsLength;
zstructFree(pds);
return status;
}
HECDSS_API int hec_dss_dataType(dss_file* dss, const char* pathname) {
return zdataType(dss->ifltab, pathname);
}
HECDSS_API int hec_dss_recordType(dss_file* dss, const char* pathname) {
zStructRecordBasics* recordBasics = zstructRecordBasicsNew(pathname);
int status = zgetRecordBasics(dss->ifltab, recordBasics);
if (status != 0)
{
hec_dss_log_message("Error reading record type from path: ");
hec_dss_log_message(pathname); // TODO strcat
zstructFree(recordBasics);
return -1;
}
int rval = recordBasics->recordType;
zstructFree(recordBasics);
return rval;
}
HECDSS_API int hec_dss_pdRetrieve(dss_file* dss, const char* pathname,
double* doubleOrdinates, const int doubleOrdinatesLength,
double* doubleValues, const int doubleValuesLength,
int* numberOrdinates, int* numberCurves,
char* unitsIndependent, const int unitsIndependentLength,
char* typeIndependent, const int typeIndependentLength,
char* unitsDependent, const int unitsDependentLength,
char* typeDependent, const int typeDependentLength,
char* labels, const int labelsLength,
char* timeZoneName, const int timeZoneNameLength)
{
zStructPairedData* pds = zstructPdNew(pathname);
int status = zpdRetrieve(dss->ifltab, pds, RETRIEVE_DOUBLES);
if (pds->numberOrdinates != doubleOrdinatesLength && status == 0)
{
hec_dss_log_message("in hec_dss_pdRetrieve the doubleOrdinatesLength argument does not match what was read from disk.");
status = -1;
}
if (pds->numberCurves * pds->numberOrdinates != doubleValuesLength && status == 0)
{
hec_dss_log_message("in hec_dss_pdRetrieve the doubleValuesLength argument does not match what was read from disk.");
status = -1;
}
if (status == 0) {
*numberOrdinates = pds->numberOrdinates;
*numberCurves = pds->numberCurves;
/// -- leaving these meta-data below out for initial version.
//*boolIndependentIsXaxis = pds->boolIndependentIsXaxis;
//*xprecision = pds->xprecision;
// *yprecision = pds->yprecision;
if (pds->unitsIndependent != NULL) {
stringCopy(unitsIndependent, unitsIndependentLength, pds->unitsIndependent, strlen(pds->unitsIndependent));
}
if (pds->unitsDependent != NULL) {
stringCopy(unitsDependent, unitsDependentLength, pds->unitsDependent, strlen(pds->unitsDependent));
}
if (pds->typeIndependent != NULL) {
stringCopy(typeIndependent, typeIndependentLength, pds->typeIndependent, strlen(pds->typeIndependent));
}
if (pds->typeDependent != NULL) {
stringCopy(typeDependent, typeDependentLength, pds->typeDependent, strlen(pds->typeDependent));
}
if (pds->timeZoneName != NULL) {
stringCopy(timeZoneName, timeZoneNameLength, pds->timeZoneName, strlen(pds->timeZoneName));
}
if (pds->labels) {
int size = pds->labelsLength > labelsLength ? labelsLength :pds->labelsLength;
for (int i = 0; i < size; i++)
labels[i] = pds->labels[i];
}
hec_dss_array_copy_double(doubleOrdinates, doubleOrdinatesLength, pds->doubleOrdinates, pds->numberOrdinates);
hec_dss_array_copy_double(doubleValues, doubleValuesLength, pds->doubleValues, pds->numberOrdinates* pds->numberCurves);
}
zstructFree(pds);
return status;
}
HECDSS_API int hec_dss_pdStore(dss_file* dss, const char* pathname,
double* doubleOrdinates, const int doubleOrdinatesLength,
double* doubleValues, const int doubleValuesLength,
const int numberOrdinates, const int numberCurves,
const char* unitsIndependent,
const char* typeIndependent,
const char* unitsDependent,
const char* typeDependent,
const char* labels, const int labelsLength,
const char* timeZoneName)
{
zStructPairedData* pds = zstructPdNewDoubles(pathname, doubleOrdinates, doubleValues,
numberOrdinates, numberCurves, unitsIndependent, typeIndependent, unitsDependent, typeDependent);
/// -- leaving these meta-data below out for initial version.
//*boolIndependentIsXaxis = pds->boolIndependentIsXaxis;
//*xprecision = pds->xprecision;
// *yprecision = pds->yprecision;
if (labels != NULL) {
pds->labels = malloc(labelsLength);
pds->allocated[zSTRUCT_PD_labels] = 1;
if (pds->labels == NULL)
return -1;
pds->labelsLength = labelsLength;
memcpy(pds->labels, labels, labelsLength);
}
pds->timeZoneName = mallocAndCopy(timeZoneName);
pds->allocated[zSTRUCT_timeZoneName] = 1;
int status = zpdStore(dss->ifltab, pds, PD_STORE_DOUBLE);
zstructFree(pds);
return status;
}
HECDSS_API int hec_dss_gridRetrieve(dss_file* dss, const char* pathname, int boolRetrieveData,
int* type, int* dataType,
int* lowerLeftCellX, int* lowerLeftCellY,
int* numberOfCellsX, int* numberOfCellsY,
int* numberOfRanges, int* srsDefinitionType,
int* timeZoneRawOffset, int* isInterval,
int* isTimeStamped,
char* dataUnits, const int dataUnitsLength,
char* dataSource, const int dataSourceLength,
char* srsName, const int srsNameLength,
char* srsDefinition, const int srsDefinitionLength,
char* timeZoneID, const int timeZoneIDLength,
float* cellSize, float* xCoordOfGridCellZero,
float* yCoordOfGridCellZero, float* nullValue,
float* maxDataValue, float* minDataValue,
float* meanDataValue,
float* rangeLimitTable, const int rangeTablesLength,
int* numberEqualOrExceedingRangeLimit,
float* data, const int dataLength ) {
zStructSpatialGrid* gridStruct = zstructSpatialGridNew(pathname);
int status = zspatialGridRetrieve(dss->ifltab, gridStruct, boolRetrieveData);
if (status == 0) {
*type = gridStruct->_type;
*dataType = gridStruct->_dataType;
*lowerLeftCellX = gridStruct->_lowerLeftCellX;
*lowerLeftCellY = gridStruct->_lowerLeftCellY;
*numberOfCellsX = gridStruct->_numberOfCellsX;
*numberOfCellsY = gridStruct->_numberOfCellsY;
*numberOfRanges = gridStruct->_numberOfRanges;
*srsDefinitionType = gridStruct->_srsDefinitionType;
*timeZoneRawOffset = gridStruct->_timeZoneRawOffset;
*isInterval = gridStruct->_isInterval;
*isTimeStamped = gridStruct->_isTimeStamped;
if (gridStruct->_dataUnits != NULL) {
stringCopy(dataUnits, dataUnitsLength, gridStruct->_dataUnits, strlen(gridStruct->_dataUnits));
}
if (gridStruct->_dataSource != NULL) {
stringCopy(dataSource, dataSourceLength, gridStruct->_dataSource, strlen(gridStruct->_dataSource));
}
if (gridStruct->_srsName != NULL) {
stringCopy(srsName, srsNameLength, gridStruct->_srsName, strlen(gridStruct->_srsName));
}
if (gridStruct->_srsDefinition != NULL) {
stringCopy(srsDefinition, srsDefinitionLength, gridStruct->_srsDefinition, strlen(gridStruct->_srsDefinition));
}
if (gridStruct->_timeZoneID != NULL) {
stringCopy(timeZoneID, timeZoneIDLength, gridStruct->_timeZoneID, strlen(gridStruct->_timeZoneID));
}
*cellSize = gridStruct->_cellSize;
*xCoordOfGridCellZero = gridStruct->_xCoordOfGridCellZero;
*yCoordOfGridCellZero = gridStruct->_yCoordOfGridCellZero;
*nullValue = gridStruct->_nullValue;
if (boolRetrieveData && gridStruct->_storageDataType == GRID_FLOAT)
{
*maxDataValue = *((float*)gridStruct->_maxDataValue);
*minDataValue = *((float*)gridStruct->_minDataValue);
*meanDataValue = *((float*)gridStruct->_meanDataValue);
int size = *numberOfRanges > rangeTablesLength ? rangeTablesLength : *numberOfRanges;
float* table = (float*)gridStruct->_rangeLimitTable;
for (int i = 0; i < size; i++) {
rangeLimitTable[i] = table[i];
numberEqualOrExceedingRangeLimit[i] = gridStruct->_numberEqualOrExceedingRangeLimit[i];
}
size = gridStruct->_numberOfCellsX * gridStruct->_numberOfCellsY;
size = size > dataLength ? dataLength : size;
table = (float*)gridStruct->_data;
for (int i = 0; i < size; i++) {
data[i] = table[i];
}
}
}
zstructFree(gridStruct);
return status;
}
float* hec_dss_allocate_float(float value) {
float* tmp = calloc(1, sizeof(float));
if( tmp != NULL)
*tmp = value;
return tmp;
}
HECDSS_API int hec_dss_gridStore(dss_file* dss, const char* pathname,
const int gridType, const int dataType,
const int lowerLeftCellX, const int lowerLeftCellY,
const int numberOfCellsX, const int numberOfCellsY,
const int numberOfRanges, const int srsDefinitionType,
const int timeZoneRawOffset, int isInterval,
const int isTimeStamped,
const int compressionSize,
const char* dataUnits,
const char* dataSource,
const char* srsName,
const char* srsDefinition,
const char* timeZoneID,
const float cellSize, const float xCoordOfGridCellZero,
const float yCoordOfGridCellZero, const float nullValue,
const float maxDataValue, const float minDataValue,
const float meanDataValue,
float* rangeLimitTable,
int* numberEqualOrExceedingRangeLimit,
float* data) {
zStructSpatialGrid* gridStruct = zstructSpatialGridNew(pathname);
gridStruct->_type = gridType;
gridStruct->_dataType = dataType;
gridStruct->_lowerLeftCellX = lowerLeftCellX;
gridStruct->_lowerLeftCellY = lowerLeftCellY;
gridStruct->_numberOfCellsX = numberOfCellsX;
gridStruct->_numberOfCellsY = numberOfCellsY;
gridStruct->_numberOfRanges = numberOfRanges;
gridStruct->_srsDefinitionType = srsDefinitionType;
gridStruct->_timeZoneRawOffset = timeZoneRawOffset;
gridStruct->_isInterval = isInterval;
gridStruct->_isTimeStamped = isTimeStamped;
gridStruct->_compressionMethod = ZLIB_COMPRESSION;
gridStruct->_dataUnits = mallocAndCopy(dataUnits);
gridStruct->_dataSource = mallocAndCopy(dataSource);
gridStruct->_srsName = mallocAndCopy(srsName);
gridStruct->_srsDefinition = mallocAndCopy(srsDefinition);
gridStruct->_timeZoneID = mallocAndCopy(timeZoneID);
gridStruct->_cellSize = cellSize;
gridStruct->_xCoordOfGridCellZero = xCoordOfGridCellZero;
gridStruct->_yCoordOfGridCellZero = yCoordOfGridCellZero;
gridStruct->_nullValue = nullValue;
gridStruct->_storageDataType = GRID_FLOAT;
gridStruct->_minDataValue = hec_dss_allocate_float(minDataValue);
gridStruct->_maxDataValue = hec_dss_allocate_float(maxDataValue);
gridStruct->_meanDataValue = hec_dss_allocate_float(meanDataValue);
gridStruct->_rangeLimitTable = rangeLimitTable;
gridStruct->_numberEqualOrExceedingRangeLimit = numberEqualOrExceedingRangeLimit;
gridStruct->_numberOfRanges = numberOfRanges;
gridStruct->_data = data;
int status = zspatialGridStore_extended(dss->ifltab, gridStruct, compressionSize);
// set NULL address to prevent zstructFree from freeing items below (they are owned by caller)
gridStruct->_numberEqualOrExceedingRangeLimit = NULL;
gridStruct->_rangeLimitTable = NULL;
gridStruct->_data = NULL;
zstructFree(gridStruct);
return status;
}
HECDSS_API int hec_dss_dateToYearMonthDay(const char* date,int*year, int* month, int* day) {
return dateToYearMonthDay(date, year, month, day);
}
HECDSS_API int hec_dss_delete(dss_file* dss, const char* pathname) {
int status = zdelete(dss->ifltab, pathname);
return status;
}
HECDSS_API int hec_dss_squeeze( const char* pathname) {
int status = zsqueeze( pathname);
return status;
}
HECDSS_API int hec_dss_dateToJulian(const char* date){
return dateToJulian(date);
}
HECDSS_API void hec_dss_julianToYearMonthDay(const int julian, int* year, int* month,int* day){
julianToYearMonthDay(julian, year, month, day);
}
HECDSS_API int hec_dss_arrayStore(dss_file* dss, const char* pathname,
int* intValues,const int intValuesLength,
float* floatValues,const int floatValuesLength,
double* doubleValues,const int doubleValuesLength){
zStructArray* array;
array = zstructArrayNew(pathname);
if (intValues != 0 && intValuesLength > 0) {
array->intArray = intValues;
array->numberIntArray = intValuesLength;
}
if (floatValues != 0 && floatValuesLength > 0) {
array->floatArray = floatValues;
array->numberFloatArray = floatValuesLength;
}
if (doubleValues != 0 && doubleValuesLength > 0) {
array->doubleArray = doubleValues;
array->numberDoubleArray = doubleValuesLength;
}
int status = zarrayStore(dss->ifltab, array);
return status;
}
HECDSS_API int hec_dss_arrayRetrieveInfo(dss_file* dss, const char* pathname,
int* intValuesRead, int* floatValuesRead, int* doubleValuesRead) {
zStructArray* array;
array = zstructArrayNew(pathname);
int status = zarrayRetrieve(dss->ifltab, array);
if (status == STATUS_OKAY) {
*intValuesRead = array->numberIntArray;
*floatValuesRead = array->numberFloatArray;
*doubleValuesRead = array->numberDoubleArray;
}
return status;
}
HECDSS_API int hec_dss_arrayRetrieve(dss_file* dss, const char* pathname,
int* intValues, const int intValuesLength,
float* floatValues, const int floatValuesLength,
double* doubleValues, const int doubleValuesLength){
zStructArray* array = zstructArrayNew(pathname);
int status = zarrayRetrieve(dss->ifltab, array);
if (status == STATUS_OKAY) {
if (intValuesLength == array->numberIntArray) {
hec_dss_array_copy_int(intValues, intValuesLength, array->intArray, array->numberIntArray);
}
if (floatValuesLength == array->numberFloatArray) {
hec_dss_array_copy_float(floatValues, floatValuesLength, array->floatArray, array->numberFloatArray);
}
if (doubleValuesLength == array->numberDoubleArray) {
hec_dss_array_copy_double(doubleValues, doubleValuesLength, array->doubleArray, array->numberDoubleArray);
}
}
return status;
}
HECDSS_API int hec_dss_textStore(dss_file* dss, const char* pathname, const char* text, int length) {
zStructText* txt = zstructTextNew(pathname);
txt->textString = mallocAndCopy(text);
txt->allocated[zSTRUCT_TX_textString] = 1;
txt->numberTextChars = length;
int status = ztextStore(dss->ifltab, txt);
zstructFree(txt);
return status;
}
HECDSS_API int hec_dss_textRetrieve(dss_file* dss, const char* pathname, char* buffer, const int bufferLength) {
zStructText* txt = zstructTextNew(pathname);
int status = ztextRetrieve(dss->ifltab, txt);
if (status == 0) {
if (txt->numberTextChars > bufferLength) {
status = HEC_DSS_BUFFER_TOO_SMALL;
}
strncpy(buffer, txt->textString, MIN(bufferLength, txt->numberTextChars));
}
zstructFree(txt);
return status;
}