VIRCAM Pipeline  2.3.10
vircam_platesol.c
1 /* $Id: vircam_platesol.c,v 1.15 2012-01-16 14:44:02 jim Exp $
2  *
3  * This file is part of the VIRCAM Pipeline
4  * Copyright (C) 2015 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 /*
22  * $Author: jim $
23  * $Date: 2012-01-16 14:44:02 $
24  * $Revision: 1.15 $
25  * $Name: not supported by cvs2svn $
26  */
27 
28 /* Includes */
29 
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33 
34 #include <stdio.h>
35 #include <cpl.h>
36 
37 #include <casu_utils.h>
38 #include <casu_mods.h>
39 
40 #include "vircam_utils.h"
41 #include "vircam_dfs.h"
42 #include "vircam_mods.h"
43 #include "vircam_paf.h"
44 
45 /* Function prototypes */
46 
47 static int vircam_platesol_create(cpl_plugin *) ;
48 static int vircam_platesol_exec(cpl_plugin *) ;
49 static int vircam_platesol_destroy(cpl_plugin *) ;
50 static int vircam_platesol_test(cpl_parameterlist *, cpl_frameset *) ;
51 static int vircam_platesol_save(void);
52 static void vircam_platesol_init(void);
53 static void vircam_platesol_tidy(void);
54 
55 static struct {
56 
57  /* Input */
58 
59  int nconst;
60  int shiftan;
61  int extenum;
62 
63 } vircam_platesol_config;
64 
65 static struct {
66  cpl_size *labels;
67  cpl_frame *img;
68  cpl_frame *mstds;
69  casu_fits *imgf;
70  casu_tfits *mstdsf;
71  FILE *paf;
72 } ps;
73 
74 
75 static char vircam_platesol_description[] =
76 "vircam_platesol -- VIRCAM plate solution fitting test recipe.\n\n"
77 "Fit a plate solution to a matched standards table and write the resulting\n"
78 "WCS to an input image header.\n\n"
79 "The program accepts the following files in the SOF:\n\n"
80 " Tag Description\n"
81 " -----------------------------------------------------------------------\n"
82 " %-21s A input uncorrected image\n"
83 " %-21s A matched standards table\n"
84 "The WCS values from the solution are written to a paf file. This is the\n"
85 "only product of this recipe\n"
86 "\n";
87 
139 /* Function code */
140 
141 
142 /*---------------------------------------------------------------------------*/
150 /*---------------------------------------------------------------------------*/
151 
152 int cpl_plugin_get_info(cpl_pluginlist *list) {
153  cpl_recipe *recipe = cpl_calloc(1,sizeof(*recipe));
154  cpl_plugin *plugin = &recipe->interface;
155  char alldesc[SZ_ALLDESC];
156  (void)snprintf(alldesc,SZ_ALLDESC,vircam_platesol_description,
157  VIRCAM_TEST_SCIENCE_RAW,VIRCAM_CAL_MSTDTAB);
158 
159  cpl_plugin_init(plugin,
160  CPL_PLUGIN_API,
161  VIRCAM_BINARY_VERSION,
162  CPL_PLUGIN_TYPE_RECIPE,
163  "vircam_platesol",
164  "VIRCAM plate solution test recipe [test]",
165  alldesc,
166  "Jim Lewis",
167  "jrl@ast.cam.ac.uk",
169  vircam_platesol_create,
170  vircam_platesol_exec,
171  vircam_platesol_destroy);
172 
173  cpl_pluginlist_append(list,plugin);
174 
175  return(0);
176 }
177 
178 /*---------------------------------------------------------------------------*/
187 /*---------------------------------------------------------------------------*/
188 
189 static int vircam_platesol_create(cpl_plugin *plugin) {
190  cpl_recipe *recipe;
191  cpl_parameter *p;
192 
193  /* Get the recipe out of the plugin */
194 
195  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
196  recipe = (cpl_recipe *)plugin;
197  else
198  return(-1);
199 
200  /* Create the parameters list in the cpl_recipe object */
201 
202  recipe->parameters = cpl_parameterlist_new();
203 
204  /* Fill in the parameters. First the number of constants */
205 
206  p = cpl_parameter_new_enum("vircam.vircam_platesol.nconst",
207  CPL_TYPE_INT,
208  "Number of plate constants",
209  "vircam.vircam_platesol",6,2,4,6);
210  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"nconst");
211  cpl_parameterlist_append(recipe->parameters,p);
212 
213  /* Now whether or not to shift the tangent point */
214 
215  p = cpl_parameter_new_value("vircam.vircam_platesol.shiftan",
216  CPL_TYPE_BOOL,
217  "Shift position of tangent point",
218  "vircam.vircam_platesol",FALSE);
219  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"shiftan");
220  cpl_parameterlist_append(recipe->parameters,p);
221 
222  /* Extension number of input frames to use */
223 
224  p = cpl_parameter_new_range("vircam.vircam_platesol.extenum",
225  CPL_TYPE_INT,
226  "Extension number to be done, 0 == all",
227  "vircam.vircam_platesol",1,0,16);
228  cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ext");
229  cpl_parameterlist_append(recipe->parameters,p);
230 
231  /* Get out of here */
232 
233  return(0);
234 }
235 
236 /*---------------------------------------------------------------------------*/
242 /*---------------------------------------------------------------------------*/
243 
244 static int vircam_platesol_exec(cpl_plugin *plugin) {
245  cpl_recipe *recipe;
246 
247  /* Get the recipe out of the plugin */
248 
249  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
250  recipe = (cpl_recipe *)plugin;
251  else
252  return(-1);
253 
254  return(vircam_platesol_test(recipe->parameters,recipe->frames));
255 }
256 
257 /*---------------------------------------------------------------------------*/
263 /*---------------------------------------------------------------------------*/
264 
265 static int vircam_platesol_destroy(cpl_plugin *plugin) {
266  cpl_recipe *recipe ;
267 
268  /* Get the recipe out of the plugin */
269 
270  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
271  recipe = (cpl_recipe *)plugin;
272  else
273  return(-1);
274 
275  cpl_parameterlist_delete(recipe->parameters);
276  return(0);
277 }
278 
279 /*---------------------------------------------------------------------------*/
286 /*---------------------------------------------------------------------------*/
287 
288 static int vircam_platesol_test(cpl_parameterlist *parlist,
289  cpl_frameset *framelist) {
290  const char *fctid="vircam_platesol";
291  cpl_parameter *p;
292  int jst,jfn,status,j;
293  cpl_size nlab;
294 
295  /* Check validity of input frameset */
296 
297  if (framelist == NULL || cpl_frameset_get_size(framelist) <= 0) {
298  cpl_msg_error(fctid,"Input framelist NULL or has no input data");
299  return(-1);
300  }
301 
302  /* Initialise some things */
303 
304  vircam_platesol_init();
305 
306  /* Get the parameters */
307 
308  p = cpl_parameterlist_find(parlist,"vircam.vircam_platesol.nconst");
309  vircam_platesol_config.nconst = cpl_parameter_get_int(p);
310  p = cpl_parameterlist_find(parlist,"vircam.vircam_platesol.shiftan");
311  vircam_platesol_config.shiftan = cpl_parameter_get_bool(p);
312  p = cpl_parameterlist_find(parlist,"vircam.vircam_platesol.extenum");
313  vircam_platesol_config.extenum = cpl_parameter_get_int(p);
314 
315  /* Sort out raw from calib frames */
316 
317  if (vircam_dfs_set_groups(framelist) != CASU_OK) {
318  cpl_msg_error(fctid,"Cannot identify RAW and CALIB frames");
319  vircam_platesol_tidy();
320  return(-1);
321  }
322 
323  /* Get the frames */
324 
325  if ((ps.labels = cpl_frameset_labelise(framelist,casu_compare_tags,
326  &nlab)) == NULL) {
327  cpl_msg_error(fctid,"Cannot labelise the input frames");
328  vircam_platesol_tidy();
329  return(-1);
330  }
331  if ((ps.mstds = casu_frameset_subgroup_1(framelist,ps.labels,nlab,
332  VIRCAM_CAL_MSTDTAB)) == NULL) {
333  cpl_msg_info(fctid,"No matched standards table found -- cannot continue");
334  vircam_platesol_tidy();
335  return(-1);
336  }
337  if ((ps.img = casu_frameset_subgroup_1(framelist,ps.labels,nlab,
338  VIRCAM_TEST_SCIENCE_RAW)) == NULL) {
339  cpl_msg_info(fctid,"No raw image found -- cannot continue");
340  vircam_platesol_tidy();
341  return(-1);
342  }
343 
344  /* Now, how many image extensions do we want to do? If the extension
345  number is zero, then we loop for all possible extensions. If it
346  isn't then we just do the extension specified */
347 
348  vircam_exten_range(vircam_platesol_config.extenum,(const cpl_frame *)ps.img,
349  &jst,&jfn);
350  if (jst == -1 || jfn == -1) {
351  cpl_msg_error(fctid,"Unable to continue");
352  vircam_platesol_tidy();
353  return(-1);
354  }
355 
356  /* Now loop for all the extension... */
357 
358  status = CASU_OK;
359  for (j = jst; j <= jfn; j++) {
360 
361  /* Load up the images */
362 
363  ps.imgf = casu_fits_load(ps.img,CPL_TYPE_FLOAT,j);
364  ps.mstdsf = casu_tfits_load(ps.mstds,j);
365  if (ps.img == NULL || ps.mstdsf == NULL) {
366  freefits(ps.imgf);
367  freetfits(ps.mstdsf);
368  cpl_msg_warning(fctid,"Unable to load one of the inputs");
369  continue;
370  }
371 
372  /* Now do the correction */
373 
374  cpl_msg_info(fctid,"Doing the plate solution for extension %" CPL_SIZE_FORMAT,
375  (cpl_size)j);
376  (void)casu_platesol(casu_fits_get_ehu(ps.imgf),NULL,
377  casu_tfits_get_table(ps.mstdsf),
378  vircam_platesol_config.nconst,
379  vircam_platesol_config.shiftan,&status);
380  if (status != CASU_OK) {
381  cpl_msg_warning(fctid,"Plate solution failed");
382  status = CASU_OK;
383  }
384 
385  /* Now save the result */
386 
387  cpl_msg_info(fctid,"Saving results for extension %" CPL_SIZE_FORMAT,
388  (cpl_size)j);
389  if (vircam_platesol_save() != 0)
390  cpl_msg_warning(fctid,"Save routine failed");
391 
392  /* Tidy a few things before the next image */
393 
394  freefits(ps.imgf);
395  freetfits(ps.mstdsf);
396  }
397  vircam_platesol_tidy();
398  return(0);
399 }
400 
401 /*---------------------------------------------------------------------------*/
406 /*---------------------------------------------------------------------------*/
407 
408 static int vircam_platesol_save(void) {
409  const char *fctid = "vircam_platesol_save";
410  const char *outfile = "platesol";
411  const char *keys[] = {"CRVAL1","CRVAL2","CRPIX1","CRPIX2","CD1_1","CD1_2",
412  "CD2_1","CD2_2","PV2_3","ESO DRS NUMBRMS",
413  "ESO DRS STDCRMS","ESO DRS WCSRAOFF",
414  "ESO DRS WCSDECOFF"};
415  int i,nkeys=13;
416  cpl_propertylist *plist,*p2,*p3;
417 
418  /* Get the propertylist we need */
419 
420  plist = casu_fits_get_ehu(ps.imgf);
421 
422  /* Extract the standard things */
423 
424  if ((p2 = vircam_paf_req_items(plist)) == NULL) {
425  cpl_msg_error(fctid,"Unable to find required items in header");
426  return(-1);
427  }
428  p3 = vircam_paf_phu_items(casu_fits_get_phu(ps.imgf));
430  freepropertylist(p3);
431 
432  /* Add in the extra stuff */
433 
434  for (i = 0; i < nkeys; i++) {
435  cpl_propertylist_copy_property(p2,plist,keys[i]);
436  if (cpl_error_get_code() != CPL_ERROR_NONE) {
437  cpl_msg_error(fctid,"A header parameter %s is missing",keys[i]);
438  cpl_propertylist_delete(p2);
439  return(-1);
440  }
441  }
442 
443  /* Now write it all out */
444 
445  if (vircam_paf_print((char *)outfile,"VIRCAM/vircam_platesol",
446  "Test QC file",p2) != CASU_OK) {
447  cpl_msg_error(fctid,"Error writing PAF");
448  cpl_propertylist_delete(p2);
449  return(-1);
450  }
451 
452  /* Tidy and exit */
453 
454  cpl_propertylist_delete(p2);
455  return(0);
456 }
457 
458 
459 /*---------------------------------------------------------------------------*/
463 /*---------------------------------------------------------------------------*/
464 
465 static void vircam_platesol_init(void) {
466  ps.labels = NULL;
467  ps.img = NULL;
468  ps.imgf = NULL;
469  ps.mstds = NULL;
470  ps.mstdsf = NULL;
471 }
472 
473 
474 /*---------------------------------------------------------------------------*/
478 /*---------------------------------------------------------------------------*/
479 
480 static void vircam_platesol_tidy(void) {
481  freespace(ps.labels);
482  freefits(ps.imgf);
483  freetfits(ps.mstdsf);
484  freeframe(ps.mstds);
485  freeframe(ps.img);
486 }
487 
488 
489 /*
490 
491 $Log: not supported by cvs2svn $
492 Revision 1.14 2012/01/15 17:40:09 jim
493 Minor modifications to take into accout the changes in cpl API for v6
494 
495 Revision 1.13 2007/10/25 19:38:22 jim
496 modified to keep lint happy
497 
498 Revision 1.12 2007/10/15 12:53:55 jim
499 Modified for compatibility with cpl_4.0
500 
501 Revision 1.11 2007/07/09 13:22:09 jim
502 Modified to use new version of vircam_exten_range
503 
504 Revision 1.10 2007/05/02 09:17:04 jim
505 uses new vircam_platesol api
506 
507 Revision 1.9 2007/04/23 12:49:43 jim
508 Modified error condition behaviour
509 
510 Revision 1.8 2007/04/13 12:27:39 jim
511 Added some extra docs
512 
513 Revision 1.7 2007/04/04 10:36:29 jim
514 Modified to use new dfs tags
515 
516 Revision 1.6 2007/03/01 12:42:59 jim
517 Modified slightly after code checking
518 
519 Revision 1.5 2007/02/15 12:17:45 jim
520 Modified to use new version of PAF files
521 
522 Revision 1.4 2006/06/15 09:59:00 jim
523 Minor changes to docs
524 
525 Revision 1.3 2006/05/04 11:53:45 jim
526 Fixed _save routine so that it's more consistent with the standard CPL
527 way of doing things
528 
529 Revision 1.2 2006/04/27 14:22:06 jim
530 Fixed docs
531 
532 Revision 1.1 2006/04/24 10:42:45 jim
533 New routine
534 
535 
536 */
537 
538 
539 
540 
casu_fits * casu_fits_load(cpl_frame *frame, cpl_type type, int nexten)
Definition: casu_fits.c:80
cpl_propertylist * casu_fits_get_phu(casu_fits *p)
Definition: casu_fits.c:531
cpl_propertylist * casu_fits_get_ehu(casu_fits *p)
Definition: casu_fits.c:576
int casu_platesol(cpl_propertylist *plist, cpl_propertylist *tlist, cpl_table *matchedstds, int nconst, int shiftan, int *status)
Work out a WCS for an image.
cpl_table * casu_tfits_get_table(casu_tfits *p)
Definition: casu_tfits.c:364
casu_tfits * casu_tfits_load(cpl_frame *table, int nexten)
Definition: casu_tfits.c:78
cpl_frame * casu_frameset_subgroup_1(cpl_frameset *frameset, cpl_size *labels, cpl_size nlab, const char *tag)
Extract a frame of a given label from a frameset.
Definition: casu_utils.c:206
int casu_compare_tags(const cpl_frame *frame1, const cpl_frame *frame2)
Compare input tags.
Definition: casu_utils.c:96
void casu_merge_propertylists(cpl_propertylist *p1, cpl_propertylist *p2)
Merge two propertylists.
Definition: casu_utils.c:399
int vircam_dfs_set_groups(cpl_frameset *set)
Definition: vircam_dfs.c:115
const char * vircam_get_license(void)
Definition: vircam_utils.c:116
void vircam_exten_range(int inexten, const cpl_frame *fr, int *out1, int *out2)
Definition: vircam_utils.c:165