DETMON Pipeline Reference Manual  1.2.7
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: not supported by cvs2svn $
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("%s-raw%05u.fits",
769  cpl_plugin_get_name(plugin),
770  (unsigned)(i+1));
771 
772  error = cpl_image_save(iempty, rawname,CPL_BPP_IEEE_FLOAT, NULL,
773  CPL_IO_DEFAULT);
774  cpl_test_eq_error(error, CPL_ERROR_NONE);
775 
776  error = cpl_frame_set_filename(f, rawname);
777  cpl_test_eq_error(error, CPL_ERROR_NONE);
778 
779  error = cpl_frame_set_tag(f, astr[i]);
780  cpl_test_eq_error(error, CPL_ERROR_NONE);
781 
782  error = cpl_frameset_insert(recipe->frames, f);
783  cpl_test_eq_error(error, CPL_ERROR_NONE);
784 
785  cpl_free(rawname);
786  }
787  cpl_image_delete(iempty);
788 
789  copy = cpl_frameset_duplicate(recipe->frames);
790 
791  recipe_exec = cpl_plugin_get_exec(plugin);
792  cpl_test(recipe_exec != NULL);
793 
794  if (recipe_exec != NULL) {
795 
796  /* Call recipe and expect consistency between return code and
797  CPL error */
798 
799  retstat = recipe_exec(plugin);
800  error = cpl_error_get_code();
801  /* Expect also the CPL error code to be set */
802  if (error == 0) {
803  cpl_test_zero(retstat);
804  } else {
805  cpl_test(retstat);
806  }
807  cpl_test_error( error );
808 
809  recipe_frameset_test_frameset_diff(recipe->frames, copy);
810 
811  for (frame = cpl_frameset_get_first(recipe->frames); frame != NULL;
812  frame = cpl_frameset_get_next(recipe->frames))
813  {
814  cpl_test_zero( remove(cpl_frame_get_filename(frame)) );
815  }
816 
817  recipe_frameset_empty(recipe->frames);
818  }
819 
820  cpl_frameset_delete(copy);
821 
822  return;
823 }
824 
825 
826 /*----------------------------------------------------------------------------*/
834 /*----------------------------------------------------------------------------*/
835 cpl_boolean irplib_plugin_has_sof_from_env(const cpl_plugin * plugin,
836  const char * envname)
837 {
838  const char * recipename = cpl_plugin_get_name(plugin);
839  const char * sof_path = envname ? getenv(envname) : NULL;
840  cpl_frameset * frames;
841  char * sof_name;
842  const cpl_frame * ffirst;
843 
844  cpl_ensure(plugin != NULL, CPL_ERROR_NULL_INPUT, CPL_FALSE);
845  cpl_ensure(envname != NULL, CPL_ERROR_NULL_INPUT, CPL_FALSE);
846  cpl_ensure(recipename != NULL, CPL_ERROR_DATA_NOT_FOUND, CPL_FALSE);
847  cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), CPL_FALSE);
848 
849  if (sof_path == NULL) return CPL_FALSE;
850 
851  sof_name = cpl_sprintf("%s/%s.sof", sof_path, recipename);
852 
853  frames = cpl_frameset_new();
854  recipe_frameset_load(frames, sof_name);
855 
856  ffirst = cpl_frameset_get_position_const(frames, 0);
857 
858  cpl_free(sof_name);
859  cpl_frameset_delete(frames);
860 
861  cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), CPL_FALSE);
862 
863  return ffirst ? CPL_TRUE : CPL_FALSE;
864 
865 }
866 
867 /*----------------------------------------------------------------------------*/
874 /*----------------------------------------------------------------------------*/
875 static void recipe_sof_test_from_env(cpl_plugin * plugin)
876 {
877  cpl_recipe * recipe = (cpl_recipe*)plugin;
878  const char * recipename = cpl_plugin_get_name(plugin);
879  const char * var_name = "RECIPE_SOF_PATH";
880  const char * sof_path = getenv(var_name);
881  cpl_error_code error;
882 
883  char * sof_name;
884 
885  if (sof_path == NULL) {
886  cpl_msg_warning(cpl_func, "Environment variable %s is unset: "
887  "No SOFs to check", var_name);
888  return;
889  }
890 
891  cpl_msg_debug(cpl_func, "Checking for SOFs in %s", sof_path);
892 
893  cpl_test_nonnull( recipename );
894  if (recipename == NULL) return;
895 
896  sof_name = cpl_sprintf("%s/%s.sof", sof_path, recipename);
897 
898  cpl_msg_debug(cpl_func, "Checking for SOF %s", sof_name);
899 
900  recipe_frameset_load(recipe->frames, sof_name);
901 
902  if (!cpl_frameset_is_empty(recipe->frames)) {
903 
904  int (*recipe_exec ) (cpl_plugin *);
905  cpl_frameset * copy = cpl_frameset_duplicate(recipe->frames);
906 
907  recipe_exec = cpl_plugin_get_exec(plugin);
908  cpl_test(recipe_exec != NULL);
909 
910  if (recipe_exec != NULL) {
911  cpl_msg_info(cpl_func,"Checking handling of SOF: %s", sof_name);
912 
913  /* Call recipe and expect zero return code */
914  cpl_test_zero( recipe_exec(plugin) );
915  /* Expect also the CPL error code to be clear */
916  cpl_test_error(CPL_ERROR_NONE);
917 
918  error = cpl_dfs_update_product_header(recipe->frames);
919  cpl_test_eq_error(error, CPL_ERROR_NONE);
920 
921  recipe_frameset_test_frameset_diff(recipe->frames, copy);
922 
923  recipe_frameset_empty(recipe->frames);
924  }
925 
926  cpl_frameset_delete(copy);
927 
928  }
929 
930  cpl_free(sof_name);
931 
932  return;
933 }
934 
935 
936 
937 /*----------------------------------------------------------------------------*/
944 /*----------------------------------------------------------------------------*/
945 static void recipe_sof_test_local(cpl_plugin * plugin)
946 {
947  cpl_recipe * recipe = (cpl_recipe*)plugin;
948  const char * recipename = cpl_plugin_get_name(plugin);
949  cpl_error_code error;
950  char * sof_name = cpl_sprintf("%s.sof", recipename);
951 
952  cpl_msg_debug(cpl_func, "Checking for SOF %s", sof_name);
953 
954  recipe_frameset_load(recipe->frames, sof_name);
955 
956  if (!cpl_frameset_is_empty(recipe->frames)) {
957 
958  int (*recipe_exec ) (cpl_plugin *);
959  cpl_frameset * copy = cpl_frameset_duplicate(recipe->frames);
960 
961  recipe_exec = cpl_plugin_get_exec(plugin);
962  cpl_test(recipe_exec != NULL);
963 
964  if (recipe_exec != NULL) {
965 
966  cpl_msg_info(cpl_func,"Checking handling of SOF: %s", sof_name);
967 
968  /* Call recipe and expect zero return code */
969  cpl_test_zero( recipe_exec(plugin) );
970  /* Expect also the CPL error code to be clear */
971  cpl_test_error(CPL_ERROR_NONE);
972 
973  error = cpl_dfs_update_product_header(recipe->frames);
974  cpl_test_eq_error( error, CPL_ERROR_NONE );
975 
976  recipe_frameset_test_frameset_diff(recipe->frames, copy);
977 
978  recipe_frameset_empty(recipe->frames);
979  }
980 
981  cpl_frameset_delete(copy);
982  }
983 
984  cpl_free(sof_name);
985 
986  return;
987 }
988 
989 
990 
991 
992 /**********************************************************************/
1006 /**********************************************************************/
1007 
1008 static void recipe_frameset_load(cpl_frameset * set, const char *name)
1009 {
1010 
1011  FILE *fp;
1012  char line[LINE_LEN_MAX];
1013  char path[LINE_LEN_MAX], group[LINE_LEN_MAX], tag[LINE_LEN_MAX];
1014  int line_number;
1015 
1016  assert( set != NULL );
1017  assert( name != NULL );
1018 
1019  fp = fopen(name, "r");
1020  if (fp == NULL) {
1021  cpl_msg_debug(cpl_func, "Unable to open SOF file '%s'", name);
1022  return;
1023  }
1024 
1025  /* Loop over all the lines in the set-of-frames file */
1026  for (line_number = 0; fgets(line, LINE_LEN_MAX - 1, fp); line_number++) {
1027 
1028  char scan_fmt[50];
1029  cpl_frame_group grp;
1030  cpl_frame * frame;
1031  int n;
1032 
1033  if (line[0] == '#') continue;
1034 
1035  snprintf(scan_fmt, 49, "%%%ds %%%ds %%%ds", LINE_LEN_MAX - 1,
1036  LINE_LEN_MAX - 1, LINE_LEN_MAX - 1);
1037  n = sscanf(line, scan_fmt, path, tag, group);
1038 
1039  if (n < 1) {
1040  cpl_msg_warning(cpl_func, "Spurious line no. %d in %s: %s",
1041  line_number, name, line);
1042  break;
1043  }
1044 
1045  /* Allocate a new frame */
1046  frame = cpl_frame_new();
1047 
1048  /* Set the filename component of the frame */
1049  cpl_frame_set_filename(frame, path);
1050 
1051  /* Set the tag component of the frame (or set a default) */
1052  cpl_frame_set_tag(frame, n == 1 ? "" : tag);
1053 
1054  cpl_frameset_insert(set, frame);
1055 
1056  /* Set the group component of the frame (or set a default) */
1057  if (n < 3) continue;
1058 
1059  if (!strcmp(group, CPL_FRAME_GROUP_RAW_ID))
1060  grp = CPL_FRAME_GROUP_RAW;
1061  else if (!strcmp(group, CPL_FRAME_GROUP_CALIB_ID))
1062  grp = CPL_FRAME_GROUP_CALIB;
1063  else if (!strcmp(group, CPL_FRAME_GROUP_PRODUCT_ID))
1064  grp = CPL_FRAME_GROUP_PRODUCT;
1065  else
1066  grp = CPL_FRAME_GROUP_NONE;
1067 
1068  cpl_frame_set_group(frame, grp);
1069  }
1070 
1071  fclose(fp);
1072 
1073  return;
1074 
1075 }
1076 
1077 
1078 /*----------------------------------------------------------------------------*/
1088 /*----------------------------------------------------------------------------*/
1089 static
1090 const cpl_parameter * irplib_parameterlist_get(const cpl_parameterlist * self,
1091  const char * instrume,
1092  const char * recipe,
1093  const char * parameter)
1094 {
1095 
1096  char * paramname;
1097  const cpl_parameter * par;
1098 
1099 
1100  cpl_ensure(instrume != NULL, CPL_ERROR_NULL_INPUT, NULL);
1101  cpl_ensure(recipe != NULL, CPL_ERROR_NULL_INPUT, NULL);
1102  cpl_ensure(parameter != NULL, CPL_ERROR_NULL_INPUT, NULL);
1103 
1104  paramname = cpl_sprintf("%s.%s.%s", instrume, recipe, parameter);
1105 
1106  par = cpl_parameterlist_find_const(self, paramname);
1107 
1108  if (par == NULL) (void)cpl_error_set_message(cpl_func,
1109  cpl_error_get_code()
1110  ? cpl_error_get_code()
1111  : CPL_ERROR_DATA_NOT_FOUND,
1112  "%s", paramname);
1113 
1114  cpl_free(paramname);
1115 
1116  return par;
1117 
1118 }
1119 
1120 
1121 /*----------------------------------------------------------------------------*/
1147 /*----------------------------------------------------------------------------*/
1148 static void recipe_frameset_empty(cpl_frameset * self)
1149 {
1150  cpl_frame * f;
1151 
1152  if (self == NULL) {
1153  cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
1154  return;
1155  }
1156 
1157  for (f = cpl_frameset_get_first(self); f != NULL;
1158  f = cpl_frameset_get_first(self))
1159  {
1160  cpl_frameset_erase_frame(self, f);
1161  }
1162 }
1163 
1164 
1165 /*----------------------------------------------------------------------------*/
1185 /*----------------------------------------------------------------------------*/
1186 static void recipe_frameset_test_frame(const cpl_frame * self)
1187 {
1188 
1189  cpl_msg_info(cpl_func, "Validating new frame: %s",
1190  cpl_frame_get_filename(self));
1191 
1192  cpl_test_nonnull(self);
1193 
1194  /* Frame must be tagged */
1195  cpl_test_nonnull(cpl_frame_get_tag(self));
1196 
1197  /* New frames must be products */
1198  cpl_test_eq(cpl_frame_get_group(self), CPL_FRAME_GROUP_PRODUCT);
1199 
1200  if (cpl_frame_get_type(self) != CPL_FRAME_TYPE_PAF) {
1201  /* All but PAF (?) must be FITS */
1202  cpl_test_fits(cpl_frame_get_filename(self));
1203  } else {
1204  /* Frame must at least have a filename */
1205  cpl_test_nonnull(cpl_frame_get_filename(self));
1206  }
1207 }
1208 
1209 /*----------------------------------------------------------------------------*/
1230 /*----------------------------------------------------------------------------*/
1231 static void recipe_frameset_test_frameset_diff(const cpl_frameset * self,
1232  const cpl_frameset * other)
1233 {
1234 
1235  const cpl_frame * frame = cpl_frameset_get_first_const(other);
1236 
1237  /* First verify that filenames in other are non-NULL */
1238  for (;frame != NULL; frame = cpl_frameset_get_next_const(other)) {
1239  const char * file = cpl_frame_get_filename(frame);
1240 
1241  if (file == NULL) {
1242  cpl_test_nonnull(cpl_frame_get_filename(frame));
1243  break;
1244  }
1245  }
1246  if (frame != NULL) return;
1247 
1248  frame = cpl_frameset_get_first_const(self);
1249 
1250  for (;frame != NULL; frame = cpl_frameset_get_next_const(self)) {
1251  const cpl_frame * cmp = cpl_frameset_get_first_const(other);
1252  const char * file = cpl_frame_get_filename(frame);
1253 
1254  if (file == NULL) {
1255  cpl_test_nonnull(cpl_frame_get_filename(frame));
1256  continue;
1257  }
1258 
1259  for (;cmp != NULL; cmp = cpl_frameset_get_next_const(other)) {
1260  const char * cfile = cpl_frame_get_filename(cmp);
1261 
1262  if (!strcmp(file, cfile)) break;
1263 
1264  }
1265  if (cmp == NULL) {
1266  /* frame is new */
1267 
1268  cpl_test_eq(cpl_frame_get_group(frame), CPL_FRAME_GROUP_PRODUCT);
1269  recipe_frameset_test_frame(frame);
1270  }
1271  }
1272 }