diff options
author | Magnus Lundborg <lundborg.magnus@gmail.com> | 2013-04-18 09:57:39 (GMT) |
---|---|---|
committer | Magnus Lundborg <lundborg.magnus@gmail.com> | 2013-04-18 09:57:39 (GMT) |
commit | b054bef7b59e535bb696b1c7a58b33f94af6ab43 (patch) | |
tree | f245b01a604da64c01faf39f439806edae8082c0 /src | |
parent | dc88a1b81befe574228c00712530a53ce9245c60 (diff) |
Add tng_num_frames_get() to get the number of frames in the trajectory. Added to reading example as well.
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/tng_io.c | 48 | ||||
-rw-r--r-- | src/tests/tng_io_read_pos.c | 16 |
2 files changed, 63 insertions, 1 deletions
diff --git a/src/lib/tng_io.c b/src/lib/tng_io.c index d985b2d..0cf92d8 100644 --- a/src/lib/tng_io.c +++ b/src/lib/tng_io.c @@ -8348,6 +8348,48 @@ tng_function_status tng_input_file_len_get(const tng_trajectory_t tng_data, return(TNG_SUCCESS); } +tng_function_status tng_num_frames_get(const tng_trajectory_t tng_data, + int64_t *n) +{ + tng_gen_block_t block; + tng_function_status stat; + int64_t file_pos; + + file_pos = tng_data->last_trajectory_frame_set_input_file_pos; + + if(file_pos <= 0) + { + return(TNG_FAILURE); + } + + tng_block_init(&block); + 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) + { + tng_block_destroy(&block); + return(TNG_FAILURE); + } + + stat = tng_block_read_next(tng_data, block, + TNG_SKIP_HASH); + tng_block_destroy(&block); + + if(stat != TNG_SUCCESS) + { + return(TNG_FAILURE); + } + + *n = tng_data->current_trajectory_frame_set.first_frame + + tng_data->current_trajectory_frame_set.n_frames; + + return(TNG_SUCCESS); +} + tng_function_status tng_num_particles_get(const tng_trajectory_t tng_data, int64_t *n) { @@ -12266,6 +12308,12 @@ tng_function_status tng_input_file_len_get_(const tng_trajectory_t tng_data, return(tng_input_file_len_get(tng_data, len)); } +tng_function_status tng_num_frames_get_(const tng_trajectory_t tng_data, + int64_t *n) +{ + return(tng_num_frames_get(tng_data, n)); +} + tng_function_status tng_num_particles_get_(const tng_trajectory_t tng_data, int64_t *n) { diff --git a/src/tests/tng_io_read_pos.c b/src/tests/tng_io_read_pos.c index af5e828..493e10e 100644 --- a/src/tests/tng_io_read_pos.c +++ b/src/tests/tng_io_read_pos.c @@ -21,7 +21,7 @@ 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; + int64_t n_particles, n_values_per_frame, n_frames, tot_n_frames; tng_data_type data_type; int i, j; int64_t particle = 0; @@ -63,6 +63,20 @@ int main(int argc, char **argv) } } + if(tng_num_frames_get(traj, &tot_n_frames) != TNG_SUCCESS) + { + printf("Cannot determine the number of frames in the file\n"); + tng_trajectory_destroy(&traj); + exit(1); + } + + printf("%"PRId64" frames in file\n", tot_n_frames); + + if(last_frame > tot_n_frames - 1) + { + last_frame = tot_n_frames - 1; + } + n_frames = last_frame - first_frame + 1; if(tng_atom_name_of_particle_nr_get(traj, particle, atom_name, |