H5Part.c 76.5 KB
Newer Older
gsell's avatar
gsell committed
1 2 3 4 5 6 7 8 9
/*! \mainpage H5Part: A Portable High Performance Parallel Data Interface to HDF5

Particle based simulations of accelerator beam-lines, especially in
six dimensional phase space, generate vast amounts of data. Even
though a subset of statistical information regarding phase space or
analysis needs to be preserved, reading and writing such enormous
restart files on massively parallel supercomputing systems remains
challenging. 

Kurt Stockinger's avatar
Kurt Stockinger committed
10
H5Part consists of Particles and Block structured Fields.
gsell's avatar
gsell committed
11

Kurt Stockinger's avatar
Kurt Stockinger committed
12
Developed by:
gsell's avatar
gsell committed
13

Kurt Stockinger's avatar
Kurt Stockinger committed
14 15 16 17 18 19 20 21 22
<UL>
<LI> Andreas Adelmann (PSI) </LI>
<LI> Achim Gsell (PSI) </LI>
<LI> Benedikt Oswald (PSI) </LI>

<LI> Wes Bethel (NERSC/LBNL)</LI>
<LI> John Shalf (NERSC/LBNL)</LI>
<LI> Cristina Siegerist (NERSC/LBNL)</LI>
</UL>
gsell's avatar
gsell committed
23 24 25 26


Papers: 

Kurt Stockinger's avatar
Kurt Stockinger committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
<UL>
<LI> A. Adelmann, R.D. Ryne, C. Siegerist, J. Shalf,"From Visualization to Data Mining with Large Data Sets," <i>
<a href="http://www.sns.gov/pac05">Particle Accelerator Conference (PAC05)</a></i>, Knoxville TN., May 16-20, 2005. (LBNL-57603)
<a href="http://vis.lbl.gov/Publications/2005/FPAT082.pdf">FPAT082.pdf</a>
</LI>


<LI> A. Adelmann, R.D. Ryne, J. Shalf, C. Siegerist,"H5Part: A Portable High Performance Parallel Data Interface for Particle Simulations," <i>
<a href="http://www.sns.gov/pac05">Particle Accelerator Conference (PAC05)</a></i>, Knoxville TN., May 16-20, 2005.
<a href="http://vis.lbl.gov/Publications/2005/FPAT083.pdf">FPAT083.pdf</a>
</LI>
</UL>

For further information contact: <a href="mailto:h5part@lists.psi.ch">h5part</a>

Last modified on April 19, 2007.

gsell's avatar
gsell committed
44 45
*/

gsell's avatar
gsell committed
46 47

/*!
gsell's avatar
gsell committed
48
  \defgroup h5part_c_api H5Part C API
gsell's avatar
gsell committed
49 50 51

*/
/*!
gsell's avatar
gsell committed
52
  \ingroup h5part_c_api
gsell's avatar
gsell committed
53 54 55
  \defgroup h5part_openclose	File Opening and Closing
*/
/*!
gsell's avatar
gsell committed
56
  \ingroup h5part_c_api
gsell's avatar
gsell committed
57 58 59
  \defgroup h5part_write	File Writing
*/  
/*!
gsell's avatar
gsell committed
60
  \ingroup h5part_c_api
gsell's avatar
gsell committed
61 62 63
  \defgroup h5part_read		File Reading
*/  
/*!
gsell's avatar
gsell committed
64
  \ingroup h5part_c_api
gsell's avatar
gsell committed
65 66 67
  \defgroup h5part_attrib	Reading and Writing Attributes
*/
/*!
gsell's avatar
gsell committed
68
  \ingroup h5part_c_api
gsell's avatar
gsell committed
69 70 71 72 73 74 75 76
  \defgroup h5part_errhandle	Error Handling
*/
/*!
  \internal
  \defgroup h5partkernel H5Part private functions 
*/


gsell's avatar
gsell committed
77 78 79 80 81 82 83 84
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>	/* va_arg - System dependent ?! */
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <hdf5.h>

gsell's avatar
gsell committed
85 86 87 88 89 90 91 92
#ifndef WIN32
#include <unistd.h>
#else /* WIN32 */
#include <io.h>
#define open  _open
#define close _close
#endif /* WIN32 */

gsell's avatar
gsell committed
93 94 95 96 97 98 99 100
#include "H5PartTypes.h"
#include "H5Part.h"
#include "H5PartPrivate.h"
#include "H5PartErrors.h"

/********* Private Variable Declarations *************/

static unsigned			_debug = 0;
gsell's avatar
gsell committed
101
static h5part_int64_t		_h5part_errno = H5PART_SUCCESS;
gsell's avatar
gsell committed
102
static h5part_error_handler	_err_handler = H5PartReportErrorHandler;
103
static char *__funcname;
gsell's avatar
gsell committed
104 105 106 107 108 109 110 111 112 113 114 115 116

/********** Declaration of private functions ******/

static h5part_int64_t
_init(
	void
	);

static h5part_int64_t
_file_is_valid (
	const H5PartFile *f
	);

gsell's avatar
gsell committed
117 118 119
/*
  error handler for hdf5
*/
gsell's avatar
gsell committed
120 121
static herr_t
_h5_error_handler (
122 123 124
#if H5_VERS_MAJOR == 1 && H5_VERS_MINOR == 8
	hid_t,
#endif
gsell's avatar
gsell committed
125 126 127 128 129
	void *
	);

/*========== File Opening/Closing ===============*/

