NACO Pipeline Reference Manual  4.4.0
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  const cpl_parameter * par = irplib_parameterlist_get(self, instrume,
130  recipe, parameter);
131 
132  if (par == NULL) {
133 
134  (void)cpl_error_set_where(cpl_func);
135 
136  return NULL;
137  } else {
138  const char * value = cpl_parameter_get_string(par);
139 
140  if (value == NULL) (void)cpl_error_set_where(cpl_func);
141 
142  return value;
143  }
144 }
145 
146 /*----------------------------------------------------------------------------*/
156 /*----------------------------------------------------------------------------*/
157 cpl_boolean irplib_parameterlist_get_bool(const cpl_parameterlist * self,
158  const char * instrume,
159  const char * recipe,
160  const char * parameter)
161 {
162  const cpl_parameter * par = irplib_parameterlist_get(self, instrume,
163  recipe, parameter);
164 
165  if (par == NULL) {
166 
167  (void)cpl_error_set_where(cpl_func);
168 
169  return CPL_FALSE;
170  } else {
171  cpl_errorstate prestate = cpl_errorstate_get();
172  const cpl_boolean value = cpl_parameter_get_bool(par);
173 
174  if (!cpl_errorstate_is_equal(prestate))
175  (void)cpl_error_set_where(cpl_func);
176 
177  return value;
178  }
179 }
180 
181 
182 /*----------------------------------------------------------------------------*/
192 /*----------------------------------------------------------------------------*/
193 int irplib_parameterlist_get_int(const cpl_parameterlist * self,
194  const char * instrume,
195  const char * recipe,
196  const char * parameter)
197 {
198  const cpl_parameter * par = irplib_parameterlist_get(self, instrume,
199  recipe, parameter);
200 
201  if (par == NULL) {
202 
203  (void)cpl_error_set_where(cpl_func);
204 
205  return 0;
206  } else {
207  cpl_errorstate prestate = cpl_errorstate_get();
208  const int value = cpl_parameter_get_int(par);
209 
210  if (!cpl_errorstate_is_equal(prestate))
211  (void)cpl_error_set_where(cpl_func);
212 
213  return value;
214  }
215 }
216 
217 /*----------------------------------------------------------------------------*/
227 /*----------------------------------------------------------------------------*/
228 double irplib_parameterlist_get_double(const cpl_parameterlist * self,
229  const char * instrume,
230  const char * recipe,
231  const char * parameter)
232 {
233  const cpl_parameter * par = irplib_parameterlist_get(self, instrume,
234  recipe, parameter);
235 
236  if (par == NULL) {
237 
238  (void)cpl_error_set_where(cpl_func);
239 
240  return 0.0;
241  } else {
242  cpl_errorstate prestate = cpl_errorstate_get();
243  const double value = cpl_parameter_get_double(par);
244 
245  if (!cpl_errorstate_is_equal(prestate))
246  (void)cpl_error_set_where(cpl_func);
247 
248  return value;
249  }
250 }
251 
252 /*----------------------------------------------------------------------------*/
266 /*----------------------------------------------------------------------------*/
267 cpl_error_code irplib_parameterlist_set_string(cpl_parameterlist * self,
268  const char * instrume,
269  const char * recipe,
270  const char * parameter,
271  const char * defvalue,
272  const char * alias,
273  const char * context,
274  const char * man)
275 {
276 
277  cpl_error_code error;
278  cpl_parameter * par;
279  char * paramname = cpl_sprintf("%s.%s.%s", instrume, recipe,
280  parameter);
281 
282  cpl_ensure_code(paramname != NULL, cpl_error_get_code());
283 
284  par = cpl_parameter_new_value(paramname, CPL_TYPE_STRING, man, context,
285  defvalue);
286  cpl_free(paramname);
287 
288  cpl_ensure_code(par != NULL, cpl_error_get_code());
289 
290  error = cpl_parameter_set_alias(par, CPL_PARAMETER_MODE_CLI,
291  alias ? alias : parameter);
292  cpl_ensure_code(!error, error);
293 
294  error = cpl_parameter_disable(par, CPL_PARAMETER_MODE_ENV);
295  cpl_ensure_code(!error, error);
296 
297  error = cpl_parameterlist_append(self, par);
298  cpl_ensure_code(!error, error);
299 
300  return CPL_ERROR_NONE;
301 }
302 
303 
304 /*----------------------------------------------------------------------------*/
318 /*----------------------------------------------------------------------------*/
319 cpl_error_code irplib_parameterlist_set_bool(cpl_parameterlist * self,
320  const char * instrume,
321  const char * recipe,
322  const char * parameter,
323  cpl_boolean defvalue,
324  const char * alias,
325  const char * context,
326  const char * man)
327 {
328 
329  cpl_error_code error;
330  cpl_parameter * par;
331  char * paramname = cpl_sprintf("%s.%s.%s", instrume, recipe,
332  parameter);
333 
334  cpl_ensure_code(paramname != NULL, cpl_error_get_code());
335 
336  par = cpl_parameter_new_value(paramname, CPL_TYPE_BOOL, man, context,
337  defvalue);
338  cpl_free(paramname);
339 
340  cpl_ensure_code(par != NULL, cpl_error_get_code());
341 
342  error = cpl_parameter_set_alias(par, CPL_PARAMETER_MODE_CLI,
343  alias ? alias : parameter);
344  cpl_ensure_code(!error, error);
345 
346  error = cpl_parameter_disable(par, CPL_PARAMETER_MODE_ENV);
347  cpl_ensure_code(!error, error);
348 
349  error = cpl_parameterlist_append(self, par);
350  cpl_ensure_code(!error, error);
351 
352  return CPL_ERROR_NONE;
353 }
354 
355 
356 
357 /*----------------------------------------------------------------------------*/
371 /*----------------------------------------------------------------------------*/
372 cpl_error_code irplib_parameterlist_set_int(cpl_parameterlist * self,
373  const char * instrume,
374  const char * recipe,
375  const char * parameter,
376  int defvalue,
377  const char * alias,
378  const char * context,
379  const char * man)
380 {
381 
382  cpl_error_code error;
383  cpl_parameter * par;
384  char * paramname = cpl_sprintf("%s.%s.%s", instrume, recipe,
385  parameter);
386 
387  cpl_ensure_code(paramname != NULL, cpl_error_get_code());
388 
389  par = cpl_parameter_new_value(paramname, CPL_TYPE_INT, man, context,
390  defvalue);
391  cpl_free(paramname);
392 
393  cpl_ensure_code(par != NULL, cpl_error_get_code());
394 
395  error = cpl_parameter_set_alias(par, CPL_PARAMETER_MODE_CLI,
396  alias ? alias : parameter);
397  cpl_ensure_code(!error, error);
398 
399  error = cpl_parameter_disable(par, CPL_PARAMETER_MODE_ENV);
400  cpl_ensure_code(!error, error);
401 
402  error = cpl_parameterlist_append(self, par);
403  cpl_ensure_code(!error, error);
404 
405  return CPL_ERROR_NONE;
406 }
407 
408 
409 /*----------------------------------------------------------------------------*/
423 /*----------------------------------------------------------------------------*/
424 cpl_error_code irplib_parameterlist_set_double(cpl_parameterlist * self,
425  const char * instrume,
426  const char * recipe,
427  const char * parameter,
428  double defvalue,
429  const char * alias,
430  const char * context,
431  const char * man)
432 {
433 
434  cpl_error_code error;
435  cpl_parameter * par;
436  char * paramname = cpl_sprintf("%s.%s.%s", instrume, recipe,
437  parameter);
438 
439  cpl_ensure_code(paramname != NULL, cpl_error_get_code());
440 
441  par = cpl_parameter_new_value(paramname, CPL_TYPE_DOUBLE, man, context,
442  defvalue);
443  cpl_free(paramname);
444 
445  cpl_ensure_code(par != NULL, cpl_error_get_code());
446 
447  error = cpl_parameter_set_alias(par, CPL_PARAMETER_MODE_CLI,
448  alias ? alias : parameter);
449  cpl_ensure_code(!error, error);
450 
451  error = cpl_parameter_disable(par, CPL_PARAMETER_MODE_ENV);
452  cpl_ensure_code(!error, error);
453 
454  error = cpl_parameterlist_append(self, par);
455  cpl_ensure_code(!error, error);
456 
457  return CPL_ERROR_NONE;
458 }
459 
460 
461 /*----------------------------------------------------------------------------*/
475 /*----------------------------------------------------------------------------*/
476 int irplib_plugin_test(cpl_pluginlist * self, size_t nstr, const char *astr[]) {
477 
478  cpl_plugin * plugin;
479  cpl_recipe * recipe;
480  int (*recipe_create) (cpl_plugin *);
481  int (*recipe_exec ) (cpl_plugin *);
482  int (*recipe_deinit) (cpl_plugin *);
483  cpl_error_code error;
484  FILE * stream;
485  cpl_boolean is_debug;
486 
487 
488  is_debug = cpl_msg_get_level() <= CPL_MSG_DEBUG ? CPL_TRUE : CPL_FALSE;
489 
490  /* Modified from CPL unit tests */
491  stream = is_debug ? stdout : fopen("/dev/null", "a");
492 
493  inistate = cpl_errorstate_get();
494 
495  assert( nstr == 0 || astr != NULL );
496 
497  plugin = cpl_pluginlist_get_first(self);
498 
499  if (plugin == NULL) {
500  cpl_msg_warning(cpl_func, "With an empty pluginlist, "
501  "no tests can be made");
502  return 0;
503  }
504 
505  cpl_plugin_dump(plugin, stream);
506 
507  recipe_create = cpl_plugin_get_init(plugin);
508  cpl_test( recipe_create != NULL);
509 
510  recipe_exec = cpl_plugin_get_exec(plugin);
511  cpl_test( recipe_exec != NULL);
512 
513  recipe_deinit = cpl_plugin_get_deinit(plugin);
514  cpl_test( recipe_deinit != NULL);
515 
516  /* Only plugins of type recipe are tested (further) */
517  if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
518  cpl_msg_warning(cpl_func, "This plugin is not of type recipe, "
519  "cannot test further");
520  return 0;
521  }
522 
523  if (recipe_create != NULL && recipe_exec != NULL && recipe_deinit != NULL) {
524 
525  cpl_test_zero(recipe_create(plugin));
526 
527  recipe = (cpl_recipe *) plugin;
528 
529  cpl_test_nonnull( recipe->parameters );
530 
531  recipe_parameterlist_set(recipe->parameters);
532 
533  cpl_parameterlist_dump(recipe->parameters, stream);
534 
535  recipe->frames = cpl_frameset_new();
536 
537  if (irplib_plugin_has_sof_from_env(plugin, "RECIPE_SOF_PATH")) {
538 
539  recipe_sof_test_from_env(plugin);
540 
541  } else {
542 
543  const cpl_msg_severity msg_level = cpl_msg_get_level();
544 
545  /* Unless the CPL_MSG_LEVEL has been explicitly set, turn off
546  terminal messaging completely while inside this function */
547  if (getenv("CPL_MSG_LEVEL") == NULL) cpl_msg_set_level(CPL_MSG_OFF);
548 
549  cpl_msg_info(cpl_func,"Checking handling of pre-existing CPL error "
550  "state - may produce warning(s)/error(s):");
551  cpl_error_set(cpl_func, CPL_ERROR_EOL);
552  /* Call recipe and expect non-zero return code */
553  cpl_test( recipe_exec(plugin) );
554  /* Expect also the CPL error code to be preserved */
555  cpl_test_error( CPL_ERROR_EOL );
556 
557  cpl_msg_info(cpl_func,"Checking handling of empty frameset - "
558  "may produce warning(s)/error(s):");
559  /* Call recipe and expect non-zero return code */
560  cpl_test( recipe_exec(plugin) );
561  error = cpl_error_get_code();
562  /* Expect also the CPL error code to be set */
563  cpl_test_error( error );
564  cpl_test( error );
565 
566  cpl_msg_info(cpl_func,"Checking handling of dummy frameset - "
567  "may produce warning(s)/error(s):");
568  do {
569  cpl_frame * f = cpl_frame_new();
570  error = cpl_frame_set_filename(f, "/dev/null");
571  cpl_test_eq_error(error, CPL_ERROR_NONE);
572  error = cpl_frame_set_tag(f, "RECIPE_DUMMY_TAG");
573  cpl_test_eq_error(error, CPL_ERROR_NONE);
574  error = cpl_frameset_insert(recipe->frames, f);
575  cpl_test_eq_error(error, CPL_ERROR_NONE);
576 
577  /* Call recipe and expect non-zero return code */
578  cpl_test( recipe_exec(plugin) );
579  error = cpl_error_get_code();
580  /* Expect also the CPL error code to be set */
581  cpl_test_error( error );
582  cpl_test( error );
583 
584  error = cpl_frameset_erase_frame(recipe->frames, f);
585  cpl_test_eq_error(error, CPL_ERROR_NONE);
586 
587  } while (0);
588 
589 #ifdef IRPLIB_TEST_RANDOM_SOF
590  recipe_sof_test_devfile(plugin, DEV_RANDOM, nstr, astr);
591 #endif
592 
593  recipe_sof_test_devfile(plugin, "/dev/null", nstr, astr);
594 
595  recipe_sof_test_devfile(plugin, ".", nstr, astr);
596 
597  recipe_sof_test_image_empty(plugin, nstr, astr);
598 
599  recipe_sof_test_local(plugin);
600 
601  cpl_msg_set_level(msg_level);
602 
603  }
604 
605  cpl_frameset_delete(recipe->frames);
606 
607  error = recipe_deinit(plugin);
608  cpl_test_eq_error(error, CPL_ERROR_NONE);
609  }
610 
611  if (stream != stdout) fclose(stream);
612 
613  return 0;
614 }
615 
618 /*----------------------------------------------------------------------------*/
628 /*----------------------------------------------------------------------------*/
629 static void recipe_parameterlist_set(cpl_parameterlist * self)
630 {
631 
632  cpl_parameter * p = cpl_parameterlist_get_first(self);
633 
634  for (; p != NULL; p = cpl_parameterlist_get_next(self)) {
635 
636  const char * envvar;
637  const char * svalue;
638 
639  /* FIXME: Needed ? */
640  if (cpl_parameter_get_default_flag(p)) continue;
641 
642  cpl_msg_debug(cpl_func, __FILE__ " line %u: OK", __LINE__);
643 
644  envvar = cpl_parameter_get_alias(p, CPL_PARAMETER_MODE_ENV);
645  svalue = envvar ? getenv(envvar) : NULL;
646 
647  switch (cpl_parameter_get_type(p)) {
648  case CPL_TYPE_BOOL: {
649  const int value
650  = svalue ? atoi(svalue) : cpl_parameter_get_default_bool(p);
651  cpl_parameter_set_bool(p, value);
652  break;
653  }
654  case CPL_TYPE_INT: {
655  const int value
656  = svalue ? atoi(svalue) : cpl_parameter_get_default_int(p);
657  cpl_parameter_set_int(p, value);
658  break;
659  }
660  case CPL_TYPE_DOUBLE: {
661  const double value
662  = svalue ? atof(svalue) : cpl_parameter_get_default_double(p);
663  cpl_parameter_set_double(p, value);
664  break;
665  }
666  case CPL_TYPE_STRING:
667  {
668  const char * s_default = cpl_parameter_get_default_string(p);
669  /* Replace NULL with "" */
670  const char * value
671  = svalue ? svalue : (s_default ? s_default : "");
672  cpl_parameter_set_string(p, value);
673  break;
674  }
675 
676  default:
677  assert( 0 ); /* It is a testing error to reach this point */
678  }
679  }
680 }
681 
682 
683 /*----------------------------------------------------------------------------*/
693 /*----------------------------------------------------------------------------*/
694 static void recipe_sof_test_devfile(cpl_plugin * plugin, const char * filename,
695  size_t nstr, const char *astr[])
696 {
697  cpl_recipe * recipe = (cpl_recipe*)plugin;
698  int (*recipe_exec) (cpl_plugin *);
699  cpl_frameset * copy;
700  cpl_error_code error;
701  size_t i;
702 
703 
704  if (nstr < 1) return;
705  if (filename == NULL) return;
706 
707  cpl_msg_info(cpl_func, "Testing recipe with %u %s as input ",
708  (unsigned)nstr, filename);
709 
710  for (i = 0; i < nstr; i++) {
711  cpl_frame * f = cpl_frame_new();
712 
713  error = cpl_frame_set_filename(f, filename);
714  cpl_test_eq_error(error, CPL_ERROR_NONE);
715 
716  error = cpl_frame_set_tag(f, astr[i]);
717  cpl_test_eq_error(error, CPL_ERROR_NONE);
718 
719  error = cpl_frameset_insert(recipe->frames, f);
720  cpl_test_eq_error(error, CPL_ERROR_NONE);
721  }
722 
723  copy = cpl_frameset_duplicate(recipe->frames);
724 
725  recipe_exec = cpl_plugin_get_exec(plugin);
726  cpl_test( recipe_exec != NULL);
727 
728  if (recipe_exec != NULL) {
729 
730  /* Call recipe and expect non-zero return code */
731  cpl_test( recipe_exec(plugin) );
732  error = cpl_error_get_code();
733  /* Expect also the CPL error code to be set */
734  cpl_test_error( error );
735  cpl_test( error );
736 
737  recipe_frameset_test_frameset_diff(recipe->frames, copy);
738 
739  recipe_frameset_empty(recipe->frames);
740  }
741 
742  cpl_frameset_delete(copy);
743 
744  return;
745 }
746 
747 /*----------------------------------------------------------------------------*/
754 /*----------------------------------------------------------------------------*/
755 static void recipe_sof_test_image_empty(cpl_plugin * plugin, size_t nstr,
756  const char *astr[])
757 {
758  cpl_recipe * recipe = (cpl_recipe*)plugin;
759  int (*recipe_exec) (cpl_plugin *);
760  cpl_frameset * copy;
761  cpl_error_code error;
762  size_t i;
763  const cpl_frame * frame;
764  cpl_image * iempty;
765  int retstat;
766 
767 
768  if (nstr < 1) return;
769 
770  cpl_msg_info(cpl_func, "Testing recipe with %u empty images as input ",
771  (unsigned)nstr);
772 
773  iempty = cpl_image_new(13, 17, CPL_TYPE_FLOAT);
774  cpl_test_nonnull(iempty);
775 
776  for (i = 0; i < nstr; i++) {
777  cpl_frame * f = cpl_frame_new();
778  char * rawname = cpl_sprintf("%s-raw%05u.fits",
779  cpl_plugin_get_name(plugin),
780  (unsigned)(i+1));
781 
782  error = cpl_image_save(iempty, rawname,CPL_BPP_IEEE_FLOAT, NULL,
783  CPL_IO_DEFAULT);
784  cpl_test_eq_error(error, CPL_ERROR_NONE);
785 
786  error = cpl_frame_set_filename(f, rawname);
787  cpl_test_eq_error(error, CPL_ERROR_NONE);
788 
789  error = cpl_frame_set_tag(f, astr[i]);
790  cpl_test_eq_error(error, CPL_ERROR_NONE);
791 
792  error = cpl_frameset_insert(recipe->frames, f);
793  cpl_test_eq_error(error, CPL_ERROR_NONE);
794 
795  cpl_free(rawname);
796  }
797  cpl_image_delete(iempty);
798 
799  copy = cpl_frameset_duplicate(recipe->frames);
800 
801  recipe_exec = cpl_plugin_get_exec(plugin);
802  cpl_test(recipe_exec != NULL);
803 
804  if (recipe_exec != NULL) {
805  cpl_frameset_iterator * iterator = NULL;
806 
807  /* Call recipe and expect consistency between return code and
808  CPL error */
809 
810  retstat = recipe_exec(plugin);
811  error = cpl_error_get_code();
812  /* Expect also the CPL error code to be set */
813  if (error == 0) {
814  cpl_test_zero(retstat);
815  } else {
816  cpl_test(retstat);
817  }
818  cpl_test_error( error );
819 
820  recipe_frameset_test_frameset_diff(recipe->frames, copy);
821 
822  for (frame = irplib_frameset_get_first_const(&iterator, recipe->frames);
823  frame != NULL;
824  frame = irplib_frameset_get_next_const(iterator))
825  {
826  cpl_test_zero( remove(cpl_frame_get_filename(frame)) );
827  }
828  cpl_frameset_iterator_delete(iterator);
829 
830  recipe_frameset_empty(recipe->frames);
831  }
832 
833  cpl_frameset_delete(copy);
834 
835  return;
836 }
837 
838 
839 /*----------------------------------------------------------------------------*/
847 /*----------------------------------------------------------------------------*/
848 cpl_boolean irplib_plugin_has_sof_from_env(const cpl_plugin * plugin,
849  const char * envname)
850 {
851  const char * recipename = cpl_plugin_get_name(plugin);
852  const char * sof_path = envname ? getenv(envname) : NULL;
853  cpl_frameset * frames;
854  char * sof_name;
855  const cpl_frame * ffirst;
856 
857  cpl_ensure(plugin != NULL, CPL_ERROR_NULL_INPUT, CPL_FALSE);
858  cpl_ensure(envname != NULL, CPL_ERROR_NULL_INPUT, CPL_FALSE);
859  cpl_ensure(recipename != NULL, CPL_ERROR_DATA_NOT_FOUND, CPL_FALSE);
860  cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), CPL_FALSE);
861 
862  if (sof_path == NULL) return CPL_FALSE;
863 
864  sof_name = cpl_sprintf("%s/%s.sof", sof_path, recipename);
865 
866  frames = cpl_frameset_new();
867  recipe_frameset_load(frames, sof_name);
868 
869  ffirst = cpl_frameset_get_position_const(frames, 0);
870 
871  cpl_free(sof_name);
872  cpl_frameset_delete(frames);
873 
874  cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), CPL_FALSE);
875 
876  return ffirst ? CPL_TRUE : CPL_FALSE;
877 
878 }
879 
880 /*----------------------------------------------------------------------------*/
887 /*----------------------------------------------------------------------------*/
888 static void recipe_sof_test_from_env(cpl_plugin * plugin)
889 {
890  cpl_recipe * recipe = (cpl_recipe*)plugin;
891  const char * recipename = cpl_plugin_get_name(plugin);
892  const char * var_name = "RECIPE_SOF_PATH";
893  const char * sof_path = getenv(var_name);
894  cpl_error_code error;
895 
896  char * sof_name;
897 
898  if (sof_path == NULL) {
899  cpl_msg_warning(cpl_func, "Environment variable %s is unset: "
900  "No SOFs to check", var_name);
901  return;
902  }
903 
904  cpl_msg_debug(cpl_func, "Checking for SOFs in %s", sof_path);
905 
906  cpl_test_nonnull( recipename );
907  if (recipename == NULL) return;
908 
909  sof_name = cpl_sprintf("%s/%s.sof", sof_path, recipename);
910 
911  cpl_msg_debug(cpl_func, "Checking for SOF %s", sof_name);
912 
913  recipe_frameset_load(recipe->frames, sof_name);
914 
915  if (!cpl_frameset_is_empty(recipe->frames)) {
916 
917  int (*recipe_exec ) (cpl_plugin *);
918  cpl_frameset * copy = cpl_frameset_duplicate(recipe->frames);
919 
920  recipe_exec = cpl_plugin_get_exec(plugin);
921  cpl_test(recipe_exec != NULL);
922 
923  if (recipe_exec != NULL) {
924  cpl_msg_info(cpl_func,"Checking handling of SOF: %s", sof_name);
925 
926  /* Call recipe and expect zero return code */
927  cpl_test_zero( recipe_exec(plugin) );
928  /* Expect also the CPL error code to be clear */
929  cpl_test_error(CPL_ERROR_NONE);
930 
931  error = cpl_dfs_update_product_header(recipe->frames);
932  cpl_test_eq_error(error, CPL_ERROR_NONE);
933 
934  recipe_frameset_test_frameset_diff(recipe->frames, copy);
935 
936  recipe_frameset_empty(recipe->frames);
937  }
938 
939  cpl_frameset_delete(copy);
940 
941  }
942 
943  cpl_free(sof_name);
944 
945  return;
946 }
947 
948 
949 
950 /*----------------------------------------------------------------------------*/
957 /*----------------------------------------------------------------------------*/
958 static void recipe_sof_test_local(cpl_plugin * plugin)
959 {
960  cpl_recipe * recipe = (cpl_recipe*)plugin;
961  const char * recipename = cpl_plugin_get_name(plugin);
962  cpl_error_code error;
963  char * sof_name = cpl_sprintf("%s.sof", recipename);
964 
965  cpl_msg_debug(cpl_func, "Checking for SOF %s", sof_name);
966 
967  recipe_frameset_load(recipe->frames, sof_name);
968 
969  if (!cpl_frameset_is_empty(recipe->frames)) {
970 
971  int (*recipe_exec ) (cpl_plugin *);
972  cpl_frameset * copy = cpl_frameset_duplicate(recipe->frames);
973 
974  recipe_exec = cpl_plugin_get_exec(plugin);
975  cpl_test(recipe_exec != NULL);
976 
977  if (recipe_exec != NULL) {
978 
979  cpl_msg_info(cpl_func,"Checking handling of SOF: %s", sof_name);
980 
981  /* Call recipe and expect zero return code */
982  cpl_test_zero( recipe_exec(plugin) );
983  /* Expect also the CPL error code to be clear */
984  cpl_test_error(CPL_ERROR_NONE);
985 
986  error = cpl_dfs_update_product_header(recipe->frames);
987  cpl_test_eq_error( error, CPL_ERROR_NONE );
988 
989  recipe_frameset_test_frameset_diff(recipe->frames, copy);
990 
991  recipe_frameset_empty(recipe->frames);
992  }
993 
994  cpl_frameset_delete(copy);
995  }
996 
997  cpl_free(sof_name);
998 
999  return;
1000 }
1001 
1002 
1003 
1004 
1005 /**********************************************************************/
1019 /**********************************************************************/
1020 
1021 static void recipe_frameset_load(cpl_frameset * set, const char *name)
1022 {
1023 
1024  FILE *fp;
1025  char line[LINE_LEN_MAX];
1026  char path[LINE_LEN_MAX], group[LINE_LEN_MAX], tag[LINE_LEN_MAX];
1027  int line_number;
1028 
1029  assert( set != NULL );
1030  assert( name != NULL );
1031 
1032  fp = fopen(name, "r");
1033  if (fp == NULL) {
1034  cpl_msg_debug(cpl_func, "Unable to open SOF file '%s'", name);
1035  return;
1036  }
1037 
1038  /* Loop over all the lines in the set-of-frames file */
1039  for (line_number = 0; fgets(line, LINE_LEN_MAX - 1, fp); line_number++) {
1040 
1041  char scan_fmt[50];
1042  cpl_frame_group grp;
1043  cpl_frame * frame;
1044  int n;
1045 
1046  if (line[0] == '#') continue;
1047 
1048  snprintf(scan_fmt, 49, "%%%ds %%%ds %%%ds", LINE_LEN_MAX - 1,
1049  LINE_LEN_MAX - 1, LINE_LEN_MAX - 1);
1050  n = sscanf(line, scan_fmt, path, tag, group);
1051 
1052  if (n < 1) {
1053  cpl_msg_warning(cpl_func, "Spurious line no. %d in %s: %s",
1054  line_number, name, line);
1055  break;
1056  }
1057 
1058  /* Allocate a new frame */
1059  frame = cpl_frame_new();
1060 
1061  /* Set the filename component of the frame */
1062  cpl_frame_set_filename(frame, path);
1063 
1064  /* Set the tag component of the frame (or set a default) */
1065  cpl_frame_set_tag(frame, n == 1 ? "" : tag);
1066 
1067  cpl_frameset_insert(set, frame);
1068 
1069  /* Set the group component of the frame (or set a default) */
1070  if (n < 3) continue;
1071 
1072  if (!strcmp(group, CPL_FRAME_GROUP_RAW_ID))
1073  grp = CPL_FRAME_GROUP_RAW;
1074  else if (!strcmp(group, CPL_FRAME_GROUP_CALIB_ID))
1075  grp = CPL_FRAME_GROUP_CALIB;
1076  else if (!strcmp(group, CPL_FRAME_GROUP_PRODUCT_ID))
1077  grp = CPL_FRAME_GROUP_PRODUCT;
1078  else
1079  grp = CPL_FRAME_GROUP_NONE;
1080 
1081  cpl_frame_set_group(frame, grp);
1082  }
1083 
1084  fclose(fp);
1085 
1086  return;
1087 
1088 }
1089 
1090 
1091 /*----------------------------------------------------------------------------*/
1101 /*----------------------------------------------------------------------------*/
1102 static
1103 const cpl_parameter * irplib_parameterlist_get(const cpl_parameterlist * self,
1104  const char * instrume,
1105  const char * recipe,
1106  const char * parameter)
1107 {
1108 
1109  char * paramname;
1110  const cpl_parameter * par;
1111 
1112 
1113  cpl_ensure(instrume != NULL, CPL_ERROR_NULL_INPUT, NULL);
1114  cpl_ensure(recipe != NULL, CPL_ERROR_NULL_INPUT, NULL);
1115  cpl_ensure(parameter != NULL, CPL_ERROR_NULL_INPUT, NULL);
1116 
1117  paramname = cpl_sprintf("%s.%s.%s", instrume, recipe, parameter);
1118 
1119  par = cpl_parameterlist_find_const(self, paramname);
1120 
1121  if (par == NULL) (void)cpl_error_set_message(cpl_func,
1122  cpl_error_get_code()
1123  ? cpl_error_get_code()
1124  : CPL_ERROR_DATA_NOT_FOUND,
1125  "%s", paramname);
1126 
1127  cpl_free(paramname);
1128 
1129  return par;
1130 
1131 }
1132 
1133 
1134 /*----------------------------------------------------------------------------*/
1160 /*----------------------------------------------------------------------------*/
1161 static void recipe_frameset_empty(cpl_frameset * self)
1162 {
1163  cpl_size i, n;
1164 
1165  if (self == NULL) {
1166  cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
1167  return;
1168  }
1169 
1170  n = cpl_frameset_get_size(self);
1171  for (i = 0; i < n; ++i)
1172  {
1173  cpl_frame * f = cpl_frameset_get_position(self, n-1-i);
1174  cpl_frameset_erase_frame(self, f);
1175  }
1176 }
1177 
1178 
1179 /*----------------------------------------------------------------------------*/
1199 /*----------------------------------------------------------------------------*/
1200 static void recipe_frameset_test_frame(const cpl_frame * self)
1201 {
1202 
1203  cpl_msg_info(cpl_func, "Validating new frame: %s",
1204  cpl_frame_get_filename(self));
1205 
1206  cpl_test_nonnull(self);
1207 
1208  /* Frame must be tagged */
1209  cpl_test_nonnull(cpl_frame_get_tag(self));
1210 
1211  /* New frames must be products */
1212  cpl_test_eq(cpl_frame_get_group(self), CPL_FRAME_GROUP_PRODUCT);
1213 
1214  if (cpl_frame_get_type(self) != CPL_FRAME_TYPE_PAF) {
1215  /* All but PAF (?) must be FITS */
1216  cpl_test_fits(cpl_frame_get_filename(self));
1217  } else {
1218  /* Frame must at least have a filename */
1219  cpl_test_nonnull(cpl_frame_get_filename(self));
1220  }
1221 }
1222 
1223 /*----------------------------------------------------------------------------*/
1244 /*----------------------------------------------------------------------------*/
1245 static void recipe_frameset_test_frameset_diff(const cpl_frameset * self,
1246  const cpl_frameset * other)
1247 {
1248 
1249  cpl_frameset_iterator * it1 = NULL;
1250  cpl_frameset_iterator * it2 = NULL;
1251  const cpl_frame * frame = irplib_frameset_get_first_const(&it2, other);
1252 
1253  /* First verify that filenames in other are non-NULL */
1254  for (;frame != NULL; frame = irplib_frameset_get_next_const(it2)) {
1255  const char * file = cpl_frame_get_filename(frame);
1256 
1257  if (file == NULL) {
1258  cpl_test_nonnull(cpl_frame_get_filename(frame));
1259  break;
1260  }
1261  }
1262  cpl_frameset_iterator_delete(it2);
1263  it2 = NULL;
1264  if (frame != NULL) return;
1265 
1266  frame = irplib_frameset_get_first_const(&it1, self);
1267 
1268  for (;frame != NULL; frame = irplib_frameset_get_next_const(it1)) {
1269  const cpl_frame * cmp;
1270  const char * file = cpl_frame_get_filename(frame);
1271 
1272  if (file == NULL) {
1273  cpl_test_nonnull(cpl_frame_get_filename(frame));
1274  continue;
1275  }
1276 
1277  cmp = irplib_frameset_get_first_const(&it2, other);
1278  for (;cmp != NULL; cmp = irplib_frameset_get_next_const(it2)) {
1279  const char * cfile = cpl_frame_get_filename(cmp);
1280 
1281  if (!strcmp(file, cfile)) break;
1282 
1283  }
1284  cpl_frameset_iterator_delete(it2);
1285  it2 = NULL;
1286  if (cmp == NULL) {
1287  /* frame is new */
1288 
1289  cpl_test_eq(cpl_frame_get_group(frame), CPL_FRAME_GROUP_PRODUCT);
1290  recipe_frameset_test_frame(frame);
1291  }
1292  }
1293 
1294  cpl_frameset_iterator_delete(it1);
1295 }