diff options
author | Thomas White <taw@physics.org> | 2021-03-30 17:01:11 +0200 |
---|---|---|
committer | Thomas White <taw@physics.org> | 2021-03-30 17:01:11 +0200 |
commit | 00f88dac5a2b63ec1c5f7ad65efef3d4f9d93b77 (patch) | |
tree | fc8a5f77c1b88f526cbdd7efb53b09183cc5ca14 /libcrystfel/src/stream.c | |
parent | ab2158fb3f8053c6277113b4fb22007859f53de8 (diff) |
Add header caching layer
This simultaneously implements --copy-header/--copy-hdf5-field and gives
access to metadata (e.g. detector position or wavelength) when loading
from a stream with header-specified values.
Diffstat (limited to 'libcrystfel/src/stream.c')
-rw-r--r-- | libcrystfel/src/stream.c | 69 |
1 files changed, 67 insertions, 2 deletions
diff --git a/libcrystfel/src/stream.c b/libcrystfel/src/stream.c index 83756c7a..68f623dd 100644 --- a/libcrystfel/src/stream.c +++ b/libcrystfel/src/stream.c @@ -639,8 +639,19 @@ int stream_write_chunk(Stream *st, const struct image *i, fprintf(st->fh, "beam_divergence = %.2e rad\n", i->div); fprintf(st->fh, "beam_bandwidth = %.2e (fraction)\n", i->bw); - /* FIXME: Better way of doing this */ - //imagefile_copy_fields(imfile, i->copyme, st->fh, ev); + for ( j=0; j<i->n_cached_headers; j++ ) { + struct header_cache_entry *ce = i->header_cache[j]; + if ( ce->type == 'f' ) { + fprintf(st->fh, "header/float/%s = %f\n", + ce->header_name, ce->val_float); + } else if ( ce->type == 'i' ) { + fprintf(st->fh, "header/int/%s = %i\n", + ce->header_name, ce->val_int); + } else { + ERROR("Unrecognised header cache type '%c'\n", + ce->type); + } + } if ( i->detgeom != NULL ) { @@ -891,6 +902,48 @@ static void read_crystal(Stream *st, struct image *image, } +static void parse_header(const char *line_in, struct image *image, char type) +{ + char *line; + char *pos; + + line = strdup(line_in); + + pos = strchr(line, ' '); + if ( pos == NULL ) { + ERROR("Invalid header line '%s' (no space)\n", line); + return; + } + pos[0] = '\0'; + + if ( strlen(line_in) < strlen(line) + 3 ) { + ERROR("Invalid header line '%s' (too short)\n", line); + return; + } + + if ( (pos[1] != '=') || (pos[2] != ' ') ) { + ERROR("Invalid header line '%s' (wrong separator)\n", line); + return; + } + + if ( type == 'f' ) { + float v; + if ( sscanf(pos+3, "%f", &v) != 1 ) { + ERROR("Invalid header line '%s' (invalid value)\n", line); + return; + } + image_cache_header_float(image, line, v); + } else { + int v; + if ( sscanf(pos+3, "%i", &v) != 1 ) { + ERROR("Invalid header line '%s' (invalid value)\n", line); + return; + } + image_cache_header_int(image, line, v); + } +} + + /** * Read the next chunk from a stream and return an image structure */ @@ -928,6 +981,18 @@ struct image *stream_read_chunk(Stream *st, StreamFlags srf) image->ev = strdup(line+7); } + if ( strncmp(line, "hdf5/", 5) == 0 ) { + parse_header(line+5, image, 'f'); + } + + if ( strncmp(line, "header/int/", 11) == 0 ) { + parse_header(line+11, image, 'i'); + } + + if ( strncmp(line, "header/float/", 13) == 0 ) { + parse_header(line+13, image, 'f'); + } + if ( strncmp(line, "indexed_by = ", 13) == 0 ) { int err = 0; image->indexed_by = get_indm_from_string_2(line+13, &err); |