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