Example #1
0
/*
 * Open existing particle files and add to fileset
 */
int artio_fileset_open_particles(artio_fileset *handle) {
	int i;
	char filename[256];
	int first_file, last_file;
	int mode;
	artio_particle_file *phandle;

	if ( handle == NULL ) {
		return ARTIO_ERR_INVALID_HANDLE;
	}

	if ( handle->open_type & ARTIO_OPEN_PARTICLES ||
			handle->open_mode != ARTIO_FILESET_READ ||
			handle->particle != NULL ) {
		return ARTIO_ERR_INVALID_FILESET_MODE;
	}
	handle->open_type |= ARTIO_OPEN_PARTICLES;

	phandle = artio_particle_file_allocate();
	if ( phandle == NULL ) {
		return ARTIO_ERR_MEMORY_ALLOCATION;
	}

	if ( artio_parameter_get_int( handle, "num_particle_files", &phandle->num_particle_files ) != ARTIO_SUCCESS ||
			artio_parameter_get_int(handle, "num_particle_species", &phandle->num_species) != ARTIO_SUCCESS ) {
		return ARTIO_ERR_PARTICLE_DATA_NOT_FOUND;
	}

	phandle->num_primary_variables = (int *)malloc(sizeof(int) * phandle->num_species);
	phandle->num_secondary_variables = (int *)malloc(sizeof(int) * phandle->num_species);
	phandle->num_particles_per_species = (int *)malloc(phandle->num_species * sizeof(int));
	if ( phandle->num_primary_variables == NULL ||
			phandle->num_secondary_variables == NULL ||
			phandle->num_particles_per_species == NULL ) {
		artio_particle_file_destroy(phandle);
		return ARTIO_ERR_MEMORY_ALLOCATION;
	}

	artio_parameter_get_int_array(handle, "num_primary_variables",
			phandle->num_species, phandle->num_primary_variables);
	artio_parameter_get_int_array(handle, "num_secondary_variables",
			phandle->num_species, phandle->num_secondary_variables);

	phandle->file_sfc_index = (int64_t *)malloc(sizeof(int64_t) * (phandle->num_particle_files + 1));
	if ( phandle->file_sfc_index == NULL ) {
		artio_particle_file_destroy(phandle);
		return ARTIO_ERR_MEMORY_ALLOCATION;
	}

	artio_parameter_get_long_array(handle, "particle_file_sfc_index",
			phandle->num_particle_files + 1, phandle->file_sfc_index);

	first_file = artio_find_file(phandle->file_sfc_index,
			phandle->num_particle_files, handle->proc_sfc_begin);
	last_file = artio_find_file(phandle->file_sfc_index,
			phandle->num_particle_files, handle->proc_sfc_end);

#ifdef ARTIO_POSIX
    if ( first_file != last_file ) {
        fprintf(stderr,"%d: ARTIO_POSIX requires one-to-one file access! first_file = %d, last_file = %d\n",
            handle->rank, first_file, last_file );
        fprintf(stderr, "%d: %ld %ld\n", handle->rank, handle->proc_sfc_begin, handle->proc_sfc_end );
        for ( i = 0; i < phandle->num_particle_files; i++ ) {
            fprintf(stderr, "%d: %ld\n", handle->rank, phandle->file_sfc_index[i]);
        }
        return ARTIO_ERR_INVALID_FILESET_MODE;
    }
#endif

	/* allocate file handles */
	phandle->ffh = (artio_fh **)malloc(phandle->num_particle_files * sizeof(artio_fh *));
	if ( phandle->ffh == NULL ) {
		artio_particle_file_destroy(phandle);
		return ARTIO_ERR_MEMORY_ALLOCATION;
	}

	for ( i = 0; i < phandle->num_particle_files; i++ ) {
		phandle->ffh[i] = NULL;
	}

	/* open files on all processes */
	for (i = 0; i < phandle->num_particle_files; i++) {
		sprintf(filename, "%s.%c%03d", handle->file_prefix,
				particle_file_suffix, i);

		mode = ARTIO_MODE_READ;
		if (i >= first_file && i <= last_file) {
			mode |= ARTIO_MODE_ACCESS;
		}
		if (handle->endian_swap) {
			mode |= ARTIO_MODE_ENDIAN_SWAP;
		}

		phandle->ffh[i] = artio_file_fopen(filename, mode, handle->context);
		if ( phandle->ffh[i] == NULL ) {
			artio_particle_file_destroy(phandle);
			return ARTIO_ERR_PARTICLE_FILE_NOT_FOUND;
		}
	}

	handle->particle = phandle;
	return ARTIO_SUCCESS;
}
int artio_parameter_get_int(artio_fileset *handle, const char * key, int32_t * value) {
	return artio_parameter_get_int_array(handle, key, 1, value);
}