From 30476dd519f311431f45a7db7cf5a8717811b69b Mon Sep 17 00:00:00 2001 From: Thomas White Date: Fri, 11 Jan 2019 11:50:34 +0100 Subject: Clean up MessagePack stuff --- libcrystfel/src/image.c | 2 + src/im-zmq.c | 217 ++++++++++++++++++++++++------------------------ 2 files changed, 111 insertions(+), 108 deletions(-) diff --git a/libcrystfel/src/image.c b/libcrystfel/src/image.c index 63fd54f9..7dfb9c97 100644 --- a/libcrystfel/src/image.c +++ b/libcrystfel/src/image.c @@ -994,6 +994,8 @@ enum imagefile_type imagefile_get_type(struct imagefile *f) struct hdfile *imagefile_get_hdfile(struct imagefile *f) { + if ( f == NULL ) return NULL; + if ( f->type != IMAGEFILE_HDF5 ) { ERROR("Not an HDF5 file!\n"); return NULL; diff --git a/src/im-zmq.c b/src/im-zmq.c index f501095a..66a58a5c 100644 --- a/src/im-zmq.c +++ b/src/im-zmq.c @@ -135,6 +135,22 @@ void im_zmq_shutdown(struct im_zmq *z) zmq_ctx_destroy(z->ctx); } + +static msgpack_object *find_msgpack_kv(msgpack_object *obj, const char *key) +{ + int i; + + for ( i=0; ivia.map.size; i++ ) { + const char *kstr; + kstr = obj->via.map.ptr[i].key.via.str.ptr; + if ( strcmp(kstr, key) == 0 ) { + return &obj->via.map.ptr[i].val; + } + } + return NULL; +} + + /** * get_peaks_msgpack: * @obj: A %msgpack_object containing data in OnDA format @@ -164,22 +180,26 @@ int get_peaks_msgpack(msgpack_object *obj, struct image *image, int num_peaks; int pk; - double peak_offset = half_pixel_shift ? 0.5 : 0.0; - int entry; char *key_str; msgpack_object map_val, peak_list; + double peak_offset = half_pixel_shift ? 0.5 : 0.0; + + if ( obj == NULL ) { + ERROR("No MessagePack object to get peaks from.\n"); + return 1; + } - /* iterate over key-value pairs in msgpack_object - * object has structure: + /* Iterate over key-value pairs in msgpack_object + * Object has structure: * {"peak_list": [[peak_x], [peak_y], [peak_i]],"key2":val2,...} */ - for ( entry = 0; entry < obj->via.map.size; entry++ ) { + for ( entry=0; entryvia.map.size; entry++ ) { key_str = (char *)obj->via.map.ptr[entry].key.via.str.ptr; - /* check if key matches "peak_list" */ - if (strncmp(key_str, "peak_list", 9) == 0) { + /* Check if key matches "peak_list" */ + if ( strncmp(key_str, "peak_list", 9) == 0 ) { map_val = obj->via.map.ptr[entry].val; - /* length of peak_x array gives number of peaks */ + /* Length of peak_x array gives number of peaks */ num_peaks = map_val.via.array.ptr[0].via.array.size; peak_list = map_val; } @@ -191,12 +211,12 @@ int get_peaks_msgpack(msgpack_object *obj, struct image *image, image->features = image_feature_list_new(); image->num_peaks = num_peaks; - for ( pk = 0; pkvia.map.size; entry++ ) { - key_str = (char *)obj->via.map.ptr[entry].key.via.str.ptr; - // Check for key is "corr_data" - if ( strncmp(key_str, "corr_data", 9) == 0 ) { - map_val = obj->via.map.ptr[entry].val; - // Iterate over key-value pairs in inner map - for ( sub_entry=0; sub_entrydet == NULL ) { ERROR("Geometry not available.\n"); return 1; } - image->dp = malloc(image->det->n_panels*sizeof(float *)); - image->bad = malloc(image->det->n_panels*sizeof(int *)); - image->sat = malloc(image->det->n_panels*sizeof(float *)); - if ( (image->dp == NULL) || (image->bad == NULL) || (image->sat == NULL) ) { - ERROR("Failed to allocate data arrays.\n"); - return 1; - } - - for ( pi=0; pidet->n_panels; pi++ ) { + corr_data_obj = find_msgpack_kv(obj, "corr_data"); + data = (double *)find_msgpack_kv(corr_data_obj, "data")->via.bin.ptr; + shape = find_msgpack_kv(corr_data_obj, "shape"); + data_height = shape->via.array.ptr[0].via.i64; + data_width = shape->via.array.ptr[1].via.i64; - struct panel *p; - int fs, ss; + image->dp = malloc(image->det->n_panels*sizeof(float *)); + image->bad = malloc(image->det->n_panels*sizeof(int *)); + image->sat = malloc(image->det->n_panels*sizeof(float *)); + if ( (image->dp == NULL) || (image->bad == NULL) || (image->sat == NULL) ) { + ERROR("Failed to allocate data arrays.\n"); + return 1; + } - p = &image->det->panels[pi]; - image->dp[pi] = malloc(p->w*p->h*sizeof(float)); - image->bad[pi] = malloc(p->w*p->h*sizeof(int)); - image->sat[pi] = malloc(p->w*p->h*sizeof(float)); - if ( (image->dp[pi] == NULL) || (image->bad[pi] == NULL) || (image->sat[pi] == NULL) ) - { - ERROR("Failed to allocate panel\n"); - return 1; - } + for ( pi=0; pidet->n_panels; pi++ ) { - if ( (p->orig_min_fs + p->w > data_width) - || (p->orig_min_ss + p->h > data_height) ) - { - ERROR("Panel %s is outside range of data provided\n", - p->name); - return 1; - } + struct panel *p; + int fs, ss; + + p = &image->det->panels[pi]; + image->dp[pi] = malloc(p->w*p->h*sizeof(float)); + image->bad[pi] = malloc(p->w*p->h*sizeof(int)); + image->sat[pi] = malloc(p->w*p->h*sizeof(float)); + if ( (image->dp[pi] == NULL) || (image->bad[pi] == NULL) || (image->sat[pi] == NULL) ) + { + ERROR("Failed to allocate panel\n"); + return 1; + } - for ( ss=0; ssh; ss++) { - for ( fs=0; fsw; fs++) { + if ( (p->orig_min_fs + p->w > data_width) + || (p->orig_min_ss + p->h > data_height) ) + { + ERROR("Panel %s is outside range of data provided\n", + p->name); + return 1; + } - int idx; - int cfs, css; - int bad = 0; + for ( ss=0; ssh; ss++) { + for ( fs=0; fsw; fs++) { - cfs = fs+p->orig_min_fs; - css = ss+p->orig_min_ss; - idx = cfs + css*data_width; + int idx; + int cfs, css; + int bad = 0; - image->dp[pi][fs+p->w*ss] = data[idx]; + cfs = fs+p->orig_min_fs; + css = ss+p->orig_min_ss; + idx = cfs + css*data_width; - if ( sat != NULL ) { - image->sat[pi][fs+p->w*ss] = sat[idx]; - } else { - image->sat[pi][fs+p->w*ss] = INFINITY; - } + image->dp[pi][fs+p->w*ss] = data[idx]; - if ( p->no_index ) bad = 1; + if ( sat != NULL ) { + image->sat[pi][fs+p->w*ss] = sat[idx]; + } else { + image->sat[pi][fs+p->w*ss] = INFINITY; + } - if ( in_bad_region(image->det, p, cfs, css) ) { - bad = 1; - } + if ( p->no_index ) bad = 1; - if ( flags != NULL ) { + if ( in_bad_region(image->det, p, cfs, css) ) { + bad = 1; + } - int f; + if ( flags != NULL ) { - f = flags[idx]; + int f; - if ( (f & image->det->mask_good) - != image->det->mask_good ) bad = 1; + f = flags[idx]; - if ( f & image->det->mask_bad ) bad = 1; + if ( (f & image->det->mask_good) + != image->det->mask_good ) bad = 1; - } - image->bad[pi][fs+p->w*ss] = bad; - } - } + if ( f & image->det->mask_bad ) bad = 1; - } + } + image->bad[pi][fs+p->w*ss] = bad; + } + } - // might need to do some freeing of memory for msgpack object here + } - if ( image->beam != NULL ) { - onda_fill_in_beam_parameters(image->beam, image); - if ( image->lambda > 1000 ) { - ERROR("Warning: Missing or nonsensical wavelength " - "(%e m).\n", - image->lambda); - } - } - onda_fill_in_clen(image->det); - fill_in_adu(image); + if ( image->beam != NULL ) { + onda_fill_in_beam_parameters(image->beam, image); + if ( image->lambda > 1000 ) { + ERROR("Warning: Missing or nonsensical wavelength " + "(%e m).\n", image->lambda); + } + } + onda_fill_in_clen(image->det); + fill_in_adu(image); - return 0; + return 0; } -- cgit v1.2.3