From 7e5536832b22036a634a74e16073b4762a416e29 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Wed, 21 Jul 2021 13:26:37 +0200 Subject: Reduce variable scope where possible --- libcrystfel/src/cell-utils.c | 2 +- libcrystfel/src/datatemplate.c | 2 +- libcrystfel/src/filters.c | 5 +++-- libcrystfel/src/image.c | 2 +- libcrystfel/src/index.c | 3 ++- libcrystfel/src/indexers/asdf.c | 21 ++++++++++++++++----- libcrystfel/src/indexers/felix.c | 2 +- libcrystfel/src/indexers/fromfile.c | 3 ++- libcrystfel/src/indexers/mosflm.c | 5 ++--- libcrystfel/src/indexers/xds.c | 2 +- libcrystfel/src/integration.c | 3 +-- libcrystfel/src/peaks.c | 4 ++-- libcrystfel/src/reflist-utils.c | 2 +- libcrystfel/src/stream.c | 6 +++--- src/ambigator.c | 3 +-- src/check_hkl.c | 2 +- src/cl-utils.c | 7 +++++-- src/get_hkl.c | 2 +- src/gui_project.c | 3 +-- src/im-zmq.c | 3 ++- src/indexamajig.c | 9 +++++---- src/list_events.c | 2 +- src/partial_sim.c | 13 +++++++------ src/pattern_sim.c | 4 +--- src/post-refinement.c | 8 +++++--- src/process_hkl.c | 11 +++++------ src/render_hkl.c | 3 ++- src/time-accounts.c | 2 +- src/whirligig.c | 3 +-- 29 files changed, 76 insertions(+), 61 deletions(-) diff --git a/libcrystfel/src/cell-utils.c b/libcrystfel/src/cell-utils.c index 2f3dfc6b..cdd4156b 100644 --- a/libcrystfel/src/cell-utils.c +++ b/libcrystfel/src/cell-utils.c @@ -1220,7 +1220,6 @@ int cell_is_sensible(UnitCell *cell) int validate_cell(UnitCell *cell) { int err = 0; - char cen, ua; if ( cell_has_parameters(cell) && !cell_is_sensible(cell) ) { ERROR("WARNING: Unit cell parameters are not sensible.\n"); @@ -1241,6 +1240,7 @@ int validate_cell(UnitCell *cell) /* For monoclinic A, B or C centering, the unique axis must be something * other than the centering. */ if ( cell_get_lattice_type(cell) == L_MONOCLINIC ) { + char cen, ua; cen = cell_get_centering(cell); ua = cell_get_unique_axis(cell); if ( ((cen == 'A') && (ua == 'a')) diff --git a/libcrystfel/src/datatemplate.c b/libcrystfel/src/datatemplate.c index 9f8f102c..a2b974a7 100644 --- a/libcrystfel/src/datatemplate.c +++ b/libcrystfel/src/datatemplate.c @@ -646,9 +646,9 @@ static int parse_field_for_panel(struct panel_template *panel, const char *key, reject = 1; } } else if ( strncmp(key, "dim", 3) == 0) { - int dim_entry; char *endptr; if ( key[3] != '\0' ) { + int dim_entry; dim_entry = strtoul(key+3, &endptr, 10); if ( endptr[0] != '\0' ) { ERROR("Invalid dimension number %s\n", diff --git a/libcrystfel/src/filters.c b/libcrystfel/src/filters.c index a9c8737a..5f6a0b2d 100644 --- a/libcrystfel/src/filters.c +++ b/libcrystfel/src/filters.c @@ -99,13 +99,14 @@ void filters_fudge_gslcblas() #define SWAP(a,b) { float t=(a);(a)=(b);(b)=t; } static float kth_smallest(float *a, int n, int k) { - long i, j, l, m; - float x; + long l, m; l = 0; m = n-1; while ( l < m ) { + long i, j; + float x; x=a[k]; i=l; j=m; diff --git a/libcrystfel/src/image.c b/libcrystfel/src/image.c index 3c5af479..9fc647ab 100644 --- a/libcrystfel/src/image.c +++ b/libcrystfel/src/image.c @@ -1140,7 +1140,6 @@ static int create_satmap(struct image *image, * but it isn't this one. Therefore make a fake * saturation map */ - long int j; int p_w, p_h; p_w = p->orig_max_fs - p->orig_min_fs + 1; @@ -1149,6 +1148,7 @@ static int create_satmap(struct image *image, image->sat[i] = malloc(p_w*p_h*sizeof(float)); if ( image->sat[i] != NULL ) { + long int j; for ( j=0; jsat[i][j] = INFINITY; } diff --git a/libcrystfel/src/index.c b/libcrystfel/src/index.c index d8a135fc..e7b8b8cf 100644 --- a/libcrystfel/src/index.c +++ b/libcrystfel/src/index.c @@ -922,7 +922,6 @@ void index_pattern_3(struct image *image, IndexingPrivate *ipriv, int *ping, for ( n=0; nn_methods; n++ ) { int done = 0; - int r; int ntry = 0; int success = 0; @@ -932,6 +931,8 @@ void index_pattern_3(struct image *image, IndexingPrivate *ipriv, int *ping, do { + int r; + r = try_indexer(image, ipriv->methods[n], ipriv, ipriv->engine_private[n], last_task); diff --git a/libcrystfel/src/indexers/asdf.c b/libcrystfel/src/indexers/asdf.c index 7030a3e7..a1f8f50d 100644 --- a/libcrystfel/src/indexers/asdf.c +++ b/libcrystfel/src/indexers/asdf.c @@ -364,7 +364,7 @@ static float find_ds_fft(double *projections, int N_projections, double d_max, memcpy(projections_sorted, projections, sizeof(double) * n); qsort(projections_sorted, n, sizeof(double), compare_doubles); - int i, k; + int i; int N = fftw.N; // number of points in fft calculation double *in = fftw.in; @@ -376,6 +376,7 @@ static float find_ds_fft(double *projections, int N_projections, double d_max, } for ( i=0; i max) { max = a; @@ -581,8 +582,6 @@ static int refine_asdf_cell(struct asdf_cell *c, gsl_vector **reflections, static int reduce_asdf_cell(struct asdf_cell *cl) { - double a, b, c, alpha, beta, gamma, ab, bc, ca, bb, cc; - gsl_vector *va = gsl_vector_alloc(3); gsl_vector *vb = gsl_vector_alloc(3); gsl_vector *vc = gsl_vector_alloc(3); @@ -591,6 +590,9 @@ static int reduce_asdf_cell(struct asdf_cell *cl) int n = 0; while ( changed ) { + + double a, b, c, alpha, beta, gamma, ab, bc, ca; + n += 1; changed = 0; @@ -612,6 +614,8 @@ static int reduce_asdf_cell(struct asdf_cell *cl) if ( changed == 0 ) { + double bb; + if ( gamma < 90 ) { gsl_vector_scale(vb, -1); gamma = 180 - gamma; @@ -634,6 +638,8 @@ static int reduce_asdf_cell(struct asdf_cell *cl) if ( changed == 0 ) { + double cc; + if ( beta < 90 ) { gsl_vector_scale(vc, -1); beta = 180 - beta; @@ -659,6 +665,9 @@ static int reduce_asdf_cell(struct asdf_cell *cl) } if ( changed == 0 ) { + + double cc; + if ( alpha < 90 ) { gsl_vector_scale(vc, -1); beta = 180 - beta; @@ -901,10 +910,12 @@ static int **generate_triplets(int N_reflections, int N_triplets_max, int *N) return 0; } - int is_in_triplets; n = 0; while ( n < N_triplets ) { + + int is_in_triplets; + /* Generate three different integer numbers < N_reflections */ i = rand() % N_reflections; j = i; diff --git a/libcrystfel/src/indexers/felix.c b/libcrystfel/src/indexers/felix.c index b7d2a8a3..db7da8c4 100644 --- a/libcrystfel/src/indexers/felix.c +++ b/libcrystfel/src/indexers/felix.c @@ -131,7 +131,6 @@ static int read_felix(struct felix_private *gp, struct image *image, float mean_ia; int ngv; char line[1024]; - int r; int n_crystals = 0; fh = fopen(filename, "r"); @@ -151,6 +150,7 @@ static int read_felix(struct felix_private *gp, struct image *image, Crystal *cr; UnitCell *cell; + int r; /* One line per grain */ if ( fgets( line, 1024, fh ) == NULL ) { diff --git a/libcrystfel/src/indexers/fromfile.c b/libcrystfel/src/indexers/fromfile.c index d7dfbd09..1716dd66 100644 --- a/libcrystfel/src/indexers/fromfile.c +++ b/libcrystfel/src/indexers/fromfile.c @@ -228,7 +228,6 @@ void *fromfile_prepare(IndexingMethod *indm, struct fromfile_options *opts) float vals[11]; struct fromfile_key key; Crystal *cr; - UnitCell *cell; size_t len; int n_sp; struct fromfile_entry *item = NULL; @@ -286,6 +285,8 @@ void *fromfile_prepare(IndexingMethod *indm, struct fromfile_options *opts) } else { + UnitCell *cell; + cr = crystal_new(); /* mm -> m */ diff --git a/libcrystfel/src/indexers/mosflm.c b/libcrystfel/src/indexers/mosflm.c index 129e6859..e77f6384 100644 --- a/libcrystfel/src/indexers/mosflm.c +++ b/libcrystfel/src/indexers/mosflm.c @@ -481,7 +481,6 @@ static char *mosflm_spacegroup_for_lattice(UnitCell *cell) static void mosflm_send_next(struct image *image, struct mosflm_data *mosflm) { char tmp[256]; - char cen; double wavelength; double a = 0, b = 0, c = 0, alpha = 0, beta = 0, gamma = 0; @@ -548,6 +547,7 @@ static void mosflm_send_next(struct image *image, struct mosflm_data *mosflm) case 9 : if ( mosflm->mp->indm & INDEXING_USE_CELL_PARAMETERS ) { + char cen; cell_get_parameters(mosflm->mp->template, &a, &b, &c, &alpha, &beta, &gamma); cen = cell_get_centering(mosflm->mp->template); @@ -886,9 +886,8 @@ const char *mosflm_probe(UnitCell *cell) fh = fdopen(pty, "r"); for ( l=0; l<10; l++ ) { - char *pos; if ( fgets(line, 1024, fh) != NULL ) { - pos = strstr(line, "Mosflm version "); + char *pos = strstr(line, "Mosflm version "); if ( pos != NULL ) ok = 1; } } diff --git a/libcrystfel/src/indexers/xds.c b/libcrystfel/src/indexers/xds.c index 3936f791..8ef496cf 100644 --- a/libcrystfel/src/indexers/xds.c +++ b/libcrystfel/src/indexers/xds.c @@ -523,8 +523,8 @@ const char *xds_probe(UnitCell *cell) fh = fdopen(pty, "r"); for ( l=0; l<10; l++ ) { - char *pos; if ( fgets(line, 1024, fh) != NULL ) { + char *pos; pos = strstr(line, "** XDS **"); if ( pos != NULL ) { ok = 1; diff --git a/libcrystfel/src/integration.c b/libcrystfel/src/integration.c index 2375388f..e797d26b 100644 --- a/libcrystfel/src/integration.c +++ b/libcrystfel/src/integration.c @@ -340,10 +340,9 @@ static void fit_gradient_bg(struct intcontext *ic, struct peak_box *bx) for ( p=0; pw; p++ ) { for ( q=0; qw; q++ ) { - double bi; - if ( bx->bm[p + ic->w*q] == BM_BG ) { + double bi; bi = boxi(ic, bx, p, q); addv(v, 0, bi*p); diff --git a/libcrystfel/src/peaks.c b/libcrystfel/src/peaks.c index 689239e5..d260b66d 100644 --- a/libcrystfel/src/peaks.c +++ b/libcrystfel/src/peaks.c @@ -288,7 +288,6 @@ static void search_peaks_in_panel(struct image *image, float threshold, double grad; int mask_fs, mask_ss; int s_fs, s_ss; - double max; unsigned int did_something; int r; int saturated; @@ -321,6 +320,7 @@ static void search_peaks_in_panel(struct image *image, float threshold, do { + double max; max = data[mask_fs+stride*mask_ss]; did_something = 0; @@ -589,7 +589,6 @@ int indexing_peak_check(struct image *image, Crystal **crystals, int n_cryst, struct imagefeature *f; double q[3]; - double h,k,l,hd,kd,ld; int j; int ok = 0; @@ -604,6 +603,7 @@ int indexing_peak_check(struct image *image, Crystal **crystals, int n_cryst, double bx, by, bz; double cx, cy, cz; double dx, dy; + double h,k,l,hd,kd,ld; crystal_get_det_shift(crystals[j], &dx, &dy); diff --git a/libcrystfel/src/reflist-utils.c b/libcrystfel/src/reflist-utils.c index 2b250eff..4a284c07 100644 --- a/libcrystfel/src/reflist-utils.c +++ b/libcrystfel/src/reflist-utils.c @@ -1099,13 +1099,13 @@ int write_to_mtz(RefList *reflist, { signed int h, k, l; double one_over_d; - int isym; get_indices(refl, &h, &k, &l); one_over_d = 2.0*resolution(cell, h, k, l); if ( (one_over_d > min_res) && (one_over_d < max_res) ) { + int isym; float refldata[7]; signed int nh, nk, nl; signed int fh, fk, fl; diff --git a/libcrystfel/src/stream.c b/libcrystfel/src/stream.c index 8ffaed1a..6c716fda 100644 --- a/libcrystfel/src/stream.c +++ b/libcrystfel/src/stream.c @@ -242,7 +242,6 @@ static RefList *read_stream_reflections_2_3(Stream *st) float intensity, sigma, fs, ss, pk, bg; char pn[32]; int r; - Reflection *refl; rval = fgets(line, 1023, st->fh); st->ln++; @@ -264,6 +263,7 @@ static RefList *read_stream_reflections_2_3(Stream *st) if ( r == 10 ) { + Reflection *refl; refl = add_refl(out, h, k, l); if ( refl == NULL ) { ERROR("Failed to add reflection\n"); @@ -313,7 +313,6 @@ static RefList *read_stream_reflections_2_1(Stream *st) char phs[1024]; int cts; int r; - Reflection *refl; rval = fgets(line, 1023, st->fh); st->ln++; @@ -333,6 +332,7 @@ static RefList *read_stream_reflections_2_1(Stream *st) first = 0; if ( r == 9 ) { + Reflection *refl; double ph; char *v; @@ -388,7 +388,6 @@ static RefList *read_stream_reflections_2_2(Stream *st) signed int h, k, l; float intensity, sigma, fs, ss, pk, bg; int r; - Reflection *refl; rval = fgets(line, 1023, st->fh); st->ln++; @@ -407,6 +406,7 @@ static RefList *read_stream_reflections_2_2(Stream *st) first = 0; if ( r == 9 ) { + Reflection *refl; refl = add_refl(out, h, k, l); if ( refl == NULL ) { ERROR("Failed to add reflection\n"); diff --git a/src/ambigator.c b/src/ambigator.c index 1657bfb6..02252cd7 100644 --- a/src/ambigator.c +++ b/src/ambigator.c @@ -119,7 +119,6 @@ static struct flist *asymm_and_merge(RefList *in, const SymOpList *sym, signed int h, k, l; signed int ha, ka, la; Reflection *cr; - double res; int group = 0; get_indices(refl, &h, &k, &l); @@ -127,7 +126,7 @@ static struct flist *asymm_and_merge(RefList *in, const SymOpList *sym, if ( cell == NULL ) { ERROR("Can't calculate resolution cutoff - no cell\n"); } else { - res = 2.0*resolution(cell, h, k, l); + double res = 2.0*resolution(cell, h, k, l); if ( res < rmin ) continue; if ( res > rmax ) continue; if ( auto_res ) { diff --git a/src/check_hkl.c b/src/check_hkl.c index c4532f31..aa972a7f 100644 --- a/src/check_hkl.c +++ b/src/check_hkl.c @@ -350,12 +350,12 @@ static void wilson_plot(RefList *list, UnitCell *cell, const SymOpList *sym, } else { int bs = (pow(3.125e9/2.0, 2.0) - s2min)/s2step + 1; double lnk, minus2B, cov00, cov01, cov11, sumsq; - double B; if ( nbins - bs < 3 ) { ERROR("Not enough bins to estimate B factor\n"); ERROR("Resolution of data, or number of bins, is too " "low.\n"); } else { + double B; double *s2fit; double *lnifit; int nbfit = 0; diff --git a/src/cl-utils.c b/src/cl-utils.c index c2c1bf1d..1b6667c4 100644 --- a/src/cl-utils.c +++ b/src/cl-utils.c @@ -188,7 +188,7 @@ cl_device_id get_cl_dev(cl_context ctx, int n) cl_device_id *dev; cl_int r; size_t size; - int i, num_devs; + int num_devs; /* Get the required size of the array */ r = clGetContextInfo(ctx, CL_CONTEXT_DEVICES, 0, NULL, &size); @@ -212,6 +212,8 @@ cl_device_id get_cl_dev(cl_context ctx, int n) if ( n < 0 ) { + int i; + STATUS("Available devices:\n"); for ( i=0; ifilenames != NULL ) { + int i; for ( i=0; in_frames; i++ ) { free(proj->filenames[i]); free(proj->events[i]); diff --git a/src/im-zmq.c b/src/im-zmq.c index 4273b600..3fd04428 100644 --- a/src/im-zmq.c +++ b/src/im-zmq.c @@ -62,7 +62,6 @@ struct im_zmq *im_zmq_connect(const char *zmq_address, const char *zmq_request) { struct im_zmq *z; - int i; z = malloc(sizeof(struct im_zmq)); if ( z == NULL ) return NULL; @@ -93,6 +92,8 @@ struct im_zmq *im_zmq_connect(const char *zmq_address, if ( zmq_request == NULL ) { + int i; + /* SUB mode */ if ( n_subscriptions == 0 ) { ERROR("WARNING: No ZeroMQ subscriptions. You should " diff --git a/src/indexamajig.c b/src/indexamajig.c index d9b0114d..9bbf7e5d 100644 --- a/src/indexamajig.c +++ b/src/indexamajig.c @@ -166,12 +166,11 @@ static void write_json_radii(FILE *fh, const char *name, static void write_methods(FILE *fh, const char *name, IndexingPrivate *ipriv) { - int i, n; - const IndexingMethod *methods; - fprintf(fh, " \"%s\": [", name); if ( ipriv != NULL ) { + const IndexingMethod *methods; + int i, n; methods = indexing_methods(ipriv, &n); for ( i=0; iipriv); write_json_cell(fh, "target_cell", args->cell); diff --git a/src/list_events.c b/src/list_events.c index 960c8929..67b27119 100644 --- a/src/list_events.c +++ b/src/list_events.c @@ -153,13 +153,13 @@ int main(int argc, char *argv[]) do { char filename[1024]; - int i; rval = fgets(filename, 1024, ifh); if ( rval != NULL ) { char **evlist; int num_events; + int i; chomp(filename); diff --git a/src/partial_sim.c b/src/partial_sim.c index efba8ebd..ff9a73ac 100644 --- a/src/partial_sim.c +++ b/src/partial_sim.c @@ -98,7 +98,6 @@ static void calculate_partials(Crystal *cr, { Reflection *refl; RefListIterator *iter; - double res; for ( refl = first_refl(crystal_get_reflections(cr), &iter); refl != NULL; @@ -108,6 +107,7 @@ static void calculate_partials(Crystal *cr, Reflection *rfull; double L, p, Ip, If; int bin; + double res; get_indices(refl, &h, &k, &l); get_asymm(sym, h, k, l, &h, &k, &l); @@ -515,9 +515,7 @@ int main(int argc, char *argv[]) int n_threads = 1; char *rval; int i; - FILE *fh; char *phist_file = NULL; - gsl_rng *rng_for_seeds; int config_random = 0; char *image_prefix = NULL; Stream *template_stream = NULL; @@ -969,6 +967,7 @@ int main(int argc, char *argv[]) fclose(fh); } else { + gsl_rng *rng_for_seeds; rng_for_seeds = gsl_rng_alloc(gsl_rng_mt19937); for ( i=0; iw; fs++ ) { double counts; - double cf; double intensity, sa; double pix_area, Lsq; double xs, ys, rx, ry; @@ -165,8 +164,7 @@ static void record_panel(struct detgeom_panel *p, float *dp, if ( do_poisson ) { counts = poisson_noise(rng, intensity * ph_per_e * sa); } else { - cf = intensity * ph_per_e * sa; - counts = cf; + counts = intensity * ph_per_e * sa; } /* Number of photons in pixel */ diff --git a/src/post-refinement.c b/src/post-refinement.c index 27a8cd6a..c6ff3974 100644 --- a/src/post-refinement.c +++ b/src/post-refinement.c @@ -268,10 +268,8 @@ static void try_reindex(Crystal *crin, const RefList *full, SymOpList *sym, SymOpList *amb, int scaleflags, PartialityModel pmodel) { - RefList *list; Crystal *cr; - UnitCell *cell; - double residual_original, residual_flipped; + double residual_original; int idx, n; if ( sym == NULL || amb == NULL ) return; @@ -285,6 +283,10 @@ static void try_reindex(Crystal *crin, const RefList *full, for ( idx=0; idx min) && (vals[i] < max) ) { - bin = (vals[i]-min)/step; + int bin = (vals[i]-min)/step; histo[bin]++; } } @@ -282,7 +281,7 @@ static int merge_crystal(RefList *model, struct image *image, Crystal *cr, Reflection *refl; RefListIterator *iter; RefList *new_refl; - double scale, cc; + double scale; new_refl = crystal_get_reflections(cr); @@ -291,6 +290,7 @@ static int merge_crystal(RefList *model, struct image *image, Crystal *cr, polarisation_correction(new_refl, crystal_get_cell(cr), p); if ( reference != NULL ) { + double cc; if ( do_scale ) { scale = scale_intensities(reference, new_refl, sym); } else { @@ -431,7 +431,6 @@ static int merge_stream(Stream *st, for ( i=0; in_crystals; i++ ) { - int r; Crystal *cr = image->crystals[i]; n_crystals_seen++; @@ -439,6 +438,7 @@ static int merge_stream(Stream *st, && (crystal_get_resolution_limit(cr) >= min_res) && (flag_even_odd == 2 || n_crystals_seen%2 == flag_even_odd) ) { + int r; n_crystals++; r = merge_crystal(model, image, cr, reference, sym, hist_vals, @@ -889,8 +889,6 @@ int main(int argc, char *argv[]) if ( twopass ) { - RefList *reference; - if ( rewind_all_streams(&stream_list) ) { ERROR("Couldn't rewind stream - scaling cannot be " @@ -899,6 +897,7 @@ int main(int argc, char *argv[]) } else { int r; + RefList *reference; STATUS("Second pass for scaling and/or CCs...\n"); diff --git a/src/render_hkl.c b/src/render_hkl.c index 94d6202a..c47a2c4d 100644 --- a/src/render_hkl.c +++ b/src/render_hkl.c @@ -421,7 +421,6 @@ static void render_za(UnitCell *cell, RefList *list, double max_val; double scale1, scale2, scale; double sep_u, sep_v, max_r; - double u, v; double as, bs, theta; double asx, asy, asz; double bsx, bsy, bsz; @@ -562,6 +561,8 @@ static void render_za(UnitCell *cell, RefList *list, if ( !noaxes ) { + double u, v; + /* Centre marker */ cairo_arc(dctx, (double)cx, (double)cy, max_r, 0, 2*M_PI); diff --git a/src/time-accounts.c b/src/time-accounts.c index 679eb5f1..77ab9b5d 100644 --- a/src/time-accounts.c +++ b/src/time-accounts.c @@ -77,13 +77,13 @@ void time_accounts_free(TimeAccounts *accs) static int find_account(TimeAccounts *accs, enum timeaccount acc) { int i; - static int warned_toomany = 0; for ( i=0; in_accs; i++ ) { if ( accs->accs[i] == acc ) return i; } if ( i == MAX_ACCOUNTS ) { + static int warned_toomany = 0; if ( !warned_toomany ) printf("Too many time accounts used!\n"); warned_toomany = 1; return MAX_ACCOUNTS; diff --git a/src/whirligig.c b/src/whirligig.c index 546301ba..611825ba 100644 --- a/src/whirligig.c +++ b/src/whirligig.c @@ -403,8 +403,6 @@ static void connect_series(struct window *win) { while ( win->join_ptr < win->ws ) { - int i; - if ( win->join_ptr == 0 ) { win->join_ptr++; continue; @@ -415,6 +413,7 @@ static void connect_series(struct window *win) /* Try to join this frame to each of the active series */ if ( win->join_ptr > 1 ) { + int i; for ( i=0; iser[i][win->join_ptr-1] != -1 ) { try_join(win, i); -- cgit v1.2.3