Note: Because the import module is very simple and does not require the traversal of input Fields, the Module Builder is not used in this example. (This C program is also found in /usr/lpp/dx/samples/program_guide/simpleimportfilter.c
01 #include <dx/dx.h>
df.h is a necessary include file for HDF library routines.
02 #include <df.h> 03 04 #define MAXRANK 3 05 06 Error m_SimpleImport(Object *in, Object *out) 07 { 08 Array a=NULL; 09 Field f=NULL; 10 char *filename; 11 int dims, counts[MAXRANK], numelements, i, j; 12 float deltas[MAXRANK*MAXRANK], origins[MAXRANK], *data;
Extract the file name from in[0], and check that it is a string.
13 if (!in[0]) { 14 DXSetError(ERROR_BAD_PARAMETER,"missing filename"); 15 goto error; 16 } 17 else if (!DXExtractString(in[0], &filename)) { 18 DXSetError(ERROR_BAD_PARAMETER, "filename must be a string"); 19 goto error; 20 }
The HDF library routine DFishdf checks the file for accessibility and for the correct (HDF) format. If the file is not accessible or is not an HDF file, the routine generates an error message.
21 if (DFishdf(filename) != 0) { 22 DXSetError(ERROR_BAD_PARAMETER, 23 "file \"%s\" is not accessible, or is not an hdf file", 24 filename); 25 goto error; 26 } 27
Initialize the HDF library.
28 DFSDrestart();
The HDF library routine DFSDgetdims returns the dimensionality of the grid (1D, 2D, etc.) in dims. The number of positions in each dimension is returned in the Array counts.
29 DFSDgetdims(filename, &dims, counts, MAXRANK);
Make a new Array (scalar).
30 a = DXNewArray(TYPE_FLOAT, CATEGORY_REAL, 0); 31 if (!a) 32 goto error;
Determine the number of elements in the data Array.
33 numelements=1; 34 for (i=0; i<dims; i++) { 35 numelements= numelements * counts[i]; 36 }
Allocate space to the data Array.
37 if (!DXAddArrayData(a, 0, numelements, NULL)) 38 goto error;
Get a pointer to memory for the data Array.
39 data = (float *)DXGetArrayData(a); 40 if (!data) 41 goto error;
The HDF library routine DFSDgetdata reads the data from the HDF file to the data Array.
42 DFSDgetdata(filename, dims, counts, data);
Create a new Field.
43 f = DXNewField(); 44 if (!f) 45 goto error;
Set the dependency of the data to "positions."
46 if (!DXSetStringAttribute((Object)a, "dep", "positions")) 47 goto error;
Set the data Array as the data component of f.
48 if (!DXSetComponentValue(f, "data", (Object)a)) 49 goto error; 50 a=NULL;
Create the connections Array. DXMakeGridConnections sets up the element type. Place the connections in the Field.
51 a = DXMakeGridConnectionsV(dims, counts); 52 if (!a) 53 goto error; 54 if (!DXSetComponentValue(f, "connections", (Object)a)) 55 goto error; 56 a=NULL;
Now create the position origin and deltas for the position (origin 0 and deltas 1 in each dimension).
57 for (i=0; i<dims; i++) { 58 origins[i] = 0.0; 59 for (j=0; j<dims; j++) { 60 if (i==j) 61 deltas[i*dims + j] = 1.0; 62 else 63 deltas[i*dims + j] = 0.0; 64 } 65 }
Create the positions Array and place it in the Field.
66 67 a = DXMakeGridPositionsV(dims, counts, origins, deltas); 68 if (!a) 69 goto error; 70 if (!DXSetComponentValue(f, "positions", (Object)a)) 71 goto error; 72 a=NULL;
DXEndField sets default attributes and creates the bounding box.
73 if (!DXEndField(f)) 74 goto error; 75
Set f as the first output of the module.
76 out[0]=f; 77 return OK; 78
On error, delete f and a.
79 error: 80 DXDelete((Object)f); 81 DXDelete((Object)a); 82 return ERROR; 83 }
[Data Explorer Home Page | Contact Data Explorer | Same document on Data Explorer Home Page ]