H5Part.c 84.9 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
<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>
22
<LI> Mark Howison (NERSC/LBNL)</LI>
Kurt Stockinger's avatar
Kurt Stockinger committed
23
</UL>
gsell's avatar
gsell committed
24 25 26 27


Papers: 

Kurt Stockinger's avatar
Kurt Stockinger committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
<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>

gsell's avatar
gsell committed
43 44
*/

gsell's avatar
gsell committed
45 46

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

*/
/*!
gsell's avatar
gsell committed
51
  \ingroup h5part_c_api
52
  \defgroup h5part_open 	File Opening and Closing
gsell's avatar
gsell committed
53 54
*/
/*!
gsell's avatar
gsell committed
55
  \ingroup h5part_c_api
56
  \defgroup h5part_model	Setting up the Data Model
gsell's avatar
gsell committed
57 58
*/  
/*!
gsell's avatar
gsell committed
59
  \ingroup h5part_c_api
60
  \defgroup h5part_data		Readind and Writing Datasets
gsell's avatar
gsell committed
61 62
*/  
/*!
gsell's avatar
gsell committed
63
  \ingroup h5part_c_api
gsell's avatar
gsell committed
64 65 66
  \defgroup h5part_attrib	Reading and Writing Attributes
*/
/*!
gsell's avatar
gsell committed
67
  \ingroup h5part_c_api
gsell's avatar
gsell committed
68 69 70 71 72 73 74 75
  \defgroup h5part_errhandle	Error Handling
*/
/*!
  \internal
  \defgroup h5partkernel H5Part private functions 
*/


gsell's avatar
gsell committed
76 77 78 79 80 81 82 83
#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
84 85 86 87 88 89 90 91
#ifndef WIN32
#include <unistd.h>
#else /* WIN32 */
#include <io.h>
#define open  _open
#define close _close
#endif /* WIN32 */

gsell's avatar
gsell committed
92 93 94 95
#include "H5Part.h"
#include "H5PartPrivate.h"
#include "H5PartErrors.h"

96 97 98
/********* Global Variable Declarations *************/
h5part_error_handler	_err_handler = H5PartReportErrorHandler;

gsell's avatar
gsell committed
99 100
/********* Private Variable Declarations *************/

101
static unsigned			_debug = H5PART_VERB_ERROR;
gsell's avatar
gsell committed
102
static h5part_int64_t		_h5part_errno = H5PART_SUCCESS;
103
static char *__funcname;
gsell's avatar
gsell committed
104 105 106 107 108 109 110 111 112

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

static h5part_int64_t
_init(
	void
	);

