From 4a235243ee6838b2745d9b3e8999d29fa76c394a Mon Sep 17 00:00:00 2001 From: Thomas White Date: Mon, 13 Sep 2010 10:54:34 +0200 Subject: facetron: Add options --- src/facetron.c | 167 +++++++++++++++--------------------------------------- src/indexamajig.c | 40 ------------- src/utils.c | 41 ++++++++++++++ src/utils.h | 6 ++ 4 files changed, 93 insertions(+), 161 deletions(-) diff --git a/src/facetron.c b/src/facetron.c index 42589526..c7c22e30 100644 --- a/src/facetron.c +++ b/src/facetron.c @@ -39,7 +39,10 @@ static void show_help(const char *s) "\n" " -i, --input= Specify the name of the input stream.\n" " Can be '-' for stdin.\n" -" -g. --geometry= Get detector geometry from file.\n" +" -g. --geometry= Get detector geometry from file.\n" +" -x, --prefix=

Prefix filenames from input file with

.\n" +" --basename Remove the directory parts of the filenames.\n" +" --no-check-prefix Don't attempt to correct the --prefix.\n" ); } @@ -113,122 +116,28 @@ static int find_chunk(FILE *fh, UnitCell **cell, char **filename) } -static void get_trial_cell(UnitCell *out, UnitCell *in, int i, double step) +static char *twiddle_filename(char *filename, int config_basename, + const char *prefix) { - double asx, asy, asz; - double bsx, bsy, bsz; - double csx, csy, csz; - - cell_get_reciprocal(in, &asx, &asy, &asz, &bsx, &bsy, &bsz, - &csx, &csy, &csz); - - switch ( i ) { - case 0 : asx += step; break; - case 1 : asx -= step; break; - case 2 : asy += step; break; - case 3 : asy -= step; break; - case 4 : asz += step; break; - case 5 : asz -= step; break; - case 6 : bsx += step; break; - case 7 : bsx -= step; break; - case 8 : bsy += step; break; - case 9 : bsy -= step; break; - case 10 : bsz += step; break; - case 11 : bsz -= step; break; - case 12 : csx += step; break; - case 13 : csx -= step; break; - case 14 : csy += step; break; - case 15 : csy -= step; break; - case 16 : csz += step; break; - case 17 : csz -= step; break; - default : break; + char *f3; + size_t len; + + if ( config_basename ) { + char *f2; + /* Happy with either the GNU or non-GNU versions of basename() + * here. Because _GNU_SOURCE is defined in configure.ac, we + * should have the GNU version. */ + f2 = strdup(basename(filename)); + free(filename); + filename = f2; } - cell_set_reciprocal(out, asx, asy, asz, bsx, bsy, bsz, csx, csy, csz); -} - - -static void try_refine(struct image *image, UnitCell *cell, - double da, double dw, double step) -{ - struct reflhit *hits; - int np; - double itot; - UnitCell *trial_cell; - int did_something; - - trial_cell = cell_new(); - - hits = find_intersections(image, cell, da, dw, &np, 0); - itot = integrate_all(image, hits, np); - STATUS("%f\n", itot); - - do { - - int i; - - did_something = 0; - - for ( i=0; i<18; i++ ) { - - struct reflhit *trial_hits; - double trial_itot; - - get_trial_cell(trial_cell, cell, i, step); - - trial_hits = find_intersections(image, trial_cell, - da, dw, &np, 0); - trial_itot = integrate_all(image, hits, np); - - if ( trial_itot > itot ) { - - double asx, asy, asz; - double bsx, bsy, bsz; - double csx, csy, csz; - - cell_get_reciprocal(trial_cell, &asx, &asy, - &asz, &bsx, &bsy, &bsz, - &csx, &csy, &csz); - cell_set_reciprocal(cell, asx, asy, asz, bsx, - bsy, bsz, csx, csy, csz); - - itot = trial_itot; - free(hits); - hits = trial_hits; - - did_something = 1; + len = strlen(prefix)+strlen(filename)+1; + f3 = malloc(len); + snprintf(f3, len, "%s%s", prefix, filename); - } else { - - free(trial_hits); - - } - } - - } while ( did_something ); - - free(trial_cell); -} - - -/* Predict peaks */ -static void pre_refine(struct image *image, UnitCell *cell, - double *da, double *dw) -{ - double asx, asy, asz; - double bsx, bsy, bsz; - double csx, csy, csz; - double istep, step; - - /* Start by changing parameters by 1% */ - cell_get_reciprocal(cell, &asx, &asy, - &asz, &bsx, &bsy, &bsz, - &csx, &csy, &csz); - istep = (asx+asy+asz+bsx+bsy+bsz+csx+csy+csz)/900.0; - - for ( step=istep; step>istep/100.0; step-=istep/100.0 ) { - try_refine(image, cell, *da, *dw, step); - } + free(filename); + return f3; } @@ -241,17 +150,23 @@ int main(int argc, char *argv[]) char *filename; struct detector *det; char *geometry = NULL; + char *prefix = NULL; + int config_basename = 0; + int config_checkprefix = 1; /* Long options */ const struct option longopts[] = { {"help", 0, NULL, 'h'}, {"input", 1, NULL, 'i'}, {"geometry", 1, NULL, 'g'}, + {"prefix", 1, NULL, 'x'}, + {"basename", 0, &config_basename, 1}, + {"no-check-prefix", 0, &config_checkprefix, 0}, {0, 0, NULL, 0} }; /* Short options */ - while ((c = getopt_long(argc, argv, "hi:g:", longopts, NULL)) != -1) { + while ((c = getopt_long(argc, argv, "hi:g:x:", longopts, NULL)) != -1) { switch (c) { case 'h' : @@ -266,6 +181,10 @@ int main(int argc, char *argv[]) geometry = strdup(optarg); break; + case 'x' : + prefix = strdup(optarg); + break; + case 0 : break; @@ -287,6 +206,14 @@ int main(int argc, char *argv[]) return ENOENT; } + if ( prefix == NULL ) { + prefix = strdup(""); + } else { + if ( config_checkprefix ) { + prefix = check_prefix(prefix); + } + } + det = get_detector_geometry(geometry); if ( det == NULL ) { ERROR("Failed to read detector geometry from '%s'\n", geometry); @@ -299,9 +226,11 @@ int main(int argc, char *argv[]) struct image image; struct hdfile *hdfile; - double da, dw; + struct reflhit *hits; int np; + filename = twiddle_filename(filename, config_basename, prefix); + STATUS("Integrating intensities from '%s'\n", filename); image.det = det; @@ -316,12 +245,7 @@ int main(int argc, char *argv[]) hdf5_read(hdfile, &image, 0); - da = 5.0e-3; /* Initial divergence */ - dw = 3.0/100.0; /* Initial bandwidth */ - - pre_refine(&image, cell, &da, &dw); - - find_intersections(&image, cell, da, dw, &np, 1); + hits = find_intersections(&image, cell, 5.0e-3, 0.0001, &np, 1); hdfile_close(hdfile); cell_free(cell); @@ -334,6 +258,7 @@ int main(int argc, char *argv[]) free(det->panels); free(det); fclose(fh); + free(prefix); return 0; } diff --git a/src/indexamajig.c b/src/indexamajig.c index f63936c5..39905fb4 100644 --- a/src/indexamajig.c +++ b/src/indexamajig.c @@ -24,8 +24,6 @@ #include #include #include -#include -#include #include "utils.h" #include "hdf5-file.h" @@ -171,44 +169,6 @@ static void show_help(const char *s) } -static char *check_prefix(char *prefix) -{ - int r; - struct stat statbuf; - char *new; - size_t len; - - /* Is "prefix" a directory? */ - r = stat(prefix, &statbuf); - if ( r != 0 ) { - /* "prefix" probably doesn't exist. This is fine - assume - * the user knows what they're doing, and that "prefix" - * suffixed with the actual filename will produce something - * sensible. */ - return prefix; - } - - if ( !S_ISDIR(statbuf.st_mode) ) { - /* Also fine, as above. */ - return prefix; - } - - /* Does the prefix end in a slash? */ - if ( prefix[strlen(prefix)-1] == '/' ) { - /* This looks sensible. */ - return prefix; - } - - STATUS("Your prefix ('%s') is a directory, but doesn't end" - " with a slash. I'm going to add it for you.\n", prefix); - len = strlen(prefix)+2; - new = malloc(len); - snprintf(new, len, "%s/", prefix); - free(prefix); - return new; -} - - static struct image *get_simage(struct image *template, int alternate) { struct image *image; diff --git a/src/utils.c b/src/utils.c index a47f234f..196cda3f 100644 --- a/src/utils.c +++ b/src/utils.c @@ -13,6 +13,8 @@ #include #include #include +#include +#include #include "utils.h" #include "image.h" @@ -442,3 +444,42 @@ ReflItemList *intersection_items(ReflItemList *i1, ReflItemList *i2) return res; } + + +char *check_prefix(char *prefix) +{ + int r; + struct stat statbuf; + char *new; + size_t len; + + /* Is "prefix" a directory? */ + r = stat(prefix, &statbuf); + if ( r != 0 ) { + /* "prefix" probably doesn't exist. This is fine - assume + * the user knows what they're doing, and that "prefix" + * suffixed with the actual filename will produce something + * sensible. */ + return prefix; + } + + if ( !S_ISDIR(statbuf.st_mode) ) { + /* Also fine, as above. */ + return prefix; + } + + /* Does the prefix end in a slash? */ + if ( prefix[strlen(prefix)-1] == '/' ) { + /* This looks sensible. */ + return prefix; + } + + STATUS("Your prefix ('%s') is a directory, but doesn't end" + " with a slash. I'm going to add it for you.\n", prefix); + STATUS("If this isn't what you want, run with --no-check-prefix.\n"); + len = strlen(prefix)+2; + new = malloc(len); + snprintf(new, len, "%s/", prefix); + free(prefix); + return new; +} diff --git a/src/utils.h b/src/utils.h index a8d7aa2d..dfd1913f 100644 --- a/src/utils.h +++ b/src/utils.h @@ -203,10 +203,16 @@ extern void union_op_items(ReflItemList *items, ReflItemList *newi); extern void union_items(ReflItemList *items, ReflItemList *newi); extern ReflItemList *intersection_items(ReflItemList *i1, ReflItemList *i2); + /* ------------------------------ Message macros ---------------------------- */ #define ERROR(...) fprintf(stderr, __VA_ARGS__) #define STATUS(...) fprintf(stderr, __VA_ARGS__) +/* ------------------------------ File handling ----------------------------- */ + +extern char *check_prefix(char *prefix); + + #endif /* UTILS_H */ -- cgit v1.2.3