aboutsummaryrefslogtreecommitdiff
path: root/libcrystfel
diff options
context:
space:
mode:
authorThomas White <taw@physics.org>2020-08-04 15:25:19 +0200
committerThomas White <taw@physics.org>2020-08-04 15:25:19 +0200
commit43b71428b68e94d22c2c5f9e8a25b1b46bc04a4c (patch)
tree435b99f1a487c6144d7ec74a60cd5c3f0a784215 /libcrystfel
parenta09f8c4cd9c4abe04b99129af07e51fdc5dad4b1 (diff)
Add image_create_for_simulation()
...and factorise common code
Diffstat (limited to 'libcrystfel')
-rw-r--r--libcrystfel/src/datatemplate.c101
-rw-r--r--libcrystfel/src/datatemplate.h2
-rw-r--r--libcrystfel/src/image.c184
-rw-r--r--libcrystfel/src/image.h8
4 files changed, 136 insertions, 159 deletions
diff --git a/libcrystfel/src/datatemplate.c b/libcrystfel/src/datatemplate.c
index 5941109e..43313441 100644
--- a/libcrystfel/src/datatemplate.c
+++ b/libcrystfel/src/datatemplate.c
@@ -1383,107 +1383,6 @@ void data_template_add_copy_header(DataTemplate *dt,
}
-static double unit_string_to_unit(const char *str)
-{
- if ( strcmp(str, "mm") == 0 ) return 1e-3;
- if ( strcmp(str, "m") == 0 ) return 1.0;
- ERROR("Invalid length unit '%s'\n", str);
- return NAN;
-}
-
-
-static double dt_get_length(const char *from)
-{
- double units;
- char *sp;
- char *fromcpy;
- double val;
- char *rval;
-
- if ( from == NULL ) return NAN;
-
- fromcpy = strdup(from);
- if ( fromcpy == NULL ) return NAN;
-
- sp = strchr(fromcpy, ' ');
- if ( sp == NULL ) {
- units = 1.0e-3;
- } else {
- units = unit_string_to_unit(sp+1);
- }
-
- sp[0] = '\0';
- val = strtod(fromcpy, &rval);
- if ( !( (*rval == '\0') && (rval != fromcpy)) ) {
- ERROR("Invalid length value: %s\n", fromcpy);
- free(fromcpy);
- return NAN;
- }
-
- return val * units;
-}
-
-
-/* If possible, i.e. if there are no references to image headers in
- * 'dt', generate a detgeom structure from it.
- *
- * NB This is probably not the function you're looking for!
- * The detgeom structure should normally come from loading an image,
- * reading a stream or similar. This function should only be used
- * when there is really no data involved, e.g. in make_pixelmap.
- */
-struct detgeom *data_template_to_detgeom(const DataTemplate *dt)
-{
- struct detgeom *detgeom;
- int i;
-
- if ( dt == NULL ) return NULL;
-
- detgeom = malloc(sizeof(struct detgeom));
- if ( detgeom == NULL ) return NULL;
-
- detgeom->panels = malloc(dt->n_panels*sizeof(struct detgeom_panel));
- if ( detgeom->panels == NULL ) return NULL;
-
- detgeom->n_panels = dt->n_panels;
-
- for ( i=0; i<dt->n_panels; i++ ) {
-
- detgeom->panels[i].name = safe_strdup(dt->panels[i].name);
-
- detgeom->panels[i].pixel_pitch = dt->panels[i].pixel_pitch;
-
- /* NB cnx,cny are in pixels, cnz is in m */
- detgeom->panels[i].cnx = dt->panels[i].cnx;
- detgeom->panels[i].cny = dt->panels[i].cny;
- detgeom->panels[i].cnz = dt_get_length(dt->panels[i].cnz_from);
-
- /* Apply offset (in m) and then convert cnz from
- * m to pixels */
- detgeom->panels[i].cnz += dt->panels[i].cnz_offset;
- detgeom->panels[i].cnz /= detgeom->panels[i].pixel_pitch;
-
- detgeom->panels[i].max_adu = dt->panels[i].max_adu;
- detgeom->panels[i].adu_per_photon = 1.0; /* FIXME ! */
-
- detgeom->panels[i].w = dt->panels[i].orig_max_fs
- - dt->panels[i].orig_min_fs + 1;
- detgeom->panels[i].h = dt->panels[i].orig_max_ss
- - dt->panels[i].orig_min_ss + 1;
-
- detgeom->panels[i].fsx = dt->panels[i].fsx;
- detgeom->panels[i].fsy = dt->panels[i].fsy;
- detgeom->panels[i].fsz = dt->panels[i].fsz;
- detgeom->panels[i].ssx = dt->panels[i].ssx;
- detgeom->panels[i].ssy = dt->panels[i].ssy;
- detgeom->panels[i].ssz = dt->panels[i].ssz;
-
- }
-
- return detgeom;
-}
-
-
static int dt_num_placeholders(const struct panel_template *p)
{
int i;
diff --git a/libcrystfel/src/datatemplate.h b/libcrystfel/src/datatemplate.h
index c6054409..88a002f0 100644
--- a/libcrystfel/src/datatemplate.h
+++ b/libcrystfel/src/datatemplate.h
@@ -69,8 +69,6 @@ extern int data_template_panel_to_file_coords(const DataTemplate *dt,
extern void data_template_add_copy_header(DataTemplate *dt,
const char *header);
-extern struct detgeom *data_template_to_detgeom(const DataTemplate *dt);
-
extern int data_template_get_slab_extents(const DataTemplate *dt, int *pw, int *ph);
extern int data_template_in_bad_region(const DataTemplate *dtempl,
diff --git a/libcrystfel/src/image.c b/libcrystfel/src/image.c
index 2e9249e8..d2fb30ba 100644
--- a/libcrystfel/src/image.c
+++ b/libcrystfel/src/image.c
@@ -397,21 +397,21 @@ static double convert_to_m(double val, int units)
}
-void create_detgeom(struct image *image, const DataTemplate *dtempl)
+int create_detgeom(struct image *image, const DataTemplate *dtempl)
{
struct detgeom *detgeom;
int i;
if ( dtempl == NULL ) {
ERROR("NULL data template!\n");
- return;
+ return 1;
}
detgeom = malloc(sizeof(struct detgeom));
- if ( detgeom == NULL ) return;
+ if ( detgeom == NULL ) return 1;
detgeom->panels = malloc(dtempl->n_panels*sizeof(struct detgeom_panel));
- if ( detgeom->panels == NULL ) return;
+ if ( detgeom->panels == NULL ) return 1;
detgeom->n_panels = dtempl->n_panels;
@@ -483,7 +483,7 @@ void create_detgeom(struct image *image, const DataTemplate *dtempl)
image->detgeom = detgeom;
- /* FIXME: spectrum */
+ return 0;
}
@@ -572,43 +572,9 @@ int image_read_image_data(struct image *image,
}
-struct image *image_read(DataTemplate *dtempl,
- const char *filename,
- const char *event,
- int no_image_data,
- int no_mask_data)
+static void set_image_parameters(struct image *image,
+ const DataTemplate *dtempl)
{
- struct image *image;
- int i;
- int r;
-
- if ( dtempl == NULL ) {
- ERROR("NULL data template!\n");
- return NULL;
- }
-
- image = image_new();
- if ( image == NULL ) {
- ERROR("Couldn't allocate image structure.\n");
- return NULL;
- }
-
- if ( !no_image_data ) {
-
- r = image_read_image_data(image, dtempl,
- filename, event);
-
- } else {
-
- r = image_set_zero_data(image, dtempl);
-
- }
-
- if ( r ) {
- image_free(image);
- return NULL;
- }
-
/* Wavelength might be needed to create detgeom (adu_per_eV) */
image->lambda = convert_to_m(get_value(image,
dtempl->wavelength_from),
@@ -620,12 +586,19 @@ struct image *image_read(DataTemplate *dtempl,
image->spectrum = spectrum_generate_gaussian(image->lambda,
image->bw);
- create_detgeom(image, dtempl);
+}
+
+
+static int create_badmap(struct image *image,
+ const DataTemplate *dtempl,
+ int no_mask_data)
+{
+ int i;
image->bad = malloc(dtempl->n_panels * sizeof(int *));
if ( image->bad == NULL ) {
ERROR("Failed to allocate bad pixel mask\n");
- return NULL;
+ return 1;
}
for ( i=0; i<dtempl->n_panels; i++ ) {
@@ -640,7 +613,7 @@ struct image *image_read(DataTemplate *dtempl,
image->bad[i] = calloc(p_w*p_h, sizeof(int));
if ( image->bad[i] == NULL ) {
ERROR("Failed to allocate bad pixel mask\n");
- return NULL;
+ return 1;
}
/* Panel marked as bad? */
@@ -669,37 +642,142 @@ struct image *image_read(DataTemplate *dtempl,
if ( (!no_mask_data) && (!p->bad) && (p->mask != NULL) )
{
if ( p->mask_file == NULL ) {
- mask_fn = filename;
+ mask_fn = image->filename;
} else {
mask_fn = p->mask_file;
}
if ( is_hdf5_file(mask_fn) ) {
- image_hdf5_read_mask(p, mask_fn, event,
+ image_hdf5_read_mask(p, mask_fn,
+ image->ev,
image->bad[i],
dtempl->mask_good,
dtempl->mask_bad);
- } else if ( is_cbf_file(filename) ) {
- image_cbf_read_mask(p, mask_fn, event,
+ } else if ( is_cbf_file(mask_fn) ) {
+ image_cbf_read_mask(p, mask_fn,
+ image->ev,
0, image->bad[i],
dtempl->mask_good,
dtempl->mask_bad);
- } else if ( is_cbfgz_file(filename) ) {
- image_cbf_read_mask(p, mask_fn, event,
+ } else if ( is_cbfgz_file(mask_fn) ) {
+ image_cbf_read_mask(p, mask_fn,
+ image->ev,
1, image->bad[i],
dtempl->mask_good,
dtempl->mask_bad);
} else {
ERROR("Unrecognised mask file type"
- " (%s)\n", filename);
- return NULL;
+ " (%s)\n", mask_fn);
+ return 1;
}
}
}
+ return 0;
+}
+
+
+static int create_satmap(struct image *image,
+ const DataTemplate *dtempl)
+{
+ /* FIXME: Implementation */
+ return 0;
+}
+
+
+struct image *image_create_for_simulation(const DataTemplate *dtempl)
+{
+ struct image *image;
+
+ if ( dtempl == NULL ) {
+ ERROR("NULL data template!\n");
+ return NULL;
+ }
+
+ image = image_new();
+ if ( image == NULL ) {
+ ERROR("Couldn't allocate image structure.\n");
+ return NULL;
+ }
+
+ if ( image_set_zero_data(image, dtempl) ) {
+ image_free(image);
+ return NULL;
+ }
- /* FIXME: Load saturation map */
+ set_image_parameters(image, dtempl);
+
+ if ( create_detgeom(image, dtempl) ) {
+ image_free(image);
+ return NULL;
+ }
+
+ if ( create_badmap(image, dtempl, 1) ) {
+ image_free(image);
+ return NULL;
+ }
+
+ if ( create_satmap(image, dtempl) ) {
+ image_free(image);
+ return NULL;
+ }
+
+ return image;
+}
+
+
+struct image *image_read(const DataTemplate *dtempl,
+ const char *filename,
+ const char *event,
+ int no_image_data,
+ int no_mask_data)
+{
+ struct image *image;
+ int r;
+
+ if ( dtempl == NULL ) {
+ ERROR("NULL data template!\n");
+ return NULL;
+ }
+
+ image = image_new();
+ if ( image == NULL ) {
+ ERROR("Couldn't allocate image structure.\n");
+ return NULL;
+ }
+
+ image->filename = strdup(filename);
+ image->ev = strdup(event);
+
+ /* Load the image data */
+ if ( !no_image_data ) {
+ r = image_read_image_data(image, dtempl,
+ filename, event);
+ } else {
+ r = image_set_zero_data(image, dtempl);
+ }
+ if ( r ) {
+ image_free(image);
+ return NULL;
+ }
+
+ set_image_parameters(image, dtempl);
+
+ if ( create_detgeom(image, dtempl) ) {
+ image_free(image);
+ return NULL;
+ }
+
+ if ( create_badmap(image, dtempl, no_mask_data) ) {
+ image_free(image);
+ return NULL;
+ }
+
+ if ( create_satmap(image, dtempl) ) {
+ image_free(image);
+ return NULL;
+ }
return image;
}
diff --git a/libcrystfel/src/image.h b/libcrystfel/src/image.h
index 9f88d0e7..a2f160af 100644
--- a/libcrystfel/src/image.h
+++ b/libcrystfel/src/image.h
@@ -186,11 +186,12 @@ extern void mark_resolution_range_as_bad(struct image *image,
double min, double max);
extern struct image *image_new(void);
-extern struct image *image_read(DataTemplate *dtempl,
+extern struct image *image_read(const DataTemplate *dtempl,
const char *filename,
const char *event,
int no_image_data,
int no_mask_data);
+extern struct image *image_create_for_simulation(const DataTemplate *dtempl);
extern void image_free(struct image *image);
extern ImageFeatureList *image_read_peaks(const DataTemplate *dtempl,
@@ -208,9 +209,10 @@ extern int image_set_zero_mask(struct image *image,
const DataTemplate *dtempl);
/* Use within libcrystfel only */
-extern void create_detgeom(struct image *image,
- const DataTemplate *dtempl);
+extern int create_detgeom(struct image *image,
+ const DataTemplate *dtempl);
+/* Use within libcrystfel only */
extern int image_read_image_data(struct image *image,
const DataTemplate *dtempl,
const char *filename,