gsell's avatar
gsell committed
130 131
static H5PartFile*
_H5Part_open_file (
gsell's avatar
gsell committed
132
	const char *filename,	/*!< [in] The name of the data file to open. */
133
	const char flags,	/*!< [in] The access mode for the file. */
gsell's avatar
gsell committed
134
	MPI_Comm comm,		/*!< [in] MPI communicator */
135 136 137 138
	int f_parallel,		/*!< [in] 0 for serial io otherwise parallel */
	h5part_int64_t align	/*!< [in] Number of bytes for setting alignment,
					  metadata block size, etc.
					  Set to 0 to disable. */
gsell's avatar
gsell committed
139
	) {
gsell's avatar
gsell committed
140
	_h5part_errno = H5PART_SUCCESS;
gsell's avatar
gsell committed
141 142 143 144 145 146 147 148
	H5PartFile *f = NULL;

	f = (H5PartFile*) malloc( sizeof (H5PartFile) );
	if( f == NULL ) {
		HANDLE_H5PART_NOMEM_ERR;
		goto error_cleanup;
	}
	memset (f, 0, sizeof (H5PartFile));
gsell's avatar
gsell committed
149

150 151 152
	f->flags = flags;
	f->throttle = 0;

gsell's avatar
gsell committed
153 154 155 156 157 158 159
	f->groupname_step = strdup ( H5PART_GROUPNAME_STEP );
	if( f->groupname_step == NULL ) {
		HANDLE_H5PART_NOMEM_ERR;
		goto error_cleanup;
	}
	f->stepno_width = 0;

160 161 162 163 164 165 166
	f->xfer_prop = f->create_prop = H5P_DEFAULT;

	f->access_prop = H5Pcreate (H5P_FILE_ACCESS);
	if (f->access_prop < 0) {
		HANDLE_H5P_CREATE_ERR;
		goto error_cleanup;
	}
gsell's avatar
gsell committed
167

gsell's avatar
gsell committed
168
	if ( f_parallel ) {
gsell's avatar
gsell committed
169
#ifdef PARALLEL_IO
gsell's avatar
gsell committed
170
		MPI_Info info = MPI_INFO_NULL;
171

gsell's avatar
gsell committed
172 173 174 175 176 177 178 179
		if (MPI_Comm_size (comm, &f->nprocs) != MPI_SUCCESS) {
			HANDLE_MPI_COMM_SIZE_ERR;
			goto error_cleanup;
		}
		if (MPI_Comm_rank (comm, &f->myproc) != MPI_SUCCESS) {
			HANDLE_MPI_COMM_RANK_ERR;
			goto error_cleanup;
		}
gsell's avatar
gsell committed
180

gsell's avatar
gsell committed
181 182
		f->pnparticles =
		  (h5part_int64_t*) malloc (f->nprocs * sizeof (h5part_int64_t));
gsell's avatar
gsell committed
183 184 185 186
		if (f->pnparticles == NULL) {
			HANDLE_H5PART_NOMEM_ERR;
			goto error_cleanup;
		}
gsell's avatar
gsell committed
187

188 189 190 191 192 193 194 195 196
		/* select the HDF5 VFD */
		if (flags & H5PART_VFD_MPIPOSIX) {
			if (f->myproc == 0) {
				_H5Part_print_info ( "Selecting MPI-POSIX VFD" );
			}
			if (H5Pset_fapl_mpiposix ( f->access_prop, comm, 0 ) < 0) {
				HANDLE_H5P_SET_FAPL_ERR;
				goto error_cleanup;
			}
gsell's avatar
gsell committed
197
		}
198 199 200 201 202 203 204 205
		else {
			if (f->myproc == 0) {
				_H5Part_print_info ( "Selecting MPI-IO VFD" );
			}
			if (H5Pset_fapl_mpio ( f->access_prop, comm, info ) < 0) {
				HANDLE_H5P_SET_FAPL_ERR;
				goto error_cleanup;
			}
206
			if (flags & H5PART_VFD_MPIIO_IND) {
207 208 209
				if (f->myproc == 0) {
					_H5Part_print_info ( "Using independent mode" );
				}
210
			} else {
211 212 213
				if (f->myproc == 0) {
					_H5Part_print_info ( "Using collective mode" );
				}
214 215 216 217 218 219 220 221 222
				f->xfer_prop = H5Pcreate (H5P_DATASET_XFER);
				if (f->xfer_prop < 0) {
					HANDLE_H5P_CREATE_ERR;
					goto error_cleanup;
				}
				if (H5Pset_dxpl_mpio ( f->xfer_prop, H5FD_MPIO_COLLECTIVE ) < 0) {
					HANDLE_H5P_SET_DXPL_MPIO_ERR;
					goto error_cleanup;
				}
223
			}
gsell's avatar
gsell committed
224
		}
gsell's avatar
gsell committed
225

226 227 228 229 230 231 232 233 234 235 236 237 238
		if (flags & H5PART_FS_LUSTRE) {
			/* extend the btree size so that metadata pieces are
			 * close to the 1MB stripe width */
			if (f->myproc == 0) {
				_H5Part_print_info (
					"Setting HDF5 btree parameter to %d",
					H5PART_BTREE_IK );
				_H5Part_print_info (
					"Extending HDF5 btree size to %d bytes at rank 3",
					24+16*H5PART_BTREE_IK+40*(2*H5PART_BTREE_IK + 1) );
			}
			f->create_prop = H5Pcreate(H5P_FILE_CREATE);
			H5Pset_istore_k (f->create_prop, H5PART_BTREE_IK);
239
#if H5_VERS_MAJOR == 1 && H5_VERS_MINOR == 8
240 241 242 243 244 245 246 247 248 249 250
			/* defer metadata cache flushing until file close */
			H5AC_cache_config_t cache_config;
			cache_config.version = H5AC__CURR_CACHE_CONFIG_VERSION;
			H5Pget_mdc_config (f->access_prop, &cache_config);
			cache_config.set_initial_size = 1;
			cache_config.initial_size = 16 * 1024 * 1024;
			cache_config.evictions_enabled = 0;
			cache_config.incr_mode = H5C_incr__off;
			cache_config.flash_incr_mode = H5C_flash_incr__off;
			cache_config.decr_mode = H5C_decr__off;
			H5Pset_mdc_config (f->access_prop, &cache_config);
251 252 253 254
#elif
			_H5Part_print_info (
					"Unable to defer metadata write: need HDF5 1.8");
#endif
255
		}
256

257
		f->comm = comm;
gsell's avatar
gsell committed
258
#endif
gsell's avatar
gsell committed
259
	} else {
gsell's avatar
gsell committed
260
		f->comm = 0;
gsell's avatar
gsell committed
261 262
		f->nprocs = 1;
		f->myproc = 0;
gsell's avatar
gsell committed
263 264
		f->pnparticles = 
			(h5part_int64_t*) malloc (f->nprocs * sizeof (h5part_int64_t));
gsell's avatar
gsell committed
265
	}
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291

	if ( align != 0 ) {
		if (f->myproc == 0) {
			_H5Part_print_info ( "Setting HDF5 alignment to %ld bytes", align );
		}
		if (H5Pset_alignment ( f->access_prop, 0, align ) < 0) {
			HANDLE_H5P_SET_FAPL_ERR;
			goto error_cleanup;
		}
		if (f->myproc == 0) {
			_H5Part_print_info ( "Setting HDF5 meta block to %ld bytes", align );
		}
		if (H5Pset_meta_block_size ( f->access_prop, align ) < 0) {
			HANDLE_H5P_SET_FAPL_ERR;
			goto error_cleanup;
		}
		/*if (f->myproc == 0) {
			_H5Part_print_info ( "Setting HDF5 sieve buffer to %ld bytes", align );
		}
		if (H5Pset_sieve_buf_size ( f->access_prop, align ) < 0) {
			HANDLE_H5P_SET_FAPL_ERR;
			goto error_cleanup;
		}*/
	}

	if ( flags & H5PART_READ ) {
gsell's avatar
gsell committed
292 293
		f->file = H5Fopen (filename, H5F_ACC_RDONLY, f->access_prop);
	}
294
	else if ( flags & H5PART_WRITE ){
gsell's avatar
gsell committed
295 296
		f->file = H5Fcreate (filename, H5F_ACC_TRUNC, f->create_prop,
				     f->access_prop);
gsell's avatar
gsell committed
297
		f->empty = 1;
gsell's avatar
gsell committed
298
	}
299
	else if ( flags & H5PART_APPEND ) {
gsell's avatar
gsell committed
300 301 302 303
		int fd = open (filename, O_RDONLY, 0);
		if ( (fd == -1) && (errno == ENOENT) ) {
			f->file = H5Fcreate(filename, H5F_ACC_TRUNC,
					    f->create_prop, f->access_prop);
gsell's avatar
gsell committed
304
			f->empty = 1;
gsell's avatar
gsell committed
305 306 307 308 309 310 311 312 313 314
		}
		else if (fd != -1) {
			close (fd);
			f->file = H5Fopen (filename, H5F_ACC_RDWR,
					   f->access_prop);
			/*
			  The following function call returns an error,
			  if f->file < 0. But we can safely ignore this.
			*/
			f->timestep = _H5Part_get_num_objects_matching_pattern(
gsell's avatar
gsell committed
315
				f->file, "/", H5G_GROUP, f->groupname_step );
gsell's avatar
gsell committed
316 317 318 319 320 321 322 323 324 325 326 327 328
			if ( f->timestep < 0 ) goto error_cleanup;
		}
	}
	else {
		HANDLE_H5PART_FILE_ACCESS_TYPE_ERR ( flags );
		goto error_cleanup;
	}

	if (f->file < 0) {
		HANDLE_H5F_OPEN_ERR ( filename, flags );
		goto error_cleanup;
	}
	f->mode = flags;
gsell's avatar
gsell committed
329
	f->timegroup = -1;
gsell's avatar
gsell committed
330 331 332 333 334 335
	f->shape = 0;
	f->diskshape = H5S_ALL;
	f->memshape = H5S_ALL;
	f->viewstart = -1;
	f->viewend = -1;

gsell's avatar
gsell committed
336 337 338 339 340 341
	_H5Part_print_debug (
		"Proc[%d]: Opened file \"%s\" val=%lld",
		f->myproc,
		filename,
		(long long)(size_t)f );

gsell's avatar
gsell committed
342 343 344 345
	return f;

 error_cleanup:
	if (f != NULL ) {
gsell's avatar
gsell committed
346 347 348
		if (f->groupname_step) {
			free (f->groupname_step);
		}
gsell's avatar
gsell committed
349 350 351 352 353 354 355 356
		if (f->pnparticles != NULL) {
			free (f->pnparticles);
		}
		free (f);
	}
	return NULL;
}

gsell's avatar
gsell committed
357
#ifdef PARALLEL_IO
gsell's avatar
gsell committed
358
/*!
gsell's avatar
gsell committed
359
  \ingroup h5part_openclose
gsell's avatar
gsell committed
360

gsell's avatar
gsell committed
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
  Opens file with specified filename. 

  If you open with flag \c H5PART_WRITE, it will truncate any
  file with the specified filename and start writing to it. If 
  you open with \c H5PART_APPEND, then you can append new timesteps.
  If you open with \c H5PART_READ, then it will open the file
  readonly.

  The typical extension for these files is \c .h5.
  
  H5PartFile should be treated as an essentially opaque
  datastructure.  It acts as the file handle, but internally
  it maintains several key state variables associated with 
  the file.

  \return	File handle or \c NULL
 */
H5PartFile*
gsell's avatar
gsell committed
379
H5PartOpenFileParallel (
gsell's avatar
gsell committed
380
	const char *filename,	/*!< [in] The name of the data file to open. */
381
	const char flags,	/*!< [in] The access mode for the file. */
gsell's avatar
gsell committed
382
	MPI_Comm comm		/*!< [in] MPI communicator */
383
	) {
384
	INIT
385 386 387
	SET_FNAME ( "H5PartOpenFileParallel" );

	int f_parallel = 1;	/* parallel i/o */
388
	h5part_int64_t align = 0; /* no alignment tuning */
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403

	return _H5Part_open_file ( filename, flags, comm, f_parallel, align );
}

/*!
  \ingroup h5part_openclose

  Opens file with specified filename, and also specifices an alignment
  value used for HDF5 tuning parameters.

  \return	File handle or \c NULL
 */
H5PartFile*
H5PartOpenFileParallelAlign (
	const char *filename,	/*!< [in] The name of the data file to open. */
404
	const char flags,	/*!< [in] The access mode for the file. */
405 406 407
	MPI_Comm comm,		/*!< [in] MPI communicator */
	h5part_int64_t align	/*!< [in] Alignment size in bytes. */
	) {
408
	INIT
409
	SET_FNAME ( "H5PartOpenFileParallelAlign" );
410

gsell's avatar
gsell committed
411
	int f_parallel = 1;	/* parallel i/o */
gsell's avatar
gsell committed
412

413
	return _H5Part_open_file ( filename, flags, comm, f_parallel, align );
gsell's avatar
gsell committed
414 415
}
#endif
gsell's avatar
gsell committed
416