static h5part_int64_t
113 114
_reset_view (
	H5PartFile *f
gsell's avatar
gsell committed
115 116
	);

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
#ifndef H5_USE_16_API
123 124
	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
	) {
140

gsell's avatar
gsell committed
141
	_h5part_errno = H5PART_SUCCESS;
gsell's avatar
gsell committed
142 143 144 145 146 147 148 149
	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
150

151 152
	f->flags = flags;

153 154
	/* set default step name */
	strncpy ( f->groupname_step, H5PART_GROUPNAME_STEP, H5PART_STEPNAME_LEN );
gsell's avatar
gsell committed
155 156
	f->stepno_width = 0;

157 158 159 160 161 162 163
	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
164

gsell's avatar
gsell committed
165
	if ( f_parallel ) {
gsell's avatar
gsell committed
166
#ifdef PARALLEL_IO
gsell's avatar
gsell committed
167
		MPI_Info info = MPI_INFO_NULL;
168

gsell's avatar
gsell committed
169 170 171 172 173 174 175 176
		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
177

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

185 186 187 188 189 190 191 192 193
		/* 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
194
		}
195 196 197 198 199 200 201 202
		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;
			}
203
			if (flags & H5PART_VFD_MPIIO_IND) {
204 205 206
				if (f->myproc == 0) {
					_H5Part_print_info ( "Using independent mode" );
				}
207
			} else {
208 209 210
				if (f->myproc == 0) {
					_H5Part_print_info ( "Using collective mode" );
				}
211 212 213 214 215 216 217 218 219
				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;
				}
220
			}
gsell's avatar
gsell committed
221
		}
gsell's avatar
gsell committed
222

223 224
		if ( flags & H5PART_FS_LUSTRE )
		{
225
			/* extend the btree size so that metadata pieces are
226 227 228 229 230 231 232 233 234
			 * close to the alignment value */
			unsigned int btree_ik = (align - 256) / 96;
			unsigned int btree_bytes = 64 + 96*btree_ik;
			if ( btree_bytes > align ) {
				HANDLE_H5PART_INVALID_ERR(
					"btree_ik", btree_ik );
				goto error_cleanup;
			}

235 236
			if (f->myproc == 0) {
				_H5Part_print_info (
237 238
					"Setting HDF5 btree parameter to %u",
					btree_ik );
239
				_H5Part_print_info (
240 241
					"Extending HDF5 btree size to %u bytes at rank 3",
					btree_bytes );
242
			}
243

244 245
			f->create_prop = H5Pcreate(H5P_FILE_CREATE);
			H5Pset_istore_k (f->create_prop, H5PART_BTREE_IK);
246

247
#ifndef H5_USE_16_API
248 249 250 251 252 253 254 255 256 257 258
			/* 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);
259
#else // H5_USE_16_API
260 261
			_H5Part_print_info (
					"Unable to defer metadata write: need HDF5 1.8");
262
#endif // H5_USE_16_API
263
		}
264

265
		f->comm = comm;
266
#endif // PARALLEL_IO
gsell's avatar
gsell committed
267
	} else {
gsell's avatar
gsell committed
268
		f->comm = 0;
gsell's avatar
gsell committed
269 270
		f->nprocs = 1;
		f->myproc = 0;
gsell's avatar
gsell committed
271 272
		f->pnparticles = 
			(h5part_int64_t*) malloc (f->nprocs * sizeof (h5part_int64_t));
gsell's avatar
gsell committed
273
	}
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299

	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 ) {
300
		f->file = H5Fopen(filename, H5F_ACC_RDONLY, f->access_prop);
gsell's avatar
gsell committed
301
	}
302
	else if ( flags & H5PART_WRITE ){
gsell's avatar
gsell committed
303
		f->file = H5Fcreate (filename, H5F_ACC_TRUNC, f->create_prop,
304
				f->access_prop);
gsell's avatar
gsell committed
305
		f->empty = 1;
gsell's avatar
gsell committed
306
	}
307
	else if ( flags & H5PART_APPEND ) {
308
		int fd = open(filename, O_RDONLY, 0);
gsell's avatar
gsell committed
309 310
		if ( (fd == -1) && (errno == ENOENT) ) {
			f->file = H5Fcreate(filename, H5F_ACC_TRUNC,
311
					f->create_prop, f->access_prop);
gsell's avatar
gsell committed
312
			f->empty = 1;
gsell's avatar
gsell committed
313 314 315
		}
		else if (fd != -1) {
			close (fd);
316 317
			f->file = H5Fopen(
				filename, H5F_ACC_RDWR, f->access_prop);
gsell's avatar
gsell committed
318 319 320 321 322
			/*
			  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
323
				f->file, "/", H5G_GROUP, f->groupname_step );
gsell's avatar
gsell committed
324 325 326 327 328 329 330 331 332 333 334 335
			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;
	}
336
	f->nparticles = 0;
gsell's avatar
gsell committed
337
	f->timegroup = -1;
338
	f->shape = H5S_ALL;
gsell's avatar
gsell committed
339 340 341 342
	f->diskshape = H5S_ALL;
	f->memshape = H5S_ALL;
	f->viewstart = -1;
	f->viewend = -1;
343
	f->throttle = 0;
gsell's avatar
gsell committed
344

gsell's avatar
gsell committed
345 346 347 348 349 350
	_H5Part_print_debug (
		"Proc[%d]: Opened file \"%s\" val=%lld",
		f->myproc,
		filename,
		(long long)(size_t)f );

gsell's avatar
gsell committed
351 352 353 354 355 356 357 358 359 360 361 362
	return f;

 error_cleanup:
	if (f != NULL ) {
		if (f->pnparticles != NULL) {
			free (f->pnparticles);
		}
		free (f);
	}
	return NULL;
}

gsell's avatar
gsell committed
363
#ifdef PARALLEL_IO
gsell's avatar
gsell committed
364
/*!
365
  \ingroup h5part_open
gsell's avatar
gsell committed
366

gsell's avatar
gsell committed
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
  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
385
H5PartOpenFileParallel (
gsell's avatar
gsell committed
386
	const char *filename,	/*!< [in] The name of the data file to open. */
387
	const char flags,	/*!< [in] The access mode for the file. */
gsell's avatar
gsell committed
388
	MPI_Comm comm		/*!< [in] MPI communicator */
389
	) {
390
	INIT
391 392 393
	SET_FNAME ( "H5PartOpenFileParallel" );

	int f_parallel = 1;	/* parallel i/o */
394
	h5part_int64_t align = 0; /* no alignment tuning */
395 396 397 398 399

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

/*!
400
  \ingroup h5part_open
401 402 403 404 405 406 407 408 409

  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. */
410
	const char flags,	/*!< [in] The access mode for the file. */
411 412 413
	MPI_Comm comm,		/*!< [in] MPI communicator */
	h5part_int64_t align	/*!< [in] Alignment size in bytes. */
	) {
414
	INIT
415
	SET_FNAME ( "H5PartOpenFileParallelAlign" );
416

gsell's avatar
gsell committed
417
	int f_parallel = 1;	/* parallel i/o */
gsell's avatar
gsell committed
418

419
	return _H5Part_open_file ( filename, flags, comm, f_parallel, align );
gsell's avatar
gsell committed
420 421
}
#endif
gsell's avatar
gsell committed
422

gsell's avatar
gsell committed
423
/*!
424
  \ingroup  h5part_open
gsell's avatar
gsell committed
425

gsell's avatar
gsell committed
426
  Opens file with specified filename. 
gsell's avatar
gsell committed
427

gsell's avatar
gsell committed
428 429 430 431 432
  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
433

gsell's avatar
gsell committed
434 435 436 437 438 439
  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
440

gsell's avatar
gsell committed
441 442
  \return	File handle or \c NULL
 */
gsell's avatar
gsell committed
443

gsell's avatar
gsell committed
444 445 446
H5PartFile*
H5PartOpenFile (
	const char *filename,	/*!< [in] The name of the data file to open. */
447
	const char flags	/*!< [in] The access mode for the file. */
gsell's avatar
gsell committed
448
	) {
449
	INIT
gsell's avatar
gsell committed
450
	SET_FNAME ( "H5PartOpenFile" );
gsell's avatar
gsell committed
451

gsell's avatar
gsell committed
452 453
	MPI_Comm comm = 0;	/* dummy */
	int f_parallel = 0;	/* serial open */
454
	int align = 0;		/* no tuning parameters */
gsell's avatar
gsell committed
455

456 457 458 459
	return _H5Part_open_file ( filename, flags, comm, f_parallel, align );
}

/*!
460
  \ingroup h5part_open
461 462 463 464 465 466 467 468 469

  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. */
470
	const char flags,	/*!< [in] The access mode for the file. */
471 472
	h5part_int64_t align	/*!< [in] Alignment size in bytes. */
) {
473 474
	INIT
	SET_FNAME ( "H5PartOpenFileAlign" );
475 476 477 478 479

	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
480 481 482 483 484 485 486
}

