Exemplo n.º 1
0
/****************************************************************
**
**  test_vlstrings_special(): Test VL string code for special
**      string cases, nil and zero-sized.
**
****************************************************************/
static void
test_vlstrings_special(void)
{
    const char *wdata[SPACE1_DIM1] = {"", "two", "three", "\0"};
    const char *wdata2[SPACE1_DIM1] = {NULL, NULL, NULL, NULL};
    char *rdata[SPACE1_DIM1];   /* Information read in */
    char *fill;                 /* Fill value */
    hid_t		fid1;		/* HDF5 File IDs		*/
    hid_t		dataset;	/* Dataset ID			*/
    hid_t		sid1;       /* Dataspace ID			*/
    hid_t		tid1;       /* Datatype ID			*/
    hid_t		dcpl;       /* Dataset creation property list ID */
    hsize_t		dims1[] = {SPACE1_DIM1};
    unsigned       i;          /* counting variable */
    herr_t		ret;		/* Generic return value		*/

    /* Output message about test being performed */
    MESSAGE(5, ("Testing Special VL Strings\n"));

    /* Create file */
    fid1 = H5Fcreate(DATAFILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
    CHECK(fid1, FAIL, "H5Fcreate");

    /* Create dataspace for datasets */
    sid1 = H5Screate_simple(SPACE1_RANK, dims1, NULL);
    CHECK(sid1, FAIL, "H5Screate_simple");

    /* Create a datatype to refer to */
    tid1 = H5Tcopy(H5T_C_S1);
    CHECK(tid1, FAIL, "H5Tcopy");

    ret = H5Tset_size(tid1,H5T_VARIABLE);
    CHECK(ret, FAIL, "H5Tset_size");

    /* Create a dataset */
    dataset = H5Dcreate2(fid1, "Dataset3", tid1, sid1, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
    CHECK(dataset, FAIL, "H5Dcreate2");

    /* Read from dataset before writing data */
    ret = H5Dread(dataset, tid1, H5S_ALL, H5S_ALL, H5P_DEFAULT, rdata);
    CHECK(ret, FAIL, "H5Dread");

    /* Check data read in */
    for(i = 0; i < SPACE1_DIM1; i++)
        if(rdata[i] != NULL)
            TestErrPrintf("VL doesn't match!, rdata[%d]=%p\n",(int)i,rdata[i]);

    /* Write dataset to disk */
    ret = H5Dwrite(dataset, tid1, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata);
    CHECK(ret, FAIL, "H5Dwrite");

    /* Read dataset from disk */
    ret = H5Dread(dataset, tid1, H5S_ALL, H5S_ALL, H5P_DEFAULT, rdata);
    CHECK(ret, FAIL, "H5Dread");

    /* Compare data read in */
    for(i = 0; i < SPACE1_DIM1; i++) {
        if(HDstrlen(wdata[i]) != HDstrlen(rdata[i])) {
            TestErrPrintf("VL data length don't match!, strlen(wdata[%d])=%d, strlen(rdata[%d])=%d\n",(int)i,(int)strlen(wdata[i]),(int)i,(int)strlen(rdata[i]));
            continue;
        } /* end if */
        if((wdata[i] == NULL && rdata[i] != NULL) || (rdata[i] == NULL && wdata[i] != NULL)) {
            TestErrPrintf("VL data values don't match!\n");
            continue;
        } /* end if */
        if(HDstrcmp(wdata[i], rdata[i]) != 0 ) {
            TestErrPrintf("VL data values don't match!, wdata[%d]=%s, rdata[%d]=%s\n",(int)i,wdata[i],(int)i,rdata[i]);
            continue;
        } /* end if */
    } /* end for */

    /* Reclaim the read VL data */
    ret = H5Dvlen_reclaim(tid1, sid1, H5P_DEFAULT, rdata);
    CHECK(ret, FAIL, "H5Dvlen_reclaim");

    /* Close Dataset */
    ret = H5Dclose(dataset);
    CHECK(ret, FAIL, "H5Dclose");

    /* Create another dataset to test nil strings */
    dcpl = H5Pcreate(H5P_DATASET_CREATE);
    CHECK(dcpl, FAIL, "H5Pcreate");

    /* Set the fill value for the second dataset */
    fill = NULL;
    ret = H5Pset_fill_value(dcpl, tid1, &fill);
    CHECK(ret, FAIL, "H5Pset_fill_value");

    dataset = H5Dcreate2(fid1, "Dataset4", tid1, sid1, H5P_DEFAULT, dcpl, H5P_DEFAULT);
    CHECK(dataset, FAIL, "H5Dcreate2");

    /* Close dataset creation property list */
    ret = H5Pclose(dcpl);
    CHECK(ret, FAIL, "H5Pclose");

    /* Read from dataset before writing data */
    ret = H5Dread(dataset, tid1, H5S_ALL, H5S_ALL, H5P_DEFAULT, rdata);
    CHECK(ret, FAIL, "H5Dread");

    /* Check data read in */
    for(i = 0; i < SPACE1_DIM1; i++)
        if(rdata[i] != NULL)
            TestErrPrintf("VL doesn't match!, rdata[%d]=%p\n",(int)i,rdata[i]);

    /* Try to write nil strings to disk. */
    ret = H5Dwrite(dataset, tid1, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata2);
    CHECK(ret, FAIL, "H5Dwrite");

    /* Read nil strings back from disk */
    ret = H5Dread(dataset, tid1, H5S_ALL, H5S_ALL, H5P_DEFAULT, rdata);
    CHECK(ret, FAIL, "H5Dread");

    /* Check data read in */
    for(i = 0; i < SPACE1_DIM1; i++)
        if(rdata[i] != NULL)
            TestErrPrintf("VL doesn't match!, rdata[%d]=%p\n",(int)i,rdata[i]);

    /* Close Dataset */
    ret = H5Dclose(dataset);
    CHECK(ret, FAIL, "H5Dclose");

    /* Close datatype */
    ret = H5Tclose(tid1);
    CHECK(ret, FAIL, "H5Tclose");

    /* Close disk dataspace */
    ret = H5Sclose(sid1);
    CHECK(ret, FAIL, "H5Sclose");

    /* Close file */
    ret = H5Fclose(fid1);
    CHECK(ret, FAIL, "H5Fclose");
}
Exemplo n.º 2
0
/*+++++++++++++++++++++++++ Main Program or Function +++++++++++++++*/
void SCIA_WR_H5_MPH( struct param_record param, 
		     const struct mph_envi *mph )
{
     register unsigned short ni = 0;

     hid_t   mph_type[NFIELDS];

     const int compress = 0;
     const char *mph_names[NFIELDS] = {
	  "product_name", "proc_stage", "ref_doc", 
	  "acquisition_station", "proc_center", "proc_time", 
	  "software_version", 
	  "sensing_start", "sensing_stop", 
	  "phase", "cycle", "rel_orbit", "abs_orbit", "state_vector_time", 
	  "delta_ut1", 
	  "x_position", "y_position", "z_position", 
	  "x_velocity", "y_velocity", "z_velocity",
	  "vector_source", "utc_sbt_time", "sat_binary_time", "clock_step", 
	  "leap_utc", "leap_sign", "leap_err", 
	  "product_err", "tot_size", "sph_size", "num_dsd", "dsd_size", 
	  "num_data_sets"
     };
/*
 * define user-defined data types of the Table-fields
 */
     mph_type[0] = H5Tcopy( H5T_C_S1 );
     (void) H5Tset_size( mph_type[0], (size_t) ENVI_FILENAME_SIZE );
     mph_type[1] = H5Tcopy( H5T_C_S1 );
     (void) H5Tset_size( mph_type[1], (size_t) 2 );
     mph_type[2] = H5Tcopy( H5T_C_S1 );
     (void) H5Tset_size( mph_type[2], (size_t) 24 );

     mph_type[3] = H5Tcopy( H5T_C_S1 );
     (void) H5Tset_size( mph_type[3], (size_t) 21 );
     mph_type[4] = H5Tcopy( H5T_C_S1 );
     (void) H5Tset_size( mph_type[4], (size_t) 7 );
     mph_type[5] = H5Tcopy( H5T_C_S1 );
     (void) H5Tset_size( mph_type[5], (size_t) UTC_STRING_LENGTH );
     mph_type[6] = H5Tcopy( H5T_C_S1 );
     (void) H5Tset_size( mph_type[6], (size_t) 15 );

     mph_type[7] = H5Tcopy( H5T_C_S1 );
     (void) H5Tset_size( mph_type[7], (size_t) UTC_STRING_LENGTH );
     mph_type[8] = H5Tcopy( H5T_C_S1 );
     (void) H5Tset_size( mph_type[8], (size_t) UTC_STRING_LENGTH );

     mph_type[9] = H5Tcopy( H5T_C_S1 );
     (void) H5Tset_size( mph_type[9], (size_t) 2 );
     mph_type[10] = H5Tcopy( H5T_NATIVE_SHORT );
     mph_type[11] = H5Tcopy( H5T_NATIVE_INT );
     mph_type[12] = H5Tcopy( H5T_NATIVE_INT );
     mph_type[13] = H5Tcopy( H5T_C_S1 );
     (void) H5Tset_size( mph_type[13], (size_t) UTC_STRING_LENGTH );
     mph_type[14] = H5Tcopy( H5T_NATIVE_DOUBLE );
     mph_type[15] = H5Tcopy( H5T_NATIVE_DOUBLE );
     mph_type[16] = H5Tcopy( H5T_NATIVE_DOUBLE );
     mph_type[17] = H5Tcopy( H5T_NATIVE_DOUBLE );
     mph_type[18] = H5Tcopy( H5T_NATIVE_DOUBLE );
     mph_type[19] = H5Tcopy( H5T_NATIVE_DOUBLE );
     mph_type[20] = H5Tcopy( H5T_NATIVE_DOUBLE );

     mph_type[21] = H5Tcopy( H5T_C_S1 );
     (void) H5Tset_size( mph_type[21], (size_t) 3 );
     mph_type[22] = H5Tcopy( H5T_C_S1 );
     (void) H5Tset_size( mph_type[22], (size_t) UTC_STRING_LENGTH );
     mph_type[23] = H5Tcopy( H5T_NATIVE_UINT );
     mph_type[24] = H5Tcopy( H5T_NATIVE_UINT );

     mph_type[25] = H5Tcopy( H5T_C_S1 );
     (void) H5Tset_size( mph_type[25], (size_t) UTC_STRING_LENGTH );
     mph_type[26] = H5Tcopy( H5T_NATIVE_SHORT );
     mph_type[27] = H5Tcopy( H5T_C_S1 );
     (void) H5Tset_size( mph_type[27], (size_t) 2 );

     mph_type[28] = H5Tcopy( H5T_C_S1 );
     (void) H5Tset_size( mph_type[28], (size_t) 2 );
     mph_type[29] = H5Tcopy( H5T_NATIVE_UINT );
     mph_type[30] = H5Tcopy( H5T_NATIVE_UINT );
     mph_type[31] = H5Tcopy( H5T_NATIVE_UINT );
     mph_type[32] = H5Tcopy( H5T_NATIVE_UINT );
     mph_type[33] = H5Tcopy( H5T_NATIVE_UINT );
/*
 * create table
 */
     (void) H5TBmake_table( "Main Product Header", param.hdf_file_id, "MPH", 
			    NFIELDS, 1, mph_size, mph_names, mph_offs, 
			    mph_type, 1, NULL, compress, mph );
/*
 * create some attributes for quick access
 */
     (void) H5LTset_attribute_int( param.hdf_file_id, "/", "abs_orbit", 
				   &mph->abs_orbit, 1 );
/*
 * close interface
 */
     do {
	  (void) H5Tclose( mph_type[ni] );
     } while ( ++ni < NFIELDS );
}
Exemplo n.º 3
0
int main (int argc, char ** argv)  
{
    char        filename [256]; 
    int         rank, size, gidx, i, j, k,l;
    MPI_Comm    comm_dummy = MPI_COMM_WORLD;  /* MPI_Comm is defined through adios_read.h */
    enum ADIOS_DATATYPES attr_type;
    void      * data = NULL;
    uint64_t    start[] = {0,0,0,0,0,0,0,0,0,0};
    uint64_t    count[MAX_DIMS], hcount[MAX_DIMS], bytes_read = 0;
    herr_t      h5_err;
    char        h5name[256],aname[256],fname[256];
    int         dims [MAX_DIMS];
    int         h5rank[MAX_DIMS];
    int         h5i, level;
    hid_t       grp_id [GMAX+1], space_id, dataset_id;
    hid_t       memspace_id, dataspace_id, att_id;
    char        ** grp_name;
    hid_t       type_id;
    hid_t       h5_type_id;
    hsize_t     adims;

    if (argc < 2) {
        printf("Usage: %s <BP-file> <HDF5-file>\n", argv[0]);
        return 1;
    }

    MPI_Init(&argc, &argv);
    h5_err = H5Eset_auto(NULL, NULL );
    ADIOS_FILE * f = adios_fopen (argv[1], comm_dummy);
    HDF5_FILE = H5Fcreate(argv[2],H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);

    /* create the complex types for HDF5 */
    complex_real_id = H5Tcreate (H5T_COMPOUND, sizeof (complex_real_t));
    H5Tinsert (complex_real_id, "real", HOFFSET(complex_real_t,re), H5T_NATIVE_FLOAT);
    H5Tinsert (complex_real_id, "imaginary", HOFFSET(complex_real_t,im), H5T_NATIVE_FLOAT);

    complex_double_id = H5Tcreate (H5T_COMPOUND, sizeof (complex_double_t));
    H5Tinsert (complex_double_id, "real", HOFFSET(complex_double_t,re), H5T_NATIVE_DOUBLE);
    H5Tinsert (complex_double_id, "imaginary", HOFFSET(complex_double_t,im), H5T_NATIVE_DOUBLE);

    if (f == NULL) {
        if (DEBUG) printf ("%s\n", adios_errmsg());
	return -1;
    }
    /* For all groups */
    for (gidx = 0; gidx < f->groups_count; gidx++) {
        if (DEBUG) printf("Group %s:\n", f->group_namelist[gidx]);
        ADIOS_GROUP * g = adios_gopen (f, f->group_namelist[gidx]);
        if (g == NULL) {
            if (DEBUG) printf ("%s\n", adios_errmsg());
            return -1;
        }
/* First create all of the groups */
        grp_id [0] = HDF5_FILE;
        for (i = 0; i < g->vars_count; i++) {
             ADIOS_VARINFO * v = adios_inq_var_byid (g, i);
             strcpy(h5name,g->var_namelist[i]);
             grp_name = bp_dirparser (h5name, &level);
             for (j = 0; j < level-1; j++) {
                grp_id [j + 1] = H5Gopen (grp_id [j], grp_name [j]);
                if (grp_id [j + 1] < 0) {
                   grp_id [j + 1] = H5Gcreate (grp_id [j], grp_name [j], 0);
                }
             }
             for (j=1; j<level; j++) {
                  H5Gclose(grp_id[j]);
             }
        }
/* Now we can write data into these scalars */        
        /* For all variables */
        if (DEBUG) printf("  Variables=%d:\n", g->vars_count);
        for (i = 0; i < g->vars_count; i++) {
             ADIOS_VARINFO * v = adios_inq_var_byid (g, i);

            uint64_t total_size = adios_type_size (v->type, v->value);
            for (j = 0; j < v->ndim; j++)
                total_size *= v->dims[j];
            strcpy(h5name,g->var_namelist[i]);
            if (DEBUG) printf("    %-9s  %s", adios_type_to_string(v->type), g->var_namelist[i]);
            h5_err = bp_getH5TypeId (v->type, &h5_type_id);
            if (v->type==adios_string) H5Tset_size(h5_type_id,strlen(v->value)); 
            if (v->ndim == 0) {
                /* Scalars do not need to be read in, we get it from the metadata
                   when using adios_inq_var */
                if (DEBUG) printf(" = %s\n", value_to_string(v->type, v->value, 0));
                 // add the hdf5 dataset, these are scalars
                for (h5i = 0;h5i<MAX_DIMS;h5i++) 
                   count[0] = 0;
                count[0] = 1; // we are writing just 1 element, RANK=1
                h5_err = bp_getH5TypeId (v->type, &h5_type_id);
                H5LTmake_dataset(HDF5_FILE,h5name,1,count,h5_type_id,v->value);
            } else {

                    h5_err = readVar(g, v,  h5name);
            }
            adios_free_varinfo (v);
        } /* variables */

        /* For all attributes */
        if (DEBUG) printf("  Attributes=%d:\n", g->attrs_count);
        for (i = 0; i < g->attrs_count; i++) {
            enum ADIOS_DATATYPES atype;
            int  asize;
	    void *adata;
            adios_get_attr_byid (g, i, &atype, &asize, &adata);
            grp_name = bp_dirparser (g->attr_namelist[i], &level);
            strcpy(aname,grp_name[level-1]); 
// the name of the attribute is the last in the array
// we then need to concat the rest together
            strcpy(fname,"/");
            for (j=0;j<level-1;j++) {
              strcat(fname,grp_name[j]); 
            }
            h5_err = bp_getH5TypeId (atype, &h5_type_id);

            // let's create the attribute
            adims = 1;
            if (atype==adios_string) H5Tset_size(h5_type_id,strlen(adata)); 
            space_id = H5Screate(H5S_SCALAR); // just a scalar
            att_id = H5Acreate(HDF5_FILE, g->attr_namelist[i], h5_type_id, space_id,H5P_DEFAULT);
            h5_err = H5Awrite(att_id, h5_type_id, adata);
            h5_err = H5Aclose(att_id);
            h5_err = H5Sclose(space_id);

            if (DEBUG) printf("    %-9s  %s = %s\n", adios_type_to_string(atype), 
                    g->attr_namelist[i], value_to_string(atype, adata, 0));
            free(adata);
        } /* attributes */

        adios_gclose (g);
    } /* groups */

    adios_fclose (f);
    h5_err =  H5Fclose(HDF5_FILE);

    MPI_Finalize();
    return 0;
}
Exemplo n.º 4
0
int
main (int argc, char *argv[])
{
  inp_t input_params;
  mdl_t source_mdl;
  net_t network;
  int cell_index;

  int verbose = 1;
  char *input_file;

  /* Parse options and command line arguments. Diplay help
     message if no (or more than one) argument is given. */

    {
      int opt;

      static struct option longopts[] = {
          {"help", no_argument, NULL, 'h'},
          {"version", no_argument, NULL, 'V'},
          {"verbose", no_argument, NULL, 'v'},
          {"quiet", no_argument, NULL, 'q'},
          {0, 0, 0, 0}
      };

      while ((opt = getopt_long (argc, argv, "hVvq", longopts, NULL)) != -1)
        {
          switch (opt)
            {
            case 'h':
              usage ();
              return EXIT_SUCCESS;
              break;
            case 'V':
              version ();
              return EXIT_SUCCESS;
              break;
            case 'v':
              verbose = 2;
              break;
            case 'q':
              verbose = 0;
              break;
            default:
              usage ();
              return EXIT_FAILURE;
            }
        };
      argc -= optind;
      argv += optind;
      if (argc != 1)
        {
          usage ();
          return EXIT_FAILURE;
        }
      input_file = argv[0];
    }

  /* Read the input file */
  if( read_input_file_names (input_file, &input_params.files, verbose) != EXIT_SUCCESS )
    {
      return EXIT_FAILURE;
    }

  /* Read the chemical network file */
  if( read_network (input_params.files.chem_file, &network, verbose) != EXIT_SUCCESS )
    {
      return EXIT_FAILURE;
    }

  /* Read the input file */
  if( read_input (input_file, &input_params, &network, verbose) != EXIT_SUCCESS )
    {
      return EXIT_FAILURE;
    }

  /* Read the source model file */
  if( read_source (input_params.files.source_file, &source_mdl, &input_params,
               verbose) != EXIT_SUCCESS )
    {
      return EXIT_FAILURE;
    }

  // Hdf5 files, datatype and dataspace
  hid_t       fid, datatype, dataspace, dataset, tsDataset, tsDataspace,  speciesDataset, speciesDataspace, speciesType;
  datatype = H5Tcopy(H5T_NATIVE_DOUBLE);

  hsize_t     dimsf[ ROUTE_DATASET_RANK ]={  source_mdl.n_cells, source_mdl.ts.n_time_steps, input_params.output.n_output_species, N_OUTPUT_ROUTES };
  fid = H5Fcreate( "astrochem_output.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); 
  dataspace = H5Screate_simple( ABUNDANCE_DATASET_RANK, dimsf, NULL);

  // Add Atributes
  hid_t simpleDataspace = H5Screate(H5S_SCALAR);
  hid_t attrType = H5Tcopy(H5T_C_S1);
  H5Tset_size ( attrType, MAX_CHAR_FILENAME );
  H5Tset_strpad(attrType,H5T_STR_NULLTERM);
  hid_t attrNetwork = H5Acreate( fid, "chem_file", attrType, simpleDataspace, H5P_DEFAULT, H5P_DEFAULT);
  H5Awrite( attrNetwork, attrType, realpath( input_params.files.chem_file, NULL ) );
  H5Aclose( attrNetwork );
  hid_t attrModel = H5Acreate( fid, "source_file", attrType, simpleDataspace, H5P_DEFAULT, H5P_DEFAULT);
  H5Awrite( attrModel, attrType, realpath( input_params.files.source_file, NULL ) );
  H5Aclose( attrModel );

  H5Tclose( attrType );
  H5Sclose( simpleDataspace );

  // Define chunk property
  hsize_t     chunk_dims[ ROUTE_DATASET_RANK ] = { 1, 1, input_params.output.n_output_species, N_OUTPUT_ROUTES };
  hid_t prop_id = H5Pcreate(H5P_DATASET_CREATE);
  H5Pset_chunk(prop_id, ABUNDANCE_DATASET_RANK , chunk_dims);

  // Create dataset
  dataset = H5Dcreate(fid, "Abundances", datatype, dataspace, H5P_DEFAULT, prop_id, H5P_DEFAULT);

  int i;
  hid_t dataspaceRoute, route_t_datatype, r_t_datatype, route_prop_id, routeGroup;
  hid_t routeDatasets[ input_params.output.n_output_species ];
  if (input_params.output.trace_routes)
    {

      // Create route dataspace
      dataspaceRoute = H5Screate_simple( ROUTE_DATASET_RANK, dimsf, NULL);

      // Create route datatype
      r_t_datatype = H5Tcreate (H5T_COMPOUND, sizeof(r_t));
      H5Tinsert( r_t_datatype, "reaction_number", HOFFSET(r_t, reaction_no ), H5T_NATIVE_INT);
      H5Tinsert( r_t_datatype, "reaction_rate", HOFFSET(r_t, rate), H5T_NATIVE_DOUBLE);

      route_t_datatype = H5Tcreate (H5T_COMPOUND, sizeof(rout_t));
      H5Tinsert( route_t_datatype, "formation_rate", HOFFSET(rout_t, formation ), r_t_datatype );
      H5Tinsert( route_t_datatype, "destruction_rate", HOFFSET(rout_t, destruction ), r_t_datatype );

      // Define route chunk property
      route_prop_id = H5Pcreate(H5P_DATASET_CREATE);
      H5Pset_chunk( route_prop_id, ROUTE_DATASET_RANK, chunk_dims);


      // Create each named route dataset
      routeGroup = H5Gcreate( fid, "Routes", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT );
      char routeName[6] = "route_";
      char tempName[ MAX_CHAR_SPECIES + sizeof( routeName ) ];
      for( i = 0; i < input_params.output.n_output_species ; i++ )
        {
          strcpy( tempName, routeName );
          strcat( tempName, network.species[input_params.output.output_species_idx[i]].name );
          routeDatasets[i] = H5Dcreate( routeGroup, tempName, route_t_datatype, dataspaceRoute, H5P_DEFAULT, route_prop_id, H5P_DEFAULT);
        }
    }
  // Timesteps and species
  hsize_t n_ts = source_mdl.ts.n_time_steps;
  hsize_t n_species =  input_params.output.n_output_species ;
  tsDataspace = H5Screate_simple( 1, &n_ts, NULL);
  speciesDataspace = H5Screate_simple( 1, &n_species, NULL);
  speciesType = H5Tcopy (H5T_C_S1);
  H5Tset_size (speciesType, MAX_CHAR_SPECIES );
  H5Tset_strpad(speciesType,H5T_STR_NULLTERM);

  // Create ts and species datasets
  tsDataset = H5Dcreate(fid, "TimeSteps", datatype, tsDataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
  speciesDataset = H5Dcreate(fid, "Species", speciesType, speciesDataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
  double* convTs = (double*) malloc( sizeof(double)* source_mdl.ts.n_time_steps );
  for( i=0; i< source_mdl.ts.n_time_steps; i++ )
    {
      convTs[i] = source_mdl.ts.time_steps[i] / CONST_MKSA_YEAR;
    }

  H5Dwrite( tsDataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, convTs );


  char speciesName [ input_params.output.n_output_species ][ MAX_CHAR_SPECIES ];
  for( i = 0; i < input_params.output.n_output_species ; i++ )
    {
      strcpy( speciesName[i], network.species[input_params.output.output_species_idx[i]].name );
    }

  H5Dwrite( speciesDataset, speciesType, H5S_ALL, H5S_ALL, H5P_DEFAULT, speciesName );

  free( convTs );
  H5Dclose( tsDataset );
  H5Dclose( speciesDataset );
  H5Tclose( speciesType );
  H5Sclose( tsDataspace );
  H5Sclose( speciesDataspace );


#ifdef HAVE_OPENMP
  /*Initialize lock*/
  omp_init_lock(&lock);


  /* Solve the ODE system for each cell. */
    {
#pragma omp parallel for schedule (dynamic, 1)
#endif
      for (cell_index = 0; cell_index < source_mdl.n_cells; cell_index++)
        {
          if (verbose >= 1)
            fprintf (stdout, "Computing abundances in cell %d...\n",
                     cell_index);
          if( full_solve ( fid, dataset, routeDatasets, dataspace, dataspaceRoute, datatype, route_t_datatype, cell_index, &input_params, source_mdl.mode,
                       &source_mdl.cell[cell_index], &network, &source_mdl.ts, verbose) != EXIT_SUCCESS )
            {
	      exit (EXIT_FAILURE);
            }
          if (verbose >= 1)
            fprintf (stdout, "Done with cell %d.\n", cell_index);
        }
#ifdef HAVE_OPENMP
    }


  /*Finished lock mechanism, destroy it*/
  omp_destroy_lock(&lock);
#endif

  /*
   * Close/release hdf5 resources.
   */
  if (input_params.output.trace_routes)
    {


      for( i = 0; i <  input_params.output.n_output_species ; i++ )
        {
          H5Dclose(routeDatasets[i] );
        }
      H5Sclose(dataspaceRoute);
      H5Gclose(routeGroup);
      H5Pclose(route_prop_id);
      H5Tclose(r_t_datatype);
      H5Tclose(route_t_datatype);
    }
  H5Dclose(dataset);
  H5Pclose(prop_id);
  H5Sclose(dataspace);
  H5Tclose(datatype);

  H5Fclose(fid);

  free_input (&input_params);
  free_mdl (&source_mdl);
  free_network (&network);
  return (EXIT_SUCCESS);
}
Exemplo n.º 5
0
int main( void )
{
 typedef struct Particle 
 {
  char   name[16];
  int    lati;
  int    longi;
  float  pressure;
  double temperature; 
 } Particle;

 Particle  dst_buf[NRECORDS+NRECORDS_ADD];

/* Define an array of Particles */
 Particle  p_data[NRECORDS] = { 
 {"zero",0,0, 0.0f, 0.0},
 {"one",10,10, 1.0f, 10.0},
 {"two",  20,20, 2.0f, 20.0},
 {"three",30,30, 3.0f, 30.0},
 {"four", 40,40, 4.0f, 40.0},
 {"five", 50,50, 5.0f, 50.0},
 {"six",  60,60, 6.0f, 60.0},
 {"seven",70,70, 7.0f, 70.0}
  };

 /* Calculate the size and the offsets of our struct members in memory */
 size_t dst_size =  sizeof( Particle );
 size_t dst_offset[NFIELDS] = { HOFFSET( Particle, name ),
                                HOFFSET( Particle, lati ),
                                HOFFSET( Particle, longi ),
                                HOFFSET( Particle, pressure ),
                                HOFFSET( Particle, temperature )};

 size_t dst_sizes[NFIELDS] = { sizeof( p_data[0].name),
                               sizeof( p_data[0].lati),
                               sizeof( p_data[0].longi),
                               sizeof( p_data[0].pressure),
                               sizeof( p_data[0].temperature)};
 
 /* Define field information */
 const char *field_names[NFIELDS] = 
 { "Name","Latitude", "Longitude", "Pressure", "Temperature" };
 hid_t      field_type[NFIELDS];
 hid_t      string_type;
 hid_t      file_id;
 hsize_t    chunk_size = 10;
 int        *fill_data = NULL;
 int        compress  = 0;
 herr_t     status; 
 int        i;

  /* Append particles */ 
 Particle particle_in[ NRECORDS_ADD ] = 
 {{ "eight",80,80, 8.0f, 80.0},
 {"nine",90,90, 9.0f, 90.0} };

 /* Initialize the field field_type */
 string_type = H5Tcopy( H5T_C_S1 );
 H5Tset_size( string_type, 16 );
 field_type[0] = string_type;
 field_type[1] = H5T_NATIVE_INT;
 field_type[2] = H5T_NATIVE_INT;
 field_type[3] = H5T_NATIVE_FLOAT;
 field_type[4] = H5T_NATIVE_DOUBLE;
   
 /* Create a new file using default properties. */
 file_id = H5Fcreate( "ex_table_02.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT );

 /* make a table */
 status=H5TBmake_table( "Table Title",file_id,TABLE_NAME,NFIELDS,NRECORDS, 
                        dst_size, field_names, dst_offset, field_type, 
                        chunk_size, fill_data, compress, p_data  );

 /* append two records */
 status=H5TBappend_records(file_id, TABLE_NAME,NRECORDS_ADD, dst_size, dst_offset, dst_sizes, 
  &particle_in );

 /* read the table */
 status=H5TBread_table( file_id, TABLE_NAME, dst_size, dst_offset, dst_sizes, dst_buf );

 /* print it by rows */
 for (i=0; i<NRECORDS+NRECORDS_ADD; i++) {
  printf ("%-5s %-5d %-5d %-5f %-5f", 
   dst_buf[i].name,
   dst_buf[i].lati,
   dst_buf[i].longi,
   dst_buf[i].pressure,
   dst_buf[i].temperature);
  printf ("\n");
 }
 
 /* close type */
 H5Tclose( string_type );
 
 /* close the file */
 H5Fclose( file_id );

 return 0;
}
Exemplo n.º 6
0
/****************************************************************
**
**  test_write_vl_string_attribute(): Test basic VL string code.
**      Tests writing VL strings as attributes
**
****************************************************************/
static void test_write_vl_string_attribute(void)
{
    hid_t file, root, dataspace, att;
    hid_t type;
    herr_t ret;
    char *string_att_check = NULL;

    /* Open the file */
    file = H5Fopen(DATAFILE, H5F_ACC_RDWR, H5P_DEFAULT);
    CHECK(file, FAIL, "H5Fopen");

    /* Create a datatype to refer to. */
    type = H5Tcopy (H5T_C_S1);
    CHECK(type, FAIL, "H5Tcopy");

    ret = H5Tset_size (type, H5T_VARIABLE);
    CHECK(ret, FAIL, "H5Tset_size");

    root = H5Gopen2(file, "/", H5P_DEFAULT);
    CHECK(root, FAIL, "H5Gopen2");

    dataspace = H5Screate(H5S_SCALAR);
    CHECK(dataspace, FAIL, "H5Screate");

    /* Test creating a "normal" sized string attribute */
    att = H5Acreate2(root, "test_scalar", type, dataspace, H5P_DEFAULT, H5P_DEFAULT);
    CHECK(att, FAIL, "H5Acreate2");

    ret = H5Awrite(att, type, &string_att);
    CHECK(ret, FAIL, "H5Awrite");

    ret = H5Aread(att, type, &string_att_check);
    CHECK(ret, FAIL, "H5Aread");

    if(HDstrcmp(string_att_check,string_att) != 0)
        TestErrPrintf("VL string attributes don't match!, string_att=%s, string_att_check=%s\n",string_att,string_att_check);

    H5free_memory(string_att_check);
    string_att_check = NULL;

    ret = H5Aclose(att);
    CHECK(ret, FAIL, "HAclose");

    /* Test creating a "large" sized string attribute */
    att = H5Acreate2(root, "test_scalar_large", type, dataspace, H5P_DEFAULT, H5P_DEFAULT);
    CHECK(att, FAIL, "H5Acreate2");

    string_att_write = (char*)HDcalloc((size_t)8192, sizeof(char));
    HDmemset(string_att_write, 'A', (size_t)8191);

    ret = H5Awrite(att, type, &string_att_write);
    CHECK(ret, FAIL, "H5Awrite");

    ret = H5Aread(att, type, &string_att_check);
    CHECK(ret, FAIL, "H5Aread");

    if(HDstrcmp(string_att_check,string_att_write) != 0)
        TestErrPrintf("VL string attributes don't match!, string_att_write=%s, string_att_check=%s\n",string_att_write,string_att_check);

    H5free_memory(string_att_check);
    string_att_check = NULL;

    /* The attribute string written is freed below, in the test_read_vl_string_attribute() test */
    /* HDfree(string_att_write); */

    ret = H5Aclose(att);
    CHECK(ret, FAIL, "HAclose");

    ret = H5Gclose(root);
    CHECK(ret, FAIL, "H5Gclose");

    ret = H5Tclose(type);
    CHECK(ret, FAIL, "H5Tclose");

    ret = H5Sclose(dataspace);
    CHECK(ret, FAIL, "H5Sclose");

    ret = H5Fclose(file);
    CHECK(ret, FAIL, "H5Fclose");

    return;
}
Exemplo n.º 7
0
/****************************************************************
**
**  test_vl_rewrite(): Test basic VL string code.
**      Tests I/O on VL strings when lots of objects in the file
**      have been linked/unlinked.
**
****************************************************************/
static void test_vl_rewrite(void)
{
    hid_t file1, file2; /* File IDs */
    hid_t type;         /* VL string datatype ID */
    hid_t space;        /* Scalar dataspace */
    char name[256];     /* Buffer for names & data */
    int i;              /* Local index variable */
    herr_t ret;         /* Generic return value */

    /* Create the VL string datatype */
    type = H5Tcopy(H5T_C_S1);
    CHECK(type, FAIL, "H5Tcopy");

    ret = H5Tset_size(type, H5T_VARIABLE);
    CHECK(ret, FAIL, "H5Tset_size");

    /* Create the scalar dataspace */
    space = H5Screate(H5S_SCALAR);
    CHECK(space, FAIL, "H5Screate");

    /* Open the files */
    file1 = H5Fcreate(DATAFILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
    CHECK(file1, FAIL, "H5Fcreate");

    file2 = H5Fcreate(DATAFILE2, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
    CHECK(file1, FAIL, "H5Fcreate");

    /* Create in file 1 */
    for(i=0; i<REWRITE_NDATASETS; i++) {
        sprintf(name, "/set_%d", i);
        write_scalar_dset(file1, type, space, name, name);
    }

    /* Effectively copy data from file 1 to 2 */
    for(i=0; i<REWRITE_NDATASETS; i++) {
        sprintf(name, "/set_%d", i);
        read_scalar_dset(file1, type, space, name, name);
        write_scalar_dset(file2, type, space, name, name);
    }

    /* Read back from file 2 */
    for(i = 0; i < REWRITE_NDATASETS; i++) {
        sprintf(name, "/set_%d", i);
        read_scalar_dset(file2, type, space, name, name);
    } /* end for */

    /* Remove from file 2. */
    for(i = 0; i < REWRITE_NDATASETS; i++) {
        sprintf(name, "/set_%d", i);
        ret = H5Ldelete(file2, name, H5P_DEFAULT);
        CHECK(ret, FAIL, "H5Ldelete");
    } /* end for */

    /* Effectively copy from file 1 to file 2 */
    for(i = 0; i < REWRITE_NDATASETS; i++) {
        sprintf(name, "/set_%d", i);
        read_scalar_dset(file1, type, space, name, name);
        write_scalar_dset(file2, type, space, name, name);
    } /* end for */

    /* Close everything */
    ret = H5Tclose(type);
    CHECK(ret, FAIL, "H5Tclose");

    ret = H5Sclose(space);
    CHECK(ret, FAIL, "H5Sclose");

    ret = H5Fclose(file1);
    CHECK(ret, FAIL, "H5Fclose");

    ret = H5Fclose(file2);
    CHECK(ret, FAIL, "H5Fclose");

    return;
} /* end test_vl_rewrite() */
/*-------------------------------------------------------------------------
 * Function:    gent_att_compound_vlstr
 *
 * Purpose:     Generate a dataset and a group.
 *              Both has an attribute with a compound datatype consisting 
 *              of a variable length string
 *
 *-------------------------------------------------------------------------
 */
static void gent_att_compound_vlstr(hid_t loc_id)
{
    typedef struct { /* Compound structure for the attribute */
        int i;
        char *v;
    } s1;
    hsize_t dim[1] = {1};	/* Dimension size */
    hid_t sid = -1; 		/* Dataspace ID */
    hid_t tid = -1; 		/* Datatype ID */
    hid_t aid = -1; 		/* Attribute ID */
    hid_t did = -1; 		/* Dataset ID */
    hid_t gid = -1; 		/* Group ID */
    hid_t vl_str_tid = -1;	/* Variable length datatype ID */
    hid_t cmpd_tid = -1;	/* Compound datatype ID */
    hid_t null_sid = -1;	/* Null dataspace ID */
    s1 buf;                 /* Buffer */

    buf.i = 9;
    buf.v = "ThisIsAString";

    /* Create an integer datatype */
    if((tid = H5Tcopy(H5T_NATIVE_INT)) < 0)
        goto error;

    /* Create a variable length string */
    if((vl_str_tid = H5Tcopy(H5T_C_S1)) < 0)
        goto error;
    if(H5Tset_size(vl_str_tid, H5T_VARIABLE) < 0)
        goto error;

    /* Create a compound datatype with a variable length string and an integer */
    if((cmpd_tid = H5Tcreate(H5T_COMPOUND, sizeof(s1))) < 0)
        goto error;
    if(H5Tinsert(cmpd_tid, "i", HOFFSET(s1, i), tid) < 0)
        goto error;
    if(H5Tinsert(cmpd_tid, "v", HOFFSET(s1, v), vl_str_tid) < 0)
        goto error;

    /* Create a dataset */
    if((null_sid = H5Screate(H5S_NULL)) < 0)
        goto error;
    if((did = H5Dcreate2(loc_id, DATASET_ATTR, H5T_NATIVE_INT, null_sid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        goto error;

    /* Attach an attribute with the compound datatype to the dataset */
    if((sid = H5Screate_simple(1, dim, dim)) < 0)
        goto error;
    if((aid = H5Acreate2(did, ATTR, cmpd_tid, sid, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        goto error;

    /* Write the attribute */
    buf.i = 9;
    buf.v = "ThisIsAString";
    if(H5Awrite(aid, cmpd_tid, &buf) < 0)
        goto error;

    /* Close the dataset and its attribute */
    if(H5Dclose(did) < 0)
        goto error;
    if(H5Aclose(aid) < 0)
        goto error;

    /* Create a group */
    if((gid = H5Gcreate2(loc_id, GROUP_ATTR, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        goto error;

    /* Attach an attribute with the compound datatype to the group */
    if((aid = H5Acreate2(gid, ATTR, cmpd_tid, sid, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        goto error;
    if(H5Awrite(aid, cmpd_tid, &buf) < 0)
        goto error;

    /* Close the group and its attribute */
    if(H5Aclose(aid) < 0)
        goto error;
    if(H5Gclose(gid) < 0)
        goto error;

    /* Close dataspaces */
    if(H5Sclose(sid) < 0)
        goto error;
    if(H5Sclose(null_sid) < 0)
        goto error;

    /* Close datatypes */
    if(H5Tclose(tid) < 0)
        goto error;
    if(H5Tclose(vl_str_tid) < 0)
        goto error;
    if(H5Tclose(cmpd_tid) < 0)
        goto error;

error:
    H5E_BEGIN_TRY {
        H5Tclose(tid);
        H5Tclose(vl_str_tid);
        H5Tclose(cmpd_tid);
        H5Sclose(null_sid);
        H5Sclose(sid);
        H5Dclose(did);
        H5Aclose(aid);
        H5Gclose(gid);
    } H5E_END_TRY;

} /* gen_att_compound_vlstr() */
Exemplo n.º 9
0
/*-------------------------------------------------------------------------
 * Function:    test_multi
 *
 * Purpose:     Tests the file handle interface for MUTLI driver
 *
 * Return:      Success:        0
 *              Failure:        -1
 *
 * Programmer:  Raymond Lu
 *              Tuesday, Sept 24, 2002
 *
 *-------------------------------------------------------------------------
 */
static herr_t
test_multi(void)
{
    hid_t       file=(-1), fapl, fapl2=(-1), dset=(-1), space=(-1);
    hid_t       root, attr, aspace, atype;
    hid_t       access_fapl = -1;
    char        filename[1024];
    int         *fhandle2=NULL, *fhandle=NULL;
    hsize_t     file_size;
    H5FD_mem_t  mt, memb_map[H5FD_MEM_NTYPES];
    hid_t       memb_fapl[H5FD_MEM_NTYPES];
    haddr_t     memb_addr[H5FD_MEM_NTYPES];
    const char  *memb_name[H5FD_MEM_NTYPES];
    char        sv[H5FD_MEM_NTYPES][32];
    hsize_t     dims[2]= {MULTI_SIZE, MULTI_SIZE};
    hsize_t     adims[1]= {1};
    char        dname[]="dataset";
    char        meta[] = "this is some metadata on this file";
    int         i, j;
    int         buf[MULTI_SIZE][MULTI_SIZE];

    TESTING("MULTI file driver");
    /* Set file access property list for MULTI driver */
    fapl = h5_fileaccess();

    HDmemset(memb_map, 0,  sizeof memb_map);
    HDmemset(memb_fapl, 0, sizeof memb_fapl);
    HDmemset(memb_name, 0, sizeof memb_name);
    HDmemset(memb_addr, 0, sizeof memb_addr);
    HDmemset(sv, 0, sizeof sv);

    for(mt=H5FD_MEM_DEFAULT; mt<H5FD_MEM_NTYPES; H5_INC_ENUM(H5FD_mem_t,mt)) {
        memb_fapl[mt] = H5P_DEFAULT;
        memb_map[mt] = H5FD_MEM_SUPER;
    }
    memb_map[H5FD_MEM_DRAW] = H5FD_MEM_DRAW;
    memb_map[H5FD_MEM_BTREE] = H5FD_MEM_BTREE;
    memb_map[H5FD_MEM_GHEAP] = H5FD_MEM_GHEAP;

    sprintf(sv[H5FD_MEM_SUPER], "%%s-%c.h5", 's');
    memb_name[H5FD_MEM_SUPER] = sv[H5FD_MEM_SUPER];
    memb_addr[H5FD_MEM_SUPER] = 0;

    sprintf(sv[H5FD_MEM_BTREE],  "%%s-%c.h5", 'b');
    memb_name[H5FD_MEM_BTREE] = sv[H5FD_MEM_BTREE];
    memb_addr[H5FD_MEM_BTREE] = HADDR_MAX/4;

    sprintf(sv[H5FD_MEM_DRAW], "%%s-%c.h5", 'r');
    memb_name[H5FD_MEM_DRAW] = sv[H5FD_MEM_DRAW];
    memb_addr[H5FD_MEM_DRAW] = HADDR_MAX/2;

    sprintf(sv[H5FD_MEM_GHEAP], "%%s-%c.h5", 'g');
    memb_name[H5FD_MEM_GHEAP] = sv[H5FD_MEM_GHEAP];
    memb_addr[H5FD_MEM_GHEAP] = (HADDR_MAX/4)*3;


    if(H5Pset_fapl_multi(fapl, memb_map, memb_fapl, memb_name, memb_addr, TRUE) < 0)
        TEST_ERROR;
    h5_fixname(FILENAME[4], fapl, filename, sizeof filename);

    if((file=H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0)
        TEST_ERROR;

    if(H5Fclose(file) < 0)
        TEST_ERROR;


    /* Test wrong ways to reopen multi files */
    if(test_multi_opens(filename) < 0)
        TEST_ERROR;

    /* Reopen the file */
    if((file=H5Fopen(filename, H5F_ACC_RDWR, fapl)) < 0)
        TEST_ERROR;

    /* Create and write data set */
    if((space=H5Screate_simple(2, dims, NULL)) < 0)
        TEST_ERROR;

    /* Retrieve the access property list... */
    if ((access_fapl = H5Fget_access_plist(file)) < 0)
        TEST_ERROR;

    /* Check that the driver is correct */
    if(H5FD_MULTI != H5Pget_driver(access_fapl))
        TEST_ERROR;

    /* ...and close the property list */
    if (H5Pclose(access_fapl) < 0)
        TEST_ERROR;

    /* Check file size API */
    if(H5Fget_filesize(file, &file_size) < 0)
        TEST_ERROR;

    /* Before any data is written, the raw data file is empty.  So
     * the file size is only the size of b-tree + HADDR_MAX/4.
     */
    if(file_size < HADDR_MAX/4 || file_size > HADDR_MAX/2)
        TEST_ERROR;

    if((dset=H5Dcreate2(file, dname, H5T_NATIVE_INT, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        TEST_ERROR;

    for(i=0; i<MULTI_SIZE; i++)
        for(j=0; j<MULTI_SIZE; j++)
            buf[i][j] = i*10000+j;
    if(H5Dwrite(dset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf) < 0)
        TEST_ERROR;

    if((fapl2=H5Pcreate(H5P_FILE_ACCESS)) < 0)
        TEST_ERROR;
    if(H5Pset_multi_type(fapl2, H5FD_MEM_SUPER) < 0)
        TEST_ERROR;
    if(H5Fget_vfd_handle(file, fapl2, (void **)&fhandle) < 0)
        TEST_ERROR;
    if(*fhandle<0)
        TEST_ERROR;

    if(H5Pset_multi_type(fapl2, H5FD_MEM_DRAW) < 0)
        TEST_ERROR;
    if(H5Fget_vfd_handle(file, fapl2, (void **)&fhandle2) < 0)
        TEST_ERROR;
    if(*fhandle2<0)
        TEST_ERROR;

    /* Check file size API */
    if(H5Fget_filesize(file, &file_size) < 0)
        TEST_ERROR;

    /* After the data is written, the file size is huge because the
     * beginning of raw data file is set at HADDR_MAX/2.  It's supposed
     * to be (HADDR_MAX/2 + 128*128*4)
     */
    if(file_size < HADDR_MAX/2 || file_size > HADDR_MAX)
        TEST_ERROR;

    if(H5Sclose(space) < 0)
        TEST_ERROR;
    if(H5Dclose(dset) < 0)
        TEST_ERROR;
    if(H5Pclose(fapl2) < 0)
        TEST_ERROR;

    /* Create and write attribute for the root group. */
    if((root = H5Gopen2(file, "/", H5P_DEFAULT)) < 0)
        FAIL_STACK_ERROR

        /* Attribute string. */
        if((atype = H5Tcopy(H5T_C_S1)) < 0)
            TEST_ERROR;

    if(H5Tset_size(atype, strlen(meta) + 1) < 0)
        TEST_ERROR;

    if(H5Tset_strpad(atype, H5T_STR_NULLTERM) < 0)
        TEST_ERROR;

    /* Create and write attribute */
    if((aspace = H5Screate_simple(1, adims, NULL)) < 0)
        TEST_ERROR;

    if((attr = H5Acreate2(root, "Metadata", atype, aspace, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        TEST_ERROR;

    if(H5Awrite(attr, atype, meta) < 0)
        TEST_ERROR;

    /* Close IDs */
    if(H5Tclose(atype) < 0)
        TEST_ERROR;
    if(H5Sclose(aspace) < 0)
        TEST_ERROR;
    if(H5Aclose(attr) < 0)
        TEST_ERROR;

    if(H5Fclose(file) < 0)
        TEST_ERROR;

    h5_cleanup(FILENAME, fapl);
    PASSED();

    return 0;

error:
    H5E_BEGIN_TRY {
        H5Sclose(space);
        H5Dclose(dset);
        H5Pclose(fapl);
        H5Pclose(fapl2);
        H5Fclose(file);
    } H5E_END_TRY;
    return -1;
}
Exemplo n.º 10
0
int main(void)
{
    hid_t fil,spc,set;
    hid_t cs6, cmp, fix;
    hid_t cmp1, cmp2, cmp3;
    hid_t plist;
    hid_t array_dt;

    hsize_t dim[2];
    hsize_t cdim[4];

    char string5[5];
    float fok[2] = {1234., 2341.};
    float fnok[2] = {5678., 6785.};
    float *fptr;

    char *data = NULL;

    int result = 0;
    herr_t error = 1;

    printf("%-70s", "Testing alignment in compound datatypes");

    strcpy(string5, "Hi!");
    HDunlink(fname);
    fil = H5Fcreate(fname, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);

    if (fil < 0) {
        puts("*FAILED*");
        return 1;
    }

    H5E_BEGIN_TRY {
        (void)H5Ldelete(fil, setname, H5P_DEFAULT);
    } H5E_END_TRY;

    cs6 = H5Tcopy(H5T_C_S1);
    H5Tset_size(cs6, sizeof(string5));
    H5Tset_strpad(cs6, H5T_STR_NULLPAD);

    cmp = H5Tcreate(H5T_COMPOUND, sizeof(fok) + sizeof(string5) + sizeof(fnok));
    H5Tinsert(cmp, "Awkward length", 0, cs6);

    cdim[0] = sizeof(fok) / sizeof(float);
    array_dt = H5Tarray_create2(H5T_NATIVE_FLOAT, 1, cdim);
    H5Tinsert(cmp, "Ok", sizeof(string5), array_dt);
    H5Tclose(array_dt);

    cdim[0] = sizeof(fnok) / sizeof(float);
    array_dt = H5Tarray_create2(H5T_NATIVE_FLOAT, 1, cdim);
    H5Tinsert(cmp, "Not Ok", sizeof(fok) + sizeof(string5), array_dt);
    H5Tclose(array_dt);

    fix = h5tools_get_native_type(cmp);

    cmp1 = H5Tcreate(H5T_COMPOUND, sizeof(fok));

    cdim[0] = sizeof(fok) / sizeof(float);
    array_dt = H5Tarray_create2(H5T_NATIVE_FLOAT, 1, cdim);
    H5Tinsert(cmp1, "Ok", 0, array_dt);
    H5Tclose(array_dt);

    cmp2 = H5Tcreate(H5T_COMPOUND, sizeof(string5));
    H5Tinsert(cmp2, "Awkward length", 0, cs6);

    cmp3 = H5Tcreate(H5T_COMPOUND, sizeof(fnok));

    cdim[0] = sizeof(fnok) / sizeof(float);
    array_dt = H5Tarray_create2(H5T_NATIVE_FLOAT, 1, cdim);
    H5Tinsert(cmp3, "Not Ok", 0, array_dt);
    H5Tclose(array_dt);

    plist = H5Pcreate(H5P_DATASET_XFER);
    if((error = H5Pset_preserve(plist, 1)) < 0) 
        goto out;

    /*
     * Create a small dataset, and write data into it we write each field
     * in turn so that we are avoid alignment issues at this point
     */
    dim[0] = 1;
    spc = H5Screate_simple(1, dim, NULL);
    set = H5Dcreate2(fil, setname, cmp, spc, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);

    H5Dwrite(set, cmp1, spc, H5S_ALL, plist, fok);
    H5Dwrite(set, cmp2, spc, H5S_ALL, plist, string5);
    H5Dwrite(set, cmp3, spc, H5S_ALL, plist, fnok);

    H5Dclose(set);

    /* Now open the set, and read it back in */
    data = malloc(H5Tget_size(fix));

    if(!data) {
        perror("malloc() failed");
        abort();
    }

    set = H5Dopen2(fil, setname, H5P_DEFAULT);

    H5Dread(set, fix, spc, H5S_ALL, H5P_DEFAULT, data);
    fptr = (float *)(data + H5Tget_member_offset(fix, 1));

out:
    if(error < 0) {
        result = 1;
        puts("*FAILED - HDF5 library error*");
    } else if(fok[0] != fptr[0] || fok[1] != fptr[1]
                    || fnok[0] != fptr[2] || fnok[1] != fptr[3]) {
        char *mname;

        result = 1;
        mname = H5Tget_member_name(fix, 0);
        printf("%14s (%2d) %6s = %s\n",
            mname ? mname : "(null)", (int)H5Tget_member_offset(fix,0),
            string5, (char *)(data + H5Tget_member_offset(fix, 0)));
        if(mname)
            free(mname);

        fptr = (float *)(data + H5Tget_member_offset(fix, 1));
        mname = H5Tget_member_name(fix, 1);
        printf("Data comparison:\n"
            "%14s (%2d) %6f = %f\n"
            "                    %6f = %f\n",
            mname ? mname : "(null)", (int)H5Tget_member_offset(fix,1),
            fok[0], fptr[0],
            fok[1], fptr[1]);
        if(mname)
            free(mname);

        fptr = (float *)(data + H5Tget_member_offset(fix, 2));
        mname = H5Tget_member_name(fix, 2);
        printf("%14s (%2d) %6f = %f\n"
            "                    %6f = %6f\n",
            mname ? mname : "(null)", (int)H5Tget_member_offset(fix,2),
            fnok[0], fptr[0],
            fnok[1], fptr[1]);
        if(mname)
            free(mname);

        fptr = (float *)(data + H5Tget_member_offset(fix, 1));
        printf("\n"
            "Short circuit\n"
            "                    %6f = %f\n"
            "                    %6f = %f\n"
            "                    %6f = %f\n"
            "                    %6f = %f\n",
            fok[0], fptr[0],
            fok[1], fptr[1],
            fnok[0], fptr[2],
            fnok[1], fptr[3]);
        puts("*FAILED - compound type alignmnent problem*");
    } else {
        puts(" PASSED");
    }

    if(data)
        free(data);
    H5Sclose(spc);
    H5Tclose(cmp);
    H5Tclose(cmp1);
    H5Tclose(cmp2);
    H5Tclose(cmp3);
    H5Pclose(plist);
    H5Fclose(fil);
    HDunlink(fname);
    fflush(stdout);
    return result;
}
Exemplo n.º 11
0
IbHdf5::IbHdf5(const QString &tableName, const QString &filePath, QObject *parent)
    : m_tableName(tableName)
    , m_filePath(filePath)
    , QObject(parent)
{

//    struct Record2
//    {
//        uint timestamp;
//        char* timeString;
//        double open;
//        double high;
//        double low;
//        double close;
//        uint volume;
//        char* sqlTimeFrame;
//    };

    m_nFields = 8;

//    m_dst_size = sizeof(Record2);

    m_dst_offset[0] = HOFFSET(Record2, timestamp);
    m_dst_offset[1] = HOFFSET(Record2, timeString);
    m_dst_offset[2] = HOFFSET(Record2, open);
    m_dst_offset[3] = HOFFSET(Record2, high);
    m_dst_offset[4] = HOFFSET(Record2, low);
    m_dst_offset[5] = HOFFSET(Record2, close);
    m_dst_offset[6] = HOFFSET(Record2, volume);
    m_dst_offset[7] = HOFFSET(Record2, sqlTimeFrame);


    m_dst_sizes[0] = sizeof(uint);
    m_dst_sizes[1] = 64;
    m_dst_sizes[2] = sizeof(double);
    m_dst_sizes[3] = sizeof(double);
    m_dst_sizes[4] = sizeof(double);
    m_dst_sizes[5] = sizeof(double);
    m_dst_sizes[6] = sizeof(uint);
    m_dst_sizes[7] = 8;


    m_fieldNames[0] = "timestamp";
    m_fieldNames[1] = "timestring";
    m_fieldNames[2] = "open";
    m_fieldNames[3] = "high";
    m_fieldNames[4] = "low";
    m_fieldNames[5] = "close";
    m_fieldNames[6] = "volume";
    m_fieldNames[7] = "timeframe";


    m_chunkSize = 10;

    hid_t timeStringType;
    hid_t timeFrameType;
    m_fillData = NULL;
    m_compress = 0;

    timeStringType = H5Tcopy(H5T_C_S1);
    H5Tset_size(timeStringType, 64);

    timeFrameType = H5Tcopy(H5T_C_S1);
    H5Tset_size(timeFrameType, 8);


    m_fieldType[0] = H5T_NATIVE_UINT;
    m_fieldType[1] = timeStringType;
    m_fieldType[2] = H5T_NATIVE_DOUBLE;
    m_fieldType[3] = H5T_NATIVE_DOUBLE;
    m_fieldType[4] = H5T_NATIVE_DOUBLE;
    m_fieldType[5] = H5T_NATIVE_DOUBLE;
    m_fieldType[6] = H5T_NATIVE_UINT;
    m_fieldType[7] = timeFrameType;

    // CREATE OR OPEN THE FILE
//    if (!QFileInfo::exists(m_filePath)) {
        m_fid = H5Fcreate (m_filePath.toLatin1().constData(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
//    }
//    else {
//        m_fid = H5Fopen(m_filePath.toLatin1().data(), H5F_ACC_RDWR, H5P_DEFAULT);
//    }


}
Exemplo n.º 12
0
/**
@details
-# Set the file extension to ".h5"
-# Open the log file
-# Create the root directory in the HDF5 file
-# For each variable to be recorded
   -# Create a fixed length packet table
   -# Associate the packet table with the temporary memory buffer storing the simulation data
-# Declare the recording group to the memory manager so that the group can be checkpointed
   and restored.
*/
int Trick::DRHDF5::format_specific_init() {

#ifdef HDF5
    unsigned int ii ;
    HDF5_INFO *hdf5_info ;
    hsize_t chunk_size = 1024;
    hid_t byte_id ;
    hid_t file_names_id, param_types_id, param_units_id, param_names_id ;
    hid_t datatype ;
    herr_t ret_value ;
    hid_t s256 ;
    std::string buf;

    file_name.append(".h5") ;

    s256 = H5Tcopy(H5T_C_S1);
    H5Tset_size(s256, 256);

    // Create a new HDF5 file with the specified name; overwrite if it exists.
    if ((file = H5Fcreate(file_name.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0) {
        message_publish(MSG_ERROR, "Can't open Data Record file %s.\n", file_name.c_str()) ;
        record = false ;
        return -1 ;
    }

    // All HDF5 objects live in the top-level "/" (root) group.
    root_group = H5Gopen(file, "/", H5P_DEFAULT);

    // Create a new group named "header" at the root ("/") level.
    header_group = H5Gcreate(file, "/header", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
    // Create a packet table (PT) that stores byte order.
    byte_id = H5PTcreate_fl(header_group, "byte_order", s256, chunk_size, 1) ;
    // Add the byte order value to the byte packet table.
    H5PTappend( byte_id, 1, byte_order.c_str() );
    // Create a packet table (PT) that stores each parameter's file location.
    file_names_id = H5PTcreate_fl(header_group, "file_names", s256, chunk_size, 1) ;
    // Create a packet table (PT) that stores each parameter's type.
    param_types_id = H5PTcreate_fl(header_group, "param_types", s256, chunk_size, 1) ;
    // Create a packet table (PT) that stores each parameter's unit.
    param_units_id = H5PTcreate_fl(header_group, "param_units", s256, chunk_size, 1) ;
    // Create a packet table (PT) that stores each parameter's name.
    param_names_id =  H5PTcreate_fl(header_group, "param_names", s256, chunk_size, 1) ;

    // Create a table for each requested parameter.
    for (ii = 0; ii < rec_buffer.size(); ii++) {

        hdf5_info = (HDF5_INFO *)malloc(sizeof(HDF5_INFO));

        /* Case statements taken from "parameter_types.h."
         *  HDF5 Native types found in "H5Tpublic.h." */
        switch (rec_buffer[ii]->ref->attr->type) {
        case TRICK_CHARACTER:
            datatype = H5T_NATIVE_CHAR;
            break;
        case TRICK_UNSIGNED_CHARACTER:
            datatype = H5T_NATIVE_UCHAR;
            break;
        case TRICK_STRING:
            datatype = s256;
            break;
        case TRICK_SHORT:
            datatype = H5T_NATIVE_SHORT;
            break;
        case TRICK_UNSIGNED_SHORT:
            datatype = H5T_NATIVE_USHORT;
            break;
        case TRICK_ENUMERATED:
        case TRICK_INTEGER:
            datatype = H5T_NATIVE_INT;
            break;
        case TRICK_UNSIGNED_INTEGER:
            datatype = H5T_NATIVE_UINT;
            break;
        case TRICK_LONG:
            datatype = H5T_NATIVE_LONG;
            break;
        case TRICK_UNSIGNED_LONG:
            datatype = H5T_NATIVE_ULONG;
            break;
        case TRICK_FLOAT:
            datatype = H5T_NATIVE_FLOAT;
            break;
        case TRICK_DOUBLE:
            datatype = H5T_NATIVE_DOUBLE;
            break;
        case TRICK_BITFIELD:
            if (rec_buffer[ii]->ref->attr->size == sizeof(int)) {
                datatype = H5T_NATIVE_INT;
            } else if (rec_buffer[ii]->ref->attr->size == sizeof(short)) {
                datatype = H5T_NATIVE_SHORT;
            } else {
                datatype = H5T_NATIVE_CHAR;
            }
            break;
        case TRICK_UNSIGNED_BITFIELD:
            if (rec_buffer[ii]->ref->attr->size == sizeof(int)) {
                datatype = H5T_NATIVE_UINT;
            } else if (rec_buffer[ii]->ref->attr->size == sizeof(short)) {
                datatype = H5T_NATIVE_USHORT;
            } else {
                datatype = H5T_NATIVE_UCHAR;
            }
            break;
        case TRICK_LONG_LONG:
            datatype = H5T_NATIVE_LLONG;
            break;
        case TRICK_UNSIGNED_LONG_LONG:
            datatype = H5T_NATIVE_ULLONG;
            break;
        case TRICK_BOOLEAN:
#if ( __sun | __APPLE__ )
            datatype = H5T_NATIVE_INT;
#else
            datatype = H5T_NATIVE_UCHAR;
#endif
            break;
        default:
            free(hdf5_info);
            continue;
        }

        /* Create packet table(s) to store "fixed-length" packets.
         *  A separate packet table (PT) is created for each variable.
         * PARAMETERS:
         *     IN: Identifier of the file or group to create the table within.
         *     IN: The name of the dataset (i.e. variable).
         *     IN: The datatype of a packet.
         *     IN: The packet table uses HDF5 chunked storage to allow it to
         *         grow. This value allows the user to set the size of a chunk.
         *         The chunk size affects performance.
         *     IN: Compression level, a value of 0 through 9. Level 0 is faster
         *         but offers the least compression; level 9 is slower but
         *         offers maximum compression. A setting of -1 indicates that
         *         no compression is desired.
         * RETURN:
         *     Returns an identifier for the new packet table, or H5I_BADID on error.
         */
        hdf5_info->dataset = H5PTcreate_fl(root_group, rec_buffer[ii]->ref->reference, datatype, chunk_size, 1) ;

        hdf5_info->drb = rec_buffer[ii] ;
        /* Add the new parameter element to the end of the vector.
         *  This effectively increases the vector size by one. */
        parameters.push_back(hdf5_info);

        // As a bonus, add a header entry for each parameter.
        /* File Name */
        buf = "log_" + group_name ;
        H5PTappend( file_names_id, 1, buf.c_str() );
        /* Param Type */
        buf = type_string(rec_buffer[ii]->ref->attr->type, rec_buffer[ii]->ref->attr->size );
        H5PTappend( param_types_id, 1, buf.c_str() );
        /* Param Units */
        H5PTappend( param_units_id, 1, rec_buffer[ii]->ref->attr->units );
        /* Param Name */
        H5PTappend( param_names_id, 1, rec_buffer[ii]->ref->reference );

    }
    ret_value = H5PTclose( byte_id );
    ret_value = H5PTclose( file_names_id );
    ret_value = H5PTclose( param_types_id );
    ret_value = H5PTclose( param_units_id );
    ret_value = H5PTclose( param_names_id );
    ret_value = H5Gclose( header_group );
#endif

    return(0);
}
int main( void )
{
 typedef struct Particle
 {
  char   name[16];
  int    lati;
  int    longi;
  float  pressure;
  double temperature;
 } Particle;

 /* Define a subset of Particle, with latitude and longitude fields */
 typedef struct Position
 {
  int    lati;
  int    longi;
 } Position;

 /* Calculate the type_size and the offsets of our struct members */
 Particle  dst_buf[NRECORDS];
 size_t dst_size =  sizeof( Particle );
 size_t dst_offset[NFIELDS] = { HOFFSET( Particle, name ),
                                HOFFSET( Particle, lati ),
                                HOFFSET( Particle, longi ),
                                HOFFSET( Particle, pressure ),
                                HOFFSET( Particle, temperature )};
 size_t dst_sizes[NFIELDS] = { sizeof( dst_buf[0].name),
                               sizeof( dst_buf[0].lati),
                               sizeof( dst_buf[0].longi),
                               sizeof( dst_buf[0].pressure),
                               sizeof( dst_buf[0].temperature)};

 size_t field_offset_pos[2] = { HOFFSET( Position, lati ),
                                HOFFSET( Position, longi )};

 /* Initially no data */
 Particle  *p_data = NULL;

 /* Define field information */
 const char *field_names[NFIELDS]  =
 { "Name","Latitude", "Longitude", "Pressure", "Temperature" };
 hid_t      field_type[NFIELDS];
 hid_t      string_type;
 hid_t      file_id;
 hsize_t    chunk_size = 10;
  Particle   fill_data[1] =
 { {"no data",-1,-1, -99.0f, -99.0} };   /* Fill value particle */
 int        compress  = 0;
 hsize_t    nfields;
 hsize_t    start;                       /* Record to start reading/writing */
 hsize_t    nrecords;                    /* Number of records to read/write */
 int        i;

 /* Define new values for the field "Pressure"  */
 float      pressure_in  [NRECORDS_ADD] =
 { 0.0f,1.0f,2.0f};
 int        field_index_pre[1]     = { 3 };
 int        field_index_pos[2]     = { 1,2 };

 /* Define new values for the fields "Latitude,Longitude"  */
 Position   position_in[NRECORDS_ADD] = { {0,0},
 {10,10},
 {20,20} };

 size_t field_sizes_pos[2]=
 {
  sizeof(position_in[0].longi),
  sizeof(position_in[0].lati)
 };

 size_t field_sizes_pre[1]=
 {
  sizeof(float)
 };

 /* Initialize the field field_type */
 string_type = H5Tcopy( H5T_C_S1 );
 H5Tset_size( string_type, 16 );
 field_type[0] = string_type;
 field_type[1] = H5T_NATIVE_INT;
 field_type[2] = H5T_NATIVE_INT;
 field_type[3] = H5T_NATIVE_FLOAT;
 field_type[4] = H5T_NATIVE_DOUBLE;

 /* Create a new file using default properties. */
 file_id = H5Fcreate( "ex_table_05.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT );

 /* Make the table */
 H5TBmake_table( "Table Title", file_id, TABLE_NAME,NFIELDS,NRECORDS,
                         dst_size,field_names, dst_offset, field_type,
                         chunk_size, fill_data, compress, p_data  );

 /* Write the pressure field starting at record 2 */
 nfields  = 1;
 start    = 2;
 nrecords = NRECORDS_ADD;
 H5TBwrite_fields_index( file_id, TABLE_NAME, nfields, field_index_pre, start, nrecords,
   sizeof( float ), 0, field_sizes_pre, pressure_in  );


 /* Write the new longitude and latitude information starting at record 2  */
 nfields  = 2;
 start    = 2;
 nrecords = NRECORDS_ADD;
 H5TBwrite_fields_index( file_id, TABLE_NAME, nfields, field_index_pos, start, nrecords,
   sizeof( Position ), field_offset_pos, field_sizes_pos, position_in  );


 /* read the table */
 H5TBread_table( file_id, TABLE_NAME, dst_size, dst_offset, dst_sizes, dst_buf );

 /* print it by rows */
 for (i=0; i<NRECORDS; i++) {
  printf ("%-5s %-5d %-5d %-5f %-5f",
   dst_buf[i].name,
   dst_buf[i].lati,
   dst_buf[i].longi,
   dst_buf[i].pressure,
   dst_buf[i].temperature);
  printf ("\n");
 }

 /* close type */
 H5Tclose( string_type );

 /* close the file */
 H5Fclose( file_id );

 return 0;

}
Exemplo n.º 14
0
int main( void )
{
 typedef struct Particle
 {
  char   name[16];
  int    lati;
  int    longi;
  float  pressure;
  double temperature;
 } Particle;

 Particle  dst_buf[NRECORDS];

 /* Calculate the size and the offsets of our struct members in memory */
 size_t dst_size =  sizeof( Particle );
 size_t dst_offset[NFIELDS] = { HOFFSET( Particle, name ),
                                HOFFSET( Particle, lati ),
                                HOFFSET( Particle, longi ),
                                HOFFSET( Particle, pressure ),
                                HOFFSET( Particle, temperature )};

 size_t dst_sizes[NFIELDS] = { sizeof( dst_buf[0].name),
                               sizeof( dst_buf[0].lati),
                               sizeof( dst_buf[0].longi),
                               sizeof( dst_buf[0].pressure),
                               sizeof( dst_buf[0].temperature)};


 /* Define an array of Particles */
 Particle  p_data[NRECORDS] = {
 {"zero",0,0, 0.0f, 0.0},
 {"one",10,10, 1.0f, 10.0},
 {"two",  20,20, 2.0f, 20.0},
 {"three",30,30, 3.0f, 30.0},
 {"four", 40,40, 4.0f, 40.0},
 {"five", 50,50, 5.0f, 50.0},
 {"six",  60,60, 6.0f, 60.0},
 {"seven",70,70, 7.0f, 70.0}
  };

  /* Define field information */
  const char *field_names[NFIELDS]  =
  { "Name","Latitude", "Longitude", "Pressure", "Temperature" };
  hid_t      field_type[NFIELDS];
  hid_t      string_type;
  hid_t      file_id;
  hsize_t    chunk_size = 10;
  int        *fill_data = NULL;
  int        compress  = 0;
  herr_t     status;
  int        i;

  /* Initialize field_type */
  string_type = H5Tcopy( H5T_C_S1 );
  H5Tset_size( string_type, 16 );
  field_type[0] = string_type;
  field_type[1] = H5T_NATIVE_INT;
  field_type[2] = H5T_NATIVE_INT;
  field_type[3] = H5T_NATIVE_FLOAT;
  field_type[4] = H5T_NATIVE_DOUBLE;

  /* Create a new file using default properties. */
  file_id = H5Fcreate( "h5_table_01.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT );
  
  /*-------------------------------------------------------------------------
   * H5TBmake_table
   *-------------------------------------------------------------------------
   */
  
  status=H5TBmake_table ("Table Title",   /* Title of the table                  */
			 file_id,         /* HDF5 object identifier to the file  */
			 TABLE_NAME,      /* Name of the table                   */
			 NFIELDS,         /* Number of fields/columns            */
			 NRECORDS,        /*  */
			 dst_size,        /* Size of the struct/table row        */
			 field_names,     /*  */
			 dst_offset,      /*  */
			 field_type,      /* Type of the individual table fields */
                         chunk_size,      /* Chunking size                       */
			 fill_data,       /*  */
			 compress,        /* Enable/disable compression          */
			 p_data           /* Array with the table data           */
			 );
  
  /*-------------------------------------------------------------------------
   * H5TBread_table
   *-------------------------------------------------------------------------
   */
  
  status = H5TBread_table (file_id,      /* HDF5 object identifier     */
			   TABLE_NAME,   /* Name of the table          */
			   dst_size,     /*  */
			   dst_offset,   /*  */
			   dst_sizes,    /*  */
			   dst_buf);     /* Buffer to return the data  */
  
  /* print it by rows */
  for (i=0; i<NRECORDS; i++) {
    printf ("%-5s %-5d %-5d %-5f %-5f",
	    dst_buf[i].name,
	    dst_buf[i].lati,
	    dst_buf[i].longi,
	    dst_buf[i].pressure,
	    dst_buf[i].temperature);
    printf ("\n");
  }
  
  /*-------------------------------------------------------------------------
   * end
   *-------------------------------------------------------------------------
   */

  /* Release object identifiers */
  H5Tclose (string_type);
  H5Fclose (file_id);
  
  return 0;
}
Exemplo n.º 15
0
/****************************************************************
**
**  test_vlstring_type(): Test VL string type.
**      Tests if VL string is treated as string.
**
****************************************************************/
static void test_vlstring_type(void)
{
    hid_t               fid;           /* HDF5 File IDs                */
    hid_t               tid_vlstr;
    H5T_cset_t          cset;
    H5T_str_t           pad;
    htri_t              vl_str;         /* Whether string is VL */
    herr_t              ret;

    /* Output message about test being performed */
    MESSAGE(5, ("Testing VL String type\n"));

    /* Open file */
    fid = H5Fopen(DATAFILE, H5F_ACC_RDWR, H5P_DEFAULT);
    CHECK(fid, FAIL, "H5Fopen");

    /* Create a datatype to refer to */
    tid_vlstr = H5Tcopy(H5T_C_S1);
    CHECK(tid_vlstr, FAIL, "H5Tcopy");

    /* Change padding and verify it */
    ret = H5Tset_strpad(tid_vlstr, H5T_STR_NULLPAD);
    CHECK(ret, FAIL, "H5Tset_strpad");
    pad = H5Tget_strpad(tid_vlstr);
    VERIFY(pad, H5T_STR_NULLPAD, "H5Tget_strpad");

    /* Convert to variable-length string */
    ret = H5Tset_size(tid_vlstr, H5T_VARIABLE);
    CHECK(ret, FAIL, "H5Tset_size");

    /* Check if datatype is VL string */
    ret = H5Tget_class(tid_vlstr);
    VERIFY(ret, H5T_STRING, "H5Tget_class");
    ret = H5Tis_variable_str(tid_vlstr);
    VERIFY(ret, TRUE, "H5Tis_variable_str");

    /* Verify that the class detects as a string */
    vl_str = H5Tdetect_class(tid_vlstr, H5T_STRING);
    CHECK(vl_str, FAIL, "H5Tdetect_class");
    VERIFY(vl_str, TRUE, "H5Tdetect_class");

    /* Check default character set and padding */
    cset = H5Tget_cset(tid_vlstr);
    VERIFY(cset, H5T_CSET_ASCII, "H5Tget_cset");
    pad = H5Tget_strpad(tid_vlstr);
    VERIFY(pad, H5T_STR_NULLPAD, "H5Tget_strpad");

    /* Commit variable-length string datatype to storage */
    ret = H5Tcommit2(fid, VLSTR_TYPE, tid_vlstr, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
    CHECK(ret, FAIL, "H5Tcommit2");

    /* Close datatype */
    ret = H5Tclose(tid_vlstr);
    CHECK(ret, FAIL, "H5Tclose");

    tid_vlstr = H5Topen2(fid, VLSTR_TYPE, H5P_DEFAULT);
    CHECK(tid_vlstr, FAIL, "H5Topen2");

    ret = H5Tclose(tid_vlstr);
    CHECK(ret, FAIL, "H5Tclose");

    ret = H5Fclose(fid);
    CHECK(ret, FAIL, "H5Fclose");


    fid = H5Fopen(DATAFILE, H5F_ACC_RDWR, H5P_DEFAULT);
    CHECK(fid, FAIL, "H5Fopen");

    /* Open the variable-length string datatype just created */
    tid_vlstr = H5Topen2(fid, VLSTR_TYPE, H5P_DEFAULT);
    CHECK(tid_vlstr, FAIL, "H5Topen2");

    /* Verify character set and padding */
    cset = H5Tget_cset(tid_vlstr);
    VERIFY(cset, H5T_CSET_ASCII, "H5Tget_cset");
    pad = H5Tget_strpad(tid_vlstr);
    VERIFY(pad, H5T_STR_NULLPAD, "H5Tget_strpad");

    /* Close datatype and file */
    ret = H5Tclose(tid_vlstr);
    CHECK(ret, FAIL, "H5Tclose");
    ret = H5Fclose(fid);
    CHECK(ret, FAIL, "H5Fclose");

} /* end test_vlstring_type() */
Exemplo n.º 16
0
bool HDF5_IO::save_element_fits(std::string filename,
                                std::string path,
                                const data_struct::xrf::Fit_Count_Dict * const element_counts,
                                size_t row_idx_start,
                                int row_idx_end,
                                size_t col_idx_start,
                                int col_idx_end)
{
    std::unique_lock<std::mutex> lock(_mutex);

//hid_t error_stack = H5Eget_current_stack();
//H5Eset_auto2(error_stack, NULL, NULL);

    std::chrono::time_point<std::chrono::system_clock> start, end;
    start = std::chrono::system_clock::now();

    hid_t    file_id, dset_id, memoryspace, filespace, dataspace_id, filetype, dataspace_ch_id, dataspace_ch_off_id, memtype, status, maps_grp_id, dset_ch_id, dcpl_id;
    herr_t   error;

    dset_id = -1;
    dset_ch_id = -1;
    hsize_t dims_out[3];
    hsize_t offset[3];
    hsize_t count[3];
    hsize_t chunk_dims[3];

    file_id = H5Fopen(filename.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);

    if (file_id < 1)
        file_id = H5Fcreate(filename.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
    if(file_id < 0)
    {
        std::cout<<"Error opening file "<<filename<<std::endl;
        return false;
    }

    //get one element
    data_struct::xrf::Fit_Counts_Array element;
    //fix this
    for(const auto& iter : *element_counts)
    {
        element = iter.second;
        break;
    }
    //H5T_FLOAT
    dims_out[0] = element_counts->size();
    dims_out[1] = element.rows();
    dims_out[2] = element.cols();
    offset[0] = 0;
    offset[1] = 0;
    offset[2] = 0;
    count[0] = 1;
    count[1] = 1;
    count[2] = dims_out[2];
    chunk_dims[0] = 1;
    chunk_dims[1] = 1;
    chunk_dims[2] = dims_out[2];

    dcpl_id = H5Pcreate(H5P_DATASET_CREATE);
    H5Pset_chunk(dcpl_id, 3, chunk_dims);
    H5Pset_deflate (dcpl_id, 7);

    memoryspace = H5Screate_simple(3, count, NULL);
    filespace = H5Screate_simple(3, dims_out, NULL);

    dataspace_id = H5Screate_simple (3, dims_out, NULL);

    dataspace_ch_id = H5Screate_simple (1, dims_out, NULL);
    dataspace_ch_off_id = H5Screate_simple (1, dims_out, NULL);

    H5Sselect_hyperslab (memoryspace, H5S_SELECT_SET, offset, NULL, count, NULL);

    maps_grp_id = H5Gopen(file_id, "MAPS", H5P_DEFAULT);
    if(maps_grp_id < 0)
        maps_grp_id = H5Gcreate(file_id, "MAPS", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
    if(maps_grp_id < 0)
    {
        std::cout<<"Error creating group 'MAPS'"<<std::endl;
        return false;
    }

    //dset_id = H5Dopen (maps_grp_id, path.c_str(), H5P_DEFAULT);
    if(dset_id < 0)
        dset_id = H5Dcreate (maps_grp_id, path.c_str(), H5T_INTEL_F64, dataspace_id, H5P_DEFAULT, dcpl_id, H5P_DEFAULT);
    if(dset_id < 0)
    {
        std::cout<<"Error creating dataset "<<path<<std::endl;
        return false;
    }

    filetype = H5Tcopy (H5T_FORTRAN_S1);
    H5Tset_size (filetype, 255);
    memtype = H5Tcopy (H5T_C_S1);
    status = H5Tset_size (memtype, 256);

    dset_ch_id = H5Dopen (maps_grp_id, "channel_names", H5P_DEFAULT);
    if(dset_ch_id < 0)
        dset_ch_id = H5Dcreate (maps_grp_id, "channel_names", filetype, dataspace_ch_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
    if(dset_ch_id < 0)
    {
        std::cout<<"Error creating 'channel_names' dataset"<<std::endl;
        return false;
    }

/*
   if (row_idx_end < row_idx_start || row_idx_end > spectra_volume->rows() -1)
    {
        row_idx_end = spectra_volume->rows();
    }
    if (col_idx_end < col_idx_start || col_idx_end > spectra_volume->cols() -1)
    {
        col_idx_end = spectra_volume->cols();
    }
*/

    //create save ordered vector by element Z number with K , L, M lines
    std::vector<std::string> element_lines;
    for (std::string el_name : data_struct::xrf::Element_Symbols)
    {
        element_lines.push_back(el_name);
    }
    for (std::string el_name : data_struct::xrf::Element_Symbols)
    {
        element_lines.push_back(el_name+"_L");
    }
    for (std::string el_name : data_struct::xrf::Element_Symbols)
    {
        element_lines.push_back(el_name+"_M");
    }


    int i=0;
    //save by element Z order
    //for(const auto& iter : *element_counts)
    for (std::string el_name : element_lines)
    {
        if(element_counts->count(el_name) < 1 )
        {
            continue;
        }
        offset[0] = i;
        element = element_counts->at(el_name);

        H5Sselect_hyperslab (dataspace_ch_id, H5S_SELECT_SET, offset, NULL, count, NULL);
        H5Sselect_hyperslab (dataspace_ch_off_id, H5S_SELECT_SET, &offset[2], NULL, count, NULL);
        status = H5Dwrite (dset_ch_id, memtype, dataspace_ch_off_id, dataspace_ch_id, H5P_DEFAULT, (void*)(el_name.c_str()));

        for(size_t row = 0; row < element.rows(); row++)
        {
            offset[1] = row;
            H5Sselect_hyperslab (filespace, H5S_SELECT_SET, offset, NULL, count, NULL);

            status = H5Dwrite (dset_id, H5T_NATIVE_DOUBLE, memoryspace, filespace, H5P_DEFAULT, (void*)&(element[row][0]));
        }
        i++;
    }

    H5Dclose(dset_id);
    H5Dclose(dset_ch_id);
    H5Sclose(memoryspace);
    H5Sclose(filespace);
    H5Sclose(dataspace_ch_off_id);
    H5Sclose(dataspace_ch_id);
    //h5Tclose(filetype);
    //h5Tclose(memtype);
    H5Pclose(dcpl_id);
    H5Sclose(dataspace_id);
    H5Gclose(maps_grp_id);
    H5Fclose(file_id);

    end = std::chrono::system_clock::now();
    std::chrono::duration<double> elapsed_seconds = end-start;

    std::cout << "\n\n save channels elapsed time: " << elapsed_seconds.count() << "s\n\n\n";



    return true;

}
Exemplo n.º 17
0
/****************************************************************
**
**  test_compact_vlstring(): Test code for storing VL strings in
**      compact datasets.
**
****************************************************************/
static void
test_compact_vlstring(void)
{
    const char *wdata[SPACE1_DIM1] = {"one", "two", "three", "four"};
    char *rdata[SPACE1_DIM1];   /* Information read in */
    hid_t		fid1;		/* HDF5 File IDs		*/
    hid_t		dataset;	/* Dataset ID			*/
    hid_t		sid1;       /* Dataspace ID			*/
    hid_t		tid1;       /* Datatype ID			*/
    hid_t		plist;      /* Dataset creation property list	*/
    hsize_t		dims1[] = {SPACE1_DIM1};
    unsigned       i;          /* counting variable */
    herr_t		ret;		/* Generic return value		*/

    /* Output message about test being performed */
    MESSAGE(5, ("Testing VL Strings in compact dataset\n"));

    /* Create file */
    fid1 = H5Fcreate(DATAFILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
    CHECK(fid1, FAIL, "H5Fcreate");

    /* Create dataspace for datasets */
    sid1 = H5Screate_simple(SPACE1_RANK, dims1, NULL);
    CHECK(sid1, FAIL, "H5Screate_simple");

    /* Create a datatype to refer to */
    tid1 = H5Tcopy (H5T_C_S1);
    CHECK(tid1, FAIL, "H5Tcopy");

    ret = H5Tset_size (tid1,H5T_VARIABLE);
    CHECK(ret, FAIL, "H5Tset_size");

    plist = H5Pcreate(H5P_DATASET_CREATE);
    CHECK(plist, FAIL, "H5Pcreate");

    ret = H5Pset_layout(plist, H5D_COMPACT);
    CHECK(ret, FAIL, "H5Pset_layout");

    /* Create a dataset */
    dataset = H5Dcreate2(fid1, "Dataset5", tid1, sid1, H5P_DEFAULT, plist, H5P_DEFAULT);
    CHECK(dataset, FAIL, "H5Dcreate2");

    /* Write dataset to disk */
    ret = H5Dwrite(dataset, tid1, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata);
    CHECK(ret, FAIL, "H5Dwrite");

    /* Read dataset from disk */
    ret = H5Dread(dataset, tid1, H5S_ALL, H5S_ALL, H5P_DEFAULT, rdata);
    CHECK(ret, FAIL, "H5Dread");

    /* Compare data read in */
    for(i = 0; i < SPACE1_DIM1; i++) {
        if(HDstrlen(wdata[i]) != HDstrlen(rdata[i])) {
            TestErrPrintf("VL data length don't match!, strlen(wdata[%d])=%d, strlen(rdata[%d])=%d\n",(int)i,(int)strlen(wdata[i]),(int)i,(int)strlen(rdata[i]));
            continue;
        } /* end if */
        if(HDstrcmp(wdata[i], rdata[i]) != 0) {
            TestErrPrintf("VL data values don't match!, wdata[%d]=%s, rdata[%d]=%s\n",(int)i,wdata[i],(int)i,rdata[i]);
            continue;
        } /* end if */
    } /* end for */

    /* Reclaim the read VL data */
    ret = H5Dvlen_reclaim(tid1, sid1, H5P_DEFAULT, rdata);
    CHECK(ret, FAIL, "H5Dvlen_reclaim");

    /* Close Dataset */
    ret = H5Dclose(dataset);
    CHECK(ret, FAIL, "H5Dclose");

    /* Close datatype */
    ret = H5Tclose(tid1);
    CHECK(ret, FAIL, "H5Tclose");

    /* Close disk dataspace */
    ret = H5Sclose(sid1);
    CHECK(ret, FAIL, "H5Sclose");

    /* Close dataset create property list */
    ret = H5Pclose(plist);
    CHECK(ret, FAIL, "H5Pclose");

    /* Close file */
    ret = H5Fclose(fid1);
    CHECK(ret, FAIL, "H5Fclose");
} /*test_compact_vlstrings*/
Exemplo n.º 18
0
op_map op_decl_map_hdf5(op_set from, op_set to, int dim, char const *file, char const *name)
{
  //create new communicator
  int my_rank, comm_size;
  MPI_Comm_dup(MPI_COMM_WORLD, &OP_MPI_HDF5_WORLD);
  MPI_Comm_rank(OP_MPI_HDF5_WORLD, &my_rank);
  MPI_Comm_size(OP_MPI_HDF5_WORLD, &comm_size);

  //MPI variables
  MPI_Info info  = MPI_INFO_NULL;

  //HDF5 APIs definitions
  hid_t       file_id; //file identifier
  hid_t plist_id;  //property list identifier
  hid_t dset_id; //dataset identifier
  hid_t       dataspace; //data space identifier
  hid_t       memspace; //memory space identifier

  hsize_t count[2]; //hyperslab selection parameters
  hsize_t offset[2];

  //Set up file access property list with parallel I/O access
  plist_id = H5Pcreate(H5P_FILE_ACCESS);
  H5Pset_fapl_mpio(plist_id, OP_MPI_HDF5_WORLD, info);

  file_id = H5Fopen(file, H5F_ACC_RDONLY, plist_id );
  H5Pclose(plist_id);

  /*find total size of this map by reading attributes*/
  int g_size;
  //open existing data set
  dset_id = H5Dopen(file_id, name, H5P_DEFAULT);
  //get OID of the attribute
  hid_t attr = H5Aopen(dset_id, "size", H5P_DEFAULT);
  //read attribute
  H5Aread(attr,H5T_NATIVE_INT,&g_size);
  H5Aclose(attr);
  H5Dclose(dset_id);

  //calculate local size of set for this mpi process
  int l_size = compute_local_size (g_size, comm_size, my_rank);

  //check if size is accurate
  if(from->size != l_size)
  {
    printf("map from set size %d in file %s and size %d do not match on rank %d\n",
        l_size,file,from->size, my_rank);
    MPI_Abort(OP_MPI_HDF5_WORLD, 2);
  }

  /*find dim with available attributes*/
  int map_dim = 0;
  //open existing data set
  dset_id = H5Dopen(file_id, name, H5P_DEFAULT);
  //get OID of the attribute
  attr = H5Aopen(dset_id, "dim", H5P_DEFAULT);
  //read attribute
  H5Aread(attr,H5T_NATIVE_INT,&map_dim);
  H5Aclose(attr);
  H5Dclose(dset_id);
  if(map_dim != dim)
  {
    printf("map.dim %d in file %s and dim %d do not match\n",map_dim,file,dim);
    MPI_Abort(OP_MPI_HDF5_WORLD, 2);
  }

  /*find type with available attributes*/
  dataspace= H5Screate(H5S_SCALAR);
  hid_t  atype = H5Tcopy(H5T_C_S1);
  H5Tset_size(atype, 10);
  //open existing data set
  dset_id = H5Dopen(file_id, name, H5P_DEFAULT);
  //get OID of the attribute
  attr = H5Aopen(dset_id, "type", H5P_DEFAULT);
  //read attribute
  char typ[10];
  H5Aread(attr,atype,typ);
  H5Aclose(attr);
  H5Sclose(dataspace);
  H5Dclose(dset_id);

  /*read in map in hyperslabs*/

  //Create the dataset with default properties and close dataspace.
  dset_id = H5Dopen(file_id, name, H5P_DEFAULT);

  //Each process defines dataset in memory and reads from a hyperslab in the file.
  int disp = 0;
  int* sizes = (int *)xmalloc(sizeof(int)*comm_size);
  MPI_Allgather(&l_size, 1, MPI_INT, sizes, 1, MPI_INT, OP_MPI_HDF5_WORLD);
  for(int i = 0; i<my_rank; i++)disp = disp + sizes[i];
  free(sizes);

  count[0] = l_size;
  count[1] = dim;
  offset[0] = disp;
  offset[1] = 0;
  memspace = H5Screate_simple(2, count, NULL);

  //Select hyperslab in the file.
  dataspace = H5Dget_space(dset_id);
  H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, offset, NULL, count, NULL);

  //Create property list for collective dataset write.
  plist_id = H5Pcreate(H5P_DATASET_XFER);
  H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_COLLECTIVE);

  //initialize data buffer and read data
  int* map = 0;
  if(strcmp(typ,"int") == 0)
  {
    map = (int *)xmalloc(sizeof(int)*l_size*dim);
    H5Dread(dset_id, H5T_NATIVE_INT, memspace, dataspace, plist_id, map);
  }
  else if (strcmp(typ,"long") == 0)
  {
    map = (int *)xmalloc(sizeof(long)*l_size*dim);
    H5Dread(dset_id, H5T_NATIVE_LONG, memspace, dataspace, plist_id, map);
  }
  else if (strcmp(typ,"long long") == 0)
  {
    map = (int *)xmalloc(sizeof(long)*l_size*dim);
    H5Dread(dset_id, H5T_NATIVE_LLONG, memspace, dataspace, plist_id, map);
  }
  else
  {
    printf("unknown type\n");
    MPI_Abort(OP_MPI_HDF5_WORLD, 2);
  }

  H5Pclose(plist_id);
  H5Sclose(memspace);
  H5Sclose(dataspace);
  H5Dclose(dset_id);

  H5Fclose(file_id);
  MPI_Comm_free(&OP_MPI_HDF5_WORLD);

  return op_decl_map(from, to, dim, map, name);
}
Exemplo n.º 19
0
/****************************************************************
**
**  test_read_vl_string_attribute(): Test basic VL string code.
**      Tests reading VL strings from attributes
**
****************************************************************/
static void test_read_vl_string_attribute(void)
{
    hid_t file, root, att;
    hid_t type;
    herr_t ret;
    char *string_att_check = NULL;

    /* Open file */
    file = H5Fopen(DATAFILE, H5F_ACC_RDONLY, H5P_DEFAULT);
    CHECK(file, FAIL, "H5Fopen");

    /* Create a datatype to refer to. */
    type = H5Tcopy (H5T_C_S1);
    CHECK(type, FAIL, "H5Tcopy");

    ret = H5Tset_size (type, H5T_VARIABLE);
    CHECK(ret, FAIL, "H5Tset_size");

    root = H5Gopen2(file, "/", H5P_DEFAULT);
    CHECK(root, FAIL, "H5Gopen2");

    /* Test reading "normal" sized string attribute */
    att = H5Aopen(root, "test_scalar", H5P_DEFAULT);
    CHECK(att, FAIL, "H5Aopen");

    ret = H5Aread(att, type, &string_att_check);
    CHECK(ret, FAIL, "H5Aread");

    if(HDstrcmp(string_att_check,string_att) != 0)
        TestErrPrintf("VL string attributes don't match!, string_att=%s, string_att_check=%s\n",string_att,string_att_check);

    H5free_memory(string_att_check);
    string_att_check = NULL;

    ret = H5Aclose(att);
    CHECK(ret, FAIL, "HAclose");

    /* Test reading "large" sized string attribute */
    att = H5Aopen(root, "test_scalar_large", H5P_DEFAULT);
    CHECK(att, FAIL, "H5Aopen");

    if(string_att_write) {
        ret = H5Aread(att, type, &string_att_check);
        CHECK(ret, FAIL, "H5Aread");

        if(HDstrcmp(string_att_check,string_att_write) != 0)
            TestErrPrintf("VL string attributes don't match!, string_att_write=%s, string_att_check=%s\n",string_att_write,string_att_check);

        H5free_memory(string_att_check);
        string_att_check = NULL;
    }

    /* Free string allocated in test_write_vl_string_attribute */
    if(string_att_write)
        HDfree(string_att_write);

    ret = H5Aclose(att);
    CHECK(ret, FAIL, "HAclose");

    ret = H5Tclose(type);
    CHECK(ret, FAIL, "H5Tclose");

    ret = H5Gclose(root);
    CHECK(ret, FAIL, "H5Gclose");

    ret = H5Fclose(file);
    CHECK(ret, FAIL, "H5Fclose");

    return;
}
Exemplo n.º 20
0
op_dat op_decl_dat_hdf5(op_set set, int dim, char const *type, char const *file, char const *name)
{
  //create new communicator
  int my_rank, comm_size;
  MPI_Comm_dup(MPI_COMM_WORLD, &OP_MPI_HDF5_WORLD);
  MPI_Comm_rank(OP_MPI_HDF5_WORLD, &my_rank);
  MPI_Comm_size(OP_MPI_HDF5_WORLD, &comm_size);

  //MPI variables
  MPI_Info info  = MPI_INFO_NULL;

  //HDF5 APIs definitions
  hid_t       file_id; //file identifier
  hid_t plist_id;  //property list identifier
  hid_t dset_id; //dataset identifier
  hid_t       dataspace; //data space identifier
  hid_t       memspace; //memory space identifier

  hsize_t count[2]; //hyperslab selection parameters
  hsize_t offset[2];
  hid_t attr;   //attribute identifier

  //Set up file access property list with parallel I/O access
  plist_id = H5Pcreate(H5P_FILE_ACCESS);
  H5Pset_fapl_mpio(plist_id, OP_MPI_HDF5_WORLD, info);

  file_id = H5Fopen(file, H5F_ACC_RDONLY, plist_id );
  H5Pclose(plist_id);


  /*find element size of this dat with available attributes*/
  size_t dat_size = 0;
  //open existing data set
  dset_id = H5Dopen(file_id, name, H5P_DEFAULT);
  //get OID of the attribute
  attr = H5Aopen(dset_id, "size", H5P_DEFAULT);
  //read attribute
  H5Aread(attr,H5T_NATIVE_INT,&dat_size);
  H5Aclose(attr);
  H5Dclose(dset_id);


  /*find dim with available attributes*/
  int dat_dim = 0;
  //open existing data set
  dset_id = H5Dopen(file_id, name, H5P_DEFAULT);
  //get OID of the attribute
  attr = H5Aopen(dset_id, "dim", H5P_DEFAULT);
  //read attribute
  H5Aread(attr,H5T_NATIVE_INT,&dat_dim);
  H5Aclose(attr);
  H5Dclose(dset_id);
  if(dat_dim != dim)
  {
    printf("dat.dim %d in file %s and dim %d do not match\n",dat_dim,file,dim);
    MPI_Abort(OP_MPI_HDF5_WORLD, 2);
  }

  /*find type with available attributes*/
  dataspace= H5Screate(H5S_SCALAR);
  hid_t  atype = H5Tcopy(H5T_C_S1);
  H5Tset_size(atype, 10);
  //open existing data set
  dset_id = H5Dopen(file_id, name, H5P_DEFAULT);
  //get OID of the attribute
  attr = H5Aopen(dset_id, "type", H5P_DEFAULT);
  //read attribute
  char typ[10];
  H5Aread(attr,atype,typ);
  H5Aclose(attr);
  H5Sclose(dataspace);
  H5Dclose(dset_id);
  if(strcmp(typ,type) != 0)
  {
    printf("dat.type %s in file %s and type %s do not match\n",typ,file,type);
    MPI_Abort(OP_MPI_HDF5_WORLD, 2);
  }


  /*read in dat in hyperslabs*/

  //Create the dataset with default properties and close dataspace.
  dset_id = H5Dopen(file_id, name, H5P_DEFAULT);

  //Each process defines dataset in memory and reads from a hyperslab in the file.
  int disp = 0;
  int* sizes = (int *)xmalloc(sizeof(int)*comm_size);
  MPI_Allgather(&(set->size), 1, MPI_INT, sizes, 1, MPI_INT, OP_MPI_HDF5_WORLD);
  for(int i = 0; i<my_rank; i++)disp = disp + sizes[i];
  free(sizes);

  count[0] = set->size;
  count[1] = dim;
  offset[0] = disp;
  offset[1] = 0;
  memspace = H5Screate_simple(2, count, NULL);

  //Select hyperslab in the file.
  dataspace = H5Dget_space(dset_id);
  H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, offset, NULL, count, NULL);

  //Create property list for collective dataset write.
  plist_id = H5Pcreate(H5P_DATASET_XFER);
  H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_COLLECTIVE);

  //initialize data buffer and read in data
  char* data = 0;
  if(strcmp(type,"double") == 0)
  {
    data = (char *)xmalloc(set->size*dim*sizeof(double));
    H5Dread(dset_id, H5T_NATIVE_DOUBLE, memspace, dataspace, plist_id, data);

    if(dat_size != dim*sizeof(double))
    {
      printf("dat.size %lu in file %s and %d*sizeof(double) do not match\n",dat_size,file,dim);
      MPI_Abort(OP_MPI_HDF5_WORLD, 2);
    }
    else
      dat_size = sizeof(double);

  }else if(strcmp(type,"float") == 0)
  {
    data = (char *)xmalloc(set->size*dim*sizeof(float));
    H5Dread(dset_id, H5T_NATIVE_FLOAT, memspace, dataspace, plist_id, data);

    if(dat_size != dim*sizeof(float))
    {
      printf("dat.size %lu in file %s and %d*sizeof(float) do not match\n",dat_size,file,dim);
      MPI_Abort(OP_MPI_HDF5_WORLD, 2);
    }
    else
      dat_size = sizeof(float);

  }else if(strcmp(type,"int") == 0)
  {
    data = (char *)xmalloc(set->size*dim*sizeof(int));
    H5Dread(dset_id, H5T_NATIVE_INT, memspace, dataspace, plist_id, data);

    if(dat_size != dim*sizeof(int))
    {
      printf("dat.size %lu in file %s and %d*sizeof(int) do not match\n",dat_size,file,dim);
      MPI_Abort(OP_MPI_HDF5_WORLD, 2);
    }
    else
      dat_size = sizeof(int);
  }else
  {
    printf("unknown type\n");
    MPI_Abort(OP_MPI_HDF5_WORLD, 2);
  }

  H5Pclose(plist_id);
  H5Sclose(memspace);
  H5Sclose(dataspace);
  H5Dclose(dset_id);

  H5Fclose(file_id);
  MPI_Comm_free(&OP_MPI_HDF5_WORLD);

  return op_decl_dat(set, dim, type, dat_size, data, name );
}
Exemplo n.º 21
0
int
main (void)
{

   hid_t   file, dataset;       /* File and dataset identifiers */

   hid_t   fid;                 /* Dataspace identifier */
   hid_t   attr1, attr2, attr3; /* Attribute identifiers */
   hid_t   attr;
   hid_t   aid1, aid2, aid3;    /* Attribute dataspace identifiers */
   hid_t   atype, atype_mem;    /* Attribute type */
   H5T_class_t  type_class;

   hsize_t fdim[] = {SIZE};
   hsize_t adim[] = {ADIM1, ADIM2};  /* Dimensions of the first attribute  */

   float matrix[ADIM1][ADIM2]; /* Attribute data */

   herr_t  ret;                /* Return value */
   H5O_info_t oinfo;           /* Object info */
   unsigned i, j;              /* Counters */
   char    string_out[80];     /* Buffer to read string attribute back */
   int     point_out;          /* Buffer to read scalar attribute back */

   /*
    * Data initialization.
    */
   int vector[] = {1, 2, 3, 4, 5, 6, 7};  /* Dataset data */
   int point = 1;                         /* Value of the scalar attribute */
   char string[] = "ABCD";                /* Value of the string attribute */


   for (i=0; i < ADIM1; i++) {            /* Values of the array attribute */
       for (j=0; j < ADIM2; j++)
       matrix[i][j] = -1.;
   }

   /*
    * Create a file.
    */
   file = H5Fcreate(H5FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);

   /*
    * Create the dataspace for the dataset in the file.
    */
   fid = H5Screate(H5S_SIMPLE);
   ret = H5Sset_extent_simple(fid, RANK, fdim, NULL);

   /*
    * Create the dataset in the file.
    */
   dataset = H5Dcreate2(file, "Dataset", H5T_NATIVE_INT, fid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);

   /*
    * Write data to the dataset.
    */
   ret = H5Dwrite(dataset, H5T_NATIVE_INT, H5S_ALL , H5S_ALL, H5P_DEFAULT, vector);

   /*
    * Create dataspace for the first attribute.
    */
   aid1 = H5Screate(H5S_SIMPLE);
   ret  = H5Sset_extent_simple(aid1, ARANK, adim, NULL);

   /*
    * Create array attribute.
    */
   attr1 = H5Acreate2(dataset, ANAME, H5T_NATIVE_FLOAT, aid1, H5P_DEFAULT, H5P_DEFAULT);

   /*
    * Write array attribute.
    */
   ret = H5Awrite(attr1, H5T_NATIVE_FLOAT, matrix);

   /*
    * Create scalar attribute.
    */
   aid2  = H5Screate(H5S_SCALAR);
   attr2 = H5Acreate2(dataset, "Integer attribute", H5T_NATIVE_INT, aid2,
                     H5P_DEFAULT, H5P_DEFAULT);

   /*
    * Write scalar attribute.
    */
   ret = H5Awrite(attr2, H5T_NATIVE_INT, &point);

   /*
    * Create string attribute.
    */
   aid3  = H5Screate(H5S_SCALAR);
   atype = H5Tcopy(H5T_C_S1);
           H5Tset_size(atype, 5);
           H5Tset_strpad(atype,H5T_STR_NULLTERM);
   attr3 = H5Acreate2(dataset, ANAMES, atype, aid3, H5P_DEFAULT, H5P_DEFAULT);

   /*
    * Write string attribute.
    */
   ret = H5Awrite(attr3, atype, string);

   /*
    * Close attribute and file dataspaces, and datatype.
    */
   ret = H5Sclose(aid1);
   ret = H5Sclose(aid2);
   ret = H5Sclose(aid3);
   ret = H5Sclose(fid);
   ret = H5Tclose(atype);

   /*
    * Close the attributes.
    */
   ret = H5Aclose(attr1);
   ret = H5Aclose(attr2);
   ret = H5Aclose(attr3);

   /*
    * Close the dataset.
    */
   ret = H5Dclose(dataset);

   /*
    * Close the file.
    */
   ret = H5Fclose(file);

   /*
    * Reopen the file.
    */
   file = H5Fopen(H5FILE_NAME, H5F_ACC_RDONLY, H5P_DEFAULT);

   /*
    * Open the dataset.
    */
   dataset = H5Dopen2(file, "Dataset", H5P_DEFAULT);

   /*
    * Attach to the scalar attribute using attribute name, then read and
    * display its value.
    */
   attr = H5Aopen(dataset, "Integer attribute", H5P_DEFAULT);
   ret  = H5Aread(attr, H5T_NATIVE_INT, &point_out);
   printf("The value of the attribute \"Integer attribute\" is %d \n", point_out);
   ret =  H5Aclose(attr);

   /*
    * Find string attribute by iterating through all attributes
    */
   ret = H5Oget_info(dataset, &oinfo);
   for(i = 0; i < (unsigned)oinfo.num_attrs; i++) {
      attr = H5Aopen_by_idx(dataset, ".", H5_INDEX_CRT_ORDER, H5_ITER_INC, (hsize_t)i, H5P_DEFAULT, H5P_DEFAULT);
      atype = H5Aget_type(attr);
      type_class = H5Tget_class(atype);
      if (type_class == H5T_STRING) {
           atype_mem = H5Tget_native_type(atype, H5T_DIR_ASCEND);
           ret   = H5Aread(attr, atype_mem, string_out);
           printf("Found string attribute; its index is %d , value =   %s \n", i, string_out);
           ret   = H5Tclose(atype_mem);
      }
       ret   = H5Aclose(attr);
       ret   = H5Tclose(atype);
    }

   /*
    * Get attribute info using iteration function.
    */
    ret = H5Aiterate2(dataset, H5_INDEX_NAME, H5_ITER_INC, NULL, attr_info, NULL);

   /*
    * Close the dataset and the file.
    */
   H5Dclose(dataset);
   H5Fclose(file);

   return 0;
}
Exemplo n.º 22
0
void op_write_hdf5(char const * file_name)
{
  printf("Writing to %s\n",file_name);

  //declare timers
  double cpu_t1, cpu_t2, wall_t1, wall_t2;
  double time;
  double max_time;
  op_timers(&cpu_t1, &wall_t1); //timer start for hdf5 file write

  //create new communicator
  int my_rank, comm_size;
  MPI_Comm_dup(MPI_COMM_WORLD, &OP_MPI_HDF5_WORLD);
  MPI_Comm_rank(OP_MPI_HDF5_WORLD, &my_rank);
  MPI_Comm_size(OP_MPI_HDF5_WORLD, &comm_size);

  //MPI variables
  MPI_Info info  = MPI_INFO_NULL;

  //HDF5 APIs definitions
  hid_t       file_id; //file identifier
  hid_t plist_id;  //property list identifier
  hid_t dset_id = 0; //dataset identifier
  hid_t       dataspace; //data space identifier
  hid_t       memspace; //memory space identifier

  hsize_t     dimsf[2]; // dataset dimensions
  hsize_t count[2]; //hyperslab selection parameters
  hsize_t offset[2];

  //Set up file access property list with parallel I/O access
  plist_id = H5Pcreate(H5P_FILE_ACCESS);
  H5Pset_fapl_mpio(plist_id, OP_MPI_HDF5_WORLD, info);

  //Create a new file collectively and release property list identifier.
  file_id = H5Fcreate(file_name, H5F_ACC_TRUNC, H5P_DEFAULT, plist_id);
  H5Pclose(plist_id);

  /*loop over all the op_sets and write them to file*/
  for(int s=0; s<OP_set_index; s++) {
    op_set set=OP_set_list[s];

    //Create the dataspace for the dataset.
    hsize_t dimsf_set[] = {1};
    dataspace = H5Screate_simple(1, dimsf_set, NULL);

    //Create the dataset with default properties and close dataspace.
    dset_id = H5Dcreate(file_id, set->name, H5T_NATIVE_INT, dataspace,
        H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);

    //Create property list for collective dataset write.
    plist_id = H5Pcreate(H5P_DATASET_XFER);
    H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_COLLECTIVE);

    int size = 0;
    int* sizes = (int *)xmalloc(sizeof(int)*comm_size);
    MPI_Allgather(&set->size, 1, MPI_INT, sizes, 1, MPI_INT, OP_MPI_HDF5_WORLD);
    for(int i = 0; i<comm_size; i++)size = size + sizes[i];

    //write data
    H5Dwrite(dset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, plist_id, &size);
    H5Sclose(dataspace);
    H5Pclose(plist_id);
    H5Dclose(dset_id);
  }


  /*loop over all the op_maps and write them to file*/
  for(int m=0; m<OP_map_index; m++) {
    op_map map=OP_map_list[m];

    //find total size of map
    int* sizes = (int *)xmalloc(sizeof(int)*comm_size);
    int g_size = 0;
    MPI_Allgather(&map->from->size, 1, MPI_INT, sizes, 1, MPI_INT, OP_MPI_HDF5_WORLD);
    for(int i = 0; i<comm_size; i++)g_size = g_size + sizes[i];

    //Create the dataspace for the dataset.
    dimsf[0] = g_size;
    dimsf[1] = map->dim;
    dataspace = H5Screate_simple(2, dimsf, NULL);

    //Create the dataset with default properties and close dataspace.
    if(sizeof(map->map[0]) == sizeof(int))
      dset_id = H5Dcreate(file_id, map->name, H5T_NATIVE_INT, dataspace,
          H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
    else if(sizeof(map->map[0]) == sizeof(long))
      dset_id = H5Dcreate(file_id, map->name, H5T_NATIVE_LONG, dataspace,
          H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
    else if(sizeof(map->map[0]) == sizeof(long long))
      dset_id = H5Dcreate(file_id, map->name, H5T_NATIVE_LLONG, dataspace,
          H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);

    H5Sclose(dataspace);


    //Each process defines dataset in memory and writes it to a hyperslab
    //in the file.
    int disp = 0;
    for(int i = 0; i<my_rank; i++)disp = disp + sizes[i];
    count[0] = map->from->size;
    count[1] = dimsf[1];
    offset[0] = disp;
    offset[1] = 0;
    memspace = H5Screate_simple(2, count, NULL);

    //Select hyperslab in the file.
    dataspace = H5Dget_space(dset_id);
    H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, offset, NULL, count, NULL);

    //Create property list for collective dataset write.
    plist_id = H5Pcreate(H5P_DATASET_XFER);
    H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_COLLECTIVE);

    //write data
    if(sizeof(map->map[0]) == sizeof(int))
      H5Dwrite(dset_id, H5T_NATIVE_INT, memspace, dataspace, plist_id, map->map);
    else if(sizeof(map->map[0]) == sizeof(long))
      H5Dwrite(dset_id, H5T_NATIVE_LONG, memspace, dataspace, plist_id, map->map);
    else if(sizeof(map->map[0]) == sizeof(long long))
      H5Dwrite(dset_id, H5T_NATIVE_LLONG, memspace, dataspace, plist_id, map->map);

    H5Pclose(plist_id);
    H5Sclose(memspace);
    H5Sclose(dataspace);
    H5Dclose(dset_id);

    free(sizes);

    /*attach attributes to map*/

    //open existing data set
    dset_id = H5Dopen(file_id, map->name, H5P_DEFAULT);
    //create the data space for the attribute
    hsize_t dims = 1;
    dataspace = H5Screate_simple(1, &dims, NULL);

    //Create an int attribute - size
    hid_t attribute = H5Acreate(dset_id, "size", H5T_NATIVE_INT, dataspace,
        H5P_DEFAULT, H5P_DEFAULT);
    //Write the attribute data.
    H5Awrite(attribute, H5T_NATIVE_INT, &g_size);
    //Close the attribute.
    H5Aclose(attribute);

    //Create an int attribute - dimension
    attribute = H5Acreate(dset_id, "dim", H5T_NATIVE_INT, dataspace,
        H5P_DEFAULT, H5P_DEFAULT);
    //Write the attribute data.
    H5Awrite(attribute, H5T_NATIVE_INT, &map->dim);
    //Close the attribute.
    H5Aclose(attribute);
    H5Sclose(dataspace);

    //Create an string attribute - type
    dataspace= H5Screate(H5S_SCALAR);
    hid_t atype = H5Tcopy(H5T_C_S1);
    H5Tset_size(atype, 10);
    attribute = H5Acreate(dset_id, "type", atype, dataspace,
        H5P_DEFAULT, H5P_DEFAULT);

    if(sizeof(map->map[0]) == sizeof(int))
      H5Awrite(attribute, atype, "int");
    if(sizeof(map->map[0]) == sizeof(long))
      H5Awrite(attribute, atype, "long");
    if(sizeof(map->map[0]) == sizeof(long long))
      H5Awrite(attribute, atype, "long long");

    H5Aclose(attribute);
    //Close the dataspace
    H5Sclose(dataspace);
    //Close to the dataset.
    H5Dclose(dset_id);
  }

  /*loop over all the op_dats and write them to file*/
  for(int d=0; d<OP_dat_index; d++) {
    op_dat dat=OP_dat_list[d];

    //find total size of map
    int* sizes = (int *)xmalloc(sizeof(int)*comm_size);
    int g_size = 0;
    MPI_Allgather(&dat->set->size, 1, MPI_INT, sizes, 1, MPI_INT, OP_MPI_HDF5_WORLD);
    for(int i = 0; i<comm_size; i++)g_size = g_size + sizes[i];

    //Create the dataspace for the dataset.
    dimsf[0] = g_size;
    dimsf[1] = dat->dim;
    dataspace = H5Screate_simple(2, dimsf, NULL);

    //Create the dataset with default properties and close dataspace.
    if(strcmp(dat->type,"double")==0)
      dset_id = H5Dcreate(file_id, dat->name, H5T_NATIVE_DOUBLE, dataspace,
          H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
    else if(strcmp(dat->type,"float")==0)
      dset_id = H5Dcreate(file_id, dat->name, H5T_NATIVE_FLOAT, dataspace,
          H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
    else if(strcmp(dat->type,"int")==0)
      dset_id = H5Dcreate(file_id, dat->name, H5T_NATIVE_INT, dataspace,
          H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
    else printf("Unknown type\n");

    H5Sclose(dataspace);

    //Each process defines dataset in memory and writes it to a hyperslab
    //in the file.
    int disp = 0;
    for(int i = 0; i<my_rank; i++)disp = disp + sizes[i];
    count[0] = dat->set->size;
    count[1] = dimsf[1];
    offset[0] = disp;
    offset[1] = 0;
    memspace = H5Screate_simple(2, count, NULL);

    //Select hyperslab in the file.
    dataspace = H5Dget_space(dset_id);
    H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, offset, NULL, count, NULL);

    //Create property list for collective dataset write.
    plist_id = H5Pcreate(H5P_DATASET_XFER);
    H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_COLLECTIVE);

    //write data
    if(strcmp(dat->type,"double") == 0)
      H5Dwrite(dset_id, H5T_NATIVE_DOUBLE, memspace, dataspace, plist_id, dat->data);
    else if(strcmp(dat->type,"float") == 0)
      H5Dwrite(dset_id, H5T_NATIVE_FLOAT, memspace, dataspace, plist_id, dat->data);
    else if(strcmp(dat->type,"int") == 0)
      H5Dwrite(dset_id, H5T_NATIVE_INT, memspace, dataspace, plist_id, dat->data);
    else printf("Unknown type\n");

    H5Pclose(plist_id);
    H5Sclose(memspace);
    H5Sclose(dataspace);
    H5Dclose(dset_id);
    free(sizes);


    /*attach attributes to dat*/

    //open existing data set
    dset_id = H5Dopen(file_id, dat->name, H5P_DEFAULT);
    //create the data space for the attribute
    hsize_t dims = 1;
    dataspace = H5Screate_simple(1, &dims, NULL);

    //Create an int attribute - size
    hid_t attribute = H5Acreate(dset_id, "size", H5T_NATIVE_INT, dataspace,
        H5P_DEFAULT, H5P_DEFAULT);
    //Write the attribute data.
    H5Awrite(attribute, H5T_NATIVE_INT, &dat->size);
    //Close the attribute.
    H5Aclose(attribute);

    //Create an int attribute - dimension
    attribute = H5Acreate(dset_id, "dim", H5T_NATIVE_INT, dataspace,
        H5P_DEFAULT, H5P_DEFAULT);
    //Write the attribute data.
    H5Awrite(attribute, H5T_NATIVE_INT, &dat->dim);
    H5Aclose(attribute);
    H5Sclose(dataspace);

    //Create an string attribute - type
    dataspace= H5Screate(H5S_SCALAR);
    hid_t atype = H5Tcopy(H5T_C_S1);
    H5Tset_size(atype, 10);
    attribute = H5Acreate(dset_id, "type", atype, dataspace,
        H5P_DEFAULT, H5P_DEFAULT);
    H5Awrite(attribute, atype, dat->type);
    H5Aclose(attribute);

    //Close the dataspace.
    H5Sclose(dataspace);
    H5Dclose(dset_id);
  }

  H5Fclose(file_id);

  op_timers(&cpu_t2, &wall_t2);  //timer stop for hdf5 file write
  //compute import/export lists creation time
  time = wall_t2-wall_t1;
  MPI_Reduce(&time, &max_time, 1, MPI_DOUBLE, MPI_MAX, MPI_ROOT, OP_MPI_HDF5_WORLD);
  //print performance results
  if(my_rank == MPI_ROOT)
  {
    printf("Max hdf5 file write time = %lf\n\n",max_time);
  }
  MPI_Comm_free(&OP_MPI_HDF5_WORLD);

}
Exemplo n.º 23
0
int main( void )
{
 typedef struct Particle
 {
  char   name[16];
  int    lati;
  int    longi;
  float  pressure;
  double temperature;
 } Particle;

 Particle  dst_buf[ NRECORDS + NRECORDS_INS ];

 /* Calculate the size and the offsets of our struct members in memory */
 size_t dst_size =  sizeof( Particle );
 size_t dst_offset[NFIELDS] = { HOFFSET( Particle, name ),
                                HOFFSET( Particle, lati ),
                                HOFFSET( Particle, longi ),
                                HOFFSET( Particle, pressure ),
                                HOFFSET( Particle, temperature )};
 size_t dst_sizes[NFIELDS] = { sizeof( dst_buf[0].name),
                               sizeof( dst_buf[0].lati),
                               sizeof( dst_buf[0].longi),
                               sizeof( dst_buf[0].pressure),
                               sizeof( dst_buf[0].temperature)};

 /* Define an array of Particles */
 Particle  p_data[NRECORDS] = {
 {"zero",0,0, 0.0f, 0.0},
 {"one",10,10, 1.0f, 10.0},
 {"two",  20,20, 2.0f, 20.0},
 {"three",30,30, 3.0f, 30.0},
 {"four", 40,40, 4.0f, 40.0},
 {"five", 50,50, 5.0f, 50.0},
 {"six",  60,60, 6.0f, 60.0},
 {"seven",70,70, 7.0f, 70.0}
  };

 /* Define field information */
 const char *field_names[NFIELDS]  =
 { "Name","Latitude", "Longitude", "Pressure", "Temperature" };
 hid_t      field_type[NFIELDS];
 hid_t      string_type;
 hid_t      file_id;
 hsize_t    chunk_size = 10;
 int        compress  = 0;
 Particle   fill_data[1] =
 { {"no data",-1,-1, -99.0f, -99.0} };   /* Fill value particle */
 hsize_t    start1;                      /* Record to start reading from 1st table */
 hsize_t    nrecords;                    /* Number of records to insert */
 hsize_t    start2;                      /* Record to start writing in 2nd table */
 herr_t     status;
 int        i;
 hsize_t    nfields_out;
 hsize_t    nrecords_out;

 /* Initialize the field field_type */
 string_type = H5Tcopy( H5T_C_S1 );
 H5Tset_size( string_type, 16 );
 field_type[0] = string_type;
 field_type[1] = H5T_NATIVE_INT;
 field_type[2] = H5T_NATIVE_INT;
 field_type[3] = H5T_NATIVE_FLOAT;
 field_type[4] = H5T_NATIVE_DOUBLE;

 /* Create a new file using default properties. */
 file_id = H5Fcreate( "ex_table_09.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT );

 /* Make 2 tables: TABLE2_NAME is empty  */
 status=H5TBmake_table( "Table Title",file_id,TABLE1_NAME,NFIELDS,NRECORDS,
                         dst_size,field_names, dst_offset, field_type,
                         chunk_size, fill_data, compress, p_data  );

 status=H5TBmake_table( "Table Title",file_id,TABLE2_NAME,NFIELDS,NRECORDS,
                         dst_size,field_names, dst_offset, field_type,
                         chunk_size, fill_data, compress, NULL  );


 /* Add 2 records from TABLE1_NAME to TABLE2_NAME  */
 start1    = 3;
 nrecords  = NRECORDS_INS;
 start2    = 6;
 status=H5TBadd_records_from( file_id, TABLE1_NAME, start1, nrecords, TABLE2_NAME, start2 );

 /* read TABLE2_NAME: it should have 2 more records now */
 status=H5TBread_table( file_id, TABLE2_NAME, dst_size, dst_offset, dst_sizes, dst_buf );

 /* Get table info  */
 status=H5TBget_table_info (file_id,TABLE2_NAME, &nfields_out, &nrecords_out );

 /* print */
 printf ("Table has %d fields and %d records\n",(int)nfields_out,(int)nrecords_out);

 /* print it by rows */
 for (i=0; i<nrecords_out; i++) {
  printf ("%-5s %-5d %-5d %-5f %-5f",
   dst_buf[i].name,
   dst_buf[i].lati,
   dst_buf[i].longi,
   dst_buf[i].pressure,
   dst_buf[i].temperature);
  printf ("\n");
 }

  /* close type */
 H5Tclose( string_type );

 /* close the file */
 H5Fclose( file_id );

 return 0;
}
Exemplo n.º 24
0
med_err _MEDattributeStringWrByName(med_idt pid,
				    const char * const path,
				    const char * const attname,
				    const med_size attsize,
				    const char * const val)
{
  med_access_mode MED_ACCESS_MODE;
  med_idt _attid=0,aid=0;
  med_err _ret=-1;
  int     type_hdf=0;
  med_bool        _attmustbecreated= MED_FALSE;
  hsize_t         _attsize=0;
  med_size        _valsize=0;

  if ( (MED_ACCESS_MODE = _MEDmodeAcces(pid) ) == MED_ACC_UNDEF ) {
    MED_ERR_(_ret,MED_ERR_INVALID,MED_ERR_ACCESSMODE, "MED_ACC_UNDEF" );
    SSCRUTE(attname); SSCRUTE(path);goto ERROR;
  }

  _valsize=strlen(val);
  if (_valsize > attsize) {
    MED_ERR_(_ret,MED_ERR_INVALID,MED_ERR_ATTRIBUTE, attname );
    ISCRUTE_size(_valsize);ISCRUTE_size(attsize);goto ERROR;
  }

  if ((aid = H5Screate(H5S_SCALAR)) < 0){
    MED_ERR_(_ret,MED_ERR_CREATE,MED_ERR_DATASPACE, attname );
    ISCRUTE_id(aid);
  }

  if ( (type_hdf = H5Tcopy(H5T_C_S1)) < 0) {
    MED_ERR_(_ret,MED_ERR_CREATE,MED_ERR_HDFTYPE, MED_ERR_NAME_MSG );
    SSCRUTE("H5T_C_S1"); goto ERROR;
  }

  if ( H5Tset_size(type_hdf,_valsize+1) < 0) {
    MED_ERR_(_ret,MED_ERR_CREATE,MED_ERR_HDFTYPE, MED_ERR_NAME_MSG );
    SSCRUTE("H5T_C_S1"); goto ERROR;
  }

  if  ( (_attid=H5Aopen_by_name( pid, path, attname, H5P_DEFAULT, H5P_DEFAULT ))  >= 0 )
    if ( MED_ACCESS_MODE == MED_ACC_RDEXT )  {
      MED_ERR_(_ret,MED_ERR_INVALID,MED_ERR_ACCESSMODE, "MED_ACC_RDEXT" );
      SSCRUTE(attname); SSCRUTE(path);goto ERROR;
    }

  if ( _attid > 0 ) {
    if ( (_attsize=H5Aget_storage_size(_attid) ) < 0 ) goto ERROR;
    if ( (_valsize+1)  > _attsize ) {
      if (H5Aclose(_attid) < 0) {
	MED_ERR_(_ret,MED_ERR_CLOSE,MED_ERR_ATTRIBUTE,"");
	ISCRUTE_id(_attid);
	goto ERROR;
	}
      if ( H5Adelete_by_name(pid,path,attname,H5P_DEFAULT) < 0 ) goto ERROR;
      _attmustbecreated=MED_TRUE;
    }
  }

  if ( (_attid < 0) || _attmustbecreated )
    if ( (_attid=H5Acreate_by_name( pid, path, attname, type_hdf, aid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT )) < 0 ) {
      MED_ERR_(_ret,MED_ERR_CREATE,MED_ERR_ATTRIBUTE, attname );
      SSCRUTE(path);goto ERROR;
    }


  if ( H5Awrite(_attid,type_hdf,val) < 0) {
    MED_ERR_(_ret,MED_ERR_WRITE,MED_ERR_ATTRIBUTE, attname );
    SSCRUTE(path);goto ERROR;
  }

  _ret=0;

 ERROR:

  if (type_hdf > 0 ) if ( H5Tclose(type_hdf) < 0) {
    MED_ERR_(_ret,MED_ERR_CLOSE,MED_ERR_HDFTYPE, MED_ERR_ID_MSG );
    ISCRUTE_int(type_hdf);
  }

  if (aid > 0 ) if ( H5Sclose(aid) < 0) {
    MED_ERR_(_ret,MED_ERR_CLOSE,MED_ERR_DATASPACE, MED_ERR_ID_MSG );
    ISCRUTE_id(aid);
  }

  if (_attid >0) if ( H5Aclose(_attid) < 0) {
    MED_ERR_(_ret,MED_ERR_CLOSE,MED_ERR_ATTRIBUTE, MED_ERR_ID_MSG );
    ISCRUTE_id(_attid);
  }


  return _ret;
}
Exemplo n.º 25
0
int
main (void)
{
    hid_t       file, filetype, memtype, space, dset;
                                            /* Handles */
    herr_t      status;
    hsize_t     dims[1] = {DIM0};
    char        *wdata[DIM0] = {"Parting", "is such", "sweet", "sorrow."},
                                            /* Write buffer */
                **rdata;                    /* Read buffer */
    int         ndims,
                i;

    /*
     * Create a new file using the default properties.
     */
    file = H5Fcreate (FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);

    /*
     * Create file and memory datatypes.  For this example we will save
     * the strings as FORTRAN strings.
     */
    filetype = H5Tcopy (H5T_FORTRAN_S1);
    status = H5Tset_size (filetype, H5T_VARIABLE);
    memtype = H5Tcopy (H5T_C_S1);
    status = H5Tset_size (memtype, H5T_VARIABLE);

    /*
     * Create dataspace.  Setting maximum size to NULL sets the maximum
     * size to be the current size.
     */
    space = H5Screate_simple (1, dims, NULL);

    /*
     * Create the dataset and write the variable-length string data to
     * it.
     */
    dset = H5Dcreate (file, DATASET, filetype, space, H5P_DEFAULT, H5P_DEFAULT,
                H5P_DEFAULT);
    status = H5Dwrite (dset, memtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata);

    /*
     * Close and release resources.
     */
    status = H5Dclose (dset);
    status = H5Sclose (space);
    status = H5Tclose (filetype);
    status = H5Tclose (memtype);
    status = H5Fclose (file);


    /*
     * Now we begin the read section of this example.  Here we assume
     * the dataset has the same name and rank, but can have any size.
     * Therefore we must allocate a new array to read in data using
     * malloc().
     */

    /*
     * Open file and dataset.
     */
    file = H5Fopen (FILE, H5F_ACC_RDONLY, H5P_DEFAULT);
    dset = H5Dopen (file, DATASET, H5P_DEFAULT);

    /*
     * Get the datatype.
     */
    filetype = H5Dget_type (dset);

    /*
     * Get dataspace and allocate memory for read buffer.
     */
    space = H5Dget_space (dset);
    ndims = H5Sget_simple_extent_dims (space, dims, NULL);
    rdata = (char **) malloc (dims[0] * sizeof (char *));

    /*
     * Create the memory datatype.
     */
    memtype = H5Tcopy (H5T_C_S1);
    status = H5Tset_size (memtype, H5T_VARIABLE);

    /*
     * Read the data.
     */
    status = H5Dread (dset, memtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, rdata);

    /*
     * Output the data to the screen.
     */
    for (i=0; i<dims[0]; i++)
        printf ("%s[%d]: %s\n", DATASET, i, rdata[i]);

    /*
     * Close and release resources.  Note that H5Dvlen_reclaim works
     * for variable-length strings as well as variable-length arrays.
     * Also note that we must still free the array of pointers stored
     * in rdata, as H5Tvlen_reclaim only frees the data these point to.
     */
    status = H5Dvlen_reclaim (memtype, space, H5P_DEFAULT, rdata);
    free (rdata);
    status = H5Dclose (dset);
    status = H5Sclose (space);
    status = H5Tclose (filetype);
    status = H5Tclose (memtype);
    status = H5Fclose (file);

    return 0;
}
Exemplo n.º 26
0
/****************************************************************
**
**  test_vlstrings_basic(): Test basic VL string code.
**      Tests simple VL string I/O
**
****************************************************************/
static void
test_vlstrings_basic(void)
{
    const char *wdata[SPACE1_DIM1]= {
        "Four score and seven years ago our forefathers brought forth on this continent a new nation,",
        "conceived in liberty and dedicated to the proposition that all men are created equal.",
        "Now we are engaged in a great civil war,",
        "testing whether that nation or any nation so conceived and so dedicated can long endure."
        };   /* Information to write */
    char *rdata[SPACE1_DIM1];   /* Information read in */
    char *wdata2;
    hid_t dataspace, dataset2;
    hid_t		fid1;		/* HDF5 File IDs		*/
    hid_t		dataset;	/* Dataset ID			*/
    hid_t		sid1;       /* Dataspace ID			*/
    hid_t		tid1;       /* Datatype ID			*/
    hid_t       xfer_pid;   /* Dataset transfer property list ID */
    hsize_t		dims1[] = {SPACE1_DIM1};
    hsize_t     size;       /* Number of bytes which will be used */
    unsigned       i;          /* counting variable */
    size_t         str_used;   /* String data in memory */
    size_t         mem_used=0; /* Memory used during allocation */
    herr_t		ret;		/* Generic return value		*/

    /* Output message about test being performed */
    MESSAGE(5, ("Testing Basic VL String Functionality\n"));

    /* Create file */
    fid1 = H5Fcreate(DATAFILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
    CHECK(fid1, FAIL, "H5Fcreate");

    /* Create dataspace for datasets */
    sid1 = H5Screate_simple(SPACE1_RANK, dims1, NULL);
    CHECK(sid1, FAIL, "H5Screate_simple");

    /* Create a datatype to refer to */
    tid1 = H5Tcopy(H5T_C_S1);
    CHECK(tid1, FAIL, "H5Tcopy");

    ret = H5Tset_size(tid1,H5T_VARIABLE);
    CHECK(ret, FAIL, "H5Tset_size");

    /* Create a dataset */
    dataset = H5Dcreate2(fid1, "Dataset1", tid1, sid1, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
    CHECK(dataset, FAIL, "H5Dcreate2");

    /* Write dataset to disk */
    ret = H5Dwrite(dataset, tid1, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata);
    CHECK(ret, FAIL, "H5Dwrite");

    dataspace = H5Screate(H5S_SCALAR);

    dataset2 = H5Dcreate2(fid1, "Dataset2", tid1, dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
    CHECK(dataset, FAIL, "H5Dcreate2");

    wdata2 = (char*)HDcalloc((size_t)65534, sizeof(char));
    HDmemset(wdata2, 'A', (size_t)65533);

    ret = H5Dwrite(dataset2, tid1, H5S_ALL, H5S_ALL, H5P_DEFAULT, &wdata2);
    CHECK(ret, FAIL, "H5Dwrite");

    H5Sclose(dataspace);
    H5Dclose(dataset2);
    HDfree(wdata2);

    /* Change to the custom memory allocation routines for reading VL string */
    xfer_pid = H5Pcreate(H5P_DATASET_XFER);
    CHECK(xfer_pid, FAIL, "H5Pcreate");

    ret=H5Pset_vlen_mem_manager(xfer_pid,test_vlstr_alloc_custom,&mem_used,test_vlstr_free_custom,&mem_used);
    CHECK(ret, FAIL, "H5Pset_vlen_mem_manager");

    /* Make certain the correct amount of memory will be used */
    ret=H5Dvlen_get_buf_size(dataset,tid1,sid1,&size);
    CHECK(ret, FAIL, "H5Dvlen_get_buf_size");

    /* Count the actual number of bytes used by the strings */
    for(i=0,str_used=0; i<SPACE1_DIM1; i++)
        str_used+=HDstrlen(wdata[i])+1;

    /* Compare against the strings actually written */
    VERIFY(size,(hsize_t)str_used,"H5Dvlen_get_buf_size");

    /* Read dataset from disk */
    ret = H5Dread(dataset, tid1, H5S_ALL, H5S_ALL, xfer_pid, rdata);
    CHECK(ret, FAIL, "H5Dread");

    /* Make certain the correct amount of memory has been used */
    VERIFY(mem_used,str_used,"H5Dread");

    /* Compare data read in */
    for(i = 0; i < SPACE1_DIM1; i++) {
        if(HDstrlen(wdata[i]) != HDstrlen(rdata[i])) {
            TestErrPrintf("VL data length don't match!, strlen(wdata[%d])=%d, strlen(rdata[%d])=%d\n",(int)i,(int)strlen(wdata[i]),(int)i,(int)strlen(rdata[i]));
            continue;
        } /* end if */
        if(HDstrcmp(wdata[i], rdata[i]) != 0 ) {
            TestErrPrintf("VL data values don't match!, wdata[%d]=%s, rdata[%d]=%s\n",(int)i,wdata[i],(int)i,rdata[i]);
            continue;
        } /* end if */
    } /* end for */

    /* Reclaim the read VL data */
    ret = H5Dvlen_reclaim(tid1,sid1,xfer_pid,rdata);
    CHECK(ret, FAIL, "H5Dvlen_reclaim");

    /* Make certain the VL memory has been freed */
    VERIFY(mem_used,0,"H5Dvlen_reclaim");

    /* Close Dataset */
    ret = H5Dclose(dataset);
    CHECK(ret, FAIL, "H5Dclose");

    /* Close datatype */
    ret = H5Tclose(tid1);
    CHECK(ret, FAIL, "H5Tclose");

    /* Close disk dataspace */
    ret = H5Sclose(sid1);
    CHECK(ret, FAIL, "H5Sclose");

    /* Close dataset transfer property list */
    ret = H5Pclose(xfer_pid);
    CHECK(ret, FAIL, "H5Pclose");

    /* Close file */
    ret = H5Fclose(fid1);
    CHECK(ret, FAIL, "H5Fclose");

} /* end test_vlstrings_basic() */
Exemplo n.º 27
0
int
main (void)
{
    hid_t       sourcetype, desttype, strtype, space;
    /* Handles */
    herr_t      status;
    hsize_t     dims[1] = {DIM0};
    reading_t   *reading;                   /* Conversion buffer */
    sensor_t    *sensor,                    /* Conversion buffer */
                bkgrd[DIM0];                /* Background buffer */
    int         i;

    /*
     * Allocate memory for conversion buffer.  We will allocate space
     * for it to hold DIM0 elements of the destination type, as the
     * type conversion is performed in place.  Of course, if the
     * destination type were smaller than the source type, we would
     * allocate space to hold DIM0 elements of the source type.
     */
    reading = (reading_t *) malloc (DIM0 * sizeof (sensor_t));

    /*
     * Assign the allocated space to a pointer of the destination type,
     * to allow the buffer to be accessed correctly after the
     * conversion has taken place.
     */
    sensor = (sensor_t *) reading;

    /*
     * Initialize data.
     */
    bkgrd[0].serial_no = 1153;
    bkgrd[0].location = "Exterior (static)";
    bkgrd[0].temperature = 53.23;
    bkgrd[0].pressure = 24.57;
    bkgrd[1].serial_no = 1184;
    bkgrd[1].location = "Intake";
    bkgrd[1].temperature = 55.12;
    bkgrd[1].pressure = 22.95;
    bkgrd[2].serial_no = 1027;
    bkgrd[2].location = "Intake manifold";
    bkgrd[2].temperature = 103.55;
    bkgrd[2].pressure = 31.23;
    bkgrd[3].serial_no = 1313;
    bkgrd[3].location = "Exhaust manifold";
    bkgrd[3].temperature = 1252.89;
    bkgrd[3].pressure = 84.11;

    reading[0].temperature = 54.84;
    reading[0].pressure = 24.76;
    reading[1].temperature = 56.63;
    reading[1].pressure = 23.10;
    reading[2].temperature = 102.69;
    reading[2].pressure = 30.97;
    reading[3].temperature = 1238.27;
    reading[3].pressure = 82.15;

    /*
     * Create variable-length string datatype.
     */
    strtype = H5Tcopy (H5T_C_S1);
    status = H5Tset_size (strtype, H5T_VARIABLE);

    /*
     * Create the compound datatype for memory.
     */
    sourcetype = H5Tcreate (H5T_COMPOUND, sizeof (reading_t));
    status = H5Tinsert (sourcetype, "Temperature (F)",
                        HOFFSET (reading_t, temperature), H5T_NATIVE_DOUBLE);
    status = H5Tinsert (sourcetype, "Pressure (inHg)",
                        HOFFSET (reading_t, pressure), H5T_NATIVE_DOUBLE);

    desttype = H5Tcreate (H5T_COMPOUND, sizeof (sensor_t));
    status = H5Tinsert (desttype, "Serial number",
                        HOFFSET (sensor_t, serial_no), H5T_NATIVE_INT);
    status = H5Tinsert (desttype, "Location", HOFFSET (sensor_t, location),
                        strtype);
    status = H5Tinsert (desttype, "Temperature (F)",
                        HOFFSET (sensor_t, temperature), H5T_NATIVE_DOUBLE);
    status = H5Tinsert (desttype, "Pressure (inHg)",
                        HOFFSET (sensor_t, pressure), H5T_NATIVE_DOUBLE);

    /*
     * Create dataspace.  Setting maximum size to NULL sets the maximum
     * size to be the current size.
     */
    space = H5Screate_simple (1, dims, NULL);

    /*
     * Convert the buffer in reading from sourcetype to desttype.
     * After this conversion we will use sensor to access the buffer,
     * as the buffer now matches its type.
     */
    status = H5Tconvert (sourcetype, desttype, DIM0, reading, bkgrd,
                         H5P_DEFAULT);

    /*
     * Output the data to the screen.
     */
    for (i=0; i<DIM0; i++) {
        printf ("sensor[%d]:\n", i);
        printf ("Serial number   : %d\n", sensor[i].serial_no);
        printf ("Location        : %s\n", sensor[i].location);
        printf ("Temperature (F) : %f\n", sensor[i].temperature);
        printf ("Pressure (inHg) : %f\n\n", sensor[i].pressure);
    }

    /*
     * Close and release resources.  In this case H5Tconvert preserves
     * the memory locations of the variable-length strings in
     * "location", so we do not need to free those strings as they were
     * initialized as string constants.
     */
    free (sensor);
    status = H5Sclose (space);
    status = H5Tclose (sourcetype);
    status = H5Tclose (desttype);
    status = H5Tclose (strtype);

    return 0;
}
Exemplo n.º 28
0
/*
** WARNING : this function returns an allocated value that must be freed.
*/
static char* readAttribute(int _iDatasetId, const char *_pstName)
{
    hid_t iAttributeId;
    hid_t iFileType, memtype, iSpace;
    herr_t status;
    hsize_t dims[1];
    size_t iDim;
    hsize_t n = 0;

    char *pstValue = NULL;

    if (H5Aiterate(_iDatasetId, H5_INDEX_NAME, H5_ITER_NATIVE, &n, find_attr_by_name, (void *)_pstName) > 0)
    {
        iAttributeId = H5Aopen(_iDatasetId, _pstName, H5P_DEFAULT);
        if (iAttributeId < 0)
        {
            return NULL;
        }
        /*
         * Get the datatype and its size.
         */
        iFileType = H5Aget_type(iAttributeId);
        iDim = H5Tget_size(iFileType);
        iDim++;                 /* Make room for null terminator */

        /*
         * Get dataspace and allocate memory for read buffer.  This is a
         * two dimensional attribute so the dynamic allocation must be done
         * in steps.
         */
        iSpace = H5Aget_space(iAttributeId);
        if (iSpace < 0)
        {
            return NULL;
        }

        status = H5Sget_simple_extent_dims(iSpace, dims, NULL);
        if (status < 0)
        {
            return NULL;
        }

        /*
         * Allocate space for string data.
         */
        pstValue = (char *)MALLOC((size_t) ((dims[0] * iDim + 1) * sizeof(char)));

        /*
         * Create the memory datatype.
         */
        memtype = H5Tcopy(H5T_C_S1);
        status = H5Tset_size(memtype, iDim);
        if (status < 0)
        {
            FREE(pstValue);
            return NULL;
        }

        /*
         * Read the data.
         */
        status = H5Aread(iAttributeId, memtype, pstValue);
        if (status < 0)
        {
            FREE(pstValue);
            return NULL;
        }

        status = H5Tclose(memtype);
        if (status < 0)
        {
            FREE(pstValue);
            return NULL;
        }

        status = H5Sclose(iSpace);
        if (status < 0)
        {
            FREE(pstValue);
            return NULL;
        }

        status = H5Tclose(iFileType);
        if (status < 0)
        {
            FREE(pstValue);
            return NULL;
        }

        status = H5Aclose(iAttributeId);
        if (status < 0)
        {
            FREE(pstValue);
            return NULL;
        }
    }
    return pstValue;

}