OMEGA Pipeline Reference Manual  1.0.5
omega_photometry.c
1 /* $Id: omega_photometry.c,v 1.21 2012-04-30 10:46:01 agabasch Exp $
2  *
3  * This file is part of the OMEGA Pipeline
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 /*
22  * $Author: agabasch $
23  * $Date: 2012-04-30 10:46:01 $
24  * $Revision: 1.21 $
25  * $Name: not supported by cvs2svn $
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 
32 /*-----------------------------------------------------------------------------
33  Includes
34  -----------------------------------------------------------------------------*/
35 
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <math.h>
39 #include <string.h>
40 
41 #include "omega_photometry.h"
42 #include "omega_catalog.h"
43 #include "omega_dfs.h"
44 #include "omega_pfits.h"
45 #include "omega_utils.h"
46 
65 /*----------------------------------------------------------------------------*/
77 /*----------------------------------------------------------------------------*/
78 
79 cpl_table *omega_photom_tbl(const char *star, double gain,
80  const cpl_frame *refcat, cpl_parameterlist *pars, cpl_table **extra)
81 {
82  int i, nsources, nstds;
83  int npars = 0;
84  int nmatches = 0;
85  double exptime = 0.0;
86  double omega_x1,omega_x2,omega_y1,omega_y2;
87  double radius = 0.0;
88  double photom_thre = 0.0;
89  double airmass = 0.0;
90  double pixscalex = 0.0;
91  double pixscaley = 0.0;
92  double seeing = 0.0;
93  double ellipticity = 0.0;
94  double aperture = 0.0;
95  char *cmd = NULL;
96  const char *text = NULL;
97  const char *path;
98  const char *sex_conf = NULL;
99  const char *sex_conv = NULL;
100  const char *sex_par = NULL;
101  const char *sex_nnw = NULL;
102  const char *srcs = "omega_cat_photom_sources.fits";
103  cpl_parameter *par;
104  cpl_array *status;
105  cpl_table *sources, *stds, *data;
106  cpl_table *matchstds = NULL;
107  cpl_matrix *from, *to;
108  cpl_wcs *wcs;
109  cpl_vector *coords;
110  cpl_propertylist *xlist = NULL;
111  cpl_propertylist *p, *filterlist;
112 
113  /* Get parameters */
114  npars = cpl_parameterlist_get_size(pars);
115  par = cpl_parameterlist_get_first(pars);
116 
117  for(i=0; i<npars; i++) {
118  text = cpl_parameter_get_alias(par, CPL_PARAMETER_MODE_CLI);
119  if(strcmp("bin-path", text) == 0) {
120  path = cpl_parameter_get_string(par) ;
121  }
122  else if(strcmp("sex-config", text) == 0) {
123  sex_conf = cpl_parameter_get_string(par) ;
124  }
125  else if(strcmp("sex-conv", text) == 0) {
126  sex_conv = cpl_parameter_get_string(par) ;
127  }
128  else if(strcmp("sex-param", text) == 0) {
129  sex_par = cpl_parameter_get_string(par) ;
130  }
131  else if(strcmp("sex-nnw", text) == 0) {
132  sex_nnw = cpl_parameter_get_string(par) ;
133  }
134  else if(strcmp("photom-thre", text) == 0){
135  photom_thre = cpl_parameter_get_double(par);
136  }
137  else if(strcmp("radius", text) == 0){
138  radius = cpl_parameter_get_double(par);
139  }
140  else if(strcmp("aperture", text) == 0){
141  aperture = cpl_parameter_get_double(par);
142  }
143  par = cpl_parameterlist_get_next(pars);
144 
145  }
146 
147 // for(i = 0; i < 4; i++) {
148  /* Create catalog of sources from image */
149  cmd = cpl_sprintf("%s/sex %s -c %s -PARAMETERS_NAME %s -FILTER_NAME %s -STARNNW_NAME %s "
150  "-FILTER Y -DETECT_THRESH %g -ANALYSIS_THRESH %g -GAIN %g -PHOT_APERTURES %g -CATALOG_TYPE FITS_1.0 "
151  "-CATALOG_NAME %s -CHECKIMAGE_NAME %s -CHECKIMAGE_TYPE OBJECTS -DETECT_MINAREA 5",
152  path, star, sex_conf, sex_par, sex_conv, sex_nnw, photom_thre, 3.0, gain, aperture,
153  srcs, "omega_ima_detected_photom_srcs.fits");
154 
155  cpl_msg_debug(cpl_func,"cmd: %s",cmd);
156  if (system(cmd) != 0) {
157  cpl_msg_error(cpl_func,"Failed to detect sources in image using Sextractor");
158  freespace(cmd);
159  return NULL;
160  }
161  freespace(cmd);
162 
163  data = cpl_table_load(srcs, 1, 0);
164  if (data == NULL) {
165  cpl_msg_error(cpl_func,"Sources table is NULL");
166  return NULL;
167  }
168 
169  /* Filter for FLAGS = 0 */
170  sources = omega_filter_table_int(data, "FLAGS", CPL_EQUAL_TO, 0, &nsources);
171  freetable(data);
172  cpl_msg_debug(cpl_func,"Number of stars in reduced image: %d", nsources);
173 
174  /* Calculate the seeing */
175  xlist = cpl_propertylist_load(star, 1);
176  omega_get_pixelscale(xlist, &pixscalex, &pixscaley);
177 
178  /* Get extra information */
179 
180  p = cpl_propertylist_load(star, 0);
181 
182  /*Add extension name to the propertylist*/
183  if (cpl_propertylist_has(xlist,"EXTNAME") == 1) {
184  cpl_propertylist_update_string(p, "EXTNAME",
185  cpl_propertylist_get_string(xlist, "EXTNAME"));
186 
187  }
188 
189  filterlist = omega_pfits_get_filter_info(p);
190  omega_pfits_get_mean_airmass(p, &airmass);
191  exptime = omega_pfits_get_exptime(p);
192  freeplist(p);
193 
194 
195 
196  if(omega_basic_param(sources, pixscalex, &seeing, &ellipticity) == -1){
197  cpl_msg_warning(cpl_func,"Cannot find stars with CLASS_STAR near 1 to calculate seeing");
198  }
199 
200  /* Append extra info into a new extension table*/
201  *extra = cpl_table_new(1);
202  cpl_table_new_column(*extra, "FILT_ID", CPL_TYPE_STRING);
203  cpl_table_set_string(*extra, "FILT_ID",0, cpl_propertylist_get_string(filterlist, "FILT_ID"));
204  cpl_table_new_column(*extra, "FILT_NAME", CPL_TYPE_STRING);
205  cpl_table_set_string(*extra, "FILT_NAME",0, cpl_propertylist_get_string(filterlist, "FILT_NAME"));
206  cpl_table_new_column(*extra, "REF_MAG_ID", CPL_TYPE_STRING);
207  cpl_table_set_string(*extra, "REF_MAG_ID",0, cpl_propertylist_get_string(filterlist, "REF_MAG_ID"));
208  cpl_table_new_column(*extra, "REF_MAG_ID_ERR", CPL_TYPE_STRING);
209  cpl_table_set_string(*extra, "REF_MAG_ID_ERR",0, cpl_propertylist_get_string(filterlist, "REF_MAG_ID_ERR"));
210  cpl_table_new_column(*extra, "CWL", CPL_TYPE_DOUBLE);
211  cpl_table_set_double(*extra, "CWL",0, cpl_propertylist_get_double(filterlist, "CWL"));
212  cpl_table_new_column(*extra, "EXPTIME", CPL_TYPE_DOUBLE);
213  if(cpl_propertylist_has(xlist, "EXPTIME") == 0)
214  cpl_table_set_double(*extra, "EXPTIME",0, exptime);
215  else
216  cpl_table_set_double(*extra, "EXPTIME",0, cpl_propertylist_get_double(xlist, "EXPTIME"));
217 
218  cpl_table_new_column(*extra, "AIRMASS", CPL_TYPE_DOUBLE);
219  cpl_table_set_double(*extra, "AIRMASS",0, airmass);
220  cpl_table_new_column(*extra, "CHIP_ID", CPL_TYPE_STRING);
221  cpl_table_set_string(*extra, "CHIP_ID", 0, omega_pfits_get_chipid(xlist));
222  cpl_table_new_column(*extra, "SEEING", CPL_TYPE_DOUBLE);
223  cpl_table_set_double(*extra, "SEEING", 0, seeing);
224  cpl_table_new_column(*extra, "ELLIPTICITY", CPL_TYPE_DOUBLE);
225  cpl_table_set_double(*extra, "ELLIPTICITY", 0, ellipticity);
226 
227  freeplist(filterlist);
228 
229  /* Sort sources table by Y_IMAGE */
230  p = cpl_propertylist_new();
231  cpl_propertylist_append_bool(p,"Y_IMAGE",0);
232  if (cpl_table_sort(sources, p) != CPL_ERROR_NONE) {
233  cpl_msg_error(cpl_func,"Cannot sort sources table");
234  cpl_propertylist_delete(p);
235  cpl_propertylist_delete(xlist);
236  freetable(sources);
237  return NULL;
238  }
239  freeplist(p);
240 
241  /* Create matrix of X,Y coordinates */
242  from = cpl_matrix_new(nsources, 2);
243  for (i=0; i<nsources; i++){
244  float xim = cpl_table_get_float(sources, "X_IMAGE", i, NULL);
245  float yim = cpl_table_get_float(sources, "Y_IMAGE", i, NULL);
246  cpl_matrix_set(from, i, 0, xim);
247  cpl_matrix_set(from, i, 1, yim);
248  }
249 
250  /* Get coverage of image */
251  if(omega_get_coverage(xlist, 0, from, &omega_x1, &omega_x2, &omega_y1, &omega_y2) != 0){
252  cpl_msg_error(cpl_func,"Cannot get coverage of image");
253  freematrix(from);
254  freeplist(xlist);
255  freetable(sources);
256  return NULL;
257  }
258  freematrix(from);
259 
260  /* Get STD reference stars from catalogue */
261  /* ramin = x1 ramax = x2 decmin = y1 decmax = y2;*/
262  /* Write in 'nstds' the number of sources in the table */
263  coords = cpl_vector_new(4);
264  cpl_vector_set(coords, 0, omega_x1);
265  cpl_vector_set(coords, 1, omega_x2);
266  cpl_vector_set(coords, 2, omega_y1);
267  cpl_vector_set(coords, 3, omega_y2);
268 
269  stds = omega_get_stds_table(cpl_frame_get_filename(refcat), coords, &nstds);
270  if (stds == NULL) {
271  cpl_msg_warning(cpl_func,"Cannot find reference standard stars in this region: "
272  "{RA-RA:DEC-DEC}={%g-%g:%g=%g}", omega_x1,omega_x2,omega_y1,omega_y2);
273  freeplist(xlist);
274  freetable(sources);
275  freevector(coords);
276  return NULL;
277  }
278  freevector(coords);
279 
280  cpl_msg_debug(cpl_func,"%d reference standards fall in region: {RA-RA:DEC-DEC}={%g-%g:%g=%g}",
281  nstds,omega_x1,omega_x2,omega_y1,omega_y2);
282 
283  /* Convert Ra, Dec to X,Y using the WCS information from image */
284  from = cpl_matrix_new(nstds, 2);
285  for (i=0; i<nstds; i++){
286  cpl_matrix_set(from, i, 0, cpl_table_get_double(stds, "Ra", i, NULL));
287  cpl_matrix_set(from, i, 1, cpl_table_get_double(stds, "Dec", i, NULL));
288  }
289 
290  to = NULL;
291  status = NULL;
292  wcs = cpl_wcs_new_from_propertylist(xlist);
293  if(cpl_wcs_convert(wcs, from, &to, &status, CPL_WCS_WORLD2PHYS) != CPL_ERROR_NONE){
294  freewcs(wcs);
295  cpl_array_delete(status);
296  freematrix(from);
297  freematrix(to);
298  freeplist(xlist);
299  freetable(sources);
300  freetable(stds);
301  cpl_msg_error(cpl_func,"Error in cpl_wcs conversion. %s",cpl_error_get_message());
302  return NULL;
303  }
304  freewcs(wcs);
305  freematrix(from);
306  freeplist(xlist);
307  cpl_array_delete(status);
308 
309  /* Add the converted x,y coordinate columns to the reference stds table */
310  cpl_table_new_column(stds,"xpredict",CPL_TYPE_FLOAT);
311  cpl_table_set_column_unit(stds,"xpredict","pixels");
312  cpl_table_new_column(stds,"ypredict",CPL_TYPE_FLOAT);
313  cpl_table_set_column_unit(stds,"ypredict","pixels");
314 
315  for(i=0; i< nstds; i++){
316  float xp = (float)cpl_matrix_get(to, i, 0);
317  float yp = (float)cpl_matrix_get(to, i, 1);
318  cpl_table_set_float(stds,"xpredict", i, xp);
319  cpl_table_set_float(stds,"ypredict", i, yp);
320  }
321  freematrix(to);
322 
323  /* Sort stds table by ypredict */
324  p = cpl_propertylist_new();
325  cpl_propertylist_append_bool(p,"ypredict",0);
326  if (cpl_table_sort(stds, p) != CPL_ERROR_NONE) {
327  cpl_msg_error(cpl_func,"Cannot sort stds table");
328  freeplist(p);
329  freetable(sources);
330  freetable(stds);
331  return NULL;
332  }
333  freeplist(p);
334 
335  /* Match sources and stds stars */
336  if (omega_matchstds(sources, stds, radius, &matchstds) != 0){
337  cpl_msg_error(cpl_func,"Failed to match reference standards to image sources. %s",cpl_error_get_message());
338  freetable(stds);
339  freetable(sources);
340  return NULL;
341  }
342 
343  nmatches = cpl_table_get_nrow(matchstds);
344  if(nmatches < 2){
345  cpl_msg_info(cpl_func,"Not enough matches (n=%d). Trying again...",nmatches);
346  /* Try again with increased radius */
347  for(i=0; i<4; i++){
348  radius = radius + 10.0;
349  freetable(matchstds);
350  if (omega_matchstds(sources, stds, radius, &matchstds) != 0){
351  cpl_msg_error(cpl_func,"Failed to match reference standards to image sources. %s",
352  cpl_error_get_message());
353  freetable(stds);
354  freetable(sources);
355  freetable(matchstds);
356  return NULL;
357  }
358  nmatches = cpl_table_get_nrow(matchstds);
359  if(nmatches < 2)
360  continue;
361 
362  }
363  }
364  if(nmatches < 2){
365  cpl_msg_warning(cpl_func,"There are no standard stars in image");
366  freetable(stds);
367  freetable(sources);
368  freetable(matchstds);
369  return NULL;
370  }
371 
372  cpl_msg_info(cpl_func,"Found %d standard stars in image", nmatches);
373 
374  freetable(stds);
375  freetable(sources);
376 
377  return matchstds;
378 }
379 
380 
381 void omega_get_extinction(cpl_table *extra, cpl_table *extinct2,
382  cpl_table *extinct1, double *extinction, cpl_table *monit2,
383  double *shift_err, double *extinction_airmass,
384  double *extinction_error_kwad)
385 {
386  /* Calculate extinction */
387  /*Get information from other tables */
388 
389  double upr = 0.;
390  double slope = 0.;
391  double shift = 0.;
392  double temp_ext = 0.;
393  double upr_ext = 0.;
394  double lwr_ext = 0.;
395  double cwl = 0.;
396  double airmass = 0.;
397  int i = 0;
398  int numrows = 0;
399  double wincr = 0;
400  double lwr = 0;
401  cwl = cpl_table_get_double(extra, "CWL", 0, NULL);
402  airmass = cpl_table_get_double(extra, "AIRMASS", 0, NULL);
403  wincr = cpl_table_get_float(extinct2, "wavel_incr", 0, NULL);
404  lwr = cwl - fmod(cwl, wincr); /*takes the float remainder of operation*/
405  numrows = cpl_table_get_nrow(extinct1);
406  for(i=0; i< numrows; i++) {
407  int number = cpl_table_get_float(extinct1,"wavelength", i, NULL);
408  if(number == lwr) {
409  lwr_ext = cpl_table_get_float(extinct1,"extinction",i, NULL);
410  break;
411  }
412  }
413  upr = cwl - fmod(cwl, wincr) + wincr;
414  for(i=0; i< numrows; i++) {
415  int number = cpl_table_get_float(extinct1,"wavelength", i, NULL);
416  if(number == upr) {
417  upr_ext = cpl_table_get_float(extinct1,"extinction",i, NULL);
418  break;
419  }
420  }
421  slope = (upr_ext - lwr_ext) / wincr;
422  temp_ext = fmod(cwl, wincr);
423  *extinction = (temp_ext * slope) + lwr_ext;
424 
425  /* Get shift from monitoring report */
426 
427  shift = cpl_table_get_float(monit2, "shift", 0, NULL);
428  *shift_err = cpl_table_get_float(monit2, "shift_err", 0, NULL);
429  *extinction_airmass = *extinction + shift;
430  *extinction = (*extinction_airmass) * (airmass);
431  *extinction_error_kwad = ((*shift_err) * (airmass)) * ((*shift_err) * (airmass));
432 
433  return;
434 }
435 
436 /*----------------------------------------------------------------------------*/
447 /*----------------------------------------------------------------------------*/
448 cpl_table *omega_zeropoints(cpl_table *matches, cpl_table *extra, const cpl_frame *monitfr,
449  const cpl_frame *extfr, const cpl_frame *colfr)
450 {
451 
452  int i = 0;
453  int j = 0;
454  int nrows = 0;
455  double extinction = 0.0;
456  double shift_err = 0.0;
457  double extinction_airmass = 0.0;
458  double meanZP = 0.0;
459  double ZPerror = 0.0;
460  double extinction_error_kwad = 0.0;
461  double weight_sum = 0.0;
462  double sum_data = 0.0;
463  double clip_median=0.;
464  double clip_stdev=0.;
465  double clip_min=0.;
466  double clip_max=0.;
467 
468  const char *refmag;
469  const char *refmagerr;
470  const char *filt_id = NULL;
471  const char *filt_name = NULL;
472 
473  cpl_table *raw_tbl, *monit1, *monit2;
474  cpl_table *extinct1, *extinct2;
475  cpl_table *coltbl = NULL;
476  cpl_table *tbl, *zptbl;
477  cpl_table * raw_tbl_tmp=NULL;
478 
479 
480  /*Load monitoring report tables*/
481  monit1 = cpl_table_load(cpl_frame_get_filename(monitfr), 1, 0);
482  if (monit1 == NULL) {
483  cpl_msg_error(cpl_func, "Cannot load 1st extension of monitoring report table");
484  return NULL;
485  }
486 
487  monit2 = cpl_table_load(cpl_frame_get_filename(monitfr), 2, 0);
488  if (monit2 == NULL) {
489  cpl_msg_error(cpl_func, "Cannot load 2nd extension of monitoring report table");
490  freetable(monit1);
491  return NULL;
492  }
493 
494  /*Load extinction curve tables*/
495  extinct1 = cpl_table_load(cpl_frame_get_filename(extfr), 1, 0);
496  if (extinct1 == NULL) {
497  cpl_msg_error(cpl_func, "Cannot load 1st extension of std extinction table");
498  freetable(monit1);
499  freetable(monit2);
500  return NULL;
501  }
502 
503  extinct2 = cpl_table_load(cpl_frame_get_filename(extfr), 2, 0);
504  if (extinct2 == NULL) {
505  cpl_msg_error(cpl_func, "Cannot load 2nd extension of std extinction table");
506  freetable(monit1);
507  freetable(monit2);
508  freetable(extinct1);
509  return NULL;
510  }
511 
512  /* Load colour terms table if present*/
513  if (colfr != NULL) {
514  coltbl = cpl_table_load(cpl_frame_get_filename(colfr), 1, 0);
515  if (coltbl == NULL){
516  cpl_msg_error(cpl_func, "Cannot load colour terms table");
517  freetable(monit1);
518  freetable(monit2);
519  freetable(extinct1);
520  freetable(extinct2);
521  return NULL;
522  }
523  }
524 
525  filt_id = cpl_table_get_string(extra, "FILT_ID", 0);
526  filt_name = cpl_table_get_string(extra, "FILT_NAME", 0);
527 
528 
529  /* Calculate instrumental magnitudes and raw ZP */
530  /*
531  * inst_mag = -2.5 * log(FLUX_ISO/EXPTIME)
532  * high = -2.5 * log( (FLUX_ISO + FLUX_ISO_ERR) / FLUX_ISO )
533  * low = -2.5 * log( (FLUX_ISO - FLUX_ISO_ERR) / FLUX_ISO )
534  * inst_mag_err = ( fabs(high) + fabs(low) ) / 2.0
535  *
536  * Using MAG_APER from Sextractor, not FLUX_ISO
537  * raw_ZP = ref_mag - inst_mag - colour_term + extinction*airmass
538  * raw_ZP_err = sqrt( ref_mag_err**2 + inst_mag_err**2 )
539  *
540  */
541  nrows = cpl_table_get_nrow(matches);
542  raw_tbl = cpl_table_new(nrows);
543  refmag = cpl_table_get_string(extra, "REF_MAG_ID", 0);
544  refmagerr = cpl_table_get_string(extra, "REF_MAG_ID_ERR", 0);
545 
546  cpl_table_duplicate_column(raw_tbl,"mag", matches, refmag);
547  cpl_table_duplicate_column(raw_tbl,"mag_err", matches, refmagerr);
548  cpl_table_duplicate_column(raw_tbl, "instmag", matches, "MAG_APER");
549  cpl_table_duplicate_column(raw_tbl,"instmag_err", matches, "MAGERR_APER");
550 
551  /* Reject outliers from source list */
552  tbl = omega_filter_table_float(raw_tbl, "instmag", CPL_LESS_THAN, 25.0, &nrows);
553  freetable(raw_tbl);
554  raw_tbl = omega_filter_table_float(tbl, "instmag", CPL_GREATER_THAN, -25.0, &nrows);
555  freetable(tbl);
556  tbl = omega_filter_table_float(raw_tbl, "mag", CPL_NOT_EQUAL_TO, 0.0, &nrows);
557  freetable(raw_tbl);
558  raw_tbl = cpl_table_duplicate(tbl);
559  freetable(tbl);
560 
561  if(cpl_table_get_column_mean(raw_tbl, "mag") == 0.0) {
562  cpl_msg_warning(cpl_func, "Reference table magnitudes (%s) are NULL for the selected stds.",refmag);
563  freetable(raw_tbl);
564  freetable(monit1);
565  freetable(monit2);
566  freetable(extinct1);
567  freetable(extinct2);
568  if(coltbl) freetable(coltbl);
569  return NULL;
570  }
571 
572  cpl_table_subtract_columns(raw_tbl,"mag","instmag");
573 
574  /* Use colour terms if present */
575  if(coltbl != NULL){
576  double colour = 0.0;
577  for(j = 0; j < 8; j++){
578  if(strcmp(filt_name, cpl_table_get_string(coltbl, "FILT_NAME", j)) == 0){
579  colour = cpl_table_get_double(coltbl, "COLOUR_TERM",j, NULL);
580  break;
581  }
582  else if(strcmp(filt_id, cpl_table_get_string(coltbl, "FILT_ID", j)) == 0){
583  colour = cpl_table_get_double(coltbl, "COLOUR_TERM",j, NULL);
584  break;
585  }
586  }
587 
588  cpl_table_subtract_scalar(raw_tbl, "mag", colour);
589  freetable(coltbl);
590  }
591 
592  cpl_table_erase_column(raw_tbl,"instmag");
593  cpl_table_name_column(raw_tbl,"mag","rawZP");
594 
595 
596  /*Preselect the zeropoint table by applying a clipping algorithm
597  * if there are enough entries */
598 
599  if(cpl_table_get_nrow(raw_tbl)>=3){
600 
601  raw_tbl_tmp=cpl_table_duplicate(raw_tbl);
602  freetable(raw_tbl);
603 
604  clip_median= cpl_table_get_column_median(raw_tbl_tmp, "rawZP");
605  clip_stdev = cpl_table_get_column_stdev(raw_tbl_tmp, "rawZP");
606  clip_min=clip_median - 2 * clip_stdev;
607  clip_max=clip_median + 2 * clip_stdev;
608 
609  cpl_msg_info(cpl_func,"Using objects with magnitudes within [%g, %g] "
610  "to derive the weighted zeropoint", clip_min, clip_max);
611 
612  /*select objects residing in the intervall [clip_min, clip_max]*/
613  cpl_table_and_selected_float(raw_tbl_tmp, "rawZP", CPL_NOT_GREATER_THAN,
614  clip_max);
615  cpl_table_and_selected_float(raw_tbl_tmp, "rawZP", CPL_NOT_LESS_THAN,
616  clip_min);
617 
618  raw_tbl=cpl_table_extract_selected(raw_tbl_tmp);
619  freetable(raw_tbl_tmp);
620 
621  }
622 
623  /* Calculate instrumental magnitudes errors*/
624  cpl_table_power_column(raw_tbl,"mag_err", 2.0);
625  cpl_table_power_column(raw_tbl,"instmag_err", 2.0);
626  cpl_table_add_columns(raw_tbl,"mag_err", "instmag_err");
627  cpl_table_power_column(raw_tbl,"mag_err",0.5);
628  cpl_table_erase_column(raw_tbl,"instmag_err");
629 
630  cpl_table_name_column(raw_tbl,"mag_err","rawZP_err");
631  omega_get_extinction(extra, extinct2, extinct1, &extinction, monit2,
632  &shift_err, &extinction_airmass, &extinction_error_kwad);
633  /* Calculate weighted average of ZP */
634  cpl_table_add_scalar(raw_tbl, "rawZP", extinction);
635  cpl_table_power_column(raw_tbl, "rawZP_err", 2.0);
636  cpl_table_add_scalar(raw_tbl, "rawZP_err", extinction_error_kwad);
637  cpl_table_power_column(raw_tbl, "rawZP_err", 0.5);
638  cpl_table_name_column(raw_tbl, "rawZP", "zeropoint");
639  cpl_table_name_column(raw_tbl, "rawZP_err", "zeropoint_err");
640 
641  cpl_table_duplicate_column(raw_tbl, "weights", raw_tbl, "zeropoint_err");
642 
643  cpl_table_power_column(raw_tbl, "weights", -2.);
644 
645 
646  weight_sum = 0.0;
647  for (i=0; i<nrows; i++) {
648  weight_sum += cpl_table_get_float(raw_tbl,"weights", i, NULL);
649  }
650 
651  cpl_table_multiply_columns(raw_tbl, "zeropoint", "weights");
652 
653  sum_data = 0.0;
654  for(i=0; i<nrows; i++) {
655  sum_data += cpl_table_get_float(raw_tbl, "zeropoint", i, NULL);
656  }
657 
658  /* Mean ZP and ZP_err*/
659  meanZP = sum_data/weight_sum;
660  ZPerror = sqrt(1./weight_sum);
661 
662  cpl_msg_debug(cpl_func,"meanZP: %f ZPerror: %f",meanZP,ZPerror);
663 
664 
665  cpl_msg_info("","Photometric parameters for filter %s",filt_name);
666  cpl_msg_info("","Weighted Average Zeropoint (mag) %0.5g", meanZP);
667  cpl_msg_info("","Zeropoint error (mag) %0.5g", ZPerror);
668  cpl_msg_info("","Extinction (mag/airmass) %0.5g", extinction_airmass);
669  cpl_msg_info("","Extinction times airmass (mag) %0.5g", extinction);
670  cpl_msg_info("","Extinction error (mag/airmass) %0.5g", shift_err);
671  cpl_msg_info("","Seeing (arcsec) %0.5g", cpl_table_get_double(extra,"SEEING",0,NULL));
672  cpl_msg_info("","Ellipticity %0.5g", cpl_table_get_double(extra,"ELLIPTICITY",0,NULL));
673 
674 
675 
676  /* Save a table with the following columns */
677  zptbl = cpl_table_new(1);
678  cpl_table_duplicate_column(zptbl,"CHIP_ID", extra, "CHIP_ID");
679  cpl_table_duplicate_column(zptbl,"FILT_ID", extra, "FILT_ID");
680  cpl_table_duplicate_column(zptbl,"FILT_NAME", extra, "FILT_NAME");
681 
682  cpl_table_new_column(zptbl,"ZEROPOINT", CPL_TYPE_DOUBLE);
683  cpl_table_set_double(zptbl,"ZEROPOINT", 0, meanZP);
684 
685  cpl_table_new_column(zptbl,"ZEROPOINT_ERR", CPL_TYPE_DOUBLE);
686  cpl_table_set_double(zptbl,"ZEROPOINT_ERR", 0, ZPerror);
687 
688  cpl_table_new_column(zptbl,"EXTINCTION", CPL_TYPE_DOUBLE);
689  cpl_table_set_double(zptbl,"EXTINCTION", 0, extinction_airmass);
690 
691  cpl_table_new_column(zptbl,"EXTINCTION_ERR", CPL_TYPE_DOUBLE);
692  cpl_table_set_double(zptbl,"EXTINCTION_ERR", 0, shift_err);
693 
694  cpl_table_duplicate_column(zptbl,"SEEING", extra, "SEEING");
695  cpl_table_duplicate_column(zptbl,"ELLIPTICITY", extra, "ELLIPTICITY");
696 
697  cpl_table_new_column(zptbl, "NSTARS", CPL_TYPE_INT);
698  cpl_table_set_int(zptbl, "NSTARS", 0, nrows);
699 
700  /*Clean up */
701  freetable(monit1);
702  freetable(monit2);
703  freetable(extinct1);
704  freetable(extinct2);
705  freetable(raw_tbl);
706 
707  return zptbl;
708 }
709 
710 
711 /*----------------------------------------------------------------------------*/
720 /*----------------------------------------------------------------------------*/
721 int omega_basic_param(cpl_table *sources, double scale, double *seeing,
722  double * ellipticity)
723 {
724 
725  int n = 0;
726  float stellarity = 1;
727  double median = 0.0;
728  cpl_table *tbl;
729 
730  *seeing = 0.0;
731 
732  cpl_table_select_all(sources);
733  cpl_table_and_selected_float(sources, "FWHM_IMAGE", CPL_GREATER_THAN, 0.1);
734  stellarity = 0.9;
735  n = cpl_table_and_selected_float(sources, "CLASS_STAR", CPL_NOT_LESS_THAN, stellarity);
736 
737  if(n <= 1) {
738  cpl_table_select_all(sources);
739  cpl_table_and_selected_float(sources, "FWHM_IMAGE", CPL_GREATER_THAN, 0.1);
740  stellarity = 0.8;
741  n = cpl_table_and_selected_float(sources, "CLASS_STAR", CPL_NOT_LESS_THAN, stellarity);
742  }
743 
744  if(n <= 1) {
745  cpl_table_select_all(sources);
746  cpl_table_and_selected_float(sources, "FWHM_IMAGE", CPL_GREATER_THAN, 0.1);
747  stellarity = 0.7;
748  n = cpl_table_and_selected_float(sources, "CLASS_STAR", CPL_NOT_LESS_THAN, stellarity);
749  }
750 
751  if(n <= 1){
752  return -1;
753  }
754 
755  //cpl_table_and_selected_float(sources, "FWHM_IMAGE", CPL_GREATER_THAN, 0.1);
756  tbl = cpl_table_extract_selected(sources);
757  median = cpl_table_get_column_median(tbl, "FWHM_IMAGE");
758  cpl_msg_debug(cpl_func,"median1: %g",median);
759  freetable(tbl);
760 
761  cpl_table_and_selected_float(sources, "FWHM_IMAGE", CPL_NOT_GREATER_THAN, median);
762  tbl = cpl_table_extract_selected(sources);
763  median = cpl_table_get_column_median(tbl, "FWHM_IMAGE");
764  cpl_msg_debug(cpl_func,"median2: %g",median);
765 
766  *seeing = median * fabs(scale)*3600;
767 
768  if(cpl_table_has_column(tbl,"ELLIPTICITY")){
769  *ellipticity = cpl_table_get_column_median(tbl, "ELLIPTICITY");
770  }
771  freetable(tbl);
772 
773  return 0;
774 }
775 
776