/*!
  Checks if a file was successfully opened.

  \return	\c H5PART_SUCCESS or error code
 */
487 488
h5part_int64_t
_H5Part_file_is_valid (
gsell's avatar
gsell committed
489 490 491 492 493 494 495 496 497 498 499 500
	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;
}

/*!
501
  \ingroup h5part_open
gsell's avatar
gsell committed
502

gsell's avatar
gsell committed
503 504 505 506 507 508 509 510 511 512 513
  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
514
	_h5part_errno = H5PART_SUCCESS;
gsell's avatar
gsell committed
515 516 517 518 519 520 521 522 523

	CHECK_FILEHANDLE ( f );

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

524 525 526 527 528 529 530 531
#ifdef PARALLEL_IO
	if ( f->multiblock && f->close_multiblock ) {
		(*f->close_multiblock) ( f );
		f->multiblock = NULL;
		f->close_multiblock = NULL;
	}
#endif

532
	if( f->shape != H5S_ALL ) {
gsell's avatar
gsell committed
533 534 535 536
		r = H5Sclose( f->shape );
		if ( r < 0 ) HANDLE_H5S_CLOSE_ERR;
		f->shape = 0;
	}
gsell's avatar
gsell committed
537
	if( f->timegroup >= 0 ) {
gsell's avatar
gsell committed
538 539
		r = H5Gclose( f->timegroup );
		if ( r < 0 ) HANDLE_H5G_CLOSE_ERR;
gsell's avatar
gsell committed
540
		f->timegroup = -1;
gsell's avatar
gsell committed
541 542 543 544 545 546
	}
	if( f->diskshape != H5S_ALL ) {
		r = H5Sclose( f->diskshape );
		if ( r < 0 ) HANDLE_H5S_CLOSE_ERR;
		f->diskshape = 0;
	}
547 548 549 550 551
	if( f->memshape != H5S_ALL ) {
		r = H5Sclose( f->memshape );
		if ( r < 0 ) HANDLE_H5S_CLOSE_ERR;
		f->memshape = 0;
	}
gsell's avatar
gsell committed
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
	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;
	}
	if( f->pnparticles ) {
		free( f->pnparticles );
	}
	free( f );

gsell's avatar
gsell committed
577
	return _h5part_errno;
gsell's avatar
gsell committed
578 579
}

580 581 582 583 584 585 586
h5part_int64_t
H5PartFileIsValid (
	H5PartFile *f
	) {
	return _H5Part_file_is_valid(f);
}

gsell's avatar
gsell committed
587
/*============== File Writing Functions ==================== */
gsell's avatar
gsell committed
588

