Arrays
HEC-DSS can store generic arrays of data for general use. An array can be an integer array, a float array, or a double array. The array size can be of any length, from one element long to as many as memory will allow.
Arrays are passed in a "zStructArray", using the function "zarrayRetrieve" to read from DSS and "zarrayStore" to write to DSS. "zstructArrayNew()" is used to create a zStructArray. When storing an array, you must set the array in zStructArray, and the number of elements in the array. The array types are:
int *intArray;
int numberIntArray;
float *floatArray;
int numberFloatArray;
double *doubleArray;
int numberDoubleArray;
All three data type arrays can be used or stored in a single zStructArray. Generally, the type of array that is stored is determined by checking for the existence of each array.
There are no absolute conventions for an array pathname, but it is recommended to follow other conventions as much as possible:
/Group Name/Location Name/Parameter/Date/Undefined/Version/
The primary functions for Arrays are:
- zStructArray* zstructArrayNew(const char* pathname)
- int zarrayRetrieve(long long *ifltab, zStructArray *arrayStruct)
- int zarrayStore(long long *ifltab, zStructArray *arrayStruct);
Where zero (0) is returned on a successful call and a negative value for unsuccessful. Do not reuse objects or containers; create a new one for every write or read.
zStructArray
#ifndef zSTRUCT_ARRAY_H
#define zSTRUCT_ARRAY_H
#include "zStructAllocation.h"
typedef struct {
// Private
int structType;
/* Required */
char *pathname;
int *intArray;
int numberIntArray;
float *floatArray;
int numberFloatArray;
double *doubleArray;
int numberDoubleArray;
/* User header, used for further description of data/gage */
/* Not often used */
int *userHeader;
int userHeaderSize; // Size user header array; maximum to read
int userHeaderNumber; // number read
// Informational only (on return only)
int dataType;
long long lastWrittenTime; // Seconds since 1970
long long fileLastWrittenTime;
char programName[17];
int numberAttributes;
char **attributeKeys;
char **attributes;
// Private - knowing which variables were allocated by the new functions,
// instead of the calling program
char allocated[zSTRUCT_length];
} zStructArray;
#endif //zSTRUCT_ARRAY_H
Example: Arrays Example
#include <stdio.h>
#include "heclib.h"
int ExampleArrays()
{
long long ifltab[250];
zStructArray *arrayStruct1;
zStructArray *arrayStruct2;
int intArray[500];
float floatArray[500];
double doubleArray[500];
int status;
int i;
// Open the DSS file; Create if it doesn't exist
status = zopen(ifltab, "C:/temp/Example7.dss");
// If an error occured, messages will be printed to standard out
if (status != STATUS_OKAY) return status;
// Gen up data
arrayStruct1 = zstructArrayNew("/Group/Location/Array Example/d/e/f/");
for (i = 0; i < 500; i++) {
intArray[i] = i + 1;
floatArray[i] = (float)i * (float)10.0;
doubleArray[i] = (double)i * 100.0;
}
arrayStruct1->intArray = intArray;
arrayStruct1->numberIntArray = 500;
arrayStruct1->floatArray = floatArray;
arrayStruct1->numberFloatArray = 500;
arrayStruct1->doubleArray = doubleArray;
arrayStruct1->numberDoubleArray = 500;
// Store
status = zarrayStore(ifltab, arrayStruct1);
if (status != STATUS_OKAY) return status;
// Retrieve and compare
arrayStruct2 = zstructArrayNew("/Group/Location/Array Example/d/e/f/");
status = zarrayRetrieve(ifltab, arrayStruct2);
if (status != STATUS_OKAY) return status;
printf("\nFirst 10 values are:\n");
for (i = 0; i < 10; i++) {
printf("%d: int = %d, float = %f, double = %f\n", i, arrayStruct2→intArray[i],
arrayStruct2->floatArray[i], arrayStruct2→doubleArray[i]);
}
// Now compare
status = 0;
for (i = 0; i < 500; i++) {
if (arrayStruct1->intArray[i] != arrayStruct2->intArray[i]) {
printf("int array does not match at ordinate %d\n", i);
status = 1;
break;
}
if (arrayStruct1->floatArray[i] != arrayStruct2->floatArray[i]) {
printf("float array does not match at ordinate %d\n", i);
status = 1;
break;
}
if (arrayStruct1->doubleArray[i] != arrayStruct2->doubleArray[i]) {
printf("double array does not match at ordinate %d\n", i);
status = 1;
break;
}
}
if (status) {
printf("Comparison failed\n");
}
else {
printf("Comparison sucessfull\n");
}
zstructFree(arrayStruct1);
zstructFree(arrayStruct2);
zclose(ifltab);
return 0;
}
=== output from program above ===
--------------------------------------------------
First 10 values are:
0: int = 1, float = 0.000000, double = 0.000000
1: int = 2, float = 10.000000, double = 100.000000
2: int = 3, float = 20.000000, double = 200.000000
3: int = 4, float = 30.000000, double = 300.000000
4: int = 5, float = 40.000000, double = 400.000000
5: int = 6, float = 50.000000, double = 500.000000
6: int = 7, float = 60.000000, double = 600.000000
7: int = 8, float = 70.000000, double = 700.000000
8: int = 9, float = 80.000000, double = 800.000000
9: int = 10, float = 90.000000, double = 900.000000
Comparison successful