diff options
author | Valerio Mariani <valerio.mariani@desy.de> | 2014-03-25 17:35:07 +0100 |
---|---|---|
committer | Thomas White <taw@physics.org> | 2014-09-05 18:01:52 +0200 |
commit | c194bf7746e3635571808856f29434499eec55b7 (patch) | |
tree | 6e52ae148b10bf46c4ca97badb06b2cfd44a55ed | |
parent | c7e4b7acbd624723c5973431c0101fe92bc3089d (diff) |
Refactoring of peaks and reflections I/O
-rw-r--r-- | libcrystfel/src/detector.c | 40 | ||||
-rw-r--r-- | libcrystfel/src/detector.h | 3 | ||||
-rw-r--r-- | libcrystfel/src/image.h | 1 | ||||
-rw-r--r-- | libcrystfel/src/reflist-utils.c | 30 | ||||
-rw-r--r-- | libcrystfel/src/reflist-utils.h | 4 | ||||
-rw-r--r-- | libcrystfel/src/stream.c | 101 | ||||
-rw-r--r-- | src/dw-hdfsee.c | 113 | ||||
-rw-r--r-- | src/pattern_sim.c | 15 | ||||
-rw-r--r-- | src/process_image.c | 6 |
9 files changed, 178 insertions, 135 deletions
diff --git a/libcrystfel/src/detector.c b/libcrystfel/src/detector.c index 4681ab28..a7829474 100644 --- a/libcrystfel/src/detector.c +++ b/libcrystfel/src/detector.c @@ -1488,3 +1488,43 @@ void mark_resolution_range_as_bad(struct image *image, } } + + +extern int single_source (struct detector *det, char *element) +{ + int pi; + char *first_datafrom = NULL; + char *curr_datafrom = NULL; + + if ( det->panels[0].data_from == NULL ) { + if ( element != NULL ) { + first_datafrom = strdup(element); + } else { + first_datafrom = strdup("/data/data"); + } + } else { + first_datafrom = strdup(det->panels[0].data_from); + } + + for ( pi=1;pi<det->n_panels;pi++ ) { + + if ( det->panels[pi].data_from == NULL ) { + if ( element != NULL ) { + curr_datafrom = strdup(element); + } else { + curr_datafrom = strdup("/data/data"); + } + } else { + curr_datafrom = strdup(det->panels[pi].data_from); + } + + if ( strcmp(curr_datafrom, first_datafrom) != 0 ) { + return 0; + } + } + + free(first_datafrom); + free(curr_datafrom); + + return 1; +} diff --git a/libcrystfel/src/detector.h b/libcrystfel/src/detector.h index fcd44ed2..6dc398d7 100644 --- a/libcrystfel/src/detector.h +++ b/libcrystfel/src/detector.h @@ -209,6 +209,9 @@ extern int write_detector_geometry(const char *filename, struct detector *det); extern void mark_resolution_range_as_bad(struct image *image, double min, double max); +extern int single_source (struct detector *det, char *element); + + #ifdef __cplusplus } #endif diff --git a/libcrystfel/src/image.h b/libcrystfel/src/image.h index 69edb768..c3fe4505 100644 --- a/libcrystfel/src/image.h +++ b/libcrystfel/src/image.h @@ -69,6 +69,7 @@ struct imagefeature { struct image *parent; double fs; double ss; + char *pn; double intensity; /* Reciprocal space coordinates (m^-1 of course) of this feature */ diff --git a/libcrystfel/src/reflist-utils.c b/libcrystfel/src/reflist-utils.c index 5d52ec93..e033791b 100644 --- a/libcrystfel/src/reflist-utils.c +++ b/libcrystfel/src/reflist-utils.c @@ -286,6 +286,12 @@ int write_reflist(const char *filename, RefList *list) RefList *read_reflections_from_file(FILE *fh) { + return read_reflections_from_file2(fh, NULL); +} + + +RefList *read_reflections_from_file2(FILE *fh, struct detector *det) +{ char *rval = NULL; RefList *out; int major_version; /* Minor version as well, but not used yet */ @@ -364,12 +370,15 @@ RefList *read_reflections_from_file(FILE *fh) signed int h, k, l; float intensity, sigma, fs, ss; char phs[1024]; + char pn[32]; int cts; int r; + struct panel *p; + float read_fs, read_ss; - r = sscanf(line, "%i %i %i %f %s %f %i %f %f", + r = sscanf(line, "%i %i %i %f %s %f %i %f %f %s", &h, &k, &l, &intensity, phs, &sigma, - &cts, &fs, &ss); + &cts, &fs, &ss, pn); if ( r != 9 ) { reflist_free(out); @@ -378,7 +387,14 @@ RefList *read_reflections_from_file(FILE *fh) refl = add_refl(out, h, k, l); set_intensity(refl, intensity); - set_detector_pos(refl, 0.0, fs, ss); + + if ( det != NULL) { + p = find_panel_by_name(det, pn); + read_ss = ss-p->orig_min_ss+p->min_ss; + read_fs = fs-p->orig_min_fs+p->min_fs; + set_detector_pos(refl, 0.0, read_fs, read_ss); + } + set_esd_intensity(refl, sigma); set_redundancy(refl, cts); @@ -396,6 +412,12 @@ RefList *read_reflections_from_file(FILE *fh) RefList *read_reflections(const char *filename) { + return read_reflections2(filename, NULL); +} + + +RefList *read_reflections2(const char *filename, struct detector *det) +{ FILE *fh; RefList *out; @@ -410,7 +432,7 @@ RefList *read_reflections(const char *filename) return NULL; } - out = read_reflections_from_file(fh); + out = read_reflections_from_file2(fh, det); fclose(fh); diff --git a/libcrystfel/src/reflist-utils.h b/libcrystfel/src/reflist-utils.h index 86f61bbf..6bd2af47 100644 --- a/libcrystfel/src/reflist-utils.h +++ b/libcrystfel/src/reflist-utils.h @@ -33,7 +33,7 @@ #ifndef REFLIST_UTILS_H #define REFLIST_UTILS_H - +#include "image.h" #include "reflist.h" #include "cell.h" #include "symmetry.h" @@ -50,8 +50,10 @@ extern void write_reflections_to_file(FILE *fh, RefList *list); extern int write_reflist(const char *filename, RefList *list); extern int write_reflist_2(const char *filename, RefList *list, SymOpList *sym); +extern RefList *read_reflections_from_file2(FILE *fh, struct detector* det); extern RefList *read_reflections_from_file(FILE *fh); +extern RefList *read_reflections2(const char *filename, struct detector* det); extern RefList *read_reflections(const char *filename); extern int check_list_symmetry(RefList *list, const SymOpList *sym); diff --git a/libcrystfel/src/stream.c b/libcrystfel/src/stream.c index 9aaa53df..e1a67714 100644 --- a/libcrystfel/src/stream.c +++ b/libcrystfel/src/stream.c @@ -78,8 +78,11 @@ static int read_peaks(FILE *fh, struct image *image) do { char line[1024]; + char pn[32]; float x, y, d, intensity; int r; + struct panel *p = NULL; + int add_x, add_y; rval = fgets(line, 1023, fh); if ( rval == NULL ) continue; @@ -87,16 +90,22 @@ static int read_peaks(FILE *fh, struct image *image) if ( strcmp(line, PEAK_LIST_END_MARKER) == 0 ) return 0; - r = sscanf(line, "%f %f %f %f", &x, &y, &d, &intensity); - if ( (r != 4) && (!first) ) { + r = sscanf(line, "%f %f %s %f %f", &x, &y, pn, &d, &intensity); + if ( (r != 5) && (!first) ) { ERROR("Failed to parse peak list line.\n"); ERROR("The failed line was: '%s'\n", line); return 1; } first = 0; - if ( r == 4 ) { - image_add_feature(image->features, x, y, + + p = find_panel_by_name(image->det, pn); + + add_x = x-p->orig_min_fs+p->min_fs; + add_y = x-p->orig_min_ss+p->min_ss; + + if ( r == 5 ) { + image_add_feature(image->features, add_x, add_y, image, intensity, NULL); } @@ -112,13 +121,16 @@ static void write_peaks(struct image *image, FILE *ofh) int i; fprintf(ofh, PEAK_LIST_START_MARKER"\n"); - fprintf(ofh, " fs/px ss/px (1/d)/nm^-1 Intensity\n"); + fprintf(ofh, " fs/px ss/px panel (1/d)/nm^-1 Intensity\n"); for ( i=0; i<image_feature_count(image->features); i++ ) { struct imagefeature *f; struct rvec r; double q; + struct panel *p; + double write_fs, write_ss; + f = image_get_feature(image->features, i); if ( f == NULL ) continue; @@ -126,8 +138,12 @@ static void write_peaks(struct image *image, FILE *ofh) r = get_q(image, f->fs, f->ss, NULL, 1.0/image->lambda); q = modulus(r.u, r.v, r.w); - fprintf(ofh, "%7.2f %7.2f %10.2f %10.2f\n", - f->fs, f->ss, q/1.0e9, f->intensity); + p = find_panel(image->det,f->fs,f->ss); + write_fs = f->fs-p->min_fs+p->orig_min_fs; + write_ss = f->ss-p->min_ss+p->orig_min_ss; + + fprintf(ofh, "%7.2f %7.2f %s %10.2f %10.2f\n", + write_fs, write_ss, p->name, q/1.0e9, f->intensity); } @@ -135,7 +151,7 @@ static void write_peaks(struct image *image, FILE *ofh) } -static RefList *read_stream_reflections_2_1(FILE *fh) +static RefList *read_stream_reflections_2_1(FILE *fh, struct detector *det) { char *rval = NULL; int first = 1; @@ -149,6 +165,7 @@ static RefList *read_stream_reflections_2_1(FILE *fh) signed int h, k, l; float intensity, sigma, fs, ss; char phs[1024]; + char pn[32]; int cts; int r; Reflection *refl; @@ -159,25 +176,29 @@ static RefList *read_stream_reflections_2_1(FILE *fh) if ( strcmp(line, REFLECTION_END_MARKER) == 0 ) return out; - r = sscanf(line, "%i %i %i %f %s %f %i %f %f", - &h, &k, &l, &intensity, phs, &sigma, &cts, &fs, &ss); - if ( (r != 9) && (!first) ) { + r = sscanf(line, "%i %i %i %f %s %f %i %f %f %s", + &h, &k, &l, &intensity, phs, &sigma, &cts, &fs, &ss, pn); + if ( (r != 10) && (!first) ) { reflist_free(out); return NULL; } first = 0; - if ( r == 9 ) { + if ( r == 10 ) { double ph; char *v; + struct panel *p; + float write_fs, write_ss; + p = find_panel_by_name(det,pn); refl = add_refl(out, h, k, l); set_intensity(refl, intensity); - set_detector_pos(refl, 0.0, fs, ss); + write_ss = ss-p->orig_min_ss+p->min_ss; + write_fs = fs-p->orig_min_fs+p->min_fs; + set_detector_pos(refl, 0.0, write_fs, write_ss); set_esd_intensity(refl, sigma); set_redundancy(refl, cts); - ph = strtod(phs, &v); if ( v != phs ) set_phase(refl, deg2rad(ph)); @@ -190,7 +211,7 @@ static RefList *read_stream_reflections_2_1(FILE *fh) } -static RefList *read_stream_reflections(FILE *fh) +static RefList *read_stream_reflections(FILE *fh, struct detector *det) { char *rval = NULL; int first = 1; @@ -204,7 +225,10 @@ static RefList *read_stream_reflections(FILE *fh) signed int h, k, l; float intensity, sigma, fs, ss, pk, bg; int r; + char pn[32]; Reflection *refl; + struct panel *p; + float write_fs, write_ss; rval = fgets(line, 1023, fh); if ( rval == NULL ) continue; @@ -212,19 +236,22 @@ static RefList *read_stream_reflections(FILE *fh) if ( strcmp(line, REFLECTION_END_MARKER) == 0 ) return out; - r = sscanf(line, "%i %i %i %f %f %f %f %f %f", - &h, &k, &l, &intensity, &sigma, &pk, &bg, &fs, &ss); - if ( (r != 9) && (!first) ) { + r = sscanf(line, "%i %i %i %f %f %f %f %f %f %s", + &h, &k, &l, &intensity, &sigma, &pk, &bg, &fs, &ss, pn); + if ( (r != 10) && (!first) ) { reflist_free(out); return NULL; } first = 0; - if ( r == 9 ) { + if ( r == 10 ) { + p = find_panel_by_name(det,pn); refl = add_refl(out, h, k, l); set_intensity(refl, intensity); - set_detector_pos(refl, 0.0, fs, ss); + write_ss = ss-p->orig_min_ss+p->min_ss; + write_fs = fs-p->orig_min_fs+p->min_fs; + set_detector_pos(refl, 0.0, write_fs, write_ss); set_esd_intensity(refl, sigma); set_redundancy(refl, 1); set_peak(refl, pk); @@ -239,13 +266,13 @@ static RefList *read_stream_reflections(FILE *fh) } -static void write_stream_reflections(FILE *fh, RefList *list) +static void write_stream_reflections(FILE *fh, RefList *list, struct image *image) { Reflection *refl; RefListIterator *iter; fprintf(fh, " h k l I sigma(I) peak background" - " fs/px ss/px\n"); + " fs/px ss/px panel\n"); for ( refl = first_refl(list, &iter); refl != NULL; @@ -255,6 +282,8 @@ static void write_stream_reflections(FILE *fh, RefList *list) signed int h, k, l; double intensity, esd_i, bg, pk; double fs, ss; + double write_fs, write_ss; + struct panel *p = NULL; get_indices(refl, &h, &k, &l); get_detector_pos(refl, &fs, &ss); @@ -266,21 +295,25 @@ static void write_stream_reflections(FILE *fh, RefList *list) /* Reflections with redundancy = 0 are not written */ if ( get_redundancy(refl) == 0 ) continue; + p = find_panel(image->det,fs,ss); + write_fs = fs-p->min_fs+p->orig_min_fs; + write_ss = ss-p->min_ss+p->orig_min_ss; + fprintf(fh, - "%4i %4i %4i %10.2f %10.2f %10.2f %10.2f %6.1f %6.1f\n", - h, k, l, intensity, esd_i, pk, bg, fs, ss); + "%4i %4i %4i %10.2f %10.2f %10.2f %10.2f %6.1f %6.1f %s\n", + h, k, l, intensity, esd_i, pk, bg, write_fs, write_ss, p->name); } } -static void write_stream_reflections_2_1(FILE *fh, RefList *list) +static void write_stream_reflections_2_1(FILE *fh, RefList *list, struct image *image) { Reflection *refl; RefListIterator *iter; fprintf(fh, " h k l I phase sigma(I) " - " counts fs/px ss/px\n"); + " counts fs/px ss/px panel\n"); for ( refl = first_refl(list, &iter); refl != NULL; @@ -291,8 +324,10 @@ static void write_stream_reflections_2_1(FILE *fh, RefList *list) double intensity, esd_i, ph; int red; double fs, ss; + double write_fs, write_ss; char phs[16]; int have_phase; + struct panel *p = NULL; get_indices(refl, &h, &k, &l); get_detector_pos(refl, &fs, &ss); @@ -310,9 +345,13 @@ static void write_stream_reflections_2_1(FILE *fh, RefList *list) strncpy(phs, " -", 15); } + p = find_panel(image->det,fs,ss); + write_fs = fs-p->min_fs+p->orig_min_fs; + write_ss = ss-p->min_ss+p->orig_min_ss; + fprintf(fh, "%3i %3i %3i %10.2f %s %10.2f %7i %6.1f %6.1f\n", - h, k, l, intensity, phs, esd_i, red, fs, ss); + h, k, l, intensity, phs, esd_i, red, write_fs, write_ss); } } @@ -395,11 +434,11 @@ static void write_crystal(Stream *st, Crystal *cr, int include_reflections) fprintf(st->fh, REFLECTION_START_MARKER"\n"); if ( AT_LEAST_VERSION(st, 2, 2) ) { - write_stream_reflections(st->fh, reflist); + write_stream_reflections(st->fh, reflist, crystal_get_image(cr)); } else { /* This function writes like a normal reflection * list was written in stream 2.1 */ - write_stream_reflections_2_1(st->fh, reflist); + write_stream_reflections_2_1(st->fh, reflist, crystal_get_image(cr)); } fprintf(st->fh, REFLECTION_END_MARKER"\n"); @@ -619,9 +658,9 @@ static void read_crystal(Stream *st, struct image *image, StreamReadFlags srf) /* The reflection list format in the stream diverges * after 2.2 */ if ( AT_LEAST_VERSION(st, 2, 2) ) { - reflist = read_stream_reflections(st->fh); + reflist = read_stream_reflections(st->fh, image->det); } else { - reflist = read_stream_reflections_2_1(st->fh); + reflist = read_stream_reflections_2_1(st->fh, image->det); } if ( reflist == NULL ) { ERROR("Failed while reading reflections\n"); diff --git a/src/dw-hdfsee.c b/src/dw-hdfsee.c index 3a2d24d2..68e1b0a9 100644 --- a/src/dw-hdfsee.c +++ b/src/dw-hdfsee.c @@ -455,6 +455,7 @@ static int draw_stuff(cairo_surface_t *surf, DisplayWindow *dw) ss = f->ss; p = find_panel(dw->image->det, fs, ss); + if ( p == NULL ) continue; xs = (fs-p->min_fs)*p->fsx + (ss-p->min_ss)*p->ssx; @@ -1008,32 +1009,46 @@ static void load_features_from_file(struct image *image, const char *filename) do { char line[1024]; float intensity, sigma, fs, ss; + float add_fs, add_ss; char phs[1024]; + char pn[32]; int r; float cts; signed int h, k, l; + struct panel *p = NULL; rval = fgets(line, 1023, fh); if ( rval == NULL ) continue; chomp(line); /* Try long format (from stream) */ - r = sscanf(line, "%i %i %i %f %s %f %f %f %f", - &h, &k, &l, &intensity, phs, &sigma, &cts, &fs, &ss); - if ( r == 9 ) { + r = sscanf(line, "%i %i %i %f %s %f %f %f %f %s", + &h, &k, &l, &intensity, phs, &sigma, &cts, &fs, &ss, pn); + + if ( r == 10 ) { char name[32]; snprintf(name, 31, "%i %i %i", h, k, l); - image_add_feature(image->features, fs, ss, image, 1.0, + + p = find_panel_by_name(image->det, pn); + + add_fs = fs-p->orig_min_fs+p->min_fs; + add_ss = ss-p->orig_min_ss+p->min_ss; + image_add_feature(image->features, add_fs, add_ss, image, 1.0, strdup(name)); continue; } - r = sscanf(line, "%f %f", &fs, &ss); - if ( r != 2 ) continue; + r = sscanf(line, "%f %f %s", &fs, &ss, pn); + if ( r != 3 ) continue; + + p = find_panel_by_name(image->det, pn); - image_add_feature(image->features, fs, ss, image, 1.0, "peak"); + add_fs = fs-p->orig_min_fs+p->min_fs; + add_ss = ss-p->orig_min_ss+p->min_ss; + image_add_feature(image->features, add_fs, add_ss, image, 1.0, "peak"); } while ( rval != NULL ); + } @@ -1202,90 +1217,6 @@ static int save_geometry_file(DisplayWindow *dw) // return 0; //} -======= -//static int load_geometry_file(DisplayWindow *dw, struct image *image, -// const char *filename) -//{ -// struct detector *geom; -// GtkWidget *w; -// int using_loaded = 0; -// if ( dw->image->det == dw->loaded_geom ) using_loaded = 1; -// -// geom = get_detector_geometry(filename); -// if ( geom == NULL ) { -// displaywindow_error(dw, "Failed to load geometry file"); -// return -1; -// } -// fill_in_values(geom, dw->hdfile); -// -// if ( (1+geom->max_fs != dw->image->width) -// || (1+geom->max_ss != dw->image->height) ) { -// -// displaywindow_error(dw, "Geometry doesn't match image."); -// return -1; -// -// } -// -// /* Sort out the mess */ -// if ( dw->loaded_geom != NULL ) free_detector_geometry(dw->loaded_geom); -// dw->loaded_geom = geom; -// if ( using_loaded ) { -// dw->image->det = dw->loaded_geom; -// } -// -// w = gtk_ui_manager_get_widget(dw->ui, -// "/ui/displaywindow/view/usegeom"); -// gtk_widget_set_sensitive(GTK_WIDGET(w), TRUE); -// gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(w), TRUE); -// dw->use_geom = 1; -// -// return 0; -//} - - -//static gint displaywindow_loadgeom_response(GtkWidget *d, gint response, -// DisplayWindow *dw) -//{ -// if ( response == GTK_RESPONSE_ACCEPT ) { -// -// char *filename; -// -// filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(d)); -// -// if ( load_geometry_file(dw, dw->image, filename) == 0 ) { -// displaywindow_update(dw); -// } -// -// g_free(filename); -// -// } -// -// gtk_widget_destroy(d); -// -// return 0; -//} - - -//static gint displaywindow_load_geom(GtkWidget *widget, DisplayWindow *dw) -//{ -// GtkWidget *d; - -// d = gtk_file_chooser_dialog_new("Load Geometry File", -// GTK_WINDOW(dw->window), -// GTK_FILE_CHOOSER_ACTION_OPEN, -// GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, -// GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, -// NULL); - -// g_signal_connect(G_OBJECT(d), "response", -// G_CALLBACK(displaywindow_loadgeom_response), dw); - -// gtk_widget_show_all(d); - -// return 0; -//} - - static gint displaywindow_peak_overlay(GtkWidget *widget, DisplayWindow *dw) { GtkWidget *d; diff --git a/src/pattern_sim.c b/src/pattern_sim.c index 361a3bcd..98dee1b6 100644 --- a/src/pattern_sim.c +++ b/src/pattern_sim.c @@ -469,6 +469,12 @@ int main(int argc, char *argv[]) ERROR("You need to specify a geometry file with --geometry\n"); return 1; } + image.det = get_detector_geometry(geometry); + if ( image.det == NULL ) { + ERROR("Failed to read detector geometry from '%s'\n", geometry); + return 1; + } + free(geometry); if ( beamfile == NULL ) { ERROR("You need to specify a beam parameter file" @@ -509,7 +515,7 @@ int main(int argc, char *argv[]) RefList *reflections; - reflections = read_reflections(intfile); + reflections = read_reflections2(intfile, image.det); if ( reflections == NULL ) { ERROR("Problem reading input file %s\n", intfile); return 1; @@ -536,13 +542,6 @@ int main(int argc, char *argv[]) } - image.det = get_detector_geometry(geometry); - if ( image.det == NULL ) { - ERROR("Failed to read detector geometry from '%s'\n", geometry); - return 1; - } - free(geometry); - image.beam = get_beam_parameters(beamfile); if ( image.beam == NULL ) { ERROR("Failed to read beam parameters from '%s'\n", beamfile); diff --git a/src/process_image.c b/src/process_image.c index 1e2c7077..71c73357 100644 --- a/src/process_image.c +++ b/src/process_image.c @@ -77,6 +77,7 @@ void process_image(const struct index_args *iargs, struct pattern_args *pargs, if ( hdfile == NULL ) return; check = hdf5_read(hdfile, &image, iargs->element, 1); + if ( check ) { hdfile_close(hdfile); return; @@ -102,6 +103,11 @@ void process_image(const struct index_args *iargs, struct pattern_args *pargs, case PEAK_HDF5: /* Get peaks from HDF5 */ + + if ( !single_source(iargs->det, iargs->element)) { + ERROR("Peaks from HDF5 file not supported with multiple panel data sources.\n"); + } + if ( get_peaks(&image, hdfile, iargs->hdf5_peak_path) ) { ERROR("Failed to get peaks from HDF5 file.\n"); } |