589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
h5part_int64_t
_H5Part_get_step_name(
	H5PartFile *f,
	const h5part_int64_t step,
	char *name
	) {

	/* 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 );
	}

	return H5PART_SUCCESS;
}

gsell's avatar
gsell committed
613 614 615 616 617 618
h5part_int64_t
H5PartDefineStepName (
	H5PartFile *f,
	const char *name,
	const h5part_int64_t width
	) {
619 620 621 622 623 624 625 626

	CHECK_FILEHANDLE ( f );

	h5part_int64_t len = H5PART_STEPNAME_LEN - width - 2;
	if ( strlen(name) > len ) {
		_H5Part_print_warn (
			"Step name has been truncated to fit within %d chars.",
			H5PART_STEPNAME_LEN );
gsell's avatar
gsell committed
627
	}
628 629

	strncpy ( f->groupname_step, name, len );
gsell's avatar
gsell committed
630
	f->stepno_width = (int)width;
631 632

	_H5Part_print_debug ( "Step name defined as '%s'", f->groupname_step );
gsell's avatar
gsell committed
633 634 635 636
	
	return H5PART_SUCCESS;
}

637 638 639 640 641
static h5part_int64_t
_set_num_particles (
	H5PartFile *f,				/*!< [in] Handle to open file */
	const h5part_int64_t nparticles,	/*!< [in] Number of particles */
	const h5part_int64_t _stride
gsell's avatar
gsell committed
642 643
	) {

644 645 646 647
	int ret;
	h5part_int64_t herr;

	hsize_t count;
gsell's avatar
gsell committed
648
#ifdef HDF5V160
649
	hssize_t start;
gsell's avatar
gsell committed
650
#else
651
	hsize_t start;
gsell's avatar
gsell committed
652
#endif
653 654
	hsize_t stride;
	hsize_t dmax = H5S_UNLIMITED;
gsell's avatar
gsell committed
655

656
#ifdef PARALLEL_IO
gsell's avatar
gsell committed
657 658 659 660
	hsize_t total;
	register int i;
#endif

661 662 663 664 665 666 667 668 669 670 671 672
	if ( nparticles <= 0 )
		return HANDLE_H5PART_INVALID_ERR ( "nparticles", nparticles );

	/* prevent invalid stride value */	
	if (_stride < 1)
	{
		_H5Part_print_warn (
			"Stride < 1 was specified: changing to 1." );
		stride = 1;
	} else {
		stride = (hsize_t) _stride;
	}
gsell's avatar
gsell committed
673 674 675 676 677 678 679 680

#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
	*/
681 682 683
	if ( f->nparticles == nparticles && stride == 1 ) {
		_H5Part_print_debug (
			"Serial mode: skipping unnecessary view creation" );
gsell's avatar
gsell committed
684 685 686
		return H5PART_SUCCESS;
	}
#endif
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712

	herr = _reset_view ( f );
	if ( herr < 0 ) return herr;

	f->nparticles = (hsize_t) nparticles;

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

	/* we need a hyperslab selection if there is striding
	 * (otherwise, the default H5S_ALL selection is ok)
	 */
	if ( stride > 1 )
	{
		start = 0;
		count = f->nparticles;
		herr = H5Sselect_hyperslab (
			f->memshape,
			H5S_SELECT_SET,
			&start,
			&stride,
			&count, NULL );
		if ( herr < 0 ) return HANDLE_H5S_SELECT_HYPERSLAB_ERR;
gsell's avatar
gsell committed
713
	}
714

gsell's avatar
gsell committed
715
#ifndef PARALLEL_IO
716 717 718 719 720
	count = f->nparticles;
	f->shape = H5Screate_simple ( 1, &count, NULL );
	if ( f->shape < 0 ) HANDLE_H5S_CREATE_SIMPLE_ERR ( count );
	f->viewstart = 0;
	f->viewend   = nparticles - 1; // view range is *inclusive*
gsell's avatar
gsell committed
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
#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
	*/
736

737
	ret = MPI_Allgather (
gsell's avatar
gsell committed
738 739
		&nparticles, 1, MPI_LONG_LONG,
		f->pnparticles, 1, MPI_LONG_LONG,
740 741 742
		f->comm );
	if ( ret != MPI_SUCCESS) return HANDLE_MPI_ALLGATHER_ERR;

gsell's avatar
gsell committed
743
	if ( f->myproc == 0 ) {
gsell's avatar
gsell committed
744
		_H5Part_print_debug ( "Particle offsets:" );
745
		for ( i=0; i<f->nprocs; i++ ) 
746
			_H5Part_print_debug ( "\t[%d] np=%lld", i,
747
					(long long) f->pnparticles[i] );
gsell's avatar
gsell committed
748 749 750
	}

	/* compute start offsets */
751
	start = 0;
gsell's avatar
gsell committed
752
	for (i=0; i<f->myproc; i++) {
753
		start += f->pnparticles[i];
gsell's avatar
gsell committed
754
	}
755 756
	f->viewstart = start;
	f->viewend   = start + f->nparticles - 1; // view range is *inclusive*
gsell's avatar
gsell committed
757
	
758
	/* compute total nparticles */
gsell's avatar
gsell committed
759 760 761 762 763
	total = 0;
	for (i=0; i < f->nprocs; i++) {
		total += f->pnparticles[i];
	}

764 765
	if ( stride > 1 ) count = stride * total;
	else count = total;
gsell's avatar
gsell committed
766

767 768 769
	/* declare overall datasize */
	f->shape = H5Screate_simple (1, &count, NULL);
	if ( f->shape < 0 ) return HANDLE_H5S_CREATE_SIMPLE_ERR ( count );
gsell's avatar
gsell committed
770 771

	/* declare overall data size  but then will select a subset */
772 773
	f->diskshape = H5Screate_simple (1, &count, NULL);
	if ( f->diskshape < 0 ) return HANDLE_H5S_CREATE_SIMPLE_ERR ( count );
gsell's avatar
gsell committed
774

775 776 777
	count = nparticles;
	stride = 1;
	herr = H5Sselect_hyperslab (
gsell's avatar
gsell committed
778 779
		f->diskshape,
		H5S_SELECT_SET,
780 781 782 783
		&start,
		&stride,
		&count, NULL );
	if ( herr < 0 ) return HANDLE_H5S_SELECT_HYPERSLAB_ERR;
gsell's avatar
gsell committed
784 785

	if ( f->timegroup < 0 ) {
786 787
		herr = _H5Part_set_step ( f, 0 );
		if ( herr < 0 ) return herr;
gsell's avatar
gsell committed
788 789 790 791 792
	}
#endif
	return H5PART_SUCCESS;
}