gsell's avatar
gsell committed
417 418
/*!
  \ingroup  h5part_openclose
gsell's avatar
gsell committed
419

gsell's avatar
gsell committed
420
  Opens file with specified filename. 
gsell's avatar
gsell committed
421

gsell's avatar
gsell committed
422 423 424 425 426
  If you open with flag \c H5PART_WRITE, it will truncate any
  file with the specified filename and start writing to it. If 
  you open with \c H5PART_APPEND, then you can append new timesteps.
  If you open with \c H5PART_READ, then it will open the file
  readonly.
gsell's avatar
gsell committed
427

gsell's avatar
gsell committed
428 429 430 431 432 433
  The typical extension for these files is \c .h5.
  
  H5PartFile should be treated as an essentially opaque
  datastructure.  It acts as the file handle, but internally
  it maintains several key state variables associated with 
  the file.
gsell's avatar
gsell committed
434

gsell's avatar
gsell committed
435 436
  \return	File handle or \c NULL
 */
gsell's avatar
gsell committed
437

gsell's avatar
gsell committed
438 439 440
H5PartFile*
H5PartOpenFile (
	const char *filename,	/*!< [in] The name of the data file to open. */
441
	const char flags	/*!< [in] The access mode for the file. */
gsell's avatar
gsell committed
442
	) {
443
	INIT
gsell's avatar
gsell committed
444
	SET_FNAME ( "H5PartOpenFile" );
gsell's avatar
gsell committed
445

gsell's avatar
gsell committed
446 447
	MPI_Comm comm = 0;	/* dummy */
	int f_parallel = 0;	/* serial open */
448
	int align = 0;		/* no tuning parameters */
gsell's avatar
gsell committed
449

450 451 452 453 454 455 456 457 458 459 460 461 462 463
	return _H5Part_open_file ( filename, flags, comm, f_parallel, align );
}

/*!
  \ingroup h5part_openclose

  Opens file with specified filename, and also specifices an alignment
  value used for HDF5 tuning parameters.

  \return	File handle or \c NULL
 */
H5PartFile*
H5PartOpenFileAlign (
	const char *filename,	/*!< [in] The name of the data file to open. */
464
	const char flags,	/*!< [in] The access mode for the file. */
465 466
	h5part_int64_t align	/*!< [in] Alignment size in bytes. */
) {
467 468
	INIT
	SET_FNAME ( "H5PartOpenFileAlign" );
469 470 471 472 473

	MPI_Comm comm = 0;	/* dummy */
	int f_parallel = 0;	/* serial open */

	return _H5Part_open_file ( filename, flags, comm, f_parallel, align );
gsell's avatar
gsell committed
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
}

/*!
  Checks if a file was successfully opened.

  \return	\c H5PART_SUCCESS or error code
 */
static h5part_int64_t
_file_is_valid (
	const H5PartFile *f	/*!< filehandle  to check validity of */
	) {

	if( f == NULL )
		return H5PART_ERR_BADFD;
	else if(f->file > 0)
		return H5PART_SUCCESS;
	else
		return H5PART_ERR_BADFD;
}

/*!
gsell's avatar
gsell committed
495 496
  \ingroup h5part_openclose

gsell's avatar
gsell committed
497 498 499 500 501 502 503 504 505 506 507
  Closes an open file.

  \return	\c H5PART_SUCCESS or error code
*/
h5part_int64_t
H5PartCloseFile (
	H5PartFile *f		/*!< [in] filehandle of the file to close */
	) {

	SET_FNAME ( "H5PartCloseFile" );
	herr_t r = 0;
gsell's avatar
gsell committed
508
	_h5part_errno = H5PART_SUCCESS;
gsell's avatar
gsell committed
509 510 511 512 513 514 515 516 517

	CHECK_FILEHANDLE ( f );

	if ( f->block && f->close_block ) {
		(*f->close_block) ( f );
		f->block = NULL;
		f->close_block = NULL;
	}

518 519 520 521 522 523 524 525
#ifdef PARALLEL_IO
	if ( f->multiblock && f->close_multiblock ) {
		(*f->close_multiblock) ( f );
		f->multiblock = NULL;
		f->close_multiblock = NULL;
	}
#endif

gsell's avatar
gsell committed
526 527 528 529 530
	if( f->shape > 0 ) {
		r = H5Sclose( f->shape );
		if ( r < 0 ) HANDLE_H5S_CLOSE_ERR;
		f->shape = 0;
	}
gsell's avatar
gsell committed
531
	if( f->timegroup >= 0 ) {
gsell's avatar
gsell committed
532 533
		r = H5Gclose( f->timegroup );
		if ( r < 0 ) HANDLE_H5G_CLOSE_ERR;
gsell's avatar
gsell committed
534
		f->timegroup = -1;
gsell's avatar
gsell committed
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
	}
	if( f->diskshape != H5S_ALL ) {
		r = H5Sclose( f->diskshape );
		if ( r < 0 ) HANDLE_H5S_CLOSE_ERR;
		f->diskshape = 0;
	}
	if( f->xfer_prop != H5P_DEFAULT ) {
		r = H5Pclose( f->xfer_prop );
		if ( r < 0 ) HANDLE_H5P_CLOSE_ERR ( "f->xfer_prop" );
		f->xfer_prop = H5P_DEFAULT;
	}
	if( f->access_prop != H5P_DEFAULT ) {
		r = H5Pclose( f->access_prop );
		if ( r < 0 ) HANDLE_H5P_CLOSE_ERR ( "f->access_prop" );
		f->access_prop = H5P_DEFAULT;
	}  
	if( f->create_prop != H5P_DEFAULT ) {
		r = H5Pclose( f->create_prop );
		if ( r < 0 ) HANDLE_H5P_CLOSE_ERR ( "f->create_prop" );
		f->create_prop = H5P_DEFAULT;
	}
	if ( f->file ) {
		r = H5Fclose( f->file );
		if ( r < 0 ) HANDLE_H5F_CLOSE_ERR;
		f->file = 0;
	}
gsell's avatar
gsell committed
561 562 563
	if (f->groupname_step) {
		free (f->groupname_step);
	}
gsell's avatar
gsell committed
564 565 566 567 568
	if( f->pnparticles ) {
		free( f->pnparticles );
	}
	free( f );

gsell's avatar
gsell committed
569
	return _h5part_errno;
gsell's avatar
gsell committed
570 571 572
}

/*============== File Writing Functions ==================== */
gsell's avatar
gsell committed
573

gsell's avatar
gsell committed
574 575 576 577 578 579 580 581 582 583
h5part_int64_t
H5PartDefineStepName (
	H5PartFile *f,
	const char *name,
	const h5part_int64_t width
	) {
	f->groupname_step = strdup ( name );
	if( f->groupname_step == NULL ) {
		return HANDLE_H5PART_NOMEM_ERR;
	}
gsell's avatar
gsell committed
584
	f->stepno_width = (int)width;
gsell's avatar
gsell committed
585 586 587 588
	
	return H5PART_SUCCESS;
}

gsell's avatar
gsell committed
589
/*!
gsell's avatar
gsell committed
590 591
  \ingroup h5part_write

gsell's avatar
gsell committed
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
  Set number of particles for current time-step.

  This function's sole purpose is to prevent 
  needless creation of new HDF5 DataSpace handles if the number of 
  particles is invariant throughout the simulation. That's its only reason 
  for existence. After you call this subroutine, all subsequent 
  operations will assume this number of particles will be written.


  \return	\c H5PART_SUCCESS or error code
 */
