DETMON Pipeline Reference Manual  1.2.4
irplib_plugin.c
1 /* $Id: irplib_plugin.c,v 1.40 2013/08/22 17:44:56 cgarcia Exp $
2  *
3  * This file is part of the irplib package
4  * Copyright (C) 2002,2003 European Southern Observatory
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA
19  */
20 
21 /*
22  * $Author: cgarcia $
23  * $Date: 2013/08/22 17:44:56 $
24  * $Revision: 1.40 $
25  * $Name: detmon-1_2_4 $
26  */
27 
28 /*-----------------------------------------------------------------------------
29  Includes
30  -----------------------------------------------------------------------------*/
31 
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35 
36 #include <string.h>
37 #include <stdlib.h>
38 #include <assert.h>
39 
40 #include <cpl.h>
41 
42 
43 #include "irplib_plugin.h"
44 
45 /*----------------------------------------------------------------------------*/
55 /*----------------------------------------------------------------------------*/
56 
57 /*-----------------------------------------------------------------------------
58  Defines
59  -----------------------------------------------------------------------------*/
60 
61 /* Maximum line length in SOF-file */
62 #ifndef LINE_LEN_MAX
63 #define LINE_LEN_MAX 1024
64 #endif
65 
66 /* This device provides quite-random data */
67 #define DEV_RANDOM "/dev/urandom"
68 
69 /* Copied from cpl_tools.h */
70 #define recipe_assert(bool) \
71  ((bool) ? (cpl_msg_debug(cpl_func, \
72  "OK in " __FILE__ " line %d (CPL-error state: '%s' in %s): %s",__LINE__, \
73  cpl_error_get_message(), cpl_error_get_where(), #bool), 0) \
74  : (cpl_msg_error(cpl_func, \
75  "Failure in " __FILE__ " line %d (CPL-error state: '%s' in %s): %s", \
76  __LINE__, cpl_error_get_message(), cpl_error_get_where(), #bool), 1))
77 
78 
79 
80 /*-----------------------------------------------------------------------------
81  Private Function prototypes
82  -----------------------------------------------------------------------------*/
83 
84 static const cpl_parameter * irplib_parameterlist_get(const cpl_parameterlist *,
85  const char *,
86  const char *,
87  const char *);
88 
89 static void recipe_parameterlist_set(cpl_parameterlist *);
90 static cpl_boolean irplib_plugin_has_sof_from_env(const cpl_plugin *,
91  const char *);
92 
93 static void recipe_frameset_load(cpl_frameset *, const char *);
94 
95 static void recipe_sof_test_devfile(cpl_plugin *, const char *, size_t,
96  const char *[]);
97 static void recipe_sof_test_image_empty(cpl_plugin *, size_t, const char *[]);
98 static void recipe_sof_test_local(cpl_plugin *);
99 static void recipe_sof_test_from_env(cpl_plugin *);
100 static void recipe_frameset_empty(cpl_frameset *);
101 static void recipe_frameset_test_frame(const cpl_frame *);
102 static void recipe_frameset_test_frameset_diff(const cpl_frameset *,
103  const cpl_frameset *);
104 
105 static cpl_errorstate inistate;
106 
109 /*-----------------------------------------------------------------------------
110  Function definitions
111  -----------------------------------------------------------------------------*/
112 
113 /*----------------------------------------------------------------------------*/
123 /*----------------------------------------------------------------------------*/
124 const char * irplib_parameterlist_get_string(const cpl_parameterlist * self,
125  const char * instrume,
126  const char * recipe,
127  const char * parameter)
128 {
129 
130  const cpl_parameter * par = irplib_parameterlist_get(self, instrume,
131  recipe, parameter);
132  const char * value;
133 
134  cpl_ensure(par != NULL, cpl_error_get_code(), NULL);
135 
136  value = cpl_parameter_get_string(par);
137 
138  if (value == NULL) (void)cpl_error_set_where(cpl_func);
139 
140  return value;
141 
142 }
143 
144 /*----------------------------------------------------------------------------*/
154 /*----------------------------------------------------------------------------*/
155 cpl_boolean irplib_parameterlist_get_bool(const cpl_parameterlist * self,
156  const char * instrume,
157  const char * recipe,
158  const char * parameter)
159 {
160 
161  const cpl_parameter * par = irplib_parameterlist_get(self, instrume,
162  recipe, parameter);
163  cpl_errorstate prestate = cpl_errorstate_get();
164  cpl_boolean value;
165 
166 
167  cpl_ensure(par != NULL, cpl_error_get_code(), CPL_FALSE);
168 
169  value = cpl_parameter_get_bool(par);
170 
171  if (!cpl_errorstate_is_equal(prestate)) (void)cpl_error_set_where(cpl_func);
172 
173  return value;
174 
175 }
176 
177 
178 /*----------------------------------------------------------------------------*/
188 /*----------------------------------------------------------------------------*/
189 int irplib_parameterlist_get_int(const cpl_parameterlist * self,
190  const char * instrume,
191  const char * recipe,
192  const char * parameter)
193 {
194 
195  const cpl_parameter * par = irplib_parameterlist_get(self, instrume,
196  recipe, parameter);
197  cpl_errorstate prestate = cpl_errorstate_get();
198  int value;
199 
200 
201  cpl_ensure(par != NULL, cpl_error_get_code(), 0);
202 
203  value = cpl_parameter_get_int(par);
204 
205  if (!cpl_errorstate_is_equal(prestate)) (void)cpl_error_set_where(cpl_func);
206 
207  return value;
208 }
209 
210 /*----------------------------------------------------------------------------*/
220 /*----------------------------------------------------------------------------*/
221 double irplib_parameterlist_get_double(const cpl_parameterlist * self,
222  const char * instrume,
223  const char * recipe,
224  const char * parameter)
225 {
226 
227  const cpl_parameter * par = irplib_parameterlist_get(self, instrume,
228  recipe, parameter);
229  cpl_errorstate prestate = cpl_errorstate_get();
230  double value;
231 
232 
233  cpl_ensure(par != NULL, cpl_error_get_code(), 0.0);
234 
235  value = cpl_parameter_get_double(par);
236 
237  if (!cpl_errorstate_is_equal(prestate)) (void)cpl_error_set_where(cpl_func);
238 
239  return value;
240 }
241 
242 /*----------------------------------------------------------------------------*/
256 /*----------------------------------------------------------------------------*/
257 cpl_error_code irplib_parameterlist_set_string(cpl_parameterlist * self,
258  const char * instrume,
259  const char * recipe,
260  const char * parameter,
261  const char * defvalue,
262  const char * alias,
263  const char * context,
264  const char * man)
265 {
266 
267  cpl_error_code error;
268  cpl_parameter * par;
269  char * paramname = cpl_sprintf("%s.%s.%s", instrume, recipe,
270  parameter);
271 
272  cpl_ensure_code(paramname != NULL, cpl_error_get_code());
273 
274  par = cpl_parameter_new_value(paramname, CPL_TYPE_STRING, man, context,
275  defvalue);
276  cpl_free(paramname);
277 
278  cpl_ensure_code(par != NULL, cpl_error_get_code());
279 
280  error = cpl_parameter_set_alias(par, CPL_PARAMETER_MODE_CLI,
281  alias ? alias : parameter);
282  cpl_ensure_code(!error, error);
283 
284  error = cpl_parameter_disable(par, CPL_PARAMETER_MODE_ENV);
285  cpl_ensure_code(!error, error);
286 
287  error = cpl_parameterlist_append(self, par);
288  cpl_ensure_code(!error, error);
289 
290  return CPL_ERROR_NONE;
291 }
292 
293 
294 /*----------------------------------------------------------------------------*/
308 /*----------------------------------------------------------------------------*/
309 cpl_error_code irplib_parameterlist_set_bool(cpl_parameterlist * self,
310  const char * instrume,
311  const char * recipe,
312  const char * parameter,
313  cpl_boolean defvalue,
314  const char * alias,
315  const char * context,
316  const char * man)
317 {
318 
319  cpl_error_code error;
320  cpl_parameter * par;
321  char * paramname = cpl_sprintf("%s.%s.%s", instrume, recipe,
322  parameter);
323 
324  cpl_ensure_code(paramname != NULL, cpl_error_get_code());
325 
326  par = cpl_parameter_new_value(paramname, CPL_TYPE_BOOL, man, context,
327  defvalue);
328  cpl_free(paramname);
329 
330  cpl_ensure_code(par != NULL, cpl_error_get_code());
331 
332  error = cpl_parameter_set_alias(par, CPL_PARAMETER_MODE_CLI,
333  alias ? alias : parameter);
334  cpl_ensure_code(!error, error);
335 
336  error = cpl_parameter_disable(par, CPL_PARAMETER_MODE_ENV);
337  cpl_ensure_code(!error, error);
338 
339  error = cpl_parameterlist_append(self, par);
340  cpl_ensure_code(!error, error);
341 
342  return CPL_ERROR_NONE;
343 }
344 
345 
346 
347 /*----------------------------------------------------------------------------*/
361 /*----------------------------------------------------------------------------*/
362 cpl_error_code irplib_parameterlist_set_int(cpl_parameterlist * self,
363  const char * instrume,
364  const char * recipe,
365  const char * parameter,
366  int defvalue,
367  const char * alias,
368  const char * context,
369  const char * man)
370 {
371 
372  cpl_error_code error;
373  cpl_parameter * par;
374  char * paramname = cpl_sprintf("%s.%s.%s", instrume, recipe,
375  parameter);
376 
377  cpl_ensure_code(paramname != NULL, cpl_error_get_code());
378 
379  par = cpl_parameter_new_value(paramname, CPL_TYPE_INT, man, context,
380  defvalue);
381  cpl_free(paramname);
382 
383  cpl_ensure_code(par != NULL, cpl_error_get_code());
384 
385  error = cpl_parameter_set_alias(par, CPL_PARAMETER_MODE_CLI,
386  alias ? alias : parameter);
387  cpl_ensure_code(!error, error);
388 
389  error = cpl_parameter_disable(par, CPL_PARAMETER_MODE_ENV);
390  cpl_ensure_code(!error, error);
391 
392  error = cpl_parameterlist_append(self, par);
393  cpl_ensure_code(!error, error);
394 
395  return CPL_ERROR_NONE;
396 }
397 
398 
399 /*----------------------------------------------------------------------------*/
413 /*----------------------------------------------------------------------------*/
414 cpl_error_code irplib_parameterlist_set_double(cpl_parameterlist * self,
415  const char * instrume,
416  const char * recipe,
417  const char * parameter,
418  double defvalue,
419  const char * alias,
420  const char * context,
421  const char * man)
422 {
423 
424  cpl_error_code error;
425  cpl_parameter * par;
426  char * paramname = cpl_sprintf("%s.%s.%s", instrume, recipe,
427  parameter);
428 
429  cpl_ensure_code(paramname != NULL, cpl_error_get_code());
430 
431  par = cpl_parameter_new_value(paramname, CPL_TYPE_DOUBLE, man, context,
432  defvalue);
433  cpl_free(paramname);
434 
435  cpl_ensure_code(par != NULL, cpl_error_get_code());
436 
437  error = cpl_parameter_set_alias(par, CPL_PARAMETER_MODE_CLI,
438  alias ? alias : parameter);
439  cpl_ensure_code(!error, error);
440 
441  error = cpl_parameter_disable(par, CPL_PARAMETER_MODE_ENV);
442  cpl_ensure_code(!error, error);
443 
444  error = cpl_parameterlist_append(self, par);
445  cpl_ensure_code(!error, error);
446 
447  return CPL_ERROR_NONE;
448 }
449 
450 
451 /*----------------------------------------------------------------------------*/
465 /*----------------------------------------------------------------------------*/
466 int irplib_plugin_test(cpl_pluginlist * self, size_t nstr, const char *astr[]) {
467 
468  cpl_plugin * plugin;
469  cpl_recipe * recipe;
470  int (*recipe_create) (cpl_plugin *);
471  int (*recipe_exec ) (cpl_plugin *);
472  int (*recipe_deinit) (cpl_plugin *);
473  cpl_error_code error;
474  FILE * stream;
475  cpl_boolean is_debug;
476 
477 
478  is_debug = cpl_msg_get_level() <= CPL_MSG_DEBUG ? CPL_TRUE : CPL_FALSE;
479 
480  /* Modified from CPL unit tests */
481  stream = is_debug ? stdout : fopen("/dev/null", "a");
482 
483  inistate = cpl_errorstate_get();
484 
485  assert( nstr == 0 || astr != NULL );
486 
487  plugin = cpl_pluginlist_get_first(self);
488 
489  if (plugin == NULL) {
490  cpl_msg_warning(cpl_func, "With an empty pluginlist, "
491  "no tests can be made");
492  return 0;
493  }
494 
495  cpl_plugin_dump(plugin, stream);
496 
497  recipe_create = cpl_plugin_get_init(plugin);
498  cpl_test( recipe_create != NULL);
499 
500  recipe_exec = cpl_plugin_get_exec(plugin);
501  cpl_test( recipe_exec != NULL);
502 
503  recipe_deinit = cpl_plugin_get_deinit(plugin);
504  cpl_test( recipe_deinit != NULL);
505 
506  /* Only plugins of type recipe are tested (further) */
507  if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
508  cpl_msg_warning(cpl_func, "This plugin is not of type recipe, "
509  "cannot test further");
510  return 0;
511  }
512 
513  if (recipe_create != NULL && recipe_exec != NULL && recipe_deinit != NULL) {
514 
515  cpl_test_zero(recipe_create(plugin));
516 
517  recipe = (cpl_recipe *) plugin;
518 
519  cpl_test_nonnull( recipe->parameters );
520 
521  recipe_parameterlist_set(recipe->parameters);
522 
523  cpl_parameterlist_dump(recipe->parameters, stream);
524 
525  recipe->frames = cpl_frameset_new();
526 
527  if (irplib_plugin_has_sof_from_env(plugin, "RECIPE_SOF_PATH")) {
528 
529  recipe_sof_test_from_env(plugin);
530 
531  } else {
532 
533  const cpl_msg_severity msg_level = cpl_msg_get_level();
534 
535  /* Unless the CPL_MSG_LEVEL has been explicitly set, turn off
536  terminal messaging completely while inside this function */
537  if (getenv("CPL_MSG_LEVEL") == NULL) cpl_msg_set_level(CPL_MSG_OFF);
538 
539  cpl_msg_info(cpl_func,"Checking handling of pre-existing CPL error "
540  "state - may produce warning(s)/error(s):");
541  cpl_error_set(cpl_func, CPL_ERROR_EOL);
542  /* Call recipe and expect non-zero return code */
543  cpl_test( recipe_exec(plugin) );
544  /* Expect also the CPL error code to be preserved */
545  cpl_test_error( CPL_ERROR_EOL );
546 
547  cpl_msg_info(cpl_func,"Checking handling of empty frameset - "
548  "may produce warning(s)/error(s):");
549  /* Call recipe and expect non-zero return code */
550  cpl_test( recipe_exec(plugin) );
551  error = cpl_error_get_code();
552  /* Expect also the CPL error code to be set */
553  cpl_test_error( error );
554  cpl_test( error );
555 
556  cpl_msg_info(cpl_func,"Checking handling of dummy frameset - "
557  "may produce warning(s)/error(s):");
558  do {
559  cpl_frame * f = cpl_frame_new();
560  error = cpl_frame_set_filename(f, "/dev/null");
561  cpl_test_eq_error(error, CPL_ERROR_NONE);
562  error = cpl_frame_set_tag(f, "RECIPE_DUMMY_TAG");
563  cpl_test_eq_error(error, CPL_ERROR_NONE);
564  error = cpl_frameset_insert(recipe->frames, f);
565  cpl_test_eq_error(error, CPL_ERROR_NONE);
566 
567  /* Call recipe and expect non-zero return code */
568  cpl_test( recipe_exec(plugin) );
569  error = cpl_error_get_code();
570  /* Expect also the CPL error code to be set */
571  cpl_test_error( error );
572  cpl_test( error );
573 
574  error = cpl_frameset_erase_frame(recipe->frames, f);
575  cpl_test_eq_error(error, CPL_ERROR_NONE);
576 
577  } while (0);
578 
579 #ifdef IRPLIB_TEST_RANDOM_SOF
580  recipe_sof_test_devfile(plugin, DEV_RANDOM, nstr, astr);
581 #endif
582 
583  recipe_sof_test_devfile(plugin, "/dev/null", nstr, astr);
584 
585  recipe_sof_test_devfile(plugin, ".", nstr, astr);
586 
587  recipe_sof_test_image_empty(plugin, nstr, astr);
588 
589  recipe_sof_test_local(plugin);
590 
591  cpl_msg_set_level(msg_level);
592 
593  }
594 
595  cpl_frameset_delete(recipe->frames);
596 
597  error = recipe_deinit(plugin);
598  cpl_test_eq_error(error, CPL_ERROR_NONE);
599  }
600 
601  if (stream != stdout) fclose(stream);
602 
603  return 0;
604 }
605 
608 /*----------------------------------------------------------------------------*/
618 /*----------------------------------------------------------------------------*/
619 static void recipe_parameterlist_set(cpl_parameterlist * self)
620 {
621 
622  cpl_parameter * p = cpl_parameterlist_get_first(self);
623 
624  for (; p != NULL; p = cpl_parameterlist_get_next(self)) {
625 
626  const char * envvar;
627  const char * svalue;
628 
629  /* FIXME: Needed ? */
630  if (cpl_parameter_get_default_flag(p)) continue;
631 
632  cpl_msg_debug(cpl_func, __FILE__ " line %u: OK", __LINE__);
633 
634  envvar = cpl_parameter_get_alias(p, CPL_PARAMETER_MODE_ENV);
635  svalue = envvar ? getenv(envvar) : NULL;
636 
637  switch (cpl_parameter_get_type(p)) {
638  case CPL_TYPE_BOOL: {
639  const int value
640  = svalue ? atoi(svalue) : cpl_parameter_get_default_bool(p);
641  cpl_parameter_set_bool(p, value);
642  break;
643  }
644  case CPL_TYPE_INT: {
645  const int value
646  = svalue ? atoi(svalue) : cpl_parameter_get_default_int(p);
647  cpl_parameter_set_int(p, value);
648  break;
649  }
650  case CPL_TYPE_DOUBLE: {
651  const double value
652  = svalue ? atof(svalue) : cpl_parameter_get_default_double(p);
653  cpl_parameter_set_double(p, value);
654  break;
655  }
656  case CPL_TYPE_STRING:
657  {
658  const char * s_default = cpl_parameter_get_default_string(p);
659  /* Replace NULL with "" */
660  const char * value
661  = svalue ? svalue : (s_default ? s_default : "");
662  cpl_parameter_set_string(p, value);
663  break;
664  }
665 
666  default:
667  assert( 0 ); /* It is a testing error to reach this point */
668  }
669  }
670 }
671 
672 
673 /*----------------------------------------------------------------------------*/
683 /*----------------------------------------------------------------------------*/
684 static void recipe_sof_test_devfile(cpl_plugin * plugin, const char * filename,
685  size_t nstr, const char *astr[])
686 {
687  cpl_recipe * recipe = (cpl_recipe*)plugin;
688  int (*recipe_exec) (cpl_plugin *);
689  cpl_frameset * copy;
690  cpl_error_code error;
691  size_t i;
692 
693 
694  if (nstr < 1) return;
695  if (filename == NULL) return;
696 
697  cpl_msg_info(cpl_func, "Testing recipe with %u %s as input ",
698  (unsigned)nstr, filename);
699 
700  for (i = 0; i < nstr; i++) {
701  cpl_frame * f = cpl_frame_new();
702 
703  error = cpl_frame_set_filename(f, filename);
704  cpl_test_eq_error(error, CPL_ERROR_NONE);
705 
706  error = cpl_frame_set_tag(f, astr[i]);
707  cpl_test_eq_error(error, CPL_ERROR_NONE);
708 
709  error = cpl_frameset_insert(recipe->frames, f);
710  cpl_test_eq_error(error, CPL_ERROR_NONE);
711  }
712 
713  copy = cpl_frameset_duplicate(recipe->frames);
714 
715  recipe_exec = cpl_plugin_get_exec(plugin);
716  cpl_test( recipe_exec != NULL);
717 
718  if (recipe_exec != NULL) {
719 
720  /* Call recipe and expect non-zero return code */
721  cpl_test( recipe_exec(plugin) );
722  error = cpl_error_get_code();
723  /* Expect also the CPL error code to be set */
724  cpl_test_error( error );
725  cpl_test( error );
726 
727  recipe_frameset_test_frameset_diff(recipe->frames, copy);
728 
729  recipe_frameset_empty(recipe->frames);
730  }
731 
732  cpl_frameset_delete(copy);
733 
734  return;
735 }
736 
737 /*----------------------------------------------------------------------------*/
744 /*----------------------------------------------------------------------------*/
745 static void recipe_sof_test_image_empty(cpl_plugin * plugin, size_t nstr,
746  const char *astr[])
747 {
748  cpl_recipe * recipe = (cpl_recipe*)plugin;
749  int (*recipe_exec) (cpl_plugin *);
750  cpl_frameset * copy;
751  cpl_error_code error;
752  size_t i;
753  cpl_frame * frame;
754  cpl_image * iempty;
755  int retstat;
756 
757 
758  if (nstr < 1) return;
759 
760  cpl_msg_info(cpl_func, "Testing recipe with %u empty images as input ",
761  (unsigned)nstr);
762 
763  iempty = cpl_image_new(13, 17, CPL_TYPE_FLOAT);
764  cpl_test_nonnull(iempty);
765 
766  for (i = 0; i < nstr; i++) {
767  cpl_frame * f = cpl_frame_new();
768  char * rawname = cpl_sprintf("raw%05u.fits", (unsigned)(i+1));
769 
770  error = cpl_image_save(iempty, rawname,CPL_BPP_IEEE_FLOAT, NULL,
771  CPL_IO_DEFAULT);
772  cpl_test_eq_error(error, CPL_ERROR_NONE);
773 
774  error = cpl_frame_set_filename(f, rawname);
775  cpl_test_eq_error(error, CPL_ERROR_NONE);
776 
777  error = cpl_frame_set_tag(f, astr[i]);
778  cpl_test_eq_error(error, CPL_ERROR_NONE);
779 
780  error = cpl_frameset_insert(recipe->frames, f);
781  cpl_test_eq_error(error, CPL_ERROR_NONE);
782 
783  cpl_free(rawname);
784  }
785  cpl_image_delete(iempty);
786 
787  copy = cpl_frameset_duplicate(recipe->frames);
788 
789  recipe_exec = cpl_plugin_get_exec(plugin);
790  cpl_test(recipe_exec != NULL);
791 
792  if (recipe_exec != NULL) {
793 
794  /* Call recipe and expect consistency between return code and
795  CPL error */
796 
797  retstat = recipe_exec(plugin);
798  error = cpl_error_get_code();
799  /* Expect also the CPL error code to be set */
800  if (error == 0) {
801  cpl_test_zero(retstat);
802  } else {
803  cpl_test(retstat);
804  }
805  cpl_test_error( error );
806 
807  recipe_frameset_test_frameset_diff(recipe->frames, copy);
808 
809  for (frame = cpl_frameset_get_first(recipe->frames); frame != NULL;
810  frame = cpl_frameset_get_next(recipe->frames))
811  {
812  cpl_test_zero( remove(cpl_frame_get_filename(frame)) );
813  }
814 
815  recipe_frameset_empty(recipe->frames);
816  }
817 
818  cpl_frameset_delete(copy);
819 
820  return;
821 }
822 
823 
824 /*----------------------------------------------------------------------------*/
832 /*----------------------------------------------------------------------------*/
833 cpl_boolean irplib_plugin_has_sof_from_env(const cpl_plugin * plugin,
834  const char * envname)
835 {
836  const char * recipename = cpl_plugin_get_name(plugin);
837  const char * sof_path = envname ? getenv(envname) : NULL;
838  cpl_frameset * frames;
839  char * sof_name;
840  const cpl_frame * ffirst;
841 
842  cpl_ensure(plugin != NULL, CPL_ERROR_NULL_INPUT, CPL_FALSE);
843  cpl_ensure(envname != NULL, CPL_ERROR_NULL_INPUT, CPL_FALSE);
844  cpl_ensure(recipename != NULL, CPL_ERROR_DATA_NOT_FOUND, CPL_FALSE);
845  cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), CPL_FALSE);
846 
847  if (sof_path == NULL) return CPL_FALSE;
848 
849  sof_name = cpl_sprintf("%s/%s.sof", sof_path, recipename);
850 
851  frames = cpl_frameset_new();
852  recipe_frameset_load(frames, sof_name);
853 
854  ffirst = cpl_frameset_get_first_const(frames);
855 
856  cpl_free(sof_name);
857  cpl_frameset_delete(frames);
858 
859  cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), CPL_FALSE);
860 
861  return ffirst ? CPL_TRUE : CPL_FALSE;
862 
863 }
864 
865 /*----------------------------------------------------------------------------*/
872 /*----------------------------------------------------------------------------*/
873 static void recipe_sof_test_from_env(cpl_plugin * plugin)
874 {
875  cpl_recipe * recipe = (cpl_recipe*)plugin;
876  const char * recipename = cpl_plugin_get_name(plugin);
877  const char * var_name = "RECIPE_SOF_PATH";
878  const char * sof_path = getenv(var_name);
879  cpl_error_code error;
880 
881  char * sof_name;
882 
883  if (sof_path == NULL) {
884  cpl_msg_warning(cpl_func, "Environment variable %s is unset: "
885  "No SOFs to check", var_name);
886  return;
887  }
888 
889  cpl_msg_debug(cpl_func, "Checking for SOFs in %s", sof_path);
890 
891  cpl_test_nonnull( recipename );
892  if (recipename == NULL) return;
893 
894  sof_name = cpl_sprintf("%s/%s.sof", sof_path, recipename);
895 
896  cpl_msg_debug(cpl_func, "Checking for SOF %s", sof_name);
897 
898  recipe_frameset_load(recipe->frames, sof_name);
899 
900  if (!cpl_frameset_is_empty(recipe->frames)) {
901 
902  int (*recipe_exec ) (cpl_plugin *);
903  cpl_frameset * copy = cpl_frameset_duplicate(recipe->frames);
904 
905  recipe_exec = cpl_plugin_get_exec(plugin);
906  cpl_test(recipe_exec != NULL);
907 
908  if (recipe_exec != NULL) {
909  cpl_msg_info(cpl_func,"Checking handling of SOF: %s", sof_name);
910 
911  /* Call recipe and expect zero return code */
912  cpl_test_zero( recipe_exec(plugin) );
913  /* Expect also the CPL error code to be clear */
914  cpl_test_error(CPL_ERROR_NONE);
915 
916  error = cpl_dfs_update_product_header(recipe->frames);
917  cpl_test_eq_error(error, CPL_ERROR_NONE);
918 
919  recipe_frameset_test_frameset_diff(recipe->frames, copy);
920 
921  recipe_frameset_empty(recipe->frames);
922  }
923 
924  cpl_frameset_delete(copy);
925 
926  }
927 
928  cpl_free(sof_name);
929 
930  return;
931 }
932 
933 
934 
935 /*----------------------------------------------------------------------------*/
942 /*----------------------------------------------------------------------------*/
943 static void recipe_sof_test_local(cpl_plugin * plugin)
944 {
945  cpl_recipe * recipe = (cpl_recipe*)plugin;
946  const char * recipename = cpl_plugin_get_name(plugin);
947  cpl_error_code error;
948  char * sof_name = cpl_sprintf("%s.sof", recipename);
949 
950  cpl_msg_debug(cpl_func, "Checking for SOF %s", sof_name);
951 
952  recipe_frameset_load(recipe->frames, sof_name);
953 
954  if (!cpl_frameset_is_empty(recipe->frames)) {
955 
956  int (*recipe_exec ) (cpl_plugin *);
957  cpl_frameset * copy = cpl_frameset_duplicate(recipe->frames);
958 
959  recipe_exec = cpl_plugin_get_exec(plugin);
960  cpl_test(recipe_exec != NULL);
961 
962  if (recipe_exec != NULL) {
963 
964  cpl_msg_info(cpl_func,"Checking handling of SOF: %s", sof_name);
965 
966  /* Call recipe and expect zero return code */
967  cpl_test_zero( recipe_exec(plugin) );
968  /* Expect also the CPL error code to be clear */
969  cpl_test_error(CPL_ERROR_NONE);
970 
971  error = cpl_dfs_update_product_header(recipe->frames);
972  cpl_test_eq_error( error, CPL_ERROR_NONE );
973 
974  recipe_frameset_test_frameset_diff(recipe->frames, copy);
975 
976  recipe_frameset_empty(recipe->frames);
977  }
978 
979  cpl_frameset_delete(copy);
980  }
981 
982  cpl_free(sof_name);
983 
984  return;
985 }
986 
987 
988 
989 
990 /**********************************************************************/
1004 /**********************************************************************/
1005 
1006 static void recipe_frameset_load(cpl_frameset * set, const char *name)
1007 {
1008 
1009  FILE *fp;
1010  char line[LINE_LEN_MAX];
1011  char path[LINE_LEN_MAX], group[LINE_LEN_MAX], tag[LINE_LEN_MAX];
1012  int line_number;
1013 
1014  assert( set != NULL );
1015  assert( name != NULL );
1016 
1017  fp = fopen(name, "r");
1018  if (fp == NULL) {
1019  cpl_msg_debug(cpl_func, "Unable to open SOF file '%s'", name);
1020  return;
1021  }
1022 
1023  /* Loop over all the lines in the set-of-frames file */
1024  for (line_number = 0; fgets(line, LINE_LEN_MAX - 1, fp); line_number++) {
1025 
1026  char scan_fmt[50];
1027  cpl_frame_group grp;
1028  cpl_frame * frame;
1029  int n;
1030 
1031  if (line[0] == '#') continue;
1032 
1033  snprintf(scan_fmt, 49, "%%%ds %%%ds %%%ds", LINE_LEN_MAX - 1,
1034  LINE_LEN_MAX - 1, LINE_LEN_MAX - 1);
1035  n = sscanf(line, scan_fmt, path, tag, group);
1036 
1037  if (n < 1) {
1038  cpl_msg_warning(cpl_func, "Spurious line no. %d in %s: %s",
1039  line_number, name, line);
1040  break;
1041  }
1042 
1043  /* Allocate a new frame */
1044  frame = cpl_frame_new();
1045 
1046  /* Set the filename component of the frame */
1047  cpl_frame_set_filename(frame, path);
1048 
1049  /* Set the tag component of the frame (or set a default) */
1050  cpl_frame_set_tag(frame, n == 1 ? "" : tag);
1051 
1052  cpl_frameset_insert(set, frame);
1053 
1054  /* Set the group component of the frame (or set a default) */
1055  if (n < 3) continue;
1056 
1057  if (!strcmp(group, CPL_FRAME_GROUP_RAW_ID))
1058  grp = CPL_FRAME_GROUP_RAW;
1059  else if (!strcmp(group, CPL_FRAME_GROUP_CALIB_ID))
1060  grp = CPL_FRAME_GROUP_CALIB;
1061  else if (!strcmp(group, CPL_FRAME_GROUP_PRODUCT_ID))
1062  grp = CPL_FRAME_GROUP_PRODUCT;
1063  else
1064  grp = CPL_FRAME_GROUP_NONE;
1065 
1066  cpl_frame_set_group(frame, grp);
1067  }
1068 
1069  fclose(fp);
1070 
1071  return;
1072 
1073 }
1074 
1075 
1076 /*----------------------------------------------------------------------------*/
1086 /*----------------------------------------------------------------------------*/
1087 static
1088 const cpl_parameter * irplib_parameterlist_get(const cpl_parameterlist * self,
1089  const char * instrume,
1090  const char * recipe,
1091  const char * parameter)
1092 {
1093 
1094  char * paramname;
1095  const cpl_parameter * par;
1096 
1097 
1098  cpl_ensure(instrume != NULL, CPL_ERROR_NULL_INPUT, NULL);
1099  cpl_ensure(recipe != NULL, CPL_ERROR_NULL_INPUT, NULL);
1100  cpl_ensure(parameter != NULL, CPL_ERROR_NULL_INPUT, NULL);
1101 
1102  paramname = cpl_sprintf("%s.%s.%s", instrume, recipe, parameter);
1103 
1104  par = cpl_parameterlist_find_const(self, paramname);
1105 
1106  if (par == NULL) (void)cpl_error_set_message(cpl_func,
1107  cpl_error_get_code()
1108  ? cpl_error_get_code()
1109  : CPL_ERROR_DATA_NOT_FOUND,
1110  "%s", paramname);
1111 
1112  cpl_free(paramname);
1113 
1114  return par;
1115 
1116 }
1117 
1118 
1119 /*----------------------------------------------------------------------------*/
1145 /*----------------------------------------------------------------------------*/
1146 static void recipe_frameset_empty(cpl_frameset * self)
1147 {
1148  cpl_frame * f;
1149 
1150  if (self == NULL) {
1151  cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
1152  return;
1153  }
1154 
1155  for (f = cpl_frameset_get_first(self); f != NULL;
1156  f = cpl_frameset_get_first(self))
1157  {
1158  cpl_frameset_erase_frame(self, f);
1159  }
1160 }
1161 
1162 
1163 /*----------------------------------------------------------------------------*/
1183 /*----------------------------------------------------------------------------*/
1184 static void recipe_frameset_test_frame(const cpl_frame * self)
1185 {
1186 
1187  cpl_msg_info(cpl_func, "Validating new frame: %s",
1188  cpl_frame_get_filename(self));
1189 
1190  cpl_test_nonnull(self);
1191 
1192  /* Frame must be tagged */
1193  cpl_test_nonnull(cpl_frame_get_tag(self));
1194 
1195  /* New frames must be products */
1196  cpl_test_eq(cpl_frame_get_group(self), CPL_FRAME_GROUP_PRODUCT);
1197 
1198  if (cpl_frame_get_type(self) != CPL_FRAME_TYPE_PAF) {
1199  /* All but PAF (?) must be FITS */
1200  cpl_test_fits(cpl_frame_get_filename(self));
1201  } else {
1202  /* Frame must at least have a filename */
1203  cpl_test_nonnull(cpl_frame_get_filename(self));
1204  }
1205 }
1206 
1207 /*----------------------------------------------------------------------------*/
1228 /*----------------------------------------------------------------------------*/
1229 static void recipe_frameset_test_frameset_diff(const cpl_frameset * self,
1230  const cpl_frameset * other)
1231 {
1232 
1233  const cpl_frame * frame = cpl_frameset_get_first_const(other);
1234 
1235  /* First verify that filenames in other are non-NULL */
1236  for (;frame != NULL; frame = cpl_frameset_get_next_const(other)) {
1237  const char * file = cpl_frame_get_filename(frame);
1238 
1239  if (file == NULL) {
1240  cpl_test_nonnull(cpl_frame_get_filename(frame));
1241  break;
1242  }
1243  }
1244  if (frame != NULL) return;
1245 
1246  frame = cpl_frameset_get_first_const(self);
1247 
1248  for (;frame != NULL; frame = cpl_frameset_get_next_const(self)) {
1249  const cpl_frame * cmp = cpl_frameset_get_first_const(other);
1250  const char * file = cpl_frame_get_filename(frame);
1251 
1252  if (file == NULL) {
1253  cpl_test_nonnull(cpl_frame_get_filename(frame));
1254  continue;
1255  }
1256 
1257  for (;cmp != NULL; cmp = cpl_frameset_get_next_const(other)) {
1258  const char * cfile = cpl_frame_get_filename(cmp);
1259 
1260  if (!strcmp(file, cfile)) break;
1261 
1262  }
1263  if (cmp == NULL) {
1264  /* frame is new */
1265 
1266  cpl_test_eq(cpl_frame_get_group(frame), CPL_FRAME_GROUP_PRODUCT);
1267  recipe_frameset_test_frame(frame);
1268  }
1269  }
1270 }