793 794 795 796 797 798 799 800 801 802 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 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 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
/*!
  \ingroup h5part_model

  Set the number of particles for the current time-step.
  After you call this subroutine, all subsequent 
  operations will assume this number of particles will be written.

  For the parallel library, the \a nparticles value is the number of
  particles that the \e individual task will write. You can use
  a different value on different tasks.
  This function uses an \c MPI_Allgather
  call to aggregate each tasks number of particles and determine
  the appropiate offsets. Because of the use of this MPI collective,
  it is advisable to call this function as
  few times as possible when running at large concurrency.

  This function assumes that your particles' data fields are in stored in
  contiguous 1D arrays.
  For instance, the fields $x$ and $y$ for your particles are stored
  in separate arrays \c x[] and \c y[].
  
  If instead you store your particles as tuples, so that the values
  are arranged $x_1,y_1,x_2,y_2$... than you need to setup striding
  (in this case with value 2) using \ref H5PartSetNumParticlesStrided.

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

	SET_FNAME ( "H5PartSetNumParticles" );
	CHECK_FILEHANDLE( f );

	h5part_int64_t herr;
	h5part_int64_t stride = 1;

	herr = _set_num_particles ( f, nparticles, stride );
	if ( herr < 0 ) return herr;

	return H5PART_SUCCESS;
}

/*!
  \ingroup h5part_model

  Set the number of particles for the current time-step.
  After you call this subroutine, all subsequent 
  operations will assume this number of particles will be written.

  For the parallel library, the \a nparticles value is the number of
  particles that the \e individual task will write. You can use
  a different value on different tasks.
  This function uses an \c MPI_Allgather
  call to aggregate each tasks number of particles and determine
  the appropiate offsets. Because of the use of this MPI collective,
  it is advisable to call this function as
  few times as possible when running at large concurrency.

  This function assumes that your particles' data fields are
  stored tuples. For instance, the fields $x$ and $y$ of your
  particles are arranged $x_1,y_1,x_2,y_2$... in a single data
  array. In this example, the stride value would be 2.
  
  If you instead have a separate array for each fields,
  such as \c x[] and \c y[],
  use \ref H5PartSetNumParticles.

  \return	\c H5PART_SUCCESS or error code
 */
h5part_int64_t
H5PartSetNumParticlesStrided (
	H5PartFile *f,				/*!< [in] Handle to open file */
	const h5part_int64_t nparticles,	/*!< [in] Number of particles */
	const h5part_int64_t stride		/*!< [in] Stride (e.g. number of fields in the particle array) */
	) {

	SET_FNAME ( "H5PartSetNumParticlesStrided" );
	CHECK_FILEHANDLE( f );

	h5part_int64_t herr;

	herr = _set_num_particles ( f, nparticles, stride );
	if ( herr < 0 ) return herr;

	return H5PART_SUCCESS;
}

gsell's avatar
gsell committed
883 884 885
static h5part_int64_t
_write_data (
	H5PartFile *f,		/*!< IN: Handle to open file */
gsell's avatar
gsell committed
886 887 888
	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
889 890 891 892
	) {
	herr_t herr;
	hid_t dataset_id;

gsell's avatar
gsell committed
893
	_H5Part_print_debug ( "Create a dataset[%s] mounted on the "
894 895
			"timestep %lld",
			name, (long long)f->timestep );
gsell's avatar
gsell committed
896

897 898 899 900 901 902 903
	if ( f->shape == H5S_ALL ) {
		_H5Part_print_warn (
			"The view is unset or invalid: please "
			"set the view or specify a number of particles." );
		return HANDLE_H5PART_BAD_VIEW_ERR ( f->viewstart, f->viewend );
	}

904
#ifndef H5_USE_16_API
905 906 907
	htri_t exists = H5Lexists ( f->timegroup, name, H5P_DEFAULT );
	if ( exists > 0 ) return HANDLE_H5D_EXISTS_ERR ( name, f->timestep );

908 909 910 911 912 913 914 915 916
	dataset_id = H5Dcreate2 ( 
		f->timegroup,
		name,
		type,
		f->shape,
		H5P_DEFAULT,
		H5P_DEFAULT,
		H5P_DEFAULT );
#else
gsell's avatar
gsell committed
917 918 919 920 921 922
	dataset_id = H5Dcreate ( 
		f->timegroup,
		name,
		type,
		f->shape,
		H5P_DEFAULT );
923
#endif
gsell's avatar
gsell committed
924 925 926
	if ( dataset_id < 0 )
		return HANDLE_H5D_CREATE_ERR ( name, f->timestep );

927 928 929
#ifdef PARALLEL_IO
	herr = _H5Part_start_throttle ( f );
	if ( herr < 0 ) return herr;
930
#endif
931

932 933 934 935 936 937 938 939
	herr = H5Dwrite (
		dataset_id,
		type,
		f->memshape,
		f->diskshape,
		f->xfer_prop,
		array );

940 941 942
#ifdef PARALLEL_IO
	herr = _H5Part_end_throttle ( f );
	if ( herr < 0 ) return herr;
943
#endif
944

gsell's avatar
gsell committed
945 946 947 948 949
	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
950 951
	f->empty = 0;

gsell's avatar
gsell committed
952 953 954 955
	return H5PART_SUCCESS;
}

/*!
956
  \ingroup h5part_data
gsell's avatar
gsell committed
957

gsell's avatar
gsell committed
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
  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
984 985
	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
986 987 988
	) {

	SET_FNAME ( "H5PartWriteDataFloat64" );
gsell's avatar
gsell committed
989
	h5part_int64_t herr;
gsell's avatar
gsell committed
990 991 992 993 994 995 996 997 998 999 1000

	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;
}

1001
/*!
1002
  \ingroup h5part_data
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046

  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
1047
/*!
1048
  \ingroup h5part_data
gsell's avatar
gsell committed
1049

gsell's avatar
gsell committed
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
  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
1076 1077
	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
1078 1079
	) {

1080
	SET_FNAME ( "H5PartWriteDataInt64" );
gsell's avatar
gsell committed
1081

gsell's avatar
gsell committed
1082
	h5part_int64_t herr;
gsell's avatar
gsell committed
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093

	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;
}

1094
/*!
1095
  \ingroup h5part_data
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140

  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
1141 1142 1143
/********************** reading and writing attribute ************************/