h5part_int64_t
H5PartSetNumParticles (
	H5PartFile *f,			/*!< [in] Handle to open file */
	h5part_int64_t nparticles	/*!< [in] Number of particles */
	) {

	SET_FNAME ( "H5PartSetNumParticles" );
	int r;
#ifdef PARALLEL_IO
#ifdef HDF5V160
	hssize_t start[1];
#else
	hsize_t start[1];
#endif

	hsize_t stride[1];
	hsize_t count[1];
	hsize_t total;
	hsize_t dmax = H5S_UNLIMITED;
	register int i;
#endif

	CHECK_FILEHANDLE( f );

#ifndef PARALLEL_IO
	/*
	  if we are not using parallel-IO, there is enough information
	   to know that we can short circuit this routine.  However,
	   for parallel IO, this is going to cause problems because
	   we don't know if things have changed globally
	*/
	if ( f->nparticles == nparticles ) {
		return H5PART_SUCCESS;
	}
#endif
	if ( f->diskshape != H5S_ALL ) {
		r = H5Sclose( f->diskshape );
		if ( r < 0 ) return HANDLE_H5S_CLOSE_ERR;
		f->diskshape = H5S_ALL;
	}
	if(f->memshape != H5S_ALL) {
		r = H5Sclose( f->memshape );
		if ( r < 0 ) return HANDLE_H5S_CLOSE_ERR;
		f->memshape = H5S_ALL;
	}
	if( f->shape ) {
		r = H5Sclose(f->shape);
		if ( r < 0 ) return HANDLE_H5S_CLOSE_ERR;
	}
	f->nparticles =(hsize_t) nparticles;
#ifndef PARALLEL_IO
	f->shape = H5Screate_simple (1,
				     &(f->nparticles),
				     NULL);
	if ( f->shape < 0 ) HANDLE_H5S_CREATE_SIMPLE_ERR ( f->nparticles );

#else /* PARALLEL_IO */
	/*
	  The Gameplan here is to declare the overall size of the on-disk
	  data structure the same way we do for the serial case.  But
	  then we must have additional "DataSpace" structures to define
	  our in-memory layout of our domain-decomposed portion of the particle
	  list as well as a "selection" of a subset of the on-disk 
	  data layout that will be written in parallel to mutually exclusive
	  regions by all of the processors during a parallel I/O operation.
	  These are f->shape, f->memshape and f->diskshape respectively.
	*/

	/*
	  acquire the number of particles to be written from each MPI process
	*/
674

gsell's avatar
gsell committed
675 676 677 678 679 680 681 682
	r = MPI_Allgather (
		&nparticles, 1, MPI_LONG_LONG,
		f->pnparticles, 1, MPI_LONG_LONG,
		f->comm);
	if ( r != MPI_SUCCESS) {
		return HANDLE_MPI_ALLGATHER_ERR;
	}
	if ( f->myproc == 0 ) {
gsell's avatar
gsell committed
683
		_H5Part_print_debug ( "Particle offsets:" );
gsell's avatar
gsell committed
684
		for(i=0;i<f->nprocs;i++) 
685
			_H5Part_print_debug ( "\t[%d] np=%lld", i,
gsell's avatar
gsell committed
686
					      (long long) f->pnparticles[i] );
gsell's avatar
gsell committed
687 688 689 690 691 692 693 694 695 696
	}
	/* should I create a selection here? */

	/* compute start offsets */
	stride[0] = 1;
	start[0] = 0;
	for (i=0; i<f->myproc; i++) {
		start[0] += f->pnparticles[i];
	}
	
697
	/* compute total nparticles */
gsell's avatar
gsell committed
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
	total = 0;
	for (i=0; i < f->nprocs; i++) {
		total += f->pnparticles[i];
	}

	/* declare overall datasize */
	f->shape = H5Screate_simple (1, &total, &total);
	if (f->shape < 0) return HANDLE_H5S_CREATE_SIMPLE_ERR ( total );


	/* declare overall data size  but then will select a subset */
	f->diskshape = H5Screate_simple (1, &total, &total);
	if (f->diskshape < 0) return HANDLE_H5S_CREATE_SIMPLE_ERR ( total );

	/* declare local memory datasize */
	f->memshape = H5Screate_simple (1, &(f->nparticles), &dmax);
	if (f->memshape < 0)
		return HANDLE_H5S_CREATE_SIMPLE_ERR ( f->nparticles );

	count[0] = nparticles;
	r = H5Sselect_hyperslab (
		f->diskshape,
		H5S_SELECT_SET,
		start,
		stride,
		count, NULL );
	if ( r < 0 ) return HANDLE_H5S_SELECT_HYPERSLAB_ERR;

	if ( f->timegroup < 0 ) {
gsell's avatar
gsell committed
727
		r = _H5Part_set_step ( f, 0 );
gsell's avatar
gsell committed
728
		if ( r < 0 ) return r;
gsell's avatar
gsell committed
729 730 731 732 733 734 735 736 737
		
	}
#endif
	return H5PART_SUCCESS;
}

static h5part_int64_t
_write_data (
	H5PartFile *f,		/*!< IN: Handle to open file */
gsell's avatar
gsell committed
738 739 740
	const char *name,	/*!< IN: Name to associate array with */
	const void *array,	/*!< IN: Array to commit to disk */
	const hid_t type	/*!< IN: Type of data */
gsell's avatar
gsell committed
741 742 743 744
	) {
	herr_t herr;
	hid_t dataset_id;

gsell's avatar
gsell committed
745
	_H5Part_print_debug ( "Create a dataset[%s] mounted on the "
gsell's avatar
gsell committed
746
			      "timestep %lld",
gsell's avatar
gsell committed
747
			      name, (long long)f->timestep );
gsell's avatar
gsell committed
748

749
#if H5_VERS_MAJOR == 1 && H5_VERS_MINOR == 8
750 751 752
	htri_t exists = H5Lexists ( f->timegroup, name, H5P_DEFAULT );
	if ( exists > 0 ) return HANDLE_H5D_EXISTS_ERR ( name, f->timestep );

753 754 755 756 757 758 759 760 761
	dataset_id = H5Dcreate2 ( 
		f->timegroup,
		name,
		type,
		f->shape,
		H5P_DEFAULT,
		H5P_DEFAULT,
		H5P_DEFAULT );
#else
gsell's avatar
gsell committed
762 763 764 765 766 767
	dataset_id = H5Dcreate ( 
		f->timegroup,
		name,
		type,
		f->shape,
		H5P_DEFAULT );
768
#endif
gsell's avatar
gsell committed
769 770 771
	if ( dataset_id < 0 )
		return HANDLE_H5D_CREATE_ERR ( name, f->timestep );

772
#if PARALLEL_IO
773 774
	herr = _H5Part_start_throttle( f );
	if (herr < 0) return herr;
775
#endif
776

777 778 779 780 781 782 783 784
	herr = H5Dwrite (
		dataset_id,
		type,
		f->memshape,
		f->diskshape,
		f->xfer_prop,
		array );

785
#if PARALLEL_IO
786 787
	herr = _H5Part_end_throttle( f );
	if (herr < 0) return herr;
788
#endif
789

gsell's avatar
gsell committed
790 791 792 793 794
	if ( herr < 0 ) return HANDLE_H5D_WRITE_ERR ( name, f->timestep );

	herr = H5Dclose ( dataset_id );
	if ( herr < 0 ) return HANDLE_H5D_CLOSE_ERR;

gsell's avatar
gsell committed
795 796
	f->empty = 0;

gsell's avatar
gsell committed
797 798 799 800
	return H5PART_SUCCESS;
}

/*!
gsell's avatar
gsell committed
801 802
  \ingroup h5part_write

gsell's avatar
gsell committed
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
  Write array of 64 bit floating point data to file.

  After setting the number of particles with \c H5PartSetNumParticles() and
  the current timestep using \c H5PartSetStep(), you can start writing datasets
  into the file. Each dataset has a name associated with it (chosen by the
  user) in order to facilitate later retrieval. The name of the dataset is
  specified in the parameter \c name, which must be a null-terminated string.

  There are no restrictions on naming of datasets, but it is useful to arrive
  at some common naming convention when sharing data with other groups.

  The writing routines also implicitly store the datatype of the array so that
  the array can be reconstructed properly on other systems with incompatible
  type representations.

  All data that is written after setting the timestep is associated with that
  timestep. While the number of particles can change for each timestep, you
  cannot change the number of particles in the middle of a given timestep.

  The data is committed to disk before the routine returns.

  \return	\c H5PART_SUCCESS or error code
 */
h5part_int64_t
H5PartWriteDataFloat64 (
	H5PartFile *f,		/*!< [in] Handle to open file */
gsell's avatar
gsell committed
829 830
	const char *name,	/*!< [in] Name to associate array with */
	const h5part_float64_t *array	/*!< [in] Array to commit to disk */
gsell's avatar
gsell committed
831 832 833
	) {

	SET_FNAME ( "H5PartWriteDataFloat64" );
gsell's avatar
gsell committed
834
	h5part_int64_t herr;
gsell's avatar
gsell committed
835 836 837 838 839 840 841 842 843 844 845

	CHECK_FILEHANDLE ( f );
	CHECK_WRITABLE_MODE( f );
	CHECK_TIMEGROUP( f );

	herr = _write_data ( f, name, (void*)array, H5T_NATIVE_DOUBLE );
	if ( herr < 0 ) return herr;

	return H5PART_SUCCESS;
}

846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
/*!
  \ingroup h5part_write

  Write array of 32 bit floating point data to file.

  After setting the number of particles with \c H5PartSetNumParticles() and
  the current timestep using \c H5PartSetStep(), you can start writing datasets
  into the file. Each dataset has a name associated with it (chosen by the
  user) in order to facilitate later retrieval. The name of the dataset is
  specified in the parameter \c name, which must be a null-terminated string.

  There are no restrictions on naming of datasets, but it is useful to arrive
  at some common naming convention when sharing data with other groups.

  The writing routines also implicitly store the datatype of the array so that
  the array can be reconstructed properly on other systems with incompatible
  type representations.

  All data that is written after setting the timestep is associated with that
  timestep. While the number of particles can change for each timestep, you
  cannot change the number of particles in the middle of a given timestep.

  The data is committed to disk before the routine returns.

  \return	\c H5PART_SUCCESS or error code
 */
h5part_int64_t
H5PartWriteDataFloat32 (
	H5PartFile *f,		/*!< [in] Handle to open file */
	const char *name,	/*!< [in] Name to associate array with */
	const h5part_float32_t *array	/*!< [in] Array to commit to disk */
	) {

	SET_FNAME ( "H5PartWriteDataFloat32" );
	h5part_int64_t herr;

	CHECK_FILEHANDLE ( f );
	CHECK_WRITABLE_MODE( f );
	CHECK_TIMEGROUP( f );

	herr = _write_data ( f, name, (void*)array, H5T_NATIVE_FLOAT );
	if ( herr < 0 ) return herr;

	return H5PART_SUCCESS;
}

gsell's avatar
gsell committed
892
/*!
gsell's avatar
gsell committed
893 894
  \ingroup h5part_write

gsell's avatar
gsell committed
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
  Write array of 64 bit integer data to file.

  After setting the number of particles with \c H5PartSetNumParticles() and
  the current timestep using \c H5PartSetStep(), you can start writing datasets
  into the file. Each dataset has a name associated with it (chosen by the
  user) in order to facilitate later retrieval. The name of the dataset is
  specified in the parameter \c name, which must be a null-terminated string.

  There are no restrictions on naming of datasets, but it is useful to arrive
  at some common naming convention when sharing data with other groups.

  The writing routines also implicitly store the datatype of the array so that
  the array can be reconstructed properly on other systems with incompatible
  type representations.

  All data that is written after setting the timestep is associated with that
  timestep. While the number of particles can change for each timestep, you
  cannot change the number of particles in the middle of a given timestep.

  The data is committed to disk before the routine returns.

  \return	\c H5PART_SUCCESS or error code
 */
