commit 30c44e864f356da6f2dca17e81710fcda7ff6975
parent dc169bee850c3b1ae230d2909791c4bbbbb6f948
Author: Eduardo Fontana Lazzari <edufonlaz@gmail.com>
Date: Sat, 1 Mar 2025 12:22:57 +0100
Extend the volume API and improve the parsing of ka
Adds an API function to retrieve the value of ka
The input file has recently changed to include the unit of ka. The
parsing now reads the unit and convert it accordingly to m^-1
Diffstat:
2 files changed, 40 insertions(+), 5 deletions(-)
diff --git a/src/sphin.h b/src/sphin.h
@@ -251,6 +251,10 @@ SPHIN_API res_T
sphin_volume_ref_put
(struct sphin_volume* volume);
+SPHIN_API res_T
+sphin_volume_get_ka
+ (struct sphin_volume* volume,
+ double* ka);
/*******************************************************************************
* API of the surface
******************************************************************************/
diff --git a/src/sphin_volume.c b/src/sphin_volume.c
@@ -136,6 +136,8 @@ parse_ka
struct txtrdr* txtrdr,
char* value) /* ka: 1.2 */
{
+ char* ka_val = NULL;
+ char* ka_unit = NULL;
char* token = NULL;
char* token_ptr = NULL;
res_T res = RES_OK;
@@ -144,13 +146,30 @@ parse_ka
ASSERT(NULL != volume);
(void) txtrdr; /* Disable unused variable warning */
- token = strtok_r(value, " \t", &token_ptr);
-
- res = cstr_to_double(token, &ka);
- if (RES_OK != res) { res = RES_BAD_ARG; goto error; }
+ /* Parse ka value */
+ ka_val = strtok_r(value, " \t", &token_ptr);
+ res = cstr_to_double(ka_val, &ka);
+ if (RES_OK != res) { goto error; }
if (ka < 0) { res = RES_BAD_ARG; goto error; }
- volume->ka = ka;
+ /* Parse ka unit and convert its value to m^-1 accordingly */
+ ka_unit = strtok_r(NULL, " \t", &token_ptr);
+
+ if (0 == strcmp(ka_unit, "cm^-1")
+ || 0 == strcmp(ka_unit, "1/cm")) {
+ volume->ka = ka / 100;
+ }
+ else if (0 == strcmp(ka_unit, "mm^-1")
+ || 0 == strcmp(ka_unit, "1/mm")) {
+ volume->ka = ka / 1000;
+ }
+ else if (0 == strcmp(ka_unit, "m^-1")
+ || 0 == strcmp(ka_unit, "1/m")) {
+ volume->ka = ka * 1; /* No conversion */
+ }
+ else { res = RES_BAD_ARG; goto error; }
+
+ /* Assert that there isnt anything but the ka value and its unit in the line */
token = strtok_r(NULL, " \t", &token_ptr);
if (NULL != token) { res = RES_BAD_ARG; goto error; }
@@ -252,3 +271,15 @@ sphin_volume_ref_put(struct sphin_volume* volume)
ref_put(&volume->ref, release_volume);
return RES_OK;
}
+
+res_T
+sphin_volume_get_ka
+ (struct sphin_volume* volume,
+ double* ka)
+{
+ if (NULL == volume) {
+ return RES_BAD_ARG;
+ }
+ *ka = volume->ka;
+ return RES_OK;
+}