/********************** private functions to handle attributes ***************/
gsell's avatar
gsell committed
1144 1145 1146 1147 1148 1149

/*!
  \ingroup h5partkernel
  @{
*/

1150
h5part_int64_t
1151
_H5Part_make_string_type(
1152
	hid_t *stype,
1153 1154
	int size
	) {
1155 1156 1157 1158 1159
	*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 H5PART_SUCCESS;
1160 1161
}

gsell's avatar
gsell committed
1162 1163 1164
/*!
   Normalize HDF5 type
*/
1165
h5part_int64_t
gsell's avatar
gsell committed
1166 1167 1168
_H5Part_normalize_h5_type (
	hid_t type
	) {
1169

gsell's avatar
gsell committed
1170 1171 1172
	H5T_class_t tclass = H5Tget_class ( type );
	int size = H5Tget_size ( type );

1173
	switch ( tclass ) {
gsell's avatar
gsell committed
1174
	case H5T_INTEGER:
1175 1176 1177 1178 1179 1180 1181
		if ( size==8 ) {
			return H5PART_INT64;
		}
	  	else if ( size==1 ) {
			return H5PART_CHAR;
		}
		break;
gsell's avatar
gsell committed
1182
	case H5T_FLOAT:
1183 1184 1185 1186 1187 1188 1189
		if ( size==8 ) {
			return H5PART_FLOAT64;
		}
		else if ( size==4 ) {
			return H5PART_FLOAT32;
		}
		break;
1190
	case H5T_STRING:
1191
		return H5PART_STRING;
gsell's avatar
gsell committed
1192
	default:
1193
		;/* NOP */
gsell's avatar
gsell committed
1194
	}
1195
	
1196
	return HANDLE_H5PART_TYPE_ERR;
gsell's avatar
gsell committed
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
}

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

	herr_t herr;
1207
	h5part_int64_t h5err;
gsell's avatar
gsell committed
1208 1209 1210 1211
	hid_t attrib_id;
	hid_t space_id;
	hid_t type_id;

1212
#ifndef H5_USE_16_API
1213
	if (! H5Aexists ( id, attrib_name )) {
1214
		_H5Part_print_warn ( "Attribute '%s' does not exist!", attrib_name );
1215 1216 1217
	}
	attrib_id = H5Aopen ( id, attrib_name, H5P_DEFAULT );
#else
gsell's avatar
gsell committed
1218
	attrib_id = H5Aopen_name ( id, attrib_name );
1219
#endif
gsell's avatar
gsell committed
1220 1221
	if ( attrib_id <= 0 ) return HANDLE_H5A_OPEN_NAME_ERR( attrib_name );

1222
	type_id = H5Aget_type ( attrib_id );
1223
	if ( type_id < 0 ) return HANDLE_H5A_GET_TYPE_ERR;
gsell's avatar
gsell committed
1224 1225 1226 1227

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

1228
	herr = H5Aread ( attrib_id, type_id, attrib_value );
gsell's avatar
gsell committed
1229 1230 1231 1232 1233
	if ( herr < 0 ) return HANDLE_H5A_READ_ERR;

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

1234
	herr = H5Tclose ( type_id );
gsell's avatar
gsell committed
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
	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
	) {

1252
	h5part_int64_t herr;
gsell's avatar
gsell committed
1253 1254
	hid_t space_id;
	hid_t attrib_id;
1255 1256
	hid_t type = attrib_type;

1257 1258 1259 1260 1261
	if ( attrib_type == H5PART_STRING )
	{
		herr = _H5Part_make_string_type ( &type, attrib_nelem );
		if ( herr < 0 ) return herr;

1262 1263 1264 1265 1266
		space_id = H5Screate (H5S_SCALAR);
		if ( space_id < 0 )
			return HANDLE_H5S_CREATE_SCALAR_ERR;
	}
	else {
1267
		space_id = H5Screate_simple ( 1, &attrib_nelem, NULL );
1268 1269 1270
		if ( space_id < 0 )
			return HANDLE_H5S_CREATE_SIMPLE_ERR ( attrib_nelem );
	}
gsell's avatar
gsell committed
1271

1272 1273 1274 1275 1276 1277
	attrib_id = H5Acreate(
		id,
		attrib_name,
		type,
		space_id,
		H5P_DEFAULT
1278
#ifndef H5_USE_16_API
1279
		, H5P_DEFAULT
1280
#endif
1281
		);
1282

gsell's avatar
gsell committed
1283 1284
	if ( attrib_id < 0 ) return HANDLE_H5A_CREATE_ERR ( attrib_name );

1285
	herr = H5Awrite ( attrib_id, type, attrib_value);
gsell's avatar
gsell committed
1286 1287 1288 1289 1290 1291 1292 1293
	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;

1294
	if ( attrib_type == H5PART_STRING ) {
1295 1296 1297 1298
		herr = H5Tclose ( type );
		if ( herr < 0 ) return HANDLE_H5T_CLOSE_ERR;
	}

gsell's avatar
gsell committed
1299 1300 1301
	return H5PART_SUCCESS;
}

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 1349 1350 1351 1352 1353 1354
h5part_int64_t
_H5Part_write_file_attrib (
	H5PartFile *f,
	const char *name,
	const hid_t type,
	const void *value,
	const hsize_t nelem
	) {

	hid_t group_id = H5Gopen ( f->file, "/"
#ifndef H5_USE_16_API
				  , H5P_DEFAULT
#endif
				  );

	if ( group_id < 0 ) return HANDLE_H5G_OPEN_ERR( "/" );

	h5part_int64_t herr = _H5Part_write_attrib (
		group_id,
		name,
		type,
		value,
		nelem );
	if ( herr < 0 ) return herr;

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

	return H5PART_SUCCESS;
}