h5part_int64_t
H5PartWriteDataInt64 (
	H5PartFile *f,		/*!< [in] Handle to open file */
gsell's avatar
gsell committed
921 922
	const char *name,	/*!< [in] Name to associate array with */
	const h5part_int64_t *array	/*!< [in] Array to commit to disk */
gsell's avatar
gsell committed
923 924
	) {

925
	SET_FNAME ( "H5PartWriteDataInt64" );
gsell's avatar
gsell committed
926

gsell's avatar
gsell committed
927
	h5part_int64_t herr;
gsell's avatar
gsell committed
928 929 930 931 932 933 934 935 936 937 938

	CHECK_FILEHANDLE ( f );
	CHECK_WRITABLE_MODE( f );
	CHECK_TIMEGROUP( f );

	herr = _write_data ( f, name, (void*)array, H5T_NATIVE_INT64 );
	if ( herr < 0 ) return herr;

	return H5PART_SUCCESS;
}

939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
/*!
  \ingroup h5part_write

  Write array of 32 bit integer data to file.

  After setting the number of particles with \c H5PartSetNumParticles() and
  the current timestep using \c H5PartSetStep(), you can start writing datasets
  into the file. Each dataset has a name associated with it (chosen by the
  user) in order to facilitate later retrieval. The name of the dataset is
  specified in the parameter \c name, which must be a null-terminated string.

  There are no restrictions on naming of datasets, but it is useful to arrive
  at some common naming convention when sharing data with other groups.

  The writing routines also implicitly store the datatype of the array so that
  the array can be reconstructed properly on other systems with incompatible
  type representations.

  All data that is written after setting the timestep is associated with that
  timestep. While the number of particles can change for each timestep, you
  cannot change the number of particles in the middle of a given timestep.

  The data is committed to disk before the routine returns.

  \return	\c H5PART_SUCCESS or error code
 */
h5part_int64_t
H5PartWriteDataInt32 (
	H5PartFile *f,		/*!< [in] Handle to open file */
	const char *name,	/*!< [in] Name to associate array with */
	const h5part_int32_t *array	/*!< [in] Array to commit to disk */
	) {

	SET_FNAME ( "H5PartWriteDataInt32" );

	h5part_int64_t herr;

	CHECK_FILEHANDLE ( f );
	CHECK_WRITABLE_MODE( f );
	CHECK_TIMEGROUP( f );

	herr = _write_data ( f, name, (void*)array, H5T_NATIVE_INT32 );
	if ( herr < 0 ) return herr;

	return H5PART_SUCCESS;
}

gsell's avatar
gsell committed
986 987 988
/********************** reading and writing attribute ************************/

/********************** private functions to handle attributes ***************/
gsell's avatar
gsell committed
989 990 991 992 993 994

/*!
  \ingroup h5partkernel
  @{
*/

995 996 997 998 999 1000 1001 1002 1003 1004 1005
hid_t
_H5Part_make_string_type(
	int size
	) {
	hid_t stype = H5Tcopy(H5T_C_S1);
	if (stype < 0) return HANDLE_H5T_STRING_ERR;
	herr_t herr = H5Tset_size(stype,size);
	if (herr < 0) return HANDLE_H5T_STRING_ERR;
	return stype;
}

gsell's avatar
gsell committed
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
/*!
   Normalize HDF5 type
*/
hid_t
_H5Part_normalize_h5_type (
	hid_t type
	) {
	H5T_class_t tclass = H5Tget_class ( type );
	int size = H5Tget_size ( type );

	switch ( tclass ){
	case H5T_INTEGER:
1018 1019 1020 1021 1022 1023 1024
	  if ( size==8 ) {
	    return H5T_NATIVE_INT64;
	  }
	  else if ( size==1 ) {
	    return H5T_NATIVE_CHAR;
	  }
	  break;
gsell's avatar
gsell committed
1025
	case H5T_FLOAT:
1026 1027 1028 1029 1030 1031 1032
	  if ( size==8 ) {
	    return H5T_NATIVE_DOUBLE;
	  }
	  else if ( size==4 ) {
	    return H5T_NATIVE_FLOAT;
	  }
	  break;
1033 1034
	case H5T_STRING:
	  return _H5Part_make_string_type(size);
gsell's avatar
gsell committed
1035
	default:
1036
	  ; /* NOP */
gsell's avatar
gsell committed
1037
	}
gsell's avatar
gsell committed
1038
	_H5Part_print_warn ( "Unknown type %d", (int)type );
1039
	
gsell's avatar
gsell committed
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
	return -1;
}

h5part_int64_t
_H5Part_read_attrib (
	hid_t id,
	const char *attrib_name,
	void *attrib_value
	) {

	herr_t herr;
	hid_t attrib_id;
	hid_t space_id;
	hid_t type_id;
	hid_t mytype;
	hsize_t nelem;

1057 1058
#if H5_VERS_MAJOR == 1 && H5_VERS_MINOR == 8
	if (! H5Aexists ( id, attrib_name )) {
1059
		_H5Part_print_warn ( "Attribute '%s' does not exist!", attrib_name );
1060 1061 1062
	}
	attrib_id = H5Aopen ( id, attrib_name, H5P_DEFAULT );
#else
gsell's avatar
gsell committed
1063
	attrib_id = H5Aopen_name ( id, attrib_name );
1064
#endif
gsell's avatar
gsell committed
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
	if ( attrib_id <= 0 ) return HANDLE_H5A_OPEN_NAME_ERR( attrib_name );

	mytype = H5Aget_type ( attrib_id );
	if ( mytype < 0 ) return HANDLE_H5A_GET_TYPE_ERR;

	space_id = H5Aget_space ( attrib_id );
	if ( space_id < 0 ) return HANDLE_H5A_GET_SPACE_ERR;

	nelem = H5Sget_simple_extent_npoints ( space_id );
	if ( nelem < 0 ) return HANDLE_H5S_GET_SIMPLE_EXTENT_NPOINTS_ERR;

	type_id = _H5Part_normalize_h5_type ( mytype );

	herr = H5Aread (attrib_id, type_id, attrib_value );
	if ( herr < 0 ) return HANDLE_H5A_READ_ERR;

	herr = H5Sclose ( space_id );
	if ( herr < 0 ) return HANDLE_H5S_CLOSE_ERR;

	herr = H5Tclose ( mytype );
	if ( herr < 0 ) return HANDLE_H5T_CLOSE_ERR;

	herr = H5Aclose ( attrib_id );
	if ( herr < 0 ) return HANDLE_H5A_CLOSE_ERR;

	return H5PART_SUCCESS;
}

h5part_int64_t
_H5Part_write_attrib (
	hid_t id,
	const char *attrib_name,
	const hid_t attrib_type,
	const void *attrib_value,
	const hsize_t attrib_nelem
	) {

	herr_t herr;
	hid_t space_id;
	hid_t attrib_id;
1105 1106 1107
	hid_t type = attrib_type;

	if (type == H5T_STRING) {
1108
		type = _H5Part_make_string_type(attrib_nelem);
1109 1110 1111 1112 1113 1114 1115 1116 1117
		space_id = H5Screate (H5S_SCALAR);
		if ( space_id < 0 )
			return HANDLE_H5S_CREATE_SCALAR_ERR;
	}
	else {
		space_id = H5Screate_simple (1, &attrib_nelem, NULL);
		if ( space_id < 0 )
			return HANDLE_H5S_CREATE_SIMPLE_ERR ( attrib_nelem );
	}
gsell's avatar
gsell committed
1118

1119 1120 1121 1122
#if H5_VERS_MAJOR == 1 && H5_VERS_MINOR == 8
	attrib_id = H5Acreate2 ( 
		id,
		attrib_name,
1123
		type,
1124 1125 1126 1127
		space_id,
		H5P_DEFAULT,
		H5P_DEFAULT );
#else
gsell's avatar
gsell committed
1128 1129 1130
	attrib_id = H5Acreate ( 
		id,
		attrib_name,
1131
		type,
gsell's avatar
gsell committed
1132 1133
		space_id,
		H5P_DEFAULT );
1134
#endif
gsell's avatar
gsell committed
1135 1136
	if ( attrib_id < 0 ) return HANDLE_H5A_CREATE_ERR ( attrib_name );

1137
	herr = H5Awrite ( attrib_id, type, attrib_value);
gsell's avatar
gsell committed
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
	if ( herr < 0 ) return HANDLE_H5A_WRITE_ERR ( attrib_name );

	herr = H5Aclose ( attrib_id );
	if ( herr < 0 ) return HANDLE_H5A_CLOSE_ERR;

	herr = H5Sclose ( space_id );
	if ( herr < 0 ) return HANDLE_H5S_CLOSE_ERR;

	return H5PART_SUCCESS;
}

