aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libcrystfel/src/image.c84
1 files changed, 64 insertions, 20 deletions
diff --git a/libcrystfel/src/image.c b/libcrystfel/src/image.c
index 3efa2954..d8e97291 100644
--- a/libcrystfel/src/image.c
+++ b/libcrystfel/src/image.c
@@ -308,39 +308,84 @@ static double get_value(struct image *image, const char *from)
}
-static double unit_string_to_unit(const char *str)
+static char *get_value_and_units(struct image *image, const char *from,
+ double *pvalue)
{
- 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 get_length(struct image *image, const char *from)
-{
- double units;
char *sp;
char *fromcpy;
- double val;
+ char *unitscpy;
- if ( from == NULL ) return NAN;
+ if ( from == NULL ) {
+ *pvalue = NAN;
+ return NULL;
+ }
fromcpy = strdup(from);
- if ( fromcpy == NULL ) return NAN;
+ if ( fromcpy == NULL ) {
+ *pvalue = NAN;
+ return NULL;
+ }
sp = strchr(fromcpy, ' ');
if ( sp == NULL ) {
- units = 1.0e-3;
+ unitscpy = NULL;
} else {
- units = unit_string_to_unit(sp+1);
+ unitscpy = strdup(sp+1);
sp[0] = '\0';
}
- val = get_value(image, fromcpy);
+ *pvalue = get_value(image, fromcpy);
free(fromcpy);
- return val * units;
+ return unitscpy;
+}
+
+
+static double get_length(struct image *image, const char *from)
+{
+ char *units;
+ double value;
+ double scale;
+
+ units = get_value_and_units(image, from, &value);
+ if ( units == NULL ) {
+ scale = 1.0e-3;
+ } else {
+ if ( strcmp(units, "mm") == 0 ) {
+ scale = 1e-3;
+ } else if ( strcmp(units, "m") == 0 ) {
+ scale = 1.0;
+ } else {
+ ERROR("Invalid length unit '%s'\n", units);
+ free(units);
+ return NAN;
+ }
+ }
+
+ free(units);
+ return value * scale;
+}
+
+
+static double get_wavelength(struct image *image, const char *from)
+{
+ char *units;
+ double value;
+
+ units = get_value_and_units(image, from, &value);
+ if ( units == NULL ) {
+ /* Default unit is eV */
+ return ph_eV_to_lambda(value);
+ } else {
+ if ( strcmp(units, "A") == 0 ) {
+ free(units);
+ return value * 1e-10;
+ } else {
+ ERROR("Invalid wavelength unit '%s'\n", units);
+ free(units);
+ return NAN;
+ }
+ }
}
@@ -395,8 +440,7 @@ static void create_detgeom(struct image *image, DataTemplate *dtempl)
}
- /* FIXME: Units for wavelength/photon energy in DataTemplate */
- image->lambda = ph_eV_to_lambda(get_value(image, dtempl->wavelength_from));
+ image->lambda = get_wavelength(image, dtempl->wavelength_from);
image->detgeom = detgeom;
/* FIXME: spectrum */