/* * process_hkl.c * * Assemble and process FEL Bragg intensities * * Copyright © 2012-2013 Deutsches Elektronen-Synchrotron DESY, * a research centre of the Helmholtz Association. * Copyright © 2012 Lorenzo Galli * * Authors: * 2009-2013 Thomas White * 2011 Andrew Martin * 2012 Lorenzo Galli * * This file is part of CrystFEL. * * CrystFEL is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * CrystFEL is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with CrystFEL. If not, see . * */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include #include "utils.h" #include "statistics.h" #include "reflist-utils.h" #include "symmetry.h" #include "stream.h" #include "reflist.h" #include "image.h" #include "crystal.h" #include "thread-pool.h" static void show_help(const char *s) { printf("Syntax: %s [options]\n\n", s); printf( "Assemble and process FEL Bragg intensities.\n" "\n" " -h, --help Display this help message.\n" " -i, --input= Specify input filename (\"-\" for stdin).\n" " -o, --output= Specify output filename for merged intensities\n" " Default: processed.hkl).\n" " -y, --symmetry= Merge according to point group .\n" "\n" " --start-after= Skip n patterns at the start of the stream.\n" " --stop-after= Stop after processing n patterns.\n" " -g, --histogram= Calculate the histogram of measurements for this\n" " reflection.\n" " -z, --hist-parameters Set the range for the histogram and the number of\n" " = bins. \n" "\n" " --scale Scale each pattern for best fit with the current\n" " model.\n" " --reference= Compare against intensities from when\n" " scaling. \n" " --no-polarisation Disable polarisation correction.\n" " --min-measurements= Require at least measurements before a\n" " reflection appears in the output. Default: 2\n" ); } static void plot_histogram(double *vals, int n, float hist_min, float hist_max, int nbins) { int i; double max = -INFINITY; double min = +INFINITY; double step; int histo[nbins]; FILE *fh; fh = fopen("histogram.dat", "w"); if ( fh == NULL ) { ERROR("Couldn't open 'histogram.dat'\n"); return; } if ( hist_min == hist_max ) { for ( i=0; i max ) max = vals[i]; if ( vals[i] < min ) min = vals[i]; } } else { min = hist_min; max = hist_max; } STATUS("min max nbins: %f %f %i\n", min, max, nbins); min--; max++; for ( i=0; i min) && (vals[i] < max) ) { bin = (vals[i]-min)/step; histo[bin]++; } } for ( i=0; ilambda; tt = angle_between(0.0, 0.0, 1.0, xl, yl, zl+ool); phi = atan2(yl, xl); pa = pow(sin(phi)*sin(tt), 2.0); pb = pow(cos(tt), 2.0); pol = 1.0 - 2.0*(1.0-pa) + (1.0+pb); intensity /= pol; } cur_intensity = get_intensity(model_version); set_intensity(model_version, cur_intensity + intensity); cur_redundancy = get_redundancy(model_version); set_redundancy(model_version, cur_redundancy+1); cur_sumsq = get_temp1(model_version); set_temp1(model_version, cur_sumsq + pow(intensity, 2.0)); if ( hist_vals != NULL ) { if ( (h==hist_h) && (k==hist_k) && (l==hist_l) ) { hist_vals[*hist_n] = intensity; *hist_n += 1; } } } return 0; } static void display_progress(int n_images, int n_crystals, int n_crystals_used) { if ( !isatty(STDERR_FILENO) ) return; if ( tcgetpgrp(STDERR_FILENO) != getpgrp() ) return; pthread_mutex_lock(&stderr_lock); fprintf(stderr, "\r%i images processed, %i crystals, %i crystals used.", n_images, n_crystals, n_crystals_used); pthread_mutex_unlock(&stderr_lock); fflush(stdout); } static int merge_all(Stream *st, RefList *model, RefList *reference, const SymOpList *sym, double *hist_vals, signed int hist_h, signed int hist_k, signed int hist_l, int *hist_i, int config_nopolar, int min_measurements) { int rval; int n_images = 0; int n_crystals = 0; int n_crystals_used = 0; Reflection *refl; RefListIterator *iter; do { struct image image; int i; image.det = NULL; /* Get data from next chunk */ rval = read_chunk(st, &image); if ( rval ) break; n_images++; for ( i=0; i ", hist_h, hist_k, hist_l); /* Put into the asymmetric cell for the target group */ get_asymm(sym, hist_h, hist_k, hist_l, &hist_h, &hist_k, &hist_l); STATUS("%i %i %i\n", hist_h, hist_k, hist_l); } if ( histo_params != NULL ) { int rr; rr = sscanf(histo_params, "%f,%f,%i", &hist_min, &hist_max, &hist_nbins); if ( rr != 3 ) { ERROR("Invalid parameters for '--hist-parameters'\n"); return 1; } free(histo_params); if ( hist_max <= hist_min ) { ERROR("Invalid range for '--hist-parameters'. " "Make sure that 'max' is greater than 'min'.\n"); return 1; } } hist_i = 0; r = merge_all(st, model, NULL, sym, hist_vals, hist_h, hist_k, hist_l, &hist_i, config_nopolar, min_measurements); fprintf(stderr, "\n"); if ( r ) { ERROR("Error while reading stream.\n"); return 1; } if ( config_scale ) { RefList *reference; if ( rewind_stream(st) ) { ERROR("Couldn't rewind stream - scaling cannot be " "performed.\n"); } else { int r; STATUS("Extra pass for scaling...\n"); reference = copy_reflist(model); reflist_free(model); model = reflist_new(); r = merge_all(st, model, reference, sym, hist_vals, hist_h, hist_k, hist_l, &hist_i, config_nopolar, min_measurements); fprintf(stderr, "\n"); if ( r ) { ERROR("Error while reading stream.\n"); return 1; } reflist_free(reference); } } if ( space_for_hist && (hist_i >= space_for_hist) ) { ERROR("Histogram array was too small!\n"); } if ( hist_vals != NULL ) { STATUS("%i %i %i was seen %i times.\n", hist_h, hist_k, hist_l, hist_i); plot_histogram(hist_vals, hist_i, hist_min, hist_max, hist_nbins); } write_reflist(output, model); close_stream(st); free(sym); reflist_free(model); free(output); return 0; }