h5part_int64_t
_H5Part_get_attrib_info (
	hid_t id,
	const h5part_int64_t attrib_idx,
	char *attrib_name,
	const h5part_int64_t len_attrib_name,
	h5part_int64_t *attrib_type,
	h5part_int64_t *attrib_nelem
	) {

	herr_t herr;
	hid_t attrib_id;
	hid_t mytype;
	hid_t space_id;

gsell's avatar
gsell committed
1164
	attrib_id = H5Aopen_idx ( id, (unsigned int)attrib_idx );
gsell's avatar
gsell committed
1165 1166 1167
	if ( attrib_id < 0 ) return HANDLE_H5A_OPEN_IDX_ERR ( attrib_idx );

	if ( attrib_nelem ) {
gsell's avatar
gsell committed
1168 1169 1170
		space_id =  H5Aget_space ( attrib_id );
		if ( space_id < 0 ) return HANDLE_H5A_GET_SPACE_ERR;

gsell's avatar
gsell committed
1171 1172 1173
		*attrib_nelem = H5Sget_simple_extent_npoints ( space_id );
		if ( *attrib_nelem < 0 )
			return HANDLE_H5S_GET_SIMPLE_EXTENT_NPOINTS_ERR;
gsell's avatar
gsell committed
1174 1175 1176

		herr = H5Sclose ( space_id );
		if ( herr < 0 ) return HANDLE_H5S_CLOSE_ERR;
gsell's avatar
gsell committed
1177 1178 1179 1180
	}
	if ( attrib_name ) {
		herr = H5Aget_name (
			attrib_id,
gsell's avatar
gsell committed
1181
			(size_t)len_attrib_name,
gsell's avatar
gsell committed
1182 1183 1184 1185
			attrib_name );
		if ( herr < 0 ) return HANDLE_H5A_GET_NAME_ERR;
	}
	if ( attrib_type ) {
gsell's avatar
gsell committed
1186 1187
		mytype = H5Aget_type ( attrib_id );
		if ( mytype < 0 ) return HANDLE_H5A_GET_TYPE_ERR;
gsell's avatar
gsell committed
1188

gsell's avatar
gsell committed
1189
		*attrib_type = _H5Part_normalize_h5_type ( mytype );
gsell's avatar
gsell committed
1190

gsell's avatar
gsell committed
1191 1192 1193
		herr = H5Tclose ( mytype );
		if ( herr < 0 ) return HANDLE_H5T_CLOSE_ERR;
	}
gsell's avatar
gsell committed
1194 1195 1196 1197 1198 1199 1200 1201 1202
	herr = H5Aclose ( attrib_id);
	if ( herr < 0 ) return HANDLE_H5A_CLOSE_ERR;

	return H5PART_SUCCESS;
}

/********************** attribute API ****************************************/

/*!
gsell's avatar
gsell committed
1203 1204
  \ingroup h5part_attrib

gsell's avatar
gsell committed
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
  Writes a string attribute bound to a file.

  This function creates a new attribute \c name with the string \c value as
  content. The attribute is bound to the file associated with the file handle 
  \c f.

  If the attribute already exists an error will be returned. There
  is currently no way to change the content of an existing attribute.

  \return	\c H5PART_SUCCESS or error code   
*/
h5part_int64_t
H5PartWriteFileAttribString (
	H5PartFile *f,		/*!< [in] Handle to open file */
	const char *attrib_name,/*!< [in] Name of attribute to create */
	const char *attrib_value/*!< [in] Value of attribute */ 
	) {

	SET_FNAME ( "H5PartWriteFileAttribString" );

	CHECK_FILEHANDLE ( f );
	CHECK_WRITABLE_MODE( f );

1228 1229 1230
#if H5_VERS_MAJOR == 1 && H5_VERS_MINOR == 8
	hid_t group_id = H5Gopen2(f->file,"/",H5P_DEFAULT);
#else
gsell's avatar
gsell committed
1231
	hid_t group_id = H5Gopen(f->file,"/");
1232
#endif
gsell's avatar
gsell committed
1233 1234 1235 1236 1237
	if ( group_id < 0 ) return HANDLE_H5G_OPEN_ERR( "/" );

	h5part_int64_t herr = _H5Part_write_attrib (
		group_id,
		attrib_name,
1238
		H5T_STRING,
gsell's avatar
gsell committed
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
		attrib_value,
		strlen ( attrib_value ) + 1 );
	if ( herr < 0 ) return herr;

	herr = H5Gclose ( group_id );
	if ( herr < 0 ) return HANDLE_H5G_CLOSE_ERR;

	return H5PART_SUCCESS;
}

/*!
gsell's avatar
gsell committed
1250 1251
  \ingroup h5part_attrib

gsell's avatar
gsell committed
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
  Writes a string attribute bound to the current time-step.

  If the attribute already exists an error will be returned. There
  is currently no way to change the content of an existing attribute.

  \return	\c H5PART_SUCCESS or error code   
*/

h5part_int64_t
H5PartWriteStepAttribString (
	H5PartFile *f,		/*!< [in] Handle to open file */
1263 1264
	const char *name,	/*!< [in] Name of attribute to create */
	const char *value	/*!< [in] Value of attribute */ 
gsell's avatar
gsell committed
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
	) {

	SET_FNAME ( "H5PartWriteStepAttribString" );

	CHECK_FILEHANDLE ( f );
	CHECK_WRITABLE_MODE( f );
	CHECK_TIMEGROUP( f );

	h5part_int64_t herr = _H5Part_write_attrib (
		f->timegroup,
1275
		name,
1276
		H5T_STRING,
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
		value,
		strlen ( value ) + 1 );
	if ( herr < 0 ) return herr;

	return H5PART_SUCCESS;
}

/*!
  \ingroup h5part_attrib

  Writes an int32 attribute bound to the current time-step.

  If the attribute already exists an error will be returned. There
  is currently no way to change the content of an existing attribute.

  \return	\c H5PART_SUCCESS or error code   
*/

h5part_int64_t
H5PartWriteStepAttribInt32 (
	H5PartFile *f,			/*!< [in] Handle to open file */
	const char *name,		/*!< [in] Name of attribute to create */
	const h5part_int32_t value	/*!< [in] Value of attribute */ 
	) {

	SET_FNAME ( "H5PartWriteStepAttribInt32" );

	CHECK_FILEHANDLE ( f );
	CHECK_WRITABLE_MODE( f );
	CHECK_TIMEGROUP( f );

	h5part_int64_t herr = _H5Part_write_attrib (
		f->timegroup,
		name,
		H5T_NATIVE_INT32,
		&value,
		1 );
	if ( herr < 0 ) return herr;

	return H5PART_SUCCESS;
}

/*!
  \ingroup h5part_attrib

  Writes a float32 attribute bound to the current time-step.

  If the attribute already exists an error will be returned. There
  is currently no way to change the content of an existing attribute.

  \return	\c H5PART_SUCCESS or error code   
*/

h5part_int64_t
H5PartWriteStepAttribFloat32 (
	H5PartFile *f,			/*!< [in] Handle to open file */
	const char *name,		/*!< [in] Name of attribute to create */
	const h5part_float32_t value	/*!< [in] Value of attribute */ 
	) {

	SET_FNAME ( "H5PartWriteStepAttribFloat32" );

	CHECK_FILEHANDLE ( f );
	CHECK_WRITABLE_MODE( f );
	CHECK_TIMEGROUP( f );

	h5part_int64_t herr = _H5Part_write_attrib (
		f->timegroup,
		name,
		H5T_NATIVE_FLOAT,
		&value,
		1 );
gsell's avatar
gsell committed
1349 1350 1351 1352 1353 1354
	if ( herr < 0 ) return herr;

	return H5PART_SUCCESS;
}

/*!
gsell's avatar
gsell committed
1355 1356
  \ingroup h5part_attrib

gsell's avatar
gsell committed
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383
  Writes a attribute bound to the current time-step.

  This function creates a new attribute \c name with the string \c value as
  content. The attribute is bound to the current time step in the file given
  by the file handle \c f.

  The value of the attribute is given the parameter \c type, which must be one
  of \c H5T_NATIVE_DOUBLE, \c H5T_NATIVE_INT64 of \c H5T_NATIVE_CHAR, the array
  \c value and the number of elements \c nelem in the array.

  If the attribute already exists an error will be returned. There
  is currently no way to change the content of an existing attribute.

  \return	\c H5PART_SUCCESS or error code   
*/

h5part_int64_t
H5PartWriteStepAttrib (
	H5PartFile *f,			/*!< [in] Handle to open file */
	const char *attrib_name,	/*!< [in] Name of attribute */
	const h5part_int64_t attrib_type,/*!< [in] Type of value. */
	const void *attrib_value,	/*!< [in] Value of attribute */ 
	const h5part_int64_t attrib_nelem/*!< [in] Number of elements */
	){

	SET_FNAME ( "H5PartWriteStepAttrib" );

gsell's avatar
gsell committed
1384
	h5part_int64_t herr;
gsell's avatar
gsell committed
1385 1386 1387 1388 1389 1390 1391 1392

	CHECK_FILEHANDLE ( f );
	CHECK_WRITABLE_MODE( f );
	CHECK_TIMEGROUP( f );

	herr = _H5Part_write_attrib (
		f->timegroup,
		attrib_name,
gsell's avatar
gsell committed
1393
		(const hid_t)attrib_type,
gsell's avatar
gsell committed
1394 1395 1396 1397 1398 1399 1400 1401
		attrib_value,
		attrib_nelem );
	if ( herr < 0 ) return herr;

	return H5PART_SUCCESS;
}

/*!
gsell's avatar
gsell committed
1402 1403
  \ingroup h5part_attrib

gsell's avatar
gsell committed
1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
  Writes a attribute bound to a file.

  This function creates a new attribute \c name with the string \c value as
  content. The attribute is bound to the file file given by the file handle
  \c f.

  The value of the attribute is given the parameter \c type, which must be one
  of H5T_NATIVE_DOUBLE, H5T_NATIVE_INT64 of H5T_NATIVE_CHAR, the array \c value
  and the number of elements \c nelem in the array.

  If the attribute already exists an error will be returned. There
  is currently no way to change the content of an existing attribute.

  \return	\c H5PART_SUCCESS or error code   
*/