h5part_int64_t
_H5Part_write_step_attrib (
	H5PartFile *f,
	const char *name,
	const hid_t type,
	const void *value,
	const hsize_t nelem
	) {

	CHECK_TIMEGROUP( f );

	h5part_int64_t herr = _H5Part_write_attrib (
		f->timegroup,
		name,
		type,
		value,
		nelem );
	if ( herr < 0 ) return herr;

	return H5PART_SUCCESS;
}

gsell's avatar
gsell committed
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
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
1370
	attrib_id = H5Aopen_idx ( id, (unsigned int)attrib_idx );
gsell's avatar
gsell committed
1371 1372 1373
	if ( attrib_id < 0 ) return HANDLE_H5A_OPEN_IDX_ERR ( attrib_idx );

	if ( attrib_nelem ) {
gsell's avatar
gsell committed
1374 1375 1376
		space_id =  H5Aget_space ( attrib_id );
		if ( space_id < 0 ) return HANDLE_H5A_GET_SPACE_ERR;

gsell's avatar
gsell committed
1377 1378 1379
		*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
1380 1381 1382

		herr = H5Sclose ( space_id );
		if ( herr < 0 ) return HANDLE_H5S_CLOSE_ERR;
gsell's avatar
gsell committed
1383 1384 1385 1386
	}
	if ( attrib_name ) {
		herr = H5Aget_name (
			attrib_id,
gsell's avatar
gsell committed
1387
			(size_t)len_attrib_name,
gsell's avatar
gsell committed
1388 1389 1390 1391
			attrib_name );
		if ( herr < 0 ) return HANDLE_H5A_GET_NAME_ERR;
	}
	if ( attrib_type ) {
gsell's avatar
gsell committed
1392 1393
		mytype = H5Aget_type ( attrib_id );
		if ( mytype < 0 ) return HANDLE_H5A_GET_TYPE_ERR;
gsell's avatar
gsell committed
1394

gsell's avatar
gsell committed
1395
		*attrib_type = _H5Part_normalize_h5_type ( mytype );
1396
		if ( *attrib_type < 0 ) return *attrib_type;
gsell's avatar
gsell committed
1397

gsell's avatar
gsell committed
1398 1399 1400
		herr = H5Tclose ( mytype );
		if ( herr < 0 ) return HANDLE_H5T_CLOSE_ERR;
	}
gsell's avatar
gsell committed
1401 1402 1403 1404 1405
	herr = H5Aclose ( attrib_id);
	if ( herr < 0 ) return HANDLE_H5A_CLOSE_ERR;

	return H5PART_SUCCESS;
}
1406 1407 1408
/*!
  }@
*/
gsell's avatar
gsell committed
1409 1410 1411 1412

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

/*!
gsell's avatar
gsell committed
1413 1414
  \ingroup h5part_attrib

1415 1416
  Writes an attribute \c name with the string \c value to
  the file root ("/").
gsell's avatar
gsell committed
1417 1418 1419 1420 1421 1422

  \return	\c H5PART_SUCCESS or error code   
*/
h5part_int64_t
H5PartWriteFileAttribString (
	H5PartFile *f,		/*!< [in] Handle to open file */
1423 1424
	const char *name,	/*!< [in] Name of attribute to create */
	const char *value	/*!< [in] Value of attribute */ 
gsell's avatar
gsell committed
1425 1426 1427 1428 1429 1430 1431
	) {

	SET_FNAME ( "H5PartWriteFileAttribString" );

	CHECK_FILEHANDLE ( f );
	CHECK_WRITABLE_MODE( f );

1432 1433 1434 1435 1436 1437
	h5part_int64_t herr = _H5Part_write_file_attrib (
		f,
		name,
		H5PART_STRING,
		value,
		strlen ( value ) + 1 );
gsell's avatar
gsell committed
1438 1439 1440 1441 1442 1443
	if ( herr < 0 ) return herr;

	return H5PART_SUCCESS;
}

/*!
gsell's avatar
gsell committed
1444 1445
  \ingroup h5part_attrib

1446 1447
  Writes an attribute \c name with the string \c value to
  the current timestep.
gsell's avatar
gsell committed
1448 1449 1450 1451 1452 1453

  \return	\c H5PART_SUCCESS or error code   
*/
h5part_int64_t
H5PartWriteStepAttribString (
	H5PartFile *f,		/*!< [in] Handle to open file */
1454 1455
	const char *name,	/*!< [in] Name of attribute to create */
	const char *value	/*!< [in] Value of attribute */ 
gsell's avatar
gsell committed
1456 1457 1458 1459 1460 1461 1462
	) {

	SET_FNAME ( "H5PartWriteStepAttribString" );

	CHECK_FILEHANDLE ( f );
	CHECK_WRITABLE_MODE( f );

1463 1464
	h5part_int64_t herr = _H5Part_write_step_attrib (
		f,
1465
		name,
1466
		H5PART_STRING,
1467 1468 1469 1470 1471 1472 1473 1474 1475 1476
		value,
		strlen ( value ) + 1 );
	if ( herr < 0 ) return herr;

	return H5PART_SUCCESS;
}

