static int init_Vdatas(coda_hdf4_product *product) { int32 vdata_ref; vdata_ref = VSgetid(product->file_id, -1); while (vdata_ref != -1) { if (product->num_vdata % BLOCK_SIZE == 0) { coda_hdf4_Vdata **vdata; int i; vdata = realloc(product->vdata, (product->num_vdata + BLOCK_SIZE) * sizeof(coda_hdf4_Vdata *)); if (vdata == NULL) { coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)", (long)(product->num_vdata + BLOCK_SIZE) * sizeof(coda_hdf4_Vdata *), __FILE__, __LINE__); return -1; } product->vdata = vdata; for (i = product->num_vdata; i < product->num_vdata + BLOCK_SIZE; i++) { product->vdata[i] = NULL; } } product->num_vdata++; product->vdata[product->num_vdata - 1] = coda_hdf4_Vdata_new(product, vdata_ref); if (product->vdata[product->num_vdata - 1] == NULL) { return -1; } vdata_ref = VSgetid(product->file_id, vdata_ref); } return 0; }
int main( ) { /************************* Variable declaration **************************/ intn status_n; /* returned status for functions returning an intn */ int32 status_32, /* returned status for functions returning an int32 */ file_id, vdata_id, vdata_ref, index = 0; /* index of the vdata in the file - manually kept */ int8 found_fields; /* TRUE if the specified fields exist in the vdata */ /********************** End of variable declaration **********************/ /* * Open the HDF file for reading. */ file_id = Hopen (FILE_NAME, DFACC_READ, 0); /* * Initialize the VS interface. */ status_n = Vstart (file_id); /* * Set the reference number to -1 to start the search from * the beginning of file. */ vdata_ref = -1; /* * Assume that the specified fields are not found in the current vdata. */ found_fields = FALSE; /* * Use VSgetid to obtain each vdata by its reference number then * attach to the vdata and search for the fields. The loop * terminates when the last vdata is reached or when a vdata which * contains the fields listed in SEARCHED_FIELDS is found. */ while ((vdata_ref = VSgetid (file_id, vdata_ref)) != FAIL) { vdata_id = VSattach (file_id, vdata_ref, "r"); if ((status_n = VSfexist (vdata_id, SEARCHED_FIELDS)) != FAIL) { found_fields = TRUE; break; } /* * Detach from the current vdata before continuing searching. */ status_32 = VSdetach (vdata_id); index++; /* advance the index by 1 for the next vdata */ } /* * Print the index of the vdata containing the fields or a "not found" * message if no such vdata is found. Also detach from the vdata found. */ if (!found_fields) printf ("Fields Position and Temperature were not found.\n"); else { printf ("Fields Position and Temperature found in the vdata at position %d\n", index); status_32 = VSdetach (vdata_id); } /* * Terminate access to the VS interface and close the HDF file. */ status_n = Vend (file_id); status_32 = Hclose (file_id); return 0; }
void main(void) { char vdata_name[MAX_NC_NAME], vdata_class[MAX_NC_NAME]; char fields[60]; int32 file_id, vdata_id, istat; int32 n_records, interlace, vdata_size, vdata_ref; int bufsz = (2 * sizeof(float32) + sizeof(char) \ + sizeof(int16)) * NRECORDS; int i; uint8 databuf[((2 * sizeof(float32)) + sizeof(char) \ + sizeof(int16)) * NRECORDS]; VOIDP fldbufptrs[4]; float32 itemp[NRECORDS], ispeed[NRECORDS]; int16 iheight[NRECORDS]; char idents[NRECORDS]; /* Open the HDF file. */ file_id = Hopen("VD_Ex4.hdf", DFACC_READ, 0); /* Initialize the Vset interface. */ istat = Vstart(file_id); /* * Get the reference number for the first Vdata in * the file. */ vdata_ref = -1; vdata_ref = VSgetid(file_id, vdata_ref); /* Attach to the first Vdata in read mode. */ vdata_id = VSattach(file_id, vdata_ref, "r"); for (i=0; i<60; i++) fields[i] = '\0'; /* Get the list of field names. */ istat =VSinquire(vdata_id, &n_records, &interlace, fields, &vdata_size, vdata_name); printf("files: %s, n_records: %d, vdata_size: %d\n", fields, n_records, vdata_size); /* Get the class. */ istat = VSgetclass(vdata_id, vdata_class); /* Determine the fields that will be read. */ istat = VSsetfields(vdata_id, fields); /* Print the Vdata information. */ printf("Current Vdata name: %s \nCurrent Vdata class: %s.\n", vdata_name, vdata_class); /* Read the data. */ istat = VSread(vdata_id, (VOIDP)databuf, n_records, FULL_INTERLACE); /* set fldbufptrs and unpack field values */ fldbufptrs[0] = &idents[0]; fldbufptrs[1] = &ispeed[0]; fldbufptrs[2] = &iheight[0]; fldbufptrs[3] = &itemp[0]; istat = VSfpack(vdata_id, _HDF_VSUNPACK, fields, databuf, bufsz, n_records, "Ident,Speed,Height,Temp", fldbufptrs); printf(" Temp Height Speed Ident\n"); for (i=0; i < n_records; i++) { printf(" %6.2f %6d %6.2f %c\n", itemp[i],iheight[i],ispeed[i],idents[i]); } /* Detach from the Vdata, close the interface and the file. */ istat = VSdetach(vdata_id); istat = Vend(file_id); istat = Hclose(file_id); }