diff options
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/tng_io.c | 24 | ||||
-rw-r--r-- | src/lib/tng_io.h | 120 |
2 files changed, 129 insertions, 15 deletions
diff --git a/src/lib/tng_io.c b/src/lib/tng_io.c index 099d88c..ddf3e0d 100644 --- a/src/lib/tng_io.c +++ b/src/lib/tng_io.c @@ -2201,7 +2201,10 @@ static tng_function_status tng_frame_set_block_read } } - file_pos = ftell(tng_data->input_file); + file_pos = ftell(tng_data->input_file) - + (block->block_contents_size + block->header_contents_size); + + tng_data->current_trajectory_frame_set_input_file_pos = file_pos; if(frame_set->n_mapping_blocks && frame_set->mappings) { @@ -6084,8 +6087,7 @@ tng_function_status tng_frame_set_find(tng_trajectory_t tng_data, tng_block_init(&block); /* Is this the right frame set? */ - if(first_frame <= frame && - frame <= last_frame) + if(first_frame <= frame && frame <= last_frame) { tng_block_destroy(&block); return(TNG_SUCCESS); @@ -6119,6 +6121,7 @@ tng_function_status tng_frame_set_find(tng_trajectory_t tng_data, fseek(tng_data->input_file, file_pos, SEEK_SET); + tng_data->current_trajectory_frame_set_input_file_pos = file_pos; /* Read block headers first to see what block is found. */ stat = tng_block_header_read(tng_data, block); if(stat == TNG_CRITICAL || block->id != TNG_TRAJECTORY_FRAME_SET) @@ -6140,6 +6143,12 @@ tng_function_status tng_frame_set_find(tng_trajectory_t tng_data, last_frame = first_frame + frame_set->n_frames - 1; file_pos = tng_data->current_trajectory_frame_set_input_file_pos; + if(frame >= first_frame && frame <= last_frame) + { + tng_block_destroy(&block); + return(TNG_SUCCESS); + } + while(file_pos > 0 && first_frame + long_stride_length * n_frames_per_frame_set < frame) { @@ -6669,6 +6678,11 @@ tng_function_status tng_frame_set_write(tng_trajectory_t tng_data, tng_data->last_trajectory_frame_set_output_file_pos = ftell(tng_data->output_file); + if(tng_data->current_trajectory_frame_set_output_file_pos <= 0) + { + return(TNG_FAILURE); + } + tng_block_init(&block); if(tng_frame_set_block_write(tng_data, block, hash_mode) != TNG_SUCCESS) @@ -6918,7 +6932,7 @@ tng_function_status tng_frame_set_new(tng_trajectory_t tng_data, tng_function_status tng_data_block_add(tng_trajectory_t tng_data, const int64_t id, const char *block_name, - const char datatype, + const tng_data_type datatype, const tng_block_type block_type_flag, int64_t n_frames, const int64_t n_values_per_frame, @@ -7118,7 +7132,7 @@ tng_function_status tng_data_block_add(tng_trajectory_t tng_data, tng_function_status tng_particle_data_block_add(tng_trajectory_t tng_data, const int64_t id, const char *block_name, - const char datatype, + const tng_data_type datatype, const tng_block_type block_type_flag, int64_t n_frames, const int64_t n_values_per_frame, diff --git a/src/lib/tng_io.h b/src/lib/tng_io.h index 4f9c260..33af0c1 100644 --- a/src/lib/tng_io.h +++ b/src/lib/tng_io.h @@ -40,11 +40,112 @@ * cmake .. * * make - * * * Test by running: - * + * * bin/tng_testing + * + * @section examples_sec Examples + * + * @subsection C + * + * #include <stdlib.h> + * #include <stdio.h> + * #include <tng_io.h> + * + * int main(int argc, char **argv) + * { + * tng_trajectory_t traj; + * union data_values ***positions = 0; // A 3-dimensional array to be populated + * int64_t n_particles, n_values_per_frame, n_frames; + * tng_data_type data_type; + * int i, j; + * int64_t particle = 0; + * // Set a default frame range + * int first_frame = 0, last_frame = 50; + * + * if(argc <= 1) + * { + * printf("No file specified\n"); + * printf("Usage:\n"); + * printf("tng_io_read_pos <tng_file> [particle number = %"PRId64"] " + * "[first_frame = %d] [last_frame = %d]\n", + * particle, first_frame, last_frame); + * exit(1); + * } + * + * // A reference must be passed to allocate memory + * if(tng_trajectory_init(&traj) != TNG_SUCCESS) + * { + * tng_trajectory_destroy(&traj); + * exit(1); + * } + * tng_input_file_set(traj, argv[1]); + * + * // Read the file headers + * tng_file_headers_read(traj, TNG_USE_HASH); + * + * if(argc >= 3) + * { + * particle = strtoll(argv[2], 0, 10); + * if(argc >= 4) + * { + * first_frame = strtoll(argv[3], 0, 10); + * if(argc >= 5) + * { + * last_frame = strtoll(argv[4], 0, 10); + * } + * } + * } + * + * n_frames = last_frame - first_frame + 1; + * + * // Get the positions of all particles in the requested frame range. + * // The positions are stored in the positions array. + * // N.B. No proper error checks. + * if(tng_particle_data_interval_get(traj, TNG_TRAJ_POSITIONS, first_frame, + * last_frame, TNG_USE_HASH, &positions, &n_particles, &n_values_per_frame, + * &data_type) != TNG_SUCCESS) + * { + * printf("Cannot read positions\n"); + * } + * else + * { + * // Print the positions of the wanted particle (zero based) + * for(i=first_frame; i<=last_frame; i++) + * { + * printf("%d", i); + * for(j=0; j<n_values_per_frame; j++) + * { + * switch(data_type) + * { + * case TNG_INT_DATA: + * printf("\t%"PRId64"", positions[i][particle][j].i); + * break; + * case TNG_FLOAT_DATA: + * printf("\t%f", positions[i][particle][j].f); + * break; + * case TNG_DOUBLE_DATA: + * printf("\t%f", positions[i][particle][j].d); + * break; + * default: + * break; + * } + * printf("\n"); + * } + * } + * } + * + * // Free memory + * if(positions) + * { + * tng_particle_data_values_free(positions, n_frames, n_particles, + * n_values_per_frame, data_type); + * } + * tng_trajectory_destroy(&traj); + * + * return(0); + * } * */ @@ -52,6 +153,7 @@ #define _TNGIO_H 1 #include <stdio.h> +#include <stdlib.h> #include <string.h> #include <inttypes.h> @@ -101,10 +203,8 @@ typedef enum {TNG_UNCOMPRESSED, typedef enum {TNG_NON_TRAJECTORY_BLOCK, TNG_TRAJECTORY_BLOCK} tng_block_type; -typedef enum {TNG_ENDIANNESS_AND_STRING_LENGTH, - TNG_GENERAL_INFO, +typedef enum {TNG_GENERAL_INFO, TNG_MOLECULES, - TNG_TRAJECTORY_IDS_AND_NAMES, TNG_TRAJECTORY_FRAME_SET, TNG_PARTICLE_MAPPING} tng_non_trajectory_block_ids; @@ -165,7 +265,7 @@ typedef struct tng_non_particle_data *tng_non_particle_data_t; union data_values { double d; float f; - int i; + int64_t i; char *c; }; @@ -1158,7 +1258,7 @@ tng_function_status tng_frame_set_new_(tng_trajectory_t tng_data, tng_function_status tng_data_block_add(tng_trajectory_t tng_data, const int64_t id, const char *block_name, - const char datatype, + const tng_data_type datatype, const tng_block_type block_type_flag, int64_t n_frames, const int64_t n_values_per_frame, @@ -1168,7 +1268,7 @@ tng_function_status tng_data_block_add(tng_trajectory_t tng_data, tng_function_status tng_data_block_add_(tng_trajectory_t tng_data, const int64_t *id, const char *block_name, - const char *datatype, + const tng_data_type *datatype, const tng_block_type *block_type_flag, int64_t *n_frames, const int64_t *n_values_per_frame, @@ -1217,7 +1317,7 @@ tng_function_status tng_data_block_add_(tng_trajectory_t tng_data, tng_function_status tng_particle_data_block_add(tng_trajectory_t tng_data, const int64_t id, const char *block_name, - const char datatype, + const tng_data_type datatype, const tng_block_type block_type_flag, int64_t n_frames, const int64_t n_values_per_frame, @@ -1230,7 +1330,7 @@ tng_function_status tng_particle_data_block_add_ (tng_trajectory_t tng_data, const int64_t *id, const char *block_name, - const char *datatype, + const tng_data_type *datatype, const tng_block_type *block_type_flag, int64_t *n_frames, const int64_t *n_values_per_frame, |