h5part_int64_t
H5PartWriteFileAttrib (
	H5PartFile *f,			/*!< [in] Handle to open file */
	const char *attrib_name,	/*!< [in] Name of attribute */
	const h5part_int64_t attrib_type,/*!< [in] Type of value. */
	const void *attrib_value,	/*!< [in] Value of attribute */ 
	const h5part_int64_t attrib_nelem/*!< [in] Number of elements */
	) {

	SET_FNAME ( "H5PartWriteFileAttrib" );

gsell's avatar
gsell committed
1431
	h5part_int64_t herr;
gsell's avatar
gsell committed
1432 1433 1434 1435 1436
	hid_t group_id;

	CHECK_FILEHANDLE ( f );
	CHECK_WRITABLE_MODE ( f );

1437 1438 1439
#if H5_VERS_MAJOR == 1 && H5_VERS_MINOR == 8
	group_id = H5Gopen2(f->file,"/",H5P_DEFAULT);
#else
gsell's avatar
gsell committed
1440
	group_id = H5Gopen(f->file,"/");
1441
#endif
gsell's avatar
gsell committed
1442 1443
	if ( group_id < 0 ) return HANDLE_H5G_OPEN_ERR( "/" );

gsell's avatar
gsell committed
1444
	herr = _H5Part_write_attrib (
gsell's avatar
gsell committed
1445 1446
		group_id,
		attrib_name,
gsell's avatar
gsell committed
1447
		(const hid_t)attrib_type,
gsell's avatar
gsell committed
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
		attrib_value,
		attrib_nelem );
	if ( herr < 0 ) return herr;

	herr = H5Gclose ( group_id );
	if ( herr < 0 ) return HANDLE_H5G_CLOSE_ERR;

	return H5PART_SUCCESS;
}

/*!
gsell's avatar
gsell committed
1459 1460
  \ingroup h5part_attrib

gsell's avatar
gsell committed
1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
  Gets the number of attributes bound to the current step.

  \return	Number of attributes bound to current time step or error code.
*/
h5part_int64_t
H5PartGetNumStepAttribs (
	H5PartFile *f			/*!< [in] Handle to open file */
	) {

	SET_FNAME ( "H5PartGetNumStepAttribs" );
	h5part_int64_t nattribs;

	CHECK_FILEHANDLE ( f );

	nattribs = H5Aget_num_attrs(f->timegroup);
	if ( nattribs < 0 ) HANDLE_H5A_GET_NUM_ATTRS_ERR;

	return nattribs;
}

/*!
gsell's avatar
gsell committed
1482 1483
  \ingroup h5part_attrib

gsell's avatar
gsell committed
1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
  Gets the number of attributes bound to the file.

  \return	Number of attributes bound to file \c f or error code.
*/
h5part_int64_t
H5PartGetNumFileAttribs (
	H5PartFile *f			/*!< [in] Handle to open file */
	) {

	SET_FNAME ( "H5PartGetNumFileAttribs" );
	herr_t herr;
	h5part_int64_t nattribs;

	CHECK_FILEHANDLE ( f );

1499 1500 1501
#if H5_VERS_MAJOR == 1 && H5_VERS_MINOR == 8
	hid_t group_id = H5Gopen2 ( f->file, "/", H5P_DEFAULT );
#else
gsell's avatar
gsell committed
1502
	hid_t group_id = H5Gopen ( f->file, "/" );
1503
#endif
gsell's avatar
gsell committed
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514
	if ( group_id < 0 ) HANDLE_H5G_OPEN_ERR ( "/" );

	nattribs = H5Aget_num_attrs ( group_id );
	if ( nattribs < 0 ) HANDLE_H5A_GET_NUM_ATTRS_ERR;

	herr = H5Gclose ( group_id );
	if ( herr < 0 ) HANDLE_H5G_CLOSE_ERR;
	return nattribs;
}

/*!
gsell's avatar
gsell committed
1515 1516
  \ingroup h5part_attrib

gsell's avatar
gsell committed
1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531
  Gets the name, type and number of elements of the step attribute
  specified by its index.

  This function can be used to retrieve all attributes bound to the
  current time-step by looping from \c 0 to the number of attribute
  minus one.  The number of attributes bound to the current
  time-step can be queried by calling the function
  \c H5PartGetNumStepAttribs().

  \return	\c H5PART_SUCCESS or error code 
*/
h5part_int64_t
H5PartGetStepAttribInfo (
	H5PartFile *f,			/*!< [in]  Handle to open file */
	const h5part_int64_t attrib_idx,/*!< [in]  Index of attribute to
1532
						   get infos about */
gsell's avatar
gsell committed
1533 1534 1535 1536 1537 1538 1539 1540
	char *attrib_name,		/*!< [out] Name of attribute */
	const h5part_int64_t len_of_attrib_name,
					/*!< [in]  length of buffer \c name */
	h5part_int64_t *attrib_type,	/*!< [out] Type of value. */
	h5part_int64_t *attrib_nelem	/*!< [out] Number of elements */
	) {
	
	SET_FNAME ( "H5PartGetStepAttribInfo" );
gsell's avatar
gsell committed
1541
	h5part_int64_t herr;
gsell's avatar
gsell committed
1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557

	CHECK_FILEHANDLE( f );

	herr = _H5Part_get_attrib_info (
		f->timegroup,
		attrib_idx,
		attrib_name,
		len_of_attrib_name,
		attrib_type,
		attrib_nelem );
	if ( herr < 0 ) return herr;

	return H5PART_SUCCESS;
}

/*!
gsell's avatar
gsell committed
1558 1559
  \ingroup h5part_attrib

gsell's avatar
gsell committed
1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574
  Gets the name, type and number of elements of the file attribute
  specified by its index.

  This function can be used to retrieve all attributes bound to the
  file \c f by looping from \c 0 to the number of attribute minus
  one.  The number of attributes bound to file \c f can be queried
  by calling the function \c H5PartGetNumFileAttribs().

  \return	\c H5PART_SUCCESS or error code 
*/

h5part_int64_t
H5PartGetFileAttribInfo (
	H5PartFile *f,			/*!< [in]  Handle to open file */
	const h5part_int64_t attrib_idx,/*!< [in]  Index of attribute to get
1575
						   infos about */
gsell's avatar
gsell committed
1576 1577 1578 1579 1580 1581 1582 1583 1584
	char *attrib_name,		/*!< [out] Name of attribute */
	const h5part_int64_t len_of_attrib_name,
					/*!< [in]  length of buffer \c name */
	h5part_int64_t *attrib_type,	/*!< [out] Type of value. */
	h5part_int64_t *attrib_nelem	/*!< [out] Number of elements */
	) {

	SET_FNAME ( "H5PartGetFileAttribInfo" );
	hid_t group_id;
gsell's avatar
gsell committed
1585
	h5part_int64_t herr;
gsell's avatar
gsell committed
1586 1587 1588

	CHECK_FILEHANDLE( f );

1589 1590 1591
#if H5_VERS_MAJOR == 1 && H5_VERS_MINOR == 8
	group_id = H5Gopen2(f->file,"/",H5P_DEFAULT);
#else
gsell's avatar
gsell committed
1592
	group_id = H5Gopen(f->file,"/");
1593
#endif
gsell's avatar
gsell committed
1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611
	if ( group_id < 0 ) return HANDLE_H5G_OPEN_ERR( "/" );

	herr = _H5Part_get_attrib_info (
		group_id,
		attrib_idx,
		attrib_name,
		len_of_attrib_name,
		attrib_type,
		attrib_nelem );
	if ( herr < 0 ) return herr;

	herr = H5Gclose ( group_id );
	if ( herr < 0 ) return herr;

	return H5PART_SUCCESS;
}

/*!
gsell's avatar
gsell committed
1612 1613
  \ingroup h5part_attrib

gsell's avatar
gsell committed
1614 1615 1616 1617 1618 1619 1620
  Reads an attribute bound to current time-step.

  \return \c H5PART_SUCCESS or error code 
*/
h5part_int64_t
H5PartReadStepAttrib (
	H5PartFile *f,			/*!< [in]  Handle to open file */
gsell's avatar
gsell committed
1621
	const char *attrib_name,	/*!< [in] Name of attribute to read */
gsell's avatar
gsell committed
1622 1623 1624 1625 1626
	void *attrib_value		/*!< [out] Value of attribute */
	) {

	SET_FNAME ( "H5PartReadStepAttrib" );

gsell's avatar
gsell committed
1627
	h5part_int64_t herr;
gsell's avatar
gsell committed
1628 1629 1630 1631 1632 1633 1634 1635 1636 1637

	CHECK_FILEHANDLE( f );

	herr = _H5Part_read_attrib ( f->timegroup, attrib_name, attrib_value );
	if ( herr < 0 ) return herr;

	return H5PART_SUCCESS;
}

