upgrading copyright year from 2015 to 2016
[hkl.git] / hkl / hkl-pseudoaxis.c
blobc2daaff679f4f24ed01787aa2af9a849142d9cd0
1 /*
2 * This file is part of the hkl library.
4 * The hkl library is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * The hkl library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with the hkl library. If not, see <http://www.gnu.org/licenses/>.
17 * Copyright (C) 2003-2016 Synchrotron SOLEIL
18 * L'Orme des Merisiers Saint-Aubin
19 * BP 48 91192 GIF-sur-YVETTE CEDEX
21 * Authors: Picca Frédéric-Emmanuel <picca@synchrotron-soleil.fr>
23 #include <stdio.h> // for fprintf, FILE
24 #include <stdlib.h> // for free
25 #include <string.h> // for NULL, strcmp
26 #include <sys/types.h> // for uint
27 #include "hkl-detector-private.h" // for hkl_detector_new_copy
28 #include "hkl-geometry-private.h" // for _HklGeometryList, etc
29 #include "hkl-macros-private.h" // for hkl_assert, HKL_MALLOC, etc
30 #include "hkl-parameter-private.h" // for hkl_parameter_list_fprintf, etc
31 #include "hkl-pseudoaxis-private.h" // for _HklEngine, _HklEngineList, etc
32 #include "hkl-sample-private.h"
33 #include "hkl.h" // for HklEngine, HklEngineList, etc
34 #include "hkl/ccan/container_of/container_of.h" // for container_of
35 #include "hkl/ccan/darray/darray.h" // for darray_foreach, darray_init, etc
37 /***********/
38 /* HklMode */
39 /***********/
41 /**
42 * hkl_mode_fprintf: (skip)
43 * @f:
44 * @self:
46 * print to a FILE the HklPSeudoAxisEngineMode members
47 **/
48 void hkl_mode_fprintf(FILE *f, const HklMode *self)
50 HklParameter **parameter;
51 const char **axis;
53 fprintf(f, "mode: \"%s\"\n", self->info->name);
54 fprintf(f, "initialized_get: %p\n", self->ops->initialized_get);
55 fprintf(f, "initialized_set: %p\n", self->ops->initialized_set);
56 fprintf(f, "get: %p\n", self->ops->get);
57 fprintf(f, "set: %p\n", self->ops->set);
59 darray_foreach(parameter, self->parameters){
60 fprintf(f, "\n ");
61 hkl_parameter_fprintf(f, *parameter);
64 fprintf(f, "axes (read) names:");
65 darray_foreach(axis, self->info->axes_r)
66 fprintf(f, " %s", *axis);
67 fprintf(f, "\n");
69 fprintf(f, "axes (write) names:");
70 darray_foreach(axis, self->info->axes_w)
71 fprintf(f, " %s", *axis);
72 fprintf(f, "\n");
75 /*************/
76 /* HklEngine */
77 /*************/
79 /**
80 * hkl_engine_name_get:
81 * @self: the this ptr
83 * Return value: the name of the HklEngine
84 **/
85 const char *hkl_engine_name_get(const HklEngine *self)
87 return self->info->name;
90 /**
91 * hkl_engine_len: (skip)
92 * @self: the this ptr
94 * Return value: the len of the pseudo axes of the HklEngine
95 **/
96 unsigned int hkl_engine_len(const HklEngine *self)
98 return darray_size(self->info->pseudo_axes);
102 * hkl_engine_pseudo_axis_names_get:
103 * @self: the this ptr
105 * Return value: (type gpointer): the pseudo_axes managed by this #HklEngine
107 const darray_string *hkl_engine_pseudo_axis_names_get(HklEngine *self)
109 return &self->pseudo_axis_names;
113 * hkl_engine_pseudo_axis_values_get:
114 * @self: the this ptr
115 * @values: (array length=n_values): the values to get
116 * @n_values: the size of the values array.
117 * @unit_type: the unit type (default or user) of the returned value
119 * Get the engine pseudo axes values
121 * return value: TRUE if succeded or FALSE otherwise.
123 int hkl_engine_pseudo_axis_values_get(HklEngine *self,
124 double values[], size_t n_values,
125 HklUnitEnum unit_type, GError **error)
127 hkl_error(error == NULL ||*error == NULL);
129 if(n_values != darray_size(self->info->pseudo_axes)){
130 g_set_error(error,
131 HKL_ENGINE_ERROR,
132 HKL_ENGINE_ERROR_PSEUDO_AXIS_VALUES_GET,
133 "cannot get engine pseudo axes, wrong number of parameter (%d) given, (%d) expected\n",
134 n_values, darray_size(self->info->pseudo_axes));
135 return FALSE;
138 if(!hkl_engine_get(self, error)){
139 g_assert(error == NULL || *error != NULL);
140 return FALSE;
142 g_assert(error == NULL || *error == NULL);
144 for(size_t i=0; i<n_values; ++i){
145 values[i] = hkl_parameter_value_get(darray_item(self->pseudo_axes, i),
146 unit_type);
148 return TRUE;
152 * hkl_engine_pseudo_axis_values_set:
153 * @self: the this ptr
154 * @values: (array length=n_values): the values to set
155 * @n_values: the size of the values array.
156 * @unit_type: the unit type (default or user) of the returned value
157 * @error: return location for a GError, or NULL
159 * Set the engine pseudo axes values
161 * Return value: #HklGeometryList or NULL if no solution was found,
162 * use hkl_geometry_list_free to release the memory once done.
164 HklGeometryList *hkl_engine_pseudo_axis_values_set(HklEngine *self,
165 double values[], size_t n_values,
166 HklUnitEnum unit_type, GError **error)
168 #if LOGGING
169 char *msg;
170 size_t msg_size;
171 FILE *stream;
172 #endif
173 HklGeometryList *solutions = NULL;
175 hkl_error(error == NULL ||*error == NULL);
177 if(n_values != darray_size(self->info->pseudo_axes)){
178 g_set_error(error,
179 HKL_ENGINE_ERROR,
180 HKL_ENGINE_ERROR_PSEUDO_AXIS_VALUES_SET,
181 "cannot set engine pseudo axes, wrong number of parameter (%d) given, (%d) expected\n",
182 n_values, darray_size(self->info->pseudo_axes));
183 goto out;
186 #if LOGGING
187 stream = open_memstream(&msg, &msg_size);
189 fprintf(stream, "%s(", __func__);
190 fprintf(stream, "self: %s, values: [", hkl_engine_name_get(self));
191 for(size_t i=0; i<n_values; ++i)
192 fprintf(stream, " %f", values[i]);
193 fprintf(stream, "], n_values: %d unit_type: %d, error: %p)", n_values, unit_type, error);
195 hkl_geometry_fprintf(stream, self->geometry);
196 hkl_sample_fprintf(stream, self->sample);
197 hkl_engine_fprintf(stream, self);
198 #endif
199 for(size_t i=0; i<n_values; ++i){
200 if(!hkl_parameter_value_set(darray_item(self->pseudo_axes, i),
201 values[i],
202 unit_type, error)){
203 goto clean_stream_out;
207 if(!hkl_engine_set(self, error)){
208 #if LOGGING
209 fflush(stream);
210 g_message(msg);
211 if(error && *error != NULL)
212 g_warning("%s", (*error)->message);
213 #endif
214 goto clean_stream_out;
217 solutions = hkl_geometry_list_new_copy(self->engines->geometries);
219 clean_stream_out:
220 #if LOGGING
221 fclose(stream);
222 free(msg);
223 #endif
224 out:
225 return solutions;
229 * hkl_engine_pseudo_axis_get:
230 * @self: the this ptr
231 * @name: the name of the expected pseudo_axis
232 * @error: return location for a GError, or NULL
234 * get the #HklParameter with the given @name.
236 * Returns: (allow-none): retun the parameter or NULL if the engine
237 * does not contain this pseudo_axis.
238 * TODO: unit test
240 const HklParameter *hkl_engine_pseudo_axis_get(const HklEngine *self,
241 const char *name,
242 GError **error)
244 HklParameter **parameter;
246 hkl_error (error == NULL || *error == NULL);
248 darray_foreach(parameter, self->pseudo_axes)
249 if(!strcmp((*parameter)->name, name))
250 return *parameter;
252 g_set_error(error,
253 HKL_ENGINE_ERROR,
254 HKL_ENGINE_ERROR_PSEUDO_AXIS_SET,
255 "This pseudo axis doesn not contain this pseudo axis \"%s\"\n",
256 name);
258 return NULL;
262 * hkl_engine_pseudo_axis_set: (skip)
263 * @self: the this ptr
264 * @name: the name of the pseudo_axis to set
265 * @parameter: the parameter to set.
266 * @error: return location for a GError, or NULL
268 * set a parameter of the #HklEngine
270 * Return value: #HklGeometryList or NULL if no solution was found,
271 * use hkl_geometry_list_free to release the memory once done.
273 HklGeometryList *hkl_engine_pseudo_axis_set(HklEngine *self,
274 const char *name,
275 const HklParameter *parameter,
276 GError **error)
278 HklParameter **p;
280 hkl_error (error == NULL || *error == NULL);
281 hkl_error (strcmp(name, parameter->name) == 0);
283 darray_foreach(p, self->pseudo_axes)
284 if(!strcmp((*p)->name, parameter->name)){
285 /* todo save the previous value to restore this value */
286 hkl_parameter_init_copy(*p, parameter, NULL);
287 if(!hkl_engine_set(self, error)){
288 g_assert(error == NULL || *error != NULL);
289 return NULL;
291 g_assert(error == NULL || *error == NULL);
293 return hkl_geometry_list_new_copy(self->engines->geometries);
296 g_set_error(error,
297 HKL_ENGINE_ERROR,
298 HKL_ENGINE_ERROR_PSEUDO_AXIS_SET,
299 "Can not find the pseudo axis \"%s\" in the \"%s\" engine\n",
300 parameter->name, self->info->name);
302 return NULL;
306 * hkl_engine_capabilities_get:
307 * @self: the this ptr
309 * return the capabilities of the engine. Theses capabilities can
310 * change, from on mode to the other. for now there is three kind of
311 * capabilities.
313 * HKL_ENGINE_CAP_READABLE: pseudo axes values can be read from the HklEngine
315 * HKL_ENGINE_CAP_WRITABLE: pseudo axes values can be set for this #HklEngine
317 * HKL_ENGINE_CAP_INITIALISABLE: this pseudo axes must be initialized
318 * with hkl_engine_initialize before
319 * reading or writing on it.
321 * Returns:
323 unsigned int hkl_engine_capabilities_get(const HklEngine *self)
325 return self->mode->ops->capabilities;
330 * hkl_engine_modes_names_get:
331 * @self: the this ptr
333 * Return value: (type gpointer): All the modes supported by the #HklEngine
335 const darray_string *hkl_engine_modes_names_get(const HklEngine *self)
337 return &self->mode_names;
341 * hkl_engine_parameters_names_get:
342 * @self: the this ptr
344 * Return value: (type gpointer): All the parameters of #HklEngine.
346 const darray_string *hkl_engine_parameters_names_get(const HklEngine *self)
348 return &self->mode->parameters_names;
352 * hkl_engine_parameters_values_get: (skip)
353 * @self: the this ptr
354 * @values: (array length=n_values): the values to get
355 * @n_values: the size of the values array.
356 * @unit_type: the unit type (default or user) of the returned value
358 * Get the engine parameters values
360 * return value: TRUE if succeded or FALSE otherwise.
362 void hkl_engine_parameters_values_get(const HklEngine *self,
363 double values[], size_t n_values,
364 HklUnitEnum unit_type)
366 g_return_if_fail (n_values == darray_size(self->mode->parameters));
368 for(size_t i=0; i<n_values; ++i)
369 values[i] = hkl_parameter_value_get(darray_item(self->mode->parameters, i),
370 unit_type);
374 * hkl_engine_parameters_values_set:
375 * @self: the this ptr
376 * @values: (array length=n_values): the values to set
377 * @n_values: the size of the values array.
378 * @unit_type: the unit type (default or user) of the returned value
379 * @error: return location for a GError, or NULL
381 * Set the engine parameters values
383 * return value: TRUE if succeded or FALSE otherwise.
385 int hkl_engine_parameters_values_set(HklEngine *self,
386 double values[], size_t n_values,
387 HklUnitEnum unit_type, GError **error)
389 hkl_error (error == NULL || *error == NULL || n_values == darray_size(self->mode->parameters));
391 for(size_t i=0; i<n_values; ++i){
392 if(!hkl_parameter_value_set(darray_item(self->mode->parameters, i),
393 values[i], unit_type, error)){
394 g_assert (error == NULL || *error != NULL);
395 return FALSE;
398 g_assert (error == NULL || *error == NULL);
400 return TRUE;
404 * hkl_engine_parameter_get:
405 * @self: the this ptr
406 * @name: the name of the expected parameter
407 * @error: return location for a GError, or NULL
409 * get the #HklParameter with the given @name.
411 * Returns: (allow-none): return the parameter or NULL if the engine
412 * does not contain this parameter.
414 const HklParameter *hkl_engine_parameter_get(const HklEngine *self,
415 const char *name,
416 GError **error)
418 HklParameter **parameter;
420 hkl_error (error == NULL || *error == NULL);
422 darray_foreach(parameter, self->mode->parameters)
423 if(!strcmp((*parameter)->name, name))
424 return *parameter;
426 g_set_error(error,
427 HKL_ENGINE_ERROR,
428 HKL_ENGINE_ERROR_PARAMETER_GET,
429 "this engine does not contain this parameter \"%s\"\n",
430 name);
432 return NULL;
436 * hkl_engine_parameter_set:
437 * @self: the this ptr
438 * @name: the name of the parameter to set.
439 * @parameter: the parameter to set.
440 * @error: return location for a GError, or NULL
442 * set a parameter of the #HklEngine
443 * TODO add an error
445 * return value: TRUE if succeded or FALSE otherwise.
447 int hkl_engine_parameter_set(HklEngine *self,
448 const char *name,
449 const HklParameter *parameter,
450 GError **error)
452 HklParameter **p;
454 hkl_error (error == NULL || *error == NULL);
456 darray_foreach(p, self->mode->parameters)
457 if(!strcmp(name, (*p)->name)){
458 const char *old_name = (*p)->name;
459 hkl_parameter_init_copy(*p, parameter, NULL);
460 /* we do not check if the name is identical so force the right name */
461 /* TODO rethink this HklParameter assignement */
462 (*p)->name = old_name;
463 return TRUE;
466 g_set_error(error,
467 HKL_ENGINE_ERROR,
468 HKL_ENGINE_ERROR_PARAMETER_SET,
469 "this engine does not contain this parameter \"%s\"\n",
470 parameter->name);
472 return FALSE;
476 * hkl_engine_current_mode_get:
477 * @self: the this ptr
479 * Returns: the current HklEngine mode
481 const char *hkl_engine_current_mode_get(const HklEngine *self)
483 return self->mode->info->name;
487 * hkl_engine_current_mode_set:
488 * @self: the HklEngine
489 * @name: the mode to select
490 * @error: return location for a GError, or NULL
492 * This method also populate the self->axes from the mode->axis_names.
493 * this is to speed the computation of the numerical axes.
495 * return value: TRUE if succeded or FALSE otherwise.
497 int hkl_engine_current_mode_set(HklEngine *self, const char *name,
498 GError **error)
500 HklMode **mode;
502 hkl_error (error == NULL || *error == NULL);
504 darray_foreach(mode, self->modes)
505 if(!strcmp((*mode)->info->name, name)){
506 hkl_engine_mode_set(self, *mode);
507 return TRUE;
510 g_set_error(error,
511 HKL_ENGINE_ERROR,
512 HKL_ENGINE_ERROR_CURRENT_MODE_SET,
513 "this engine does not contain this mode \"%s\"\n",
514 name);
516 return FALSE;
520 * hkl_engine_axis_names_get:
521 * @self: the this ptr
522 * @mode:
524 * return a list of axes relevant when reading or writing on the
525 * #HklEngine.
527 * exemple, for a K6C diffractometer in "lifting detector" mode, your
528 * Engine control only 3 motors when writing, but the hkl values
529 * depends on the 6 motors when reading.
531 * Returns: (type gpointer):
533 const darray_string *hkl_engine_axis_names_get(const HklEngine *self,
534 HklEngineAxisNamesGet mode)
536 switch(mode){
537 case HKL_ENGINE_AXIS_NAMES_GET_READ:
538 return &self->mode->info->axes_r;
539 case HKL_ENGINE_AXIS_NAMES_GET_WRITE:
540 return &self->mode->info->axes_w;
541 default:
542 return NULL;
546 int hkl_engine_initialized_get(const HklEngine *self)
548 return hkl_mode_initialized_get(self->mode);
552 * hkl_engine_initialized_set:
553 * @self: the HklEngine
554 * @initialized: TRUE or FALSE to activated or deactivate the HklEngine
555 * @error: return location for a GError, or NULL
557 * initialize the HklEngine
559 * return value: TRUE if succeded or FALSE otherwise.
561 int hkl_engine_initialized_set(HklEngine *self, int initialized, GError **error)
563 hkl_error (error == NULL || *error == NULL);
565 if(!self->geometry || !self->detector || !self->sample
566 || !self->mode) {
567 g_set_error(error,
568 HKL_ENGINE_ERROR,
569 HKL_ENGINE_ERROR_INITIALIZE,
570 "Internal error");
571 return FALSE;
574 return hkl_mode_initialized_set(self->mode,
575 self,
576 self->engines->geometry,
577 self->engines->detector,
578 self->engines->sample,
579 initialized,
580 error);
584 * hkl_engine_dependencies_get:
585 * @self: the this ptr
587 * return all dependencies of the engine which are not mode parameters
588 * or axes. The possible combination of values are defined in
589 * #HklEngineDependencies enum.
591 * return value:
593 unsigned int hkl_engine_dependencies_get(const HklEngine *self)
595 return self->info->dependencies;
599 * hkl_engine_fprintf: (skip)
600 * @f: the FILE
601 * @self: the HklEngine
603 * print to a FILE the HklEngine
605 void hkl_engine_fprintf(FILE *f, const HklEngine *self)
607 HklParameter **pseudo_axis;
609 fprintf(f, "\nPseudoAxesEngine : \"%s\"", self->info->name);
611 /* mode */
612 if (self->mode) {
613 fprintf(f, " %s", self->mode->info->name);
614 for(uint i=0; i<darray_size(self->mode->parameters); ++i){
615 fprintf(f, "\n ");
616 hkl_parameter_fprintf(
618 darray_item(self->mode->parameters, i));
620 fprintf(f, "\n");
623 /* the pseudoAxes part */
624 darray_foreach(pseudo_axis, self->pseudo_axes){
625 fprintf(f, "\n ");
626 hkl_parameter_fprintf(f, *pseudo_axis);
629 if(self->engines->geometries->n_items != 0){
630 fprintf(f, "\n ");
631 hkl_geometry_list_fprintf(f, self->engines->geometries);
633 fprintf(f, "\n");
636 /*****************/
637 /* HklEngineList */
638 /*****************/
641 * hkl_engine_list_free: (skip)
642 * @self: the #HklEngineList to destroy
644 * destructor
646 void hkl_engine_list_free(HklEngineList *self)
648 hkl_engine_list_clear(self);
649 hkl_geometry_list_free(self->geometries);
650 free(self);
654 * hkl_engine_list_engines_get: (skip)
655 * @self: the this ptr
657 * Return: a pointer on the engine array
659 darray_engine *hkl_engine_list_engines_get(HklEngineList *self)
661 return (darray_engine *)self;
665 * hkl_engine_list_geometry_get: (skip)
666 * @self: the this ptr
668 * Return: a pointer on the geometry member
670 HklGeometry *hkl_engine_list_geometry_get(HklEngineList *self)
672 return self->geometry;
675 int hkl_engine_list_geometry_set(HklEngineList *self, const HklGeometry *geometry)
677 if(!hkl_geometry_set(self->geometry, geometry))
678 return FALSE;
680 hkl_engine_list_get(self);
682 return TRUE;
686 * hkl_engine_list_select_solution:
687 * @self: the this ptr
688 * @item: the #HklGeoemtryListItem selected.
690 * this method set the geometry member with the selected solution.
692 * return value: TRUE if succeded or FALSE otherwise.
694 int hkl_engine_list_select_solution(HklEngineList *self,
695 const HklGeometryListItem *item)
697 return hkl_geometry_init_geometry(self->geometry, item->geometry);
701 * hkl_engine_list_engine_get_by_name:
702 * @self: the this ptr
703 * @name: the name of the requested #HklPseudoAxisEngin
704 * @error: return location for a GError, or NULL
706 * get the #HklEngine by its name from the list.
708 * Returns: (transfer none) (allow-none): the requested engine
710 HklEngine *hkl_engine_list_engine_get_by_name(HklEngineList *self,
711 const char *name,
712 GError **error)
714 HklEngine **engine;
716 hkl_error (error == NULL || *error == NULL);
718 darray_foreach(engine, *self){
719 if (!strcmp((*engine)->info->name, name))
720 return *engine;
723 g_set_error(error,
724 HKL_ENGINE_LIST_ERROR,
725 HKL_ENGINE_LIST_ERROR_ENGINE_GET_BY_NAME,
726 "this engine list does not contain this engine \"%s\"",
727 name);
729 return NULL;
733 * hkl_engine_list_init:
734 * @self: the engine list
735 * @geometry: the associated #HklGeometry
736 * @detector: the associated #HklDetector
737 * @sample: the associated #HklSample
739 * before using an engine list you must associate all engines to a
740 * Geometry, a detector and a sample.
742 void hkl_engine_list_init(HklEngineList *self,
743 HklGeometry *geometry,
744 HklDetector *detector,
745 HklSample *sample)
747 HklEngine **engine;
749 self->geometry = geometry;
750 self->detector = detector;
751 self->sample = sample;
753 darray_foreach(engine, *self){
754 hkl_engine_prepare_internal(*engine);
759 * hkl_engine_list_get:
760 * @self: the list of #HklEngine
762 * apply the get method to all the #HklEngine of the list
763 * after this it is possible to retrive all the #HklPseudoAxis values.
765 * Returns: HKL_SUCCESS or HKL_FAIL if one of the #HklEngine
766 * get method failed.
768 int hkl_engine_list_get(HklEngineList *self)
770 HklEngine **engine;
771 int res = TRUE;
773 darray_foreach(engine, *self){
774 res &= hkl_engine_get(*engine, NULL);
777 return res;
781 * hkl_engine_list_fprintf: (skip)
782 * @f: the File
783 * @self: the list
785 * print to a FILE the #HklEngineList
787 void hkl_engine_list_fprintf(FILE *f,
788 const HklEngineList *self)
790 HklEngine **engine;
792 darray_foreach(engine, *self){
793 hkl_engine_fprintf(f, *engine);