intn write_attr(char *fname) { int32 sd_id, sds_id; intn status; /* * Open the file and initialize the SD interface. */ sd_id = SDstart (fname, DFACC_WRITE); if (sd_id == FAIL) { fprintf(stderr, "SDstart() failed.\n"); return -1; } /* Get the identifier for the data set "Latitude". */ sds_id = SDselect(sd_id, 0); if (sds_id == FAIL) { fprintf(stderr, "SDselect() failed.\n"); return -1; } /* Set an attribute. */ status = SDsetattr(sds_id, "units", DFNT_CHAR8, 13, (VOIDP)"degrees_north"); if (status == FAIL) { fprintf(stderr, "SDsetattr() failed.\n"); return -1; } /* * Terminate access to the data set. */ status = SDendaccess (sds_id); /* Get the identifier for the data set "Longitude". */ sds_id = SDselect(sd_id, 1); if (sds_id == FAIL) { fprintf(stderr, "SDselect() failed.\n"); return -1; } /* Set an attribute. */ status = SDsetattr(sds_id, "units", DFNT_CHAR8, 12, (VOIDP)"degrees_east"); if (status == FAIL) { fprintf(stderr, "SDsetattr() failed.\n"); return -1; } /* * Terminate access to the data set. */ status = SDendaccess (sds_id); /* * Terminate access to the SD interface and close the file. */ status = SDend (sd_id); return 0; }
bool PutAttrString(int32 sds_id, Myhdf_attr_t *attr, char *string) /* !C****************************************************************************** !Description: 'PutAttrString' writes an attribute from a parameter of type 'double' to a HDF file. !Input Parameters: sds_id SDS id attr Attribute data structure; the following fields are used: name, type, nval !Output Parameters: val An array of values from the HDF attribute (converted from type 'double' to the native type (returns) Status: 'true' = okay 'false' = error writing the attribute information !Team Unique Header: ! Design Notes: 1. The values in the attribute are converted from the stored type to 'double' type. 2. The HDF file is assumed to be open for SD (Science Data) access. 3. If the attribute has more than 'MYHDF_MAX_NATTR_VAL' values, an error status is returned. 4. Error messages are handled with the 'RETURN_ERROR' macro. !END**************************************************************************** */ { char8 val_char8[MYHDF_MAX_NATTR_VAL]; int i; void *buf; if (attr->nval <= 0 || attr->nval > MYHDF_MAX_NATTR_VAL) RETURN_ERROR("invalid number of values", "PutAttrString", false); if (attr->type != DFNT_CHAR8) RETURN_ERROR("invalid type -- not string (char8)", "PutAttrString", false); if (sizeof(char8) == sizeof(char)) buf = (void *)string; else { for (i = 0; i < attr->nval; i++) val_char8[i] = (char8)string[i]; buf = (void *)val_char8; } if (SDsetattr(sds_id, attr->name, attr->type, attr->nval, buf) == HDF_ERROR) RETURN_ERROR("setting attribute", "PutAttrString", false); return true; }
/* Write global attributes to HDF output file. Returns 0 if no errors, non-zero otherwise. */ int write_global_attributes(int32 sd_id, char *MOD021KMfile, char *MOD02HKMfile, char *MOD02QKMfile, float maxsolz, int sealevel, int TOA, int nearest) { char *ptr; int j; float32 f32; uint8 u8; ptr = PROCESS_VERSION_NUMBER; j = strlen(ptr); if (SDsetattr(sd_id, "ProcessVersionNumber", DFNT_CHAR8, j, ptr)) return 1; if (MOD021KMfile) { j = strlen(MOD021KMfile); if (SDsetattr(sd_id, "1km_input_file", DFNT_CHAR8, j, MOD021KMfile)) return 1; } if (MOD02HKMfile) { j = strlen(MOD02HKMfile); if (SDsetattr(sd_id, "500m_input_file", DFNT_CHAR8, j, MOD02HKMfile)) return 1; } if (MOD02QKMfile) { j = strlen(MOD02QKMfile); if (SDsetattr(sd_id, "250m_input_file", DFNT_CHAR8, j, MOD02QKMfile)) return 1; } f32 = maxsolz; if (SDsetattr(sd_id, "MaxSolarZenithAngle", DFNT_FLOAT32, 1, &f32)) return 1; u8 = (uint8) sealevel; if (SDsetattr(sd_id, "sealevel", DFNT_UINT8, 1, &u8)) return 1; u8 = (uint8) TOA; if (SDsetattr(sd_id, "toa", DFNT_UINT8, 1, &u8)) return 1; u8 = (uint8) nearest; if (SDsetattr(sd_id, "nearest", DFNT_UINT8, 1, &u8)) return 1; return 0; }
/****************************************************************************** MODULE: put_attr_string PURPOSE: Writes a string attribute to the HDF file. RETURN VALUE: Type = int Value Description ----- ----------- ERROR Error occurred writing this attribute SUCCESS Successful completion PROJECT: Land Satellites Data System Science Research and Development (LSRD) at the USGS EROS HISTORY: Date Programmer Reason -------- --------------- ------------------------------------- 1/3/2012 Gail Schmidt Original Development (based on input routines from the LEDAPS lndsr application) NOTES: ******************************************************************************/ int put_attr_string ( int32 sds_id, /* I: SDS ID to write attribute to */ Espa_hdf_attr_t *attr, /* I: attribute data structure */ char *string /* I: string value to be written */ ) { char FUNC_NAME[] = "put_attr_string"; /* function name */ char errmsg[STR_SIZE]; /* error message */ int i; /* looping variable for the number of values */ void *buf = NULL; /* void pointer to actual data array */ char8 val_char8[MYHDF_MAX_STRING]; if (attr->nval <= 0 || attr->nval > MYHDF_MAX_STRING) { sprintf (errmsg, "Invalid number of attribute values"); error_handler (true, FUNC_NAME, errmsg); return (ERROR); } /* Validate the data type is character */ if (attr->type != DFNT_CHAR8) { sprintf (errmsg, "Invalid data type - should be string (char8)"); error_handler (true, FUNC_NAME, errmsg); return (ERROR); } /* Set up the void buffer and write the attribute */ if (sizeof(char8) == sizeof(char)) buf = (void *)string; else { for (i = 0; i < attr->nval; i++) val_char8[i] = (char8)string[i]; buf = (void *)val_char8; } if (SDsetattr(sds_id, attr->name, attr->type, attr->nval, buf) == HDF_ERROR) { sprintf (errmsg, "Error writing attribute"); error_handler (true, FUNC_NAME, errmsg); return (ERROR); } return (SUCCESS); }
/****************************************************************************** MODULE: put_attr_double PURPOSE: Writes an attribute from a parameter type of double to the HDF file. The double value is converted to the native data type before writing. RETURN VALUE: Type = int Value Description ----- ----------- ERROR Error occurred writing this attribute SUCCESS Successful completion PROJECT: Land Satellites Data System Science Research and Development (LSRD) at the USGS EROS HISTORY: Date Programmer Reason -------- --------------- ------------------------------------- 1/3/2012 Gail Schmidt Original Development (based on input routines from the LEDAPS lndsr application) 1/8/2013 Gail Schmidt Modified to not add 0.5 to the floating point attribute values before writing to the HDF file. That is only appropriate when converting float to int and not float to float. NOTES: ******************************************************************************/ int put_attr_double ( int32 sds_id, /* I: SDS ID to write attribute to */ Espa_hdf_attr_t *attr, /* I: attribute data structure */ double *val /* I: array of values to be written as native type */ ) { char FUNC_NAME[] = "put_attr_double"; /* function name */ char errmsg[STR_SIZE]; /* error message */ int i; /* looping variable for the number of values */ void *buf = NULL; /* void pointer to actual data array */ char8 val_char8[MYHDF_MAX_NATTR_VAL]; int8 val_int8[MYHDF_MAX_NATTR_VAL]; uint8 val_uint8[MYHDF_MAX_NATTR_VAL]; int16 val_int16[MYHDF_MAX_NATTR_VAL]; uint16 val_uint16[MYHDF_MAX_NATTR_VAL]; int32 val_int32[MYHDF_MAX_NATTR_VAL]; uint32 val_uint32[MYHDF_MAX_NATTR_VAL]; float32 val_float32[MYHDF_MAX_NATTR_VAL]; float64 val_float64[MYHDF_MAX_NATTR_VAL]; if (attr->nval <= 0 || attr->nval > MYHDF_MAX_NATTR_VAL) { sprintf (errmsg, "Invalid number of attribute values"); error_handler (true, FUNC_NAME, errmsg); return (ERROR); } /* Write the attribute based on its data type */ switch (attr->type) { case DFNT_CHAR8: for (i = 0; i < attr->nval; i++) { if (val[i] >= ((double)MYHDF_CHAR8H)) val_char8[i] = MYHDF_CHAR8H; else if (val[i] <= ((double)MYHDF_CHAR8L)) val_char8[i] = MYHDF_CHAR8L; else if (val[i] >= 0.0) val_char8[i] = (char8)(val[i] + 0.5); else val_char8[i] = -((char8)(-val[i] + 0.5)); } buf = (void *)val_char8; break; case DFNT_INT8: for (i = 0; i < attr->nval; i++) { if (val[i] >= ((double)MYHDF_INT8H)) val_int8[i] = MYHDF_INT8H; else if (val[i] <= ((double)MYHDF_INT8L)) val_int8[i] = MYHDF_INT8L; else if (val[i] >= 0.0) val_int8[i] = (int8)(val[i] + 0.5); else val_int8[i] = -((int8)(-val[i] + 0.5)); } buf = (void *)val_int8; break; case DFNT_UINT8: for (i = 0; i < attr->nval; i++) { if (val[i] >= ((double)MYHDF_UINT8H)) val_uint8[i] = MYHDF_UINT8H; else if (val[i] <= ((double)MYHDF_UINT8L)) val_uint8[i] = MYHDF_UINT8L; else if (val[i] >= 0.0) val_uint8[i] = (uint8)(val[i] + 0.5); else val_uint8[i] = -((uint8)(-val[i] + 0.5)); } buf = (void *)val_uint8; break; case DFNT_INT16: for (i = 0; i < attr->nval; i++) { if (val[i] >= ((double)MYHDF_INT16H)) val_int16[i] = MYHDF_INT16H; else if (val[i] <= ((double)MYHDF_INT16L)) val_int16[i] = MYHDF_INT16L; else if (val[i] >= 0.0) val_int16[i] = (int16)( val[i] + 0.5); else val_int16[i] = -((int16)(-val[i] + 0.5)); } buf = (void *)val_int16; break; case DFNT_UINT16: for (i = 0; i < attr->nval; i++) { if (val[i] >= ((double)MYHDF_UINT16H)) val_uint16[i] = MYHDF_UINT16H; else if (val[i] <= ((double)MYHDF_UINT16L)) val_uint16[i] = MYHDF_UINT16L; else if (val[i] >= 0.0) val_uint16[i] = (uint16)( val[i] + 0.5); else val_uint16[i] = -((uint16)(-val[i] + 0.5)); } buf = (void *)val_uint16; break; case DFNT_INT32: for (i = 0; i < attr->nval; i++) { if (val[i] >= ((double)MYHDF_INT32H)) val_int32[i] = MYHDF_INT32H; else if (val[i] <= ((double)MYHDF_INT32L)) val_int32[i] = MYHDF_INT32L; else if (val[i] >= 0.0) val_int32[i] = (int32)( val[i] + 0.5); else val_int32[i] = -((int32)(-val[i] + 0.5)); } buf = (void *)val_int32; break; case DFNT_UINT32: for (i = 0; i < attr->nval; i++) { if (val[i] >= ((double)MYHDF_UINT32H)) val_uint32[i] = MYHDF_UINT32H; else if (val[i] <= ((double)MYHDF_UINT32L)) val_uint32[i] = MYHDF_UINT32L; else if (val[i] >= 0.0) val_uint32[i] = (uint32)( val[i] + 0.5); else val_uint32[i] = -((uint32)(-val[i] + 0.5)); } buf = (void *)val_uint32; break; case DFNT_FLOAT32: for (i = 0; i < attr->nval; i++) { if (val[i] >= ((double)MYHDF_FLOAT32H)) val_float32[i] = MYHDF_FLOAT32H; else if (val[i] <= ((double)MYHDF_FLOAT32L)) val_float32[i] = MYHDF_FLOAT32L; else val_float32[i] = (float32) val[i]; } buf = (void *)val_float32; break; case DFNT_FLOAT64: if (sizeof (float64) == sizeof (double)) buf = (void *)val; else { for (i = 0; i < attr->nval; i++) val_float64[i] = val[i]; buf = (void *)val_float64; } break; default: sprintf (errmsg, "Unsupported attribute data type"); error_handler (true, FUNC_NAME, errmsg); return (ERROR); } if (SDsetattr(sds_id, attr->name, attr->type, attr->nval, buf) == HDF_ERROR) { sprintf (errmsg, "Error writing attribute"); error_handler (true, FUNC_NAME, errmsg); return (ERROR); } return (SUCCESS); }
int main(void) { int32 sd1_id; /* SD interface identifier */ int32 sd2_id; /* SD interface identifier */ int32 sds1_id; /* SDS identifier */ int32 sds2_id; /* SDS identifier */ int32 dim_sizes[2]; /* sizes of the SDS dimensions */ int32 start[2]; /* start location to write */ int32 edges[2]; /* number of elements to write */ int32 n_values; int32 buf1a[Y_LENGTH][X_LENGTH] = {{1,1},{1,1},{5,6}}; int32 buf1b[Y_LENGTH][X_LENGTH] = {{1,2},{3,4},{5,6}}; /* percent test: compare divide by zero, both zero */ int32 buf2a[Y_LENGTH][X_LENGTH] = {{100,100},{100,0},{0,100}}; int32 buf2b[Y_LENGTH][X_LENGTH] = {{120,80},{0,100},{0,50}}; /* global attributes */ char8 bufga1[] = "Storm_track_data1"; char8 bufga2[] = "Storm_track_data2"; float32 bufa1[2] = {1., 1.}; float32 bufa2[2] = {1., 2.}; /*vdata*/ int32 file1_id; int32 file2_id; int32 vdata1_id; int32 vdata2_id; char8 vdata1_buf1 [N_RECORDS_1] = {'V', 'D', 'A', 'T', 'A'}; char8 vdata1_buf2 [N_RECORDS_1] = {'X', 'D', 'A', 'T', 'A'}; int32 vdata2_buf1 [N_RECORDS_2][ORDER_2] = {{1, 2, 3, 4}, {5, 6, 7, 8}}; int32 vdata2_buf2 [N_RECORDS_2][ORDER_2] = {{1, 1, 1, 1}, {5, 6, 7, 8}}; float32 vdata3_buf1[N_RECORDS_2][N_VALS_PER_REC]={{1,2,3,4,5,6},{7,8,9,10,11,12}}; float32 vdata3_buf2[N_RECORDS_2][N_VALS_PER_REC]={{1,1,1,1,1,1},{7,8,9,10,11,12}}; /* Define the location and size of the data to be written to the data set*/ start[0] = 0; start[1] = 0; edges[0] = Y_LENGTH; edges[1] = X_LENGTH; /* Define the dimensions of the array to be created */ dim_sizes[0] = Y_LENGTH; dim_sizes[1] = X_LENGTH; /*------------------------------------------------------------------------- * SD data *------------------------------------------------------------------------- */ /* Create the files and initialize the SD interface */ if ((sd1_id = SDstart (FILE1_NAME, DFACC_CREATE))==FAIL) goto error; if ((sd2_id = SDstart (FILE2_NAME, DFACC_CREATE))==FAIL) goto error; /* Set a global attribute */ n_values = sizeof(bufga1); if ( SDsetattr (sd1_id, FILE_ATTR_NAME, DFNT_CHAR8, n_values, (VOIDP)bufga1)==FAIL) goto error; if ( SDsetattr (sd2_id, FILE_ATTR_NAME, DFNT_CHAR8, n_values, (VOIDP)bufga2)==FAIL) goto error; /* Create the data sets */ if ((sds1_id = SDcreate (sd1_id, "dset1", DFNT_INT32, RANK, dim_sizes))==FAIL) goto error; if ((sds2_id = SDcreate (sd2_id, "dset1", DFNT_INT32, RANK, dim_sizes))==FAIL) goto error; /* Assign attribute */ n_values = 2; if ( SDsetattr (sds1_id, SDS_ATTR_NAME, DFNT_FLOAT32, n_values, (VOIDP)bufa1)==FAIL) goto error; if ( SDsetattr (sds2_id, SDS_ATTR_NAME, DFNT_FLOAT32, n_values, (VOIDP)bufa2)==FAIL) goto error; /* Write the stored data to the data set */ if ( SDwritedata (sds1_id, start, NULL, edges, (VOIDP)buf1a)==FAIL) goto error; if ( SDwritedata (sds2_id, start, NULL, edges, (VOIDP)buf1b)==FAIL) goto error; /* Terminate access to the data set */ if ( SDendaccess (sds1_id)==FAIL) goto error; if ( SDendaccess (sds2_id)==FAIL) goto error; /* Create another data set */ if (( sds1_id = SDcreate (sd1_id, "dset2", DFNT_INT32, RANK, dim_sizes))==FAIL) goto error; if (( sds2_id = SDcreate (sd2_id, "dset2", DFNT_INT32, RANK, dim_sizes))==FAIL) goto error; if ( SDwritedata (sds1_id, start, NULL, edges, (VOIDP)buf1a)==FAIL) goto error; if ( SDwritedata (sds2_id, start, NULL, edges, (VOIDP)buf1b)==FAIL) goto error; if ( SDendaccess (sds1_id)==FAIL) goto error; if ( SDendaccess (sds2_id)==FAIL) goto error; /* data sets for -p test */ if (( sds1_id = SDcreate (sd1_id, "dset3", DFNT_INT32, RANK, dim_sizes))==FAIL) goto error; if (( sds2_id = SDcreate (sd2_id, "dset3", DFNT_INT32, RANK, dim_sizes))==FAIL) goto error; if ( SDwritedata (sds1_id, start, NULL, edges, (VOIDP)buf2a)==FAIL) goto error; if ( SDwritedata (sds2_id, start, NULL, edges, (VOIDP)buf2b)==FAIL) goto error; if ( SDendaccess (sds1_id)==FAIL) goto error; if ( SDendaccess (sds2_id)==FAIL) goto error; /*------------------------------------------------------------------------- * end SD *------------------------------------------------------------------------- */ /* Terminate access to the SD interface and close the file */ if ( SDend (sd1_id)==FAIL) goto error; if ( SDend (sd2_id)==FAIL) goto error; /*------------------------------------------------------------------------- * VD data *------------------------------------------------------------------------- */ /* Open the HDF file for writing */ if ((file1_id = Hopen (FILE1_NAME, DFACC_WRITE, 0))==FAIL) goto error; if ((file2_id = Hopen (FILE2_NAME, DFACC_WRITE, 0))==FAIL) goto error; /* Initialize the VS interface */ if ( Vstart (file1_id)==FAIL) goto error; if ( Vstart (file2_id)==FAIL) goto error; /*------------------------------------------------------------------------- * VD data one field *------------------------------------------------------------------------- */ /* Create the first vdata and populate it with data from vdata1_buf */ if (VHstoredata (file1_id, FIELD1_NAME, (uint8 *)vdata1_buf1, N_RECORDS_1, DFNT_CHAR8, VDATA1_NAME, CLASS1_NAME)==FAIL) goto error; if (VHstoredata (file2_id, FIELD1_NAME, (uint8 *)vdata1_buf2, N_RECORDS_1, DFNT_CHAR8, VDATA1_NAME, CLASS1_NAME)==FAIL) goto error; /*------------------------------------------------------------------------- * VD data one field, order 4 *------------------------------------------------------------------------- */ /* Create the second vdata and populate it with data from vdata2_buf */ if ( VHstoredatam (file1_id, FIELD2_NAME, (uint8 *)vdata2_buf1, N_RECORDS_2, DFNT_INT32, VDATA2_NAME, CLASS2_NAME, ORDER_2)==FAIL) goto error; if ( VHstoredatam (file2_id, FIELD2_NAME, (uint8 *)vdata2_buf2, N_RECORDS_2, DFNT_INT32, VDATA2_NAME, CLASS2_NAME, ORDER_2)==FAIL) goto error; /*------------------------------------------------------------------------- * VD data several fields *------------------------------------------------------------------------- */ /* Create a new vdata */ if ((vdata1_id = VSattach (file1_id, -1, "w"))==FAIL) goto error; if ((vdata2_id = VSattach (file2_id, -1, "w"))==FAIL) goto error; /* Set name and class name of the vdata */ if ( VSsetname (vdata1_id, VDATA3_NAME)==FAIL) goto error; if ( VSsetclass (vdata1_id, CLASS3_NAME)==FAIL) goto error; if ( VSsetname (vdata2_id, VDATA3_NAME)==FAIL) goto error; if ( VSsetclass (vdata2_id, CLASS3_NAME)==FAIL) goto error; /* Define fields */ if ( VSfdefine (vdata1_id, FIELD3_NAME1, DFNT_FLOAT32, ORDER3_1 )==FAIL) goto error; if ( VSfdefine (vdata1_id, FIELD3_NAME2, DFNT_FLOAT32, ORDER3_2 )==FAIL) goto error; if ( VSfdefine (vdata1_id, FIELD3_NAME3, DFNT_FLOAT32, ORDER3_3 )==FAIL) goto error; if ( VSsetfields (vdata1_id, FIELDNAME3_LIST )==FAIL) goto error; if ( VSfdefine (vdata2_id, FIELD3_NAME1, DFNT_FLOAT32, ORDER3_1 )==FAIL) goto error; if ( VSfdefine (vdata2_id, FIELD3_NAME2, DFNT_FLOAT32, ORDER3_2 )==FAIL) goto error; if ( VSfdefine (vdata2_id, FIELD3_NAME3, DFNT_FLOAT32, ORDER3_3 )==FAIL) goto error; if ( VSsetfields (vdata2_id, FIELDNAME3_LIST)==FAIL) goto error; /* Write the data with full interlacing mode */ if ( VSwrite (vdata1_id, (uint8 *)vdata3_buf1, N_RECORDS_2, FULL_INTERLACE)==FAIL) goto error; if ( VSwrite (vdata2_id, (uint8 *)vdata3_buf2, N_RECORDS_2, FULL_INTERLACE)==FAIL) goto error; if ( VSdetach (vdata1_id)==FAIL) goto error; if ( VSdetach (vdata2_id)==FAIL) goto error; /*------------------------------------------------------------------------- * end VD data *------------------------------------------------------------------------- */ /* Terminate access to the VS interface and close the HDF file */ if ( Vend (file1_id)==FAIL) goto error; if ( Vend (file2_id)==FAIL) goto error; if ( Hclose (file1_id)==FAIL) goto error; if ( Hclose (file2_id)==FAIL) goto error; /*------------------------------------------------------------------------- * write 2 big files for hyperslab reading *------------------------------------------------------------------------- */ if (do_big_file()==FAIL) goto error; /*------------------------------------------------------------------------- * groups *------------------------------------------------------------------------- */ if (do_groups()==FAIL) goto error; return 0; error: printf("Error...Exiting...\n"); return 1; }
int init_output_sds(int32 sd_id, unsigned char *process, SDS outsds[Nbands], SDS sds[Nitems], int gzip, int verbose) { int ib; int32 dim_id; char *dimname1, *dimname2; HDF_CHUNK_DEF chunk_def; /* same fill value will be used for all output SDSs */ static int16 fillvalue = FILL_INT16; /* band naming convention will be "CorrRefl_XX" (11 characters + terminating null) */ char name[16]; /* write SDS-specific attributes and dimension names */ for (ib = 0; ib < Nbands; ib++) { if (!process[ib]) continue; outsds[ib].num_type = DFNT_INT16; outsds[ib].factor = 0.0001; outsds[ib].offset = 0; outsds[ib].rank = 2; sprintf(name, "CorrRefl_%2.2d", ib + 1); if ( !(outsds[ib].name = strdup(name)) ) return 1; outsds[ib].Nl = outsds[ib].dim_sizes[0] = sds[ib].Nl; outsds[ib].Np = outsds[ib].dim_sizes[1] = sds[ib].Np; outsds[ib].rowsperscan = sds[ib].rowsperscan; if (verbose) printf("Creating SDS %s: %dx%d\n", outsds[ib].name, outsds[ib].Np, outsds[ib].Nl); if ((outsds[ib].id = SDcreate(sd_id, outsds[ib].name, outsds[ib].num_type, outsds[ib].rank, outsds[ib].dim_sizes)) == -1) { fprintf(stderr, "Cannot create SDS %s\n", outsds[ib].name); return 1; } outsds[ib].fillvalue = &fillvalue; if ( SDsetfillvalue(outsds[ib].id, outsds[ib].fillvalue) ) { fprintf(stderr, "Cannot write fill value of SDS %s\n", outsds[ib].name); return 1; } if ( SDsetattr(outsds[ib].id, "scale_factor", DFNT_FLOAT64, 1, &outsds[ib].factor) == -1 || SDsetattr(outsds[ib].id, "add_offset", DFNT_FLOAT64, 1, &outsds[ib].offset) == -1 ) { fprintf(stderr, "Cannot write scale factor and offset of SDS \"%s\"\n", outsds[ib].name); return 1; } if ( SDsetattr(outsds[ib].id, "units", DFNT_CHAR8, 4, "none") == -1 ) { fprintf(stderr, "Cannot write units attribute of SDS \"%s\"\n", outsds[ib].name); return 1; } /* set dimensions */ outsds[ib].start[1] = 0; outsds[ib].edges[0] = outsds[ib].rowsperscan; outsds[ib].edges[1] = outsds[ib].Np; /* allocate memory for band output data */ outsds[ib].data = malloc(outsds[ib].rowsperscan * outsds[ib].Np * DFKNTsize(outsds[ib].num_type)); if (!outsds[ib].data) { fputs("Error allocating memory.\n", stderr); return 1; } /* set optional compression */ if (gzip) { chunk_def.chunk_lengths[0] = chunk_def.comp.chunk_lengths[0] = outsds[ib].edges[0]; chunk_def.chunk_lengths[1] = chunk_def.comp.chunk_lengths[1] = outsds[ib].edges[1]; chunk_def.comp.comp_type = COMP_CODE_DEFLATE; chunk_def.comp.cinfo.deflate.level = 4; if (SDsetchunk(outsds[ib].id, chunk_def, HDF_CHUNK | HDF_COMP) == FAIL) { fprintf(stderr, "Cannot set chunks for SDS %s\n", outsds[ib].name); return 1; } } set_dimnames(outsds[ib].Np, &dimname1, &dimname2); if (verbose) printf("(%s x %s)\n", dimname1, dimname2); /* dimension names */ if ((dim_id = SDgetdimid(outsds[ib].id, 0)) == -1) { fputs("Error getting dimension ID1.\n", stderr); return 1; } if (SDsetdimname(dim_id, dimname1) == -1) { fprintf(stderr, "Cannot set first dimension name for SDS %s\n", outsds[ib].name); return 1; } if ((dim_id = SDgetdimid(outsds[ib].id, 1)) == -1) { fputs("Error getting dimension ID2.\n", stderr); return 1; } if (SDsetdimname(dim_id, dimname2) == -1) { fprintf(stderr, "Cannot set second dimension name for SDS %s\n", outsds[ib].name); return 1; } } return 0; }
bool PutAttrDouble(int32 sds_id, Myhdf_attr_t *attr, double *val) /* !C****************************************************************************** !Description: 'PutAttrDouble' writes an attribute from a parameter of type 'double' to a HDF file. !Input Parameters: sds_id SDS id attr Attribute data structure; the following fields are used: name, type, nval !Output Parameters: val An array of values from the HDF attribute (converted from type 'double' to the native type (returns) Status: 'true' = okay 'false' = error writing the attribute information !Team Unique Header: ! Design Notes: 1. The values in the attribute are converted from the stored type to 'double' type. 2. The HDF file is assumed to be open for SD (Science Data) access. 3. If the attribute has more than 'MYHDF_MAX_NATTR_VAL' values, an error status is returned. 4. Error messages are handled with the 'RETURN_ERROR' macro. !END**************************************************************************** */ { char8 val_char8[MYHDF_MAX_NATTR_VAL]; int8 val_int8[MYHDF_MAX_NATTR_VAL]; uint8 val_uint8[MYHDF_MAX_NATTR_VAL]; int16 val_int16[MYHDF_MAX_NATTR_VAL]; uint16 val_uint16[MYHDF_MAX_NATTR_VAL]; int32 val_int32[MYHDF_MAX_NATTR_VAL]; uint32 val_uint32[MYHDF_MAX_NATTR_VAL]; float32 val_float32[MYHDF_MAX_NATTR_VAL]; float64 val_float64[MYHDF_MAX_NATTR_VAL]; int i; void *buf; if (attr->nval <= 0 || attr->nval > MYHDF_MAX_NATTR_VAL) RETURN_ERROR("invalid number of values", "PutAttrDouble", false); switch (attr->type) { case DFNT_CHAR8: for (i = 0; i < attr->nval; i++) { if ( val[i] >= ((double)MYHDF_CHAR8H)) val_char8[i] = MYHDF_CHAR8H; else if (val[i] <= ((double)MYHDF_CHAR8L)) val_char8[i] = MYHDF_CHAR8L; else if (val[i] >= 0.0) val_char8[i] = (char8)(val[i] + 0.5); else val_char8[i] = -((char8)(-val[i] + 0.5)); } buf = (void *)val_char8; break; case DFNT_INT8: for (i = 0; i < attr->nval; i++) { if ( val[i] >= ((double)MYHDF_INT8H)) val_int8[i] = MYHDF_INT8H; else if (val[i] <= ((double)MYHDF_INT8L)) val_int8[i] = MYHDF_INT8L; else if (val[i] >= 0.0) val_int8[i] = (int8)( val[i] + 0.5); else val_int8[i] = -((int8)(-val[i] + 0.5)); } buf = (void *)val_int8; break; case DFNT_UINT8: for (i = 0; i < attr->nval; i++) { if ( val[i] >= ((double)MYHDF_UINT8H)) val_uint8[i] = MYHDF_UINT8H; else if (val[i] <= ((double)MYHDF_UINT8L)) val_uint8[i] = MYHDF_UINT8L; else if (val[i] >= 0.0) val_uint8[i] = (uint8)( val[i] + 0.5); else val_uint8[i] = -((uint8)(-val[i] + 0.5)); } buf = (void *)val_uint8; break; case DFNT_INT16: for (i = 0; i < attr->nval; i++) { if ( val[i] >= ((double)MYHDF_INT16H)) val_int16[i] = MYHDF_INT16H; else if (val[i] <= ((double)MYHDF_INT16L)) val_int16[i] = MYHDF_INT16L; else if (val[i] >= 0.0) val_int16[i] = (int16)( val[i] + 0.5); else val_int16[i] = -((int16)(-val[i] + 0.5)); } buf = (void *)val_int16; break; case DFNT_UINT16: for (i = 0; i < attr->nval; i++) { if ( val[i] >= ((double)MYHDF_UINT16H)) val_uint16[i] = MYHDF_UINT16H; else if (val[i] <= ((double)MYHDF_UINT16L)) val_uint16[i] = MYHDF_UINT16L; else if (val[i] >= 0.0) val_uint16[i] = (uint16)( val[i] + 0.5); else val_uint16[i] = -((uint16)(-val[i] + 0.5)); } buf = (void *)val_uint16; break; case DFNT_INT32: for (i = 0; i < attr->nval; i++) { if ( val[i] >= ((double)MYHDF_INT32H)) val_int32[i] = MYHDF_INT32H; else if (val[i] <= ((double)MYHDF_INT32L)) val_int32[i] = MYHDF_INT32L; else if (val[i] >= 0.0) val_int32[i] = (int32)( val[i] + 0.5); else val_int32[i] = -((int32)(-val[i] + 0.5)); } buf = (void *)val_int32; break; case DFNT_UINT32: for (i = 0; i < attr->nval; i++) { if ( val[i] >= ((double)MYHDF_UINT32H)) val_uint32[i] = MYHDF_UINT32H; else if (val[i] <= ((double)MYHDF_UINT32L)) val_uint32[i] = MYHDF_UINT32L; else if (val[i] >= 0.0) val_uint32[i] = (uint32)( val[i] + 0.5); else val_uint32[i] = -((uint32)(-val[i] + 0.5)); } buf = (void *)val_uint32; break; case DFNT_FLOAT32: for (i = 0; i < attr->nval; i++) { if ( val[i] >= ((double)MYHDF_FLOAT32H)) val_float32[i] = MYHDF_FLOAT32H; else if (val[i] <= ((double)MYHDF_FLOAT32L)) val_float32[i] = MYHDF_FLOAT32L; else if (val[i] >= 0.0) val_float32[i] = (float32)( val[i] + 0.5); else val_float32[i] = -((float32)(-val[i] + 0.5)); } buf = (void *)val_float32; break; case DFNT_FLOAT64: if (sizeof(float64) == sizeof(double)) buf = (void *)val; else { for (i = 0; i < attr->nval; i++) val_float64[i] = val[i]; buf = (void *)val_float64; } break; default: RETURN_ERROR("unimplmented type", "PutAttrDouble", false); } if (SDsetattr(sds_id, attr->name, attr->type, attr->nval, buf) == HDF_ERROR) RETURN_ERROR("setting attribute", "PutAttrDouble", false); return true; }
static intn test_dim1_SDS1(void) { char sds_name[20]; float32 sds1_data[] = {0.1, 2.3, 4.5, 6.7, 8.9}; float32 out_data[5]; int32 dimsize[1]; int32 sds_id, file_id, dim_id, index; int32 start=0, stride=1; int32 scale1 [5] = {101,102,103,104,105}, scale1_out[5]; int32 num_type, array_rank, count; int32 n_datasets, n_file_attrs, n_local_attrs, n_vars = 0; intn datanum, ranknum, status =0, i, idx, idx1, idx2; hdf_varlist_t* var_list; intn is_coord = FALSE; char attr_name[H4_MAX_NC_NAME], attr_values[80]; intn num_errs = 0; /* number of errors so far */ file_id = SDstart(FILE1, DFACC_CREATE); CHECK(file_id, FAIL, "SDstart"); /* Create a one-dim dataset named VAR1_NAME, of type DFNT_FLOAT32. */ dimsize[0] = 5; sds_id = SDcreate(file_id, VAR1_NAME, DFNT_FLOAT32, 1, dimsize); CHECK(sds_id, FAIL, "SDcreate"); /* Set the dimension name to be the same as its dataset. */ dim_id = SDgetdimid(sds_id, 0); CHECK(dim_id, FAIL, "SDgetdimid"); status = SDsetdimname(dim_id, VAR1_NAME); /* status = SDsetdimname(dim_id, VAR1_NAME); */ CHECK(status, FAIL, "SDsetdimname"); /* Get file info and verify that there is 1 dataset in the file. */ status = SDfileinfo(file_id, &n_datasets, &n_file_attrs); CHECK(status, FAIL, "SDfileinfo"); VERIFY(n_datasets, 1, "SDfileinfo"); /* Set an attribute to dimension VAR1_NAME. */ status = SDsetattr(dim_id, ATTR1_NAME, DFNT_CHAR8, ATTR1_LEN, ATTR1_VAL); CHECK(status, FAIL, "SDsetattr"); /* Set an attribute to dataset VAR1_NAME. */ status = SDsetattr(sds_id, ATTR2_NAME, DFNT_CHAR8, ATTR2_LEN, ATTR2_VAL); CHECK(status, FAIL, "SDsetattr"); /* Get file info and verify that there are 2 datasets in the file: 1 SDS and 1 coordinate variable (because of SDsetattr dim) */ status = SDfileinfo(file_id, &n_datasets, &n_file_attrs); CHECK(status, FAIL, "SDfileinfo"); VERIFY(n_datasets, 2, "SDfileinfo"); /* Write data to the SDS */ status = SDwritedata(sds_id, &start, &stride, dimsize, (VOIDP)sds1_data); CHECK(status, FAIL, "SDwritedata"); /* Close dataset and file. */ status = SDendaccess(sds_id); CHECK(status, FAIL, "SDendaccess"); status = SDend(file_id); CHECK(status, FAIL, "SDend"); /* Open the file again to check its data */ file_id = SDstart(FILE1, DFACC_RDWR); CHECK(file_id, FAIL, "SDstart"); /* Check variable type and attributes of each element in the file */ /* Get the number of variables of name VAR1_NAME */ status = SDgetnumvars_byname(file_id, VAR1_NAME, &n_vars); if (n_vars == 1) { /* Get index of dataset VAR1_NAME */ index = SDnametoindex(file_id, VAR1_NAME); CHECK(index, FAIL, "SDnametoindex"); } else { /* Get the list of all variables of named VAR1_NAME */ var_list = (hdf_varlist_t *)HDmalloc(n_vars * sizeof(hdf_varlist_t)); status = SDnametoindices(file_id, VAR1_NAME, var_list); /* In this case, the first variable is a dataset */ for (idx = 0; idx < n_vars; idx++) { if (var_list[idx].var_type == IS_SDSVAR) { index = var_list[idx].var_index; VERIFY(index, 0, "SDnametoindices"); } } } sds_id = SDselect(file_id, index); CHECK(sds_id, FAIL, "SDselect"); /* Verify that this variable is a dataset. */ is_coord = SDiscoordvar(sds_id); VERIFY(is_coord, FALSE, "SDiscoordvar"); /* Read and verify the information of the SDS' first attribute. */ status = SDattrinfo(sds_id, 0, attr_name, &num_type, &count); CHECK(status, FAIL, "SDattrinfo"); VERIFY(count, ATTR2_LEN, "SDattrinfo"); VERIFY(HDstrncmp(attr_name, ATTR2_NAME, 14), 0, "SDattrinfo"); /* Read and verify the values of the SDS' first attribute. */ status = SDreadattr(sds_id, 0, attr_values); CHECK(status, FAIL, "SDreadattr"); if (HDstrncmp(attr_values, ATTR2_VAL, ATTR2_LEN) != 0) { fprintf(stderr, "Unmatched attribute values for SDS %s: is <%s>, should be <%s>\n", VAR1_NAME, attr_values, ATTR2_VAL); num_errs++; } /* Get access to the SDS' first dimension. */ dim_id = SDgetdimid(sds_id, 0); CHECK(dim_id, FAIL, "SDgetdimid"); /* Read and verify the information of the dimension's first attribute. */ status = SDattrinfo(dim_id, 0, attr_name, &num_type, &count); CHECK(status, FAIL, "SDattrinfo"); VERIFY(count, 19, "SDattrinfo"); VERIFY(HDstrncmp(attr_name, ATTR1_NAME, 21), 0, "SDattrinfo"); /* Read and verify the values of the dimension's first attribute. */ status = SDreadattr(dim_id, 0, attr_values); CHECK(status, FAIL, "SDreadattr"); if (HDstrncmp(attr_values, ATTR1_VAL, ATTR1_LEN) != 0) { fprintf(stderr, "Unmatched attribute values for dimension %s: is <%s>, should be <%s>\n", VAR1_NAME, attr_values, ATTR1_VAL); num_errs++; } /* Verify again that the number of datasets in the file is 2, 1 SDS and 1 coordinate variable */ status = SDfileinfo(file_id, &n_datasets, &n_file_attrs); CHECK(status, FAIL, "SDfileinfo"); VERIFY(n_datasets, 2, "SDfileinfo"); VERIFY(n_file_attrs, 0, "SDfileinfo"); /* Read and verify the dataset's data */ status = SDreaddata (sds_id, &start, NULL, dimsize, &out_data); CHECK(status, FAIL, "SDreaddata"); for (idx1 = 0; idx1 < dimsize[0]; idx1++) if (out_data[idx1] != sds1_data[idx1]) { fprintf(stderr, "Read value (%f) differs from written (%f) at [%d]\n", out_data[idx1], sds1_data[idx1], idx1); num_errs++; } /* Close dataset and file. */ status = SDendaccess(sds_id); CHECK(status, FAIL, "SDendaccess"); status = SDend(file_id); CHECK(status, FAIL, "SDend"); /* Return the number of errors that's been kept track of so far */ return num_errs; } /* test_dim1_SDS1 */
static intn test_named_vars(void) { char sds_name[20]; float32 sds1_data[] = {0.1, 2.3, 4.5, 6.7, 8.9}; float32 sds2_data[2][3] = {{0.1, 2.3, 4.5}, {4.5, 6.7, 8.9}}; int32 dimsize[1], dimsize2[2]; int32 sds_id, sds1_id, sds2_id, sds3_id, sds4_id, sds5_id; int32 file_id, dim_id, index; int32 start=0, stride=1, stat; int32 start2[2]={0,0}, stride2[2]={1,1}; int32 scale1 [5] = {101,102,103,104,105}, scale1_out[5]; int32 array_rank; int32 n_datasets, n_file_attrs, n_local_attrs, n_vars=0; float32 out_data2[2][3]; intn datanum, ranknum, status =0, idx, idx1, idx2; intn is_coordvar=FALSE; hdf_varlist_t *allvars, *varlistp; intn num_errs = 0; /* number of errors so far */ char line[40]; char contents[7][40]={ "#0 SDS 2-dim 'Common Name'", "#1 SDS 2-dim 'Common Name'", "#2 SDS 1-dim 'One Dimension'", "#3 Coordinate 1-dim 'Common Name'", "#4 SDS 1-dim 'One Dimension'", "#5 SDS 1-dim 'Another Name'", "#6 Coordinate 1-dim 'Another Name'"}; file_id = SDstart(FILE3, DFACC_CREATE); CHECK(file_id, FAIL, "SDstart"); dimsize2[0] = 2; dimsize2[1] = 3; /* Create first COMMON_NAME data set. */ sds1_id = SDcreate(file_id, COMMON_NAME, DFNT_FLOAT32, 2, dimsize2); CHECK(sds1_id, FAIL, "SDcreate"); status = SDendaccess(sds1_id); CHECK(status, FAIL, "SDendaccess"); /* Create second COMMON_NAME data set. */ sds2_id = SDcreate(file_id, COMMON_NAME, DFNT_FLOAT32, 2, dimsize2); CHECK(sds2_id, FAIL, "SDcreate"); status = SDendaccess(sds2_id); CHECK(status, FAIL, "SDendaccess"); dimsize[0] = 5; sds3_id = SDcreate(file_id, ONEDIM_NAME, DFNT_FLOAT32, 1, dimsize); CHECK(sds3_id, FAIL, "SDcreate"); /* Set the dimension name to be the same as the previous 2 datasets */ dim_id = SDgetdimid(sds3_id, 0); CHECK(dim_id, FAIL, "SDgetdimid"); status = SDsetdimname(dim_id, COMMON_NAME); CHECK(status, FAIL, "SDsetdimname"); /* Get file info and verify that there are 3 datasets in the file */ status = SDfileinfo(file_id, &n_datasets, &n_file_attrs); CHECK(status, FAIL, "SDfileinfo"); VERIFY(n_datasets, 3, "SDfileinfo"); /* Write values to the dimension COMMON_NAME (same name as first 2 datasets) */ status = SDsetdimscale (dim_id, dimsize[0], DFNT_INT32, scale1); CHECK(status, FAIL, "SDsetdimscale"); /* Get file info and verify that there are 4 datasets in the file */ status = SDfileinfo(file_id, &n_datasets, &n_file_attrs); CHECK(status, FAIL, "SDfileinfo"); VERIFY(n_datasets, 4, "SDfileinfo"); dimsize[0] = 8; sds4_id = SDcreate(file_id, ONEDIM_NAME, DFNT_FLOAT32, 1, dimsize); CHECK(sds4_id, FAIL, "SDcreate"); /* Set the dimension name to be the same as the previous 2 datasets */ dim_id = SDgetdimid(sds4_id, 0); CHECK(dim_id, FAIL, "SDgetdimid"); status = SDsetdimname(dim_id, ANOTHER_NAME); CHECK(status, FAIL, "SDsetdimname"); sds5_id = SDcreate(file_id, ANOTHER_NAME, DFNT_FLOAT32, 1, dimsize); CHECK(sds5_id, FAIL, "SDcreate"); /* Get file info and verify that there are 6 datasets in the file */ status = SDfileinfo(file_id, &n_datasets, &n_file_attrs); CHECK(status, FAIL, "SDfileinfo"); VERIFY(n_datasets, 6, "SDfileinfo"); status = SDsetattr(dim_id, ATTR1_NAME, DFNT_CHAR8, ATTR1_LEN, ATTR1_VAL); CHECK(status, FAIL, "SDsetattr"); /* Get file info and verify that there are 7 datasets in the file */ status = SDfileinfo(file_id, &n_datasets, &n_file_attrs); CHECK(status, FAIL, "SDfileinfo"); VERIFY(n_datasets, 7, "SDfileinfo"); /* Verify again that the number of datasets in the file is 7 */ status = SDfileinfo(file_id, &n_datasets, &n_file_attrs); CHECK(status, FAIL, "SDfileinfo"); VERIFY(n_datasets, 7, "SDfileinfo"); /* There are 3 variables of name COMMON_NAME */ status = SDgetnumvars_byname(file_id, COMMON_NAME, &n_vars); CHECK(status, FAIL, "SDfileinfo"); VERIFY(n_vars, 3, "SDfileinfo"); allvars = (hdf_varlist_t *)HDmalloc(n_vars * sizeof(hdf_varlist_t)); status = SDnametoindices(file_id, COMMON_NAME, allvars); CHECK(status, FAIL, "SDfileinfo"); /* Compare file contents with predefined text to verify */ for (idx = 0; idx < n_datasets; idx++) { sds_id = SDselect(file_id, idx); CHECK(sds_id, FAIL, "SDselect"); status = SDgetinfo(sds_id, sds_name, &array_rank, NULL, NULL, NULL); CHECK(status, FAIL, "SDgetinfo"); is_coordvar = SDiscoordvar(sds_id); if (is_coordvar) sprintf(line,"#%d Coordinate %d-dim '%s'\n", idx, array_rank, sds_name); else sprintf(line,"#%d SDS %d-dim '%s'\n", idx, array_rank, sds_name); if (strncmp(contents[idx], line, strlen(contents[idx])) != 0) { fprintf(stderr, "File contents are incorrect in testing variable types at variable of index %d\n", idx); } } status = SDend(file_id); CHECK(status, FAIL, "SDend"); /* Return the number of errors that's been kept track of so far */ return num_errs; } /* test_named_vars */
int main(int argc,char **argv) { int32 sdsout_id; int32 dimout_id; int32 sdout_id; int32 index,count,start[MAX_VAR_DIMS]; int32 edge[MAX_VAR_DIMS]; int32 rank,data_type; int32 dim_sizes[MAX_VAR_DIMS]; char name[MAX_NC_NAME]; char names[2][MAX_NC_NAME]; int write_metadata; float32 scalef=1.0; float32 addoff=0.0; char* oz_units= "Dobson"; int16 doy; int idoy,year,nlats,nlons; float minlat, minlon, maxlat, maxlon, latsteps,lonsteps; short int* data; int16 base_date[3]; float lat_array[1024], lon_array[1024]; char* platform= argc>3 ? argv[3] : (char*) "Earthprobe"; if (argc<3) { fprintf(stderr,"usage: %s <input> <output> <platform>\n",argv[0]); exit(-1); } verbose=0; /**** open input ****/ /* determine NLINE by sensor - Feng */ if(strstr(argv[1], "omi")) { NLINE = 15; longline = 25; shortline = 10; } else { NLINE =12; longline =25; shortline = 13; } printf("%d %d %d\n", NLINE, longline, shortline); read_ozone(argv[1], &data, &idoy, &year, &nlats,&nlons, &minlat, &minlon, &maxlat, &maxlon, &latsteps, &lonsteps, lat_array, lon_array); if ( nlats!=180 || fabs(minlat+ 89.500)>0.0001 || fabs(maxlat- 89.500)>0.0001 || nlons!=288 || fabs(minlon+179.375)>0.0001 || fabs(maxlon-179.375)>0.0001 || fabs(latsteps-1.0)>0.0001 || fabs(lonsteps-1.25)>0.0001 )printf( "*** unexpected values ***\n nlats=%d nlons=%d minlat=%f maxlat=%f minlon=%f maxlon=%f\n" ,nlats,nlons,minlat,maxlat,minlon,maxlon); /**** check and open output ****/ write_metadata=0; if ((sdout_id=SDstart(argv[2], DFACC_RDONLY))<0) { if ((sdout_id=SDstart(argv[2], DFACC_CREATE))<0) { fprintf(stderr,"can't create output %s\n",argv[2]); exit(-1); } write_metadata=1; } else { SDend(sdout_id); if ((sdout_id=SDstart(argv[2], DFACC_WRITE))<0) { fprintf(stderr,"can't open output %s\n",argv[2]); exit(-1); } } doy=(int16)idoy; /**** Determine the contents of the file ****/ if (write_metadata) { /**** Copy global attributes to output if creating a new file Write Day Of Year to output ****/ base_date[0]= year; base_date[1]=1; base_date[2]=1; SDsetattr(sdout_id, "base_date", DFNT_INT16,3,base_date); SDsetattr(sdout_id, "Day Of Year", DFNT_INT16,1,&doy); count=strlen(platform)+1; printf("*** platform=(%s) len=%d ***\n",platform,count); SDsetattr(sdout_id,"Platform",DFNT_CHAR8,count,(void*)platform); /**** Copy lat/lon SDS ****/ /************************/ /**** Latitude (lat) ****/ /************************/ dim_sizes[0]= nlats; strcpy(name,"lat"); strcpy(names[0],"lat"); rank=1; if ((sdsout_id=SDcreate(sdout_id,name,DFNT_FLOAT32,rank,&dim_sizes[0]))<0) return -1; start[0]=0; edge[0]=dim_sizes[0]; if (SDwritedata(sdsout_id,start,NULL,edge,lat_array)<0) return -1; dimout_id=SDgetdimid(sdsout_id, 0); SDsetdimname(dimout_id, name); /*************************/ /**** Longitude (lon) ****/ /*************************/ dim_sizes[0]= nlons; strcpy(name,"lon"); strcpy(names[1],"lon"); rank=1; if ((sdsout_id=SDcreate(sdout_id,name,DFNT_FLOAT32,rank,&dim_sizes[0]))<0) return -1; start[0]=0; edge[0]=dim_sizes[0]; if (SDwritedata(sdsout_id,start,NULL,edge,lon_array)<0) return -1; dimout_id=SDgetdimid(sdsout_id, 0); SDsetdimname(dimout_id, name); /************************/ /**** Ozone (ozone) ****/ /************************/ dim_sizes[0]= nlats; dim_sizes[1]= nlons; strcpy(name,"ozone"); rank=2; if ((sdsout_id=SDcreate(sdout_id,name,DFNT_INT16,rank,&dim_sizes[0]))<0) return -1; start[0]=0; start[1]=0; edge[0]=dim_sizes[0]; edge[1]=dim_sizes[1]; if (SDwritedata(sdsout_id,start,NULL,edge,data)<0) return -1; for (index=0;index<rank;index++) { dimout_id=SDgetdimid(sdsout_id, index); SDsetdimname(dimout_id, names[index]); } strcpy(name,"scale_factor"); count=1; data_type= DFNT_FLOAT32; SDsetattr(sdsout_id, name, data_type, count, (void*)&scalef); strcpy(name,"add_offset"); count=1; data_type= DFNT_FLOAT32; SDsetattr(sdsout_id, name, data_type, count, (void*)&addoff); strcpy(name,"units"); count=strlen(oz_units)+1; data_type= DFNT_CHAR8; SDsetattr(sdsout_id, name, data_type, count, (void*)oz_units); } /**** Close input & output ****/ SDend(sdout_id); return 0; }
/************************************************************************* MODULE: TransferAttr PURPOSE: This module reads the user-defined core, archive, and structural metadata from the original (input) HDF file and writes it as new user-defined metadata in the new HDF file. The new file-level attribute namefields in the output HDF file are of the form "Old+<original field name>" and can be extracted from the output HDF file using the SDfindattr and SDattrinfo commands from the HDf library. RETURN VALUE: Type = bool Value Description ----- ----------- true Successful completion false Error processing HISTORY: Version Date Programmer Code Reason ------- ---- ---------- ---- ------ 01/04 Gail Schmidt Original Development based on Doug Ilg's metadmp.c program. NOTES: **************************************************************************/ bool TransferAttr ( int32 fid_old, /* I: SDS id # for the old HDF file */ int32 fid_new, /* I: SDS id # for the new HDF file */ char *attr /* I: Filename handle of the attribute to move */ ) { intn status; /* this is the var that holds return val */ int32 my_attr_index; /* holds return val from SDfindattr */ int32 data_type; /* holds attribute's data type */ int32 n_values; /* stores # of vals of the attribute */ char *file_data = NULL; /* char ptr used to allocate temp space during transfer of attribute info */ char attr_name[MAX_NC_NAME]; /* holds attribute's name */ char new_attr_name[MAX_NC_NAME + 3]; /* holds new attr name */ /* look for attribute in the old HDF file */ my_attr_index = SDfindattr( fid_old, attr ); /* only proceed if the attribute was found */ if ( my_attr_index == -1 ) return( false ); /* get size of old HDF file attribute */ status = SDattrinfo( fid_old, my_attr_index, attr_name, &data_type, &n_values ); /* only proceed if successful read of attribute info */ if ( status == -1 ) return( false ); /* attempt to allocate memory for old HDF file attribute contents (add one character for the end of string character) */ file_data = ( char * ) calloc( n_values+1, sizeof(char) ); if ( file_data == NULL ) LOG_RETURN_ERROR( "unable to allocate memory for reading old HDF file " "metadata", "TransferAttr", false); /* read attribute from the old HDF file */ status = SDreadattr( fid_old, my_attr_index, file_data ); if(status == -1 ) { free( file_data ); return( false ); } /* modify the attribute name from old HDF file prior to appending metadata to new HDF file; put prefix "Old" in front of old name */ strcpy( new_attr_name, "Old" ); strcat( new_attr_name, attr_name ); new_attr_name[ strlen(new_attr_name) + 1 ] = '\0'; /* attempt to write old metadata to output HDF file */ status = SDsetattr( fid_new, new_attr_name, DFNT_CHAR8, strlen(file_data), (VOIDP)file_data ); /* free dynamically allocated memory */ free( file_data ); return ( true ); }
bool TransferAttributes( Param_t *param, int32 old_fid, int32 new_fid, int proc_sds ) { int j = 0; int curr_band, curr_sds; int32 old_sds, new_sds; int32 sds_index; int32 nattr, attr_index; int32 data_type, n_values; char attr_name[1024], sds_name[1024], *buffer = NULL; int32 rank; int32 dims[10]; /* loop through the remaining SDS's and add them only if their output resolution is the same as the current SDSs resolution */ for ( curr_sds = proc_sds; curr_sds < param->num_input_sds; curr_sds++ ) { /* if this SDS is not the same resolution as proc_sds, then do not output metadata attributes for the SDS */ if ( param->output_pixel_size[curr_sds] != param->output_pixel_size[proc_sds] ) continue; /* get the SDS index */ sds_index = SDnametoindex( old_fid, param->input_sds_name_list[curr_sds] ); /* open old SDS */ old_sds = SDselect( old_fid, sds_index ); /* get various SDS info */ SDgetinfo( old_sds, sds_name, &rank, dims, &data_type, &nattr ); /* loop through the bands in this SDS, writing the same info to each band that was processed */ for ( curr_band = 0; curr_band < param->input_sds_nbands[curr_sds]; curr_band++ ) { /* if this band wasn't processed, skip to the next band */ if (param->input_sds_bands[curr_sds][curr_band] == 0) continue; /* open new SDS, for this SDS/band combination */ new_sds = SDselect( new_fid, j++ ); /* for each SDS attribute */ for ( attr_index = 0; attr_index < nattr; attr_index++ ) { /* get SDS attribute info */ SDattrinfo( old_sds, attr_index, attr_name, &data_type, &n_values ); /* allocate memory */ switch (data_type) { case 6: case 26: case 27: buffer = calloc(n_values, 8); break; case 5: case 24: case 25: buffer = calloc(n_values, 4); break; case 22: case 23: buffer = calloc(n_values, 2); break; default: buffer = calloc(n_values, 1); break; } /* make sure the buffer was allocated */ if ( buffer == NULL ) LOG_RETURN_ERROR( "unable to allocate space for buffer", "TransferAttributes", false); /* get SDS attribute values */ if ( SDreadattr( old_sds, attr_index, buffer ) == -1 ) LOG_RETURN_ERROR("unable to find attribute in old HDF file", "TransferAttributes", false); /* write SDS attribute to output file */ if (SDsetattr( new_sds, attr_name, data_type, n_values, buffer ) == -1 ) LOG_RETURN_ERROR( "unable to write attribute to new HDF " "file", "TransferAttributes", false); } /* for attr_index */ /* free memory */ free(buffer); /* close current SDS */ SDendaccess( new_sds ); } /* for curr_band */ /* close current SDS */ SDendaccess( old_sds ); } /* for curr_sds */ return true; }