/*!
gsell's avatar
gsell committed
1638 1639
  \ingroup h5part_attrib

gsell's avatar
gsell committed
1640 1641 1642 1643 1644 1645 1646
  Reads an attribute bound to file \c f.

  \return \c H5PART_SUCCESS or error code 
*/
h5part_int64_t
H5PartReadFileAttrib ( 
	H5PartFile *f,
gsell's avatar
gsell committed
1647
	const char *attrib_name,
gsell's avatar
gsell committed
1648 1649 1650 1651 1652 1653
	void *attrib_value
	) {

	SET_FNAME ( "H5PartReadFileAttrib" );

	hid_t group_id;
gsell's avatar
gsell committed
1654
	h5part_int64_t herr;
gsell's avatar
gsell committed
1655 1656 1657

	CHECK_FILEHANDLE( f );

1658 1659 1660
#if H5_VERS_MAJOR == 1 && H5_VERS_MINOR == 8
	group_id = H5Gopen2(f->file,"/",H5P_DEFAULT);
#else
gsell's avatar
gsell committed
1661
	group_id = H5Gopen(f->file,"/");
1662
#endif
gsell's avatar
gsell committed
1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686
	if ( group_id < 0 ) return HANDLE_H5G_OPEN_ERR( "/" );

	herr = _H5Part_read_attrib ( group_id, attrib_name, attrib_value );
	if ( herr < 0 ) return herr;

	herr = H5Gclose ( group_id );
	if ( herr < 0 ) return HANDLE_H5G_CLOSE_ERR;

	return H5PART_SUCCESS;
}


/*================== File Reading Routines =================*/
/*
  H5PartSetStep:


  So you use this to random-access the file for a particular timestep.
  Failure to explicitly set the timestep on each read will leave you
  stuck on the same timestep for *all* of your reads.  That is to say
  the writes auto-advance the file pointer, but the reads do not
  (they require explicit advancing by selecting a particular timestep).
*/

gsell's avatar
gsell committed
1687
h5part_int64_t
gsell's avatar
gsell committed
1688
_H5Part_set_step (
gsell's avatar
gsell committed
1689 1690 1691 1692 1693 1694
	H5PartFile *f,			/*!< [in]  Handle to open file */
	const h5part_int64_t step	/*!< [in]  Time-step to set. */
	) {

	char name[128];

1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707
	/* Work around sprintf bug on older systems */
	if (f->stepno_width == 0 && step == 0) {
	    sprintf (
		    name,
		    "%s#%0*lld",
		    f->groupname_step, 1, (long long) step );
	}
	else {
	    sprintf (
		    name,
		    "%s#%0*lld",
		    f->groupname_step, f->stepno_width, (long long) step );
	}
1708

1709
	if ( (!(f->mode & H5PART_READ)) && _H5Part_have_group ( f->file, name ) ) {
gsell's avatar
gsell committed
1710 1711 1712 1713
		return HANDLE_H5PART_STEP_EXISTS_ERR ( step );
	}

	if ( f->timegroup >= 0 ) {
1714
		herr_t herr = H5Gclose ( f->timegroup );
gsell's avatar
gsell committed
1715 1716 1717 1718 1719
		if ( herr < 0 ) return HANDLE_H5G_CLOSE_ERR;
	}
	f->timegroup = -1;
	f->timestep = step;

1720
	if( f->mode & H5PART_READ ) {
1721
		_H5Part_print_debug (
gsell's avatar
gsell committed
1722 1723 1724 1725 1726
			"Proc[%d]: Set step to #%lld for file %lld",
			f->myproc,
			(long long)step,
			(long long)(size_t) f );

1727 1728 1729
#if H5_VERS_MAJOR == 1 && H5_VERS_MINOR == 8
		f->timegroup = H5Gopen2 ( f->file, name, H5P_DEFAULT ); 
#else
gsell's avatar
gsell committed
1730
		f->timegroup = H5Gopen ( f->file, name ); 
1731
#endif
gsell's avatar
gsell committed
1732 1733 1734 1735 1736 1737 1738 1739 1740
		if ( f->timegroup < 0 ) return HANDLE_H5G_OPEN_ERR( name );
	}
	else {
		_H5Part_print_debug (
			"Proc[%d]: Create step #%lld for file %lld", 
			f->myproc,
			(long long)step,
			(long long)(size_t) f );

1741 1742 1743 1744 1745 1746 1747 1748
#if H5_VERS_MAJOR == 1 && H5_VERS_MINOR == 8
		f->timegroup = H5Gcreate2 (
			f->file,
			name,
			0,
			H5P_DEFAULT,
			H5P_DEFAULT );
#else
gsell's avatar
gsell committed
1749
		f->timegroup = H5Gcreate ( f->file, name, 0 );
1750
#endif
gsell's avatar
gsell committed
1751 1752 1753 1754 1755 1756
		if ( f->timegroup < 0 ) return HANDLE_H5G_CREATE_ERR ( name );
	}

	return H5PART_SUCCESS;
}

gsell's avatar
gsell committed
1757
/*!
gsell's avatar
gsell committed
1758 1759
  \ingroup h5part_read

gsell's avatar
gsell committed
1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775
  Set the current time-step.

  When writing data to a file the current time step must be set first
  (even if there is only one). In write-mode this function creates a new
  time-step! You are not allowed to step to an already existing time-step.
  This prevents you from overwriting existing data. Another consequence is,
  that you \b must write all data before going to the next time-step.

  In read-mode you can use this function to random-access the file for a
  particular timestep.

  \return \c H5PART_SUCCESS or error code 
*/
h5part_int64_t
H5PartSetStep (
	H5PartFile *f,			/*!< [in]  Handle to open file */
gsell's avatar
gsell committed
1776
	const h5part_int64_t step	/*!< [in]  Time-step to set. */
gsell's avatar
gsell committed
1777 1778 1779 1780 1781 1782
	) {

	SET_FNAME ( "H5PartSetStep" );

	CHECK_FILEHANDLE ( f );

gsell's avatar
gsell committed
1783
	return _H5Part_set_step ( f, step );
gsell's avatar
gsell committed
1784 1785 1786 1787
}

/********************** query file structure *********************************/

1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804
/*!
  \ingroup h5part_kernel

  Test whether a group named \c name exists at location \c id.
*/
h5part_int64_t
_H5Part_have_group (
	const hid_t id,
	const char *name
	) {
#if H5_VERS_MAJOR == 1 && H5_VERS_MINOR == 8
	return (H5Lexists( id, name, H5P_DEFAULT ) ? 1 : 0);
#else
	return (H5Gget_objinfo( id, name, 1, NULL ) >= 0 ? 1 : 0);
#endif
}

gsell's avatar
gsell committed
1805 1806
/*!
  \ingroup h5part_kernel
gsell's avatar
gsell committed
1807

gsell's avatar
gsell committed
1808 1809
  Iterator for \c H5Giterate().
*/
gsell's avatar
gsell committed
1810 1811 1812 1813 1814 1815 1816
herr_t
_H5Part_iteration_operator (
	hid_t group_id,		/*!< [in]  group id */
	const char *member_name,/*!< [in]  group name */
	void *operator_data	/*!< [in,out] data passed to the iterator */
	) {

gsell's avatar
gsell committed
1817
	struct _iter_op_data *data = (struct _iter_op_data*)operator_data;
gsell's avatar
gsell committed
1818 1819
	herr_t herr;

gsell's avatar
gsell committed
1820
	if ( data->type != H5G_UNKNOWN ) {
1821 1822
		hid_t obj_id = H5Oopen( group_id, member_name, H5P_DEFAULT );
		if ( obj_id < 0 ) return (herr_t)HANDLE_H5O_OPEN_ERR ( member_name );
1823 1824
#if H5_VERS_MAJOR == 1 && H5_VERS_MINOR == 8
                H5O_info_t objinfo;
1825
		herr = H5Oget_info ( group_id, &objinfo );
1826 1827 1828
		if ( herr < 0 ) return (herr_t)HANDLE_H5G_GET_OBJINFO_ERR ( member_name );
#else
	        H5G_stat_t objinfo;
gsell's avatar
gsell committed
1829
		herr = H5Gget_objinfo ( group_id, member_name, 1, &objinfo );
gsell's avatar
gsell committed
1830
		if ( herr < 0 ) return (herr_t)HANDLE_H5G_GET_OBJINFO_ERR ( member_name );
1831
#endif
1832
		H5Oclose( obj_id );
gsell's avatar
gsell committed
1833 1834 1835
		if ( objinfo.type != data->type )
			return 0;/* don't count, continue iteration */
	}
gsell's avatar
gsell committed
1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853

	if ( data->name && (data->stop_idx == data->count) ) {
		memset ( data->name, 0, data->len );
		strncpy ( data->name, member_name, data->len-1 );
		
		return 1;	/* stop iteration */
	}
	/*
	  count only if pattern is NULL or member name matches
	*/
	if ( !data->pattern ||
	     (strncmp (member_name, data->pattern, strlen(data->pattern)) == 0)
	      ) {
		data->count++;
	}
	return 0;		/* continue iteration */
}

gsell's avatar
gsell committed
1854 1855 1856 1857 1858
/*!
  \ingroup h5part_kernel

  Iterator for \c H5Giterate().
*/
gsell's avatar
gsell committed
1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872
h5part_int64_t
_H5Part_get_num_objects (
	hid_t group_id,
	const char *group_name,
	const hid_t type
	) {

	return _H5Part_get_num_objects_matching_pattern (
		group_id,
		group_name,
		type,
		NULL );
}

gsell's avatar
gsell committed
1873 1874 1875 1876 1877
/*!
  \ingroup h5part_kernel

  Iterator for \c H5Giterate().
*/
gsell's avatar
gsell committed
1878 1879 1880 1881 1882 1883 1884