/*!
  \ingroup h5part_attrib

1477 1478 1479
  Writes an attribute \c name with values in the array \c data
  of \c nelem elements to
  the current timestep.
gsell's avatar
gsell committed
1480

1481 1482
  The type of \c data must ve specified using one of the folowing
  macros:
gsell's avatar
gsell committed
1483

1484 1485 1486 1487 1488 1489
  - \c H5PART_FLOAT64 (for \c h5part_float64_t)
  - \c H5PART_FLOAT32 (for \c h5part_float32_t)
  - \c H5PART_INT64 (for \c h5part_int64_t)
  - \c H5PART_INT32 (for \c h5part_int32_t)
  - \c H5PART_CHAR (for \c char)
  - \c H5PART_STRING (for \c char*)
gsell's avatar
gsell committed
1490 1491 1492 1493 1494 1495

  \return	\c H5PART_SUCCESS or error code   
*/
h5part_int64_t
H5PartWriteStepAttrib (
	H5PartFile *f,			/*!< [in] Handle to open file */
1496 1497 1498 1499
	const char *name,		/*!< [in] Name of attribute */
	const h5part_int64_t type,	/*!< [in] Type of values */
	const void *data,		/*!< [in] Array of attribute values */ 
	const h5part_int64_t nelem	/*!< [in] Number of array elements */
gsell's avatar
gsell committed
1500 1501 1502 1503 1504 1505 1506 1507
	){

	SET_FNAME ( "H5PartWriteStepAttrib" );

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

1508 1509 1510 1511 1512 1513
	h5part_int64_t herr = _H5Part_write_step_attrib (
		f,
		name,
		(const hid_t)type,
		data,
		nelem );
gsell's avatar
gsell committed
1514 1515 1516 1517 1518 1519
	if ( herr < 0 ) return herr;

	return H5PART_SUCCESS;
}

/*!
gsell's avatar
gsell committed
1520 1521
  \ingroup h5part_attrib

1522 1523 1524
  Writes an attribute \c name with values in the array \c data
  of \c nelem elements to
  the file root ("/").
gsell's avatar
gsell committed
1525

1526 1527
  The type of \c data must ve specified using one of the folowing
  macros:
gsell's avatar
gsell committed
1528

1529 1530 1531 1532 1533 1534
  - \c H5PART_FLOAT64 (for \c h5part_float64_t)
  - \c H5PART_FLOAT32 (for \c h5part_float32_t)
  - \c H5PART_INT64 (for \c h5part_int64_t)
  - \c H5PART_INT32 (for \c h5part_int32_t)
  - \c H5PART_CHAR (for \c char)
  - \c H5PART_STRING (for \c char*)
gsell's avatar
gsell committed
1535 1536 1537 1538 1539 1540

  \return	\c H5PART_SUCCESS or error code   
*/
h5part_int64_t
H5PartWriteFileAttrib (
	H5PartFile *f,			/*!< [in] Handle to open file */
1541 1542 1543 1544
	const char *name,		/*!< [in] Name of attribute */
	const h5part_int64_t type,	/*!< [in] Type of values */
	const void *data,		/*!< [in] Array of attribute values */ 
	const h5part_int64_t nelem	/*!< [in] Number of array elements */
gsell's avatar
gsell committed
1545 1546 1547 1548 1549 1550 1551
	) {

	SET_FNAME ( "H5PartWriteFileAttrib" );

	CHECK_FILEHANDLE ( f );
	CHECK_WRITABLE_MODE ( f );

1552 1553 1554 1555 1556 1557
	h5part_int64_t herr = _H5Part_write_file_attrib (
		f,
		name,
		(const hid_t)type,
		data,
		nelem );
gsell's avatar
gsell committed
1558 1559 1560 1561 1562 1563
	if ( herr < 0 ) return herr;

	return H5PART_SUCCESS;
}

/*!
gsell's avatar
gsell committed
1564 1565
  \ingroup h5part_attrib

gsell's avatar
gsell committed
1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577
  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" );
	CHECK_FILEHANDLE ( f );

1578 1579
	h5part_int64_t nattribs;

gsell's avatar
gsell committed
1580 1581 1582 1583 1584 1585 1586
	nattribs = H5Aget_num_attrs(f->timegroup);
	if ( nattribs < 0 ) HANDLE_H5A_GET_NUM_ATTRS_ERR;

	return nattribs;
}

/*!
gsell's avatar
gsell committed
1587 1588
  \ingroup h5part_attrib

gsell's avatar
gsell committed
1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603
  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 );

1604 1605 1606 1607

	hid_t group_id = H5Gopen ( f->file, "/"
#ifndef H5_USE_16_API
			   , H5P_DEFAULT
1608
#endif
1609 1610
			   );

gsell's avatar
gsell committed
1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621
	if ( group_id < 0 ) HANDLE_H5G_OPEN_ERR ( "/" );

	nattribs = H5Aget_num_attrs ( group_id