OMEGA Pipeline Reference Manual  1.0.5
omega_trim.c
1 /* $Id: omega_trim.c,v 1.3 2013-01-08 08:09:12 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: 2013-01-08 08:09:12 $
24  * $Revision: 1.3 $
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 
40 #include "omega_trim.h"
41 #include "omega_fits.h"
42 #include "omega_pfits.h"
43 #include "omega_stats.h"
44 #include "omega_utils.h"
45 
87 cpl_image * TrimOscanCorrect(const cpl_frame *frame, int oscan, int extn)
88 {
89 
90  int x0 = 1;
91  int x1 = 1;
92  int ry0 = 1;
93  int ry1 = 1;
94 
95  const char *_id = "TrimOscanCorrect";
96 
97  cpl_image *image=NULL;
98  cpl_image *trim_image=NULL;
99  cpl_image *tmp_img=NULL;
100  cpl_vector *region=NULL;
101  cpl_stats *px = NULL; /* *prescan_x_stat;*/
102  cpl_stats *py = NULL; /* *prescan_y_stat;*/
103  cpl_stats *ox = NULL; /* *ovrscan_x_stat;*/
104  cpl_stats *oy = NULL; /* *ovrscan_y_stat;*/
105 
106  if(frame == NULL) {
107  cpl_msg_error(_id,"NULL input frame. %s",cpl_error_get_message());
108  return NULL;
109  }
110 
111  image = cpl_image_load(cpl_frame_get_filename(frame), CPL_TYPE_FLOAT, 0, extn);
112  if(image == NULL) {
113  cpl_msg_error(_id,"Cannot load input image. %s",cpl_error_get_message());
114  return NULL;
115  }
116 
117  if(oscan == 0){
118  tmp_img = cpl_image_duplicate(image);
119  }
120 
121  /* For overscan methods 1--4, do the following statistics */
122  if (oscan > 0 && oscan < 5){
123  /*
124  Here the global variables of pre and overscan
125  are populated
126  */
127 
128  DoStatistics(frame, image, extn, &px,&py,&ox,&oy);
129 
130  switch(oscan){
131  case 1:
132  if(px != NULL){
133  tmp_img = cpl_image_subtract_scalar_create(image,cpl_stats_get_median(px));
134  }
135  else{
136  tmp_img = cpl_image_duplicate(image);
137  cpl_msg_warning(_id,"The overscan method %d may not suit these data",oscan);
138  }
139  break;
140 
141  case 2:
142  if(ox != NULL){
143  tmp_img = cpl_image_subtract_scalar_create(image,cpl_stats_get_median(ox));
144  }
145  else{
146  tmp_img = cpl_image_duplicate(image);
147  cpl_msg_warning(_id,"The overscan method %d may not suit these data",oscan);
148  }
149  break;
150 
151  case 3:
152  if(py != NULL){
153  tmp_img = cpl_image_subtract_scalar_create(image,cpl_stats_get_median(py));
154  }
155  else{
156  tmp_img = cpl_image_duplicate(image);
157  cpl_msg_warning(_id,"The overscan method %d may not suit these data",oscan);
158  }
159  break;
160 
161  case 4:
162  if(oy != NULL){
163  tmp_img = cpl_image_subtract_scalar_create(image,cpl_stats_get_median(oy));
164  }
165  else{
166  tmp_img = cpl_image_duplicate(image);
167  cpl_msg_warning(_id,"The overscan method %d may not suit these data",oscan);
168  }
169  break;
170  }
171 
172  }
173  else {
174  cpl_propertylist *plist = cpl_propertylist_load(cpl_frame_get_filename(frame), extn);
175  if(oscan == 5){
176  region = omega_get_scan_coord(plist, 0);
177  tmp_img = oscan_row(image, (int)cpl_vector_get(region,0),(int)cpl_vector_get(region,2));
178  freevector(region);
179  }
180  if(oscan == 6){
181  region = omega_get_scan_coord(plist, 2);
182  tmp_img = oscan_row(image, (int)cpl_vector_get(region,0),(int)cpl_vector_get(region,2));
183  freevector(region);
184  }
185  freeplist(plist);
186  }
187 
188  region = get_trim_region(frame, extn);
189  x0 = (int)cpl_vector_get(region,0);
190  ry0 = (int)cpl_vector_get(region,1);
191  x1 = (int)cpl_vector_get(region,2);
192  ry1 = (int)cpl_vector_get(region,3);
193 
194  trim_image = cpl_image_extract(tmp_img, x0, ry0, x1, ry1);
195  if (trim_image == NULL) {
196  cpl_msg_error (_id,"Cannot extract trimmed image <%s>", cpl_error_get_message());
197  freeimage(image);
198  freeimage(tmp_img);
199  freevector(region);
200  freestats(px);
201  freestats(py);
202  freestats(ox);
203  freestats(oy);
204  return NULL;
205  }
206 
207  freeimage(image);
208  freeimage(tmp_img);
209  freevector(region);
210  freestats(px);
211  freestats(py);
212  freestats(ox);
213  freestats(oy);
214 
215  return trim_image;
216 }
217 
248 cpl_image *omega_trim_oscan_correct(omega_fits *ofits, int oscan)
249 {
250 
251  int x0 = 1;
252  int x1 = 1;
253  int ry0 = 1;
254  int ry1 = 1;
255 
256  cpl_image *trim_image;
257  cpl_image *tmp_img;
258  cpl_vector *region;
259  cpl_propertylist *xlist;
260  cpl_stats *px = NULL; /* *prescan_x_stat;*/
261  cpl_stats *py = NULL; /* *prescan_y_stat;*/
262  cpl_stats *ox = NULL; /* *ovrscan_x_stat;*/
263  cpl_stats *oy = NULL; /* *ovrscan_y_stat;*/
264 
265  if(ofits == NULL) {
266  cpl_msg_error(cpl_func,"NULL input file. %s",cpl_error_get_message());
267  return NULL;
268  }
269 
270  if(oscan == 0){
271  tmp_img = cpl_image_duplicate(omega_fits_get_image(ofits));
272  }
273 
274  /* For overscan methods 1--4, do the following statistics */
275  if (oscan > 0 && oscan < 5){
276 
277  omega_scan_stats(ofits,&px ,&py, &ox, &oy);
278 
279  switch(oscan){
280  case 1:
281  if(px != NULL){
282  tmp_img = cpl_image_subtract_scalar_create(omega_fits_get_image(ofits),cpl_stats_get_median(px));
283  }
284  else{
285  tmp_img = cpl_image_duplicate(omega_fits_get_image(ofits));
286  cpl_msg_warning(cpl_func,"The overscan method %d may not suit these data",oscan);
287  }
288  break;
289 
290  case 2:
291  if(ox != NULL){
292  tmp_img = cpl_image_subtract_scalar_create(omega_fits_get_image(ofits),cpl_stats_get_median(ox));
293  }
294  else{
295  tmp_img = cpl_image_duplicate(omega_fits_get_image(ofits));
296  cpl_msg_warning(cpl_func,"The overscan method %d may not suit these data",oscan);
297  }
298  break;
299 
300  case 3:
301  if(py != NULL){
302  tmp_img = cpl_image_subtract_scalar_create(omega_fits_get_image(ofits),cpl_stats_get_median(py));
303  }
304  else{
305  tmp_img = cpl_image_duplicate(omega_fits_get_image(ofits));
306  cpl_msg_warning(cpl_func,"The overscan method %d may not suit these data",oscan);
307  }
308  break;
309 
310  case 4:
311  if(oy != NULL){
312  tmp_img = cpl_image_subtract_scalar_create(omega_fits_get_image(ofits),cpl_stats_get_median(oy));
313  }
314  else{
315  tmp_img = cpl_image_duplicate(omega_fits_get_image(ofits));
316  cpl_msg_warning(cpl_func,"The overscan method %d may not suit these data",oscan);
317  }
318  break;
319  }
320 
321  }
322  else {
323  xlist = omega_fits_get_ehu(ofits);
324  if(oscan == 5){
325  region = omega_get_scan_coord(xlist, 0);
326  tmp_img = oscan_row(omega_fits_get_image(ofits), (int)cpl_vector_get(region,0),
327  (int)cpl_vector_get(region,2));
328  }
329  if(oscan == 6){
330  region = omega_get_scan_coord(xlist, 2);
331  tmp_img = oscan_row(omega_fits_get_image(ofits), (int)cpl_vector_get(region,0),
332  (int)cpl_vector_get(region,2));
333  }
334  xlist = NULL;
335  }
336 
337  region = omega_get_trim_region(ofits);
338  x0 = (int)cpl_vector_get(region,0);
339  ry0 = (int)cpl_vector_get(region,1);
340  x1 = (int)cpl_vector_get(region,2);
341  ry1 = (int)cpl_vector_get(region,3);
342 
343  trim_image = cpl_image_extract(tmp_img, x0, ry0, x1, ry1);
344  if (trim_image == NULL) {
345  cpl_msg_error (cpl_func,"Cannot extract trimmed image <%s>", cpl_error_get_message());
346  freeimage(tmp_img);
347  freevector(region);
348  freestats(px);
349  freestats(py);
350  freestats(ox);
351  freestats(oy);
352  return NULL;
353  }
354 
355  freeimage(tmp_img);
356  freevector(region);
357  freestats(px);
358  freestats(py);
359  freestats(ox);
360  freestats(oy);
361 
362  return trim_image;
363 }
364 
365 
366 /*-------------------------------------------------------------------------*/
375 /*--------------------------------------------------------------------------*/
376 cpl_image *oscan_row (cpl_image *img, int x1, int x2)
377 {
378 
379  int i = 0;
380  int j = 0;
381  int lx = 256;
382  int ly = 256;
383  int offset = 0;
384  int oscanoffset = 0;
385  int oscansize = 0;
386  float med = 0.0f;
387  /* float * parr;*/
388  float * pindata;
389  float * poutdata;
390 
391  cpl_image *image_out;
392 
393  /*Start algorithm */
394  lx = cpl_image_get_size_x (img);
395  ly = cpl_image_get_size_y (img);
396 
397  image_out = cpl_image_new (lx, ly, CPL_TYPE_FLOAT);
398 
399  oscansize = x2-x1+1;
400  /* parr = cpl_calloc(oscansize, sizeof(float));*/
401 
402  pindata = (float *)cpl_image_get_data (img);
403  poutdata = (float *)cpl_image_get_data (image_out);
404 
405  for (i=0; i<ly; i++)
406  {
407  offset = i * lx;
408  oscanoffset = offset+x1-1;
409  med = 0.0;
410 
411  for (j=0; j<oscansize; j++)
412  {
413  med += pindata[oscanoffset+j];
414  }
415 
416  /* med = median_pixelvalue(parr, oscansize); */
417  med /= oscansize;
418 
419  for (j=0; j<lx; j++){
420  poutdata[offset+j] = pindata[offset+j]-med;
421  }
422  }
423 
424  /* cpl_free(parr);*/
425 
426  return image_out;
427 }
428 
429 
443 cpl_vector * get_trim_region(const cpl_frame *frame, int extnum)
444 {
445 
446  int orient = 0;
447  int naxis1 = 0;
448  int naxis2 = 0;
449  int ovscx = 0;
450  int ovscy = 0;
451  int prscx = 0;
452  int prscy = 0;
453  int x0 = 1;
454  int x1 = 1;
455  int ry0 = 1;
456  int ry1 = 1;
457 
458  const char *filename = NULL;
459 
460  cpl_propertylist *xplist;
461  cpl_vector *region;
462  cpl_vector *sregion;
463 
464 
465  if(frame == NULL){
466  cpl_msg_error(cpl_func,"Input frame is NULL");
467  return NULL;
468  }
469 
470  filename = cpl_frame_get_filename(frame);
471 
472  /*Get extension header*/
473  xplist = cpl_propertylist_load(filename, extnum);
474  if(xplist == NULL) {
475  cpl_msg_error(cpl_func,"Cannot load header");
476  return NULL;
477  }
478 
479  naxis1 = cpl_propertylist_get_int(xplist, "NAXIS1");
480  naxis2 = cpl_propertylist_get_int(xplist, "NAXIS2");
481  if (naxis1 == 0 || naxis2 == 0){
482  cpl_msg_error(cpl_func,"NAXIS1 or NAXIS2 is 0");
483  freeplist(xplist);
484  return NULL;
485  }
486 
487  /*Get pre and overscan regions from header*/
488  sregion = omega_pfits_get_preovscan(xplist);
489  if(sregion == NULL){
490  cpl_msg_warning(cpl_func,"Cannot get pre and overscan regions");
491  freeplist(xplist);
492  return NULL;
493  }
494 
495  prscx = cpl_vector_get(sregion,0);
496  prscy = cpl_vector_get(sregion,1);
497  ovscx = cpl_vector_get(sregion,2);
498  ovscy = cpl_vector_get(sregion,3);
499 
500  freeplist(xplist);
501 
502  /* Get trim coordinates */
503  /* Switch by orientation of CCD layout */
504  orient = omega_get_chip_orientation(extnum);
505 
506  switch(orient){
507  case 1:
508  x0 = prscx + 1;
509  ry0 = prscy + 1;
510  x1 = naxis1 - ovscx;
511  ry1 = naxis2 - ovscy;
512  break;
513  case 2:
514  x0 = ovscx + 1;
515  ry0 = ovscy + 1;
516  x1 = naxis1 - prscx;
517  ry1 = naxis2 - prscy;
518  break;
519  default:
520  x0 = prscx + 1;
521  ry0 = prscy + 1;
522  x1 = naxis1 - ovscx;
523  ry1 = naxis2 - ovscy;
524  cpl_msg_warning(cpl_func,"Using default chip layout");
525  break;
526  }
527 
528 
529  /* Original: Get trim coordinates based on orientation keyword */
530  /* switch (chipornt) {
531  case 1:
532  x0 = prscx + 1;
533  ry0 = prscy + 1;
534  x1 = naxis1 - ovscx;
535  ry1 = naxis2 - ovscy;
536  break;
537 
538  case 2:
539  x0 = prscx + 1;
540  ry0 = ovscy + 1;
541  x1 = naxis1 - ovscx;
542  ry1 = naxis2 - prscy;
543  break;
544 
545  case 3:
546  x0 = ovscx + 1;
547  ry0 = prscy+ 1;
548  x1 = naxis1 - prscx;
549  ry1 = naxis2 - ovscy;
550  break;
551 
552  case 4:
553  x0 = ovscx + 1;
554  ry0 = ovscy + 1;
555  x1 = naxis1 - prscx;
556  ry1 = naxis2 - prscy;
557  break;
558 
559  default:
560  x0 = prscx + 1;
561  ry0 = prscy + 1;
562  x1 = naxis1 - ovscx;
563  ry1 = naxis2 - ovscy;
564  break;
565  }*/
566 
567  region = cpl_vector_new(4);
568  cpl_vector_set(region, 0, x0);
569  cpl_vector_set(region, 1, ry0);
570  cpl_vector_set(region, 2, x1);
571  cpl_vector_set(region, 3, ry1);
572 
573  freevector(sregion);
574 
575  return region;
576 
577 }
578 
593 cpl_vector * omega_get_trim_region(omega_fits *ofits)
594 {
595 
596  int orient = 0;
597  int naxis1 = 0;
598  int naxis2 = 0;
599  int ovscx = 0;
600  int ovscy = 0;
601  int prscx = 0;
602  int prscy = 0;
603  int x0 = 1;
604  int x1 = 1;
605  int ry0 = 1;
606  int ry1 = 1;
607 
608  cpl_vector *region;
609  cpl_vector *sregion;
610  cpl_propertylist *xlist;
611 
612 
613  if(ofits == NULL){
614  cpl_msg_error(cpl_func,"Input frame is NULL");
615  return NULL;
616  }
617 
618  xlist = omega_fits_get_ehu(ofits);
619  naxis1 = cpl_propertylist_get_int(xlist, "NAXIS1");
620  naxis2 = cpl_propertylist_get_int(xlist, "NAXIS2");
621  if (naxis1 == 0 || naxis2 == 0){
622  cpl_msg_error(cpl_func,"NAXIS1 or NAXIS2 is 0");
623  xlist = NULL;
624  return NULL;
625  }
626 
627  /*Get pre and overscan regions from header*/
628  sregion = omega_pfits_get_preovscan(xlist);
629  if(sregion == NULL){
630  cpl_msg_warning(cpl_func,"Cannot get pre and overscan regions");
631  return NULL;
632  }
633 
634  prscx = cpl_vector_get(sregion,0);
635  prscy = cpl_vector_get(sregion,1);
636  ovscx = cpl_vector_get(sregion,2);
637  ovscy = cpl_vector_get(sregion,3);
638 
639  /* Get trim coordinates */
640  /* Switch by orientation of CCD layout */
642  switch(orient){
643  case 1:
644  x0 = prscx + 1;
645  ry0 = prscy + 1;
646  x1 = naxis1 - ovscx;
647  ry1 = naxis2 - ovscy;
648  break;
649  case 2:
650  x0 = ovscx + 1;
651  ry0 = ovscy + 1;
652  x1 = naxis1 - prscx;
653  ry1 = naxis2 - prscy;
654  break;
655  default:
656  x0 = prscx + 1;
657  ry0 = prscy + 1;
658  x1 = naxis1 - ovscx;
659  ry1 = naxis2 - ovscy;
660  cpl_msg_warning(cpl_func,"Using default chip layout");
661  break;
662  }
663 
664  region = cpl_vector_new(4);
665  cpl_vector_set(region, 0, x0);
666  cpl_vector_set(region, 1, ry0);
667  cpl_vector_set(region, 2, x1);
668  cpl_vector_set(region, 3, ry1);
669 
670  freevector(sregion);
671  xlist = NULL;
672 
673  return region;
674 
675 }
676 
692 void DoStatistics (const cpl_frame *frame, cpl_image *image, int extnum,
693  cpl_stats **px,cpl_stats **py,cpl_stats **ox,cpl_stats **oy)
694 {
695  int x0 = 0;
696  int ry0 = 0;
697  int x1 = 0;
698  int ry1 = 0;
699  int iscan = 0;
700  double threshold = 1.0; /*FIXME: pass this as a parameter*/
701  int iter = 5; /*FIXME: pass this as a parameter*/
702  cpl_vector *region;
703  cpl_propertylist *plist;
704 
705 
706  if((plist = cpl_propertylist_load(cpl_frame_get_filename(frame), extnum)) == NULL)
707  return;
708 
709  /*
710  Returns a vector of type x0,ry0,x1,ry1
711  which are the coordinates of the trim section
712  */
713  /*
714  * Get pre/overscan regions based on iscan index:
715  * 0 = Prescan X region
716  * 1 = Prescan Y region
717  * 2 = Overscan X region
718  * 3 = Overscan Y region
719  */
720  iscan = 0; /*PRSCX*/
721  region = omega_get_scan_coord(plist, iscan);
722  x0 = (int)cpl_vector_get(region,0);
723  ry0 = (int)cpl_vector_get(region,1);
724  x1 = (int)cpl_vector_get(region,2);
725  ry1 = (int)cpl_vector_get(region,3);
726 
727  /* region = GetPrescanXRegion(frame, extnum);
728  x0 = (int)cpl_vector_get(region,0);
729  ry0 = (int)cpl_vector_get(region,1);
730  x1 = (int)cpl_vector_get(region,2);
731  ry1 = (int)cpl_vector_get(region,3);*/
732 
733  if (x1 - x0 +1 > 0){
734  *px = omega_iter_stat_opts(image,region, threshold, iter);
735  }
736  freevector(region);
737 
738  iscan = 1; /*PRSCY*/
739  region = omega_get_scan_coord(plist, iscan);
740  x0 = (int)cpl_vector_get(region,0);
741  ry0 = (int)cpl_vector_get(region,1);
742  x1 = (int)cpl_vector_get(region,2);
743  ry1 = (int)cpl_vector_get(region,3);
744 
745  /* region = GetPrescanYRegion(frame, extnum);
746  x0 = (int)cpl_vector_get(region,0);
747  ry0 = (int)cpl_vector_get(region,1);
748  x1 = (int)cpl_vector_get(region,2);
749  ry1 = (int)cpl_vector_get(region,3);*/
750 
751  if (ry1 - ry0 +1 > 0){
752  *py = omega_iter_stat_opts(image,region,threshold, iter);
753  }
754  freevector(region);
755 
756  iscan = 2; /*OVSCX*/
757  region = omega_get_scan_coord(plist, iscan);
758  x0 = (int)cpl_vector_get(region,0);
759  ry0 = (int)cpl_vector_get(region,1);
760  x1 = (int)cpl_vector_get(region,2);
761  ry1 = (int)cpl_vector_get(region,3);
762 
763  /* region = GetOverscanXRegion(frame, extnum);
764  x0 = (int)cpl_vector_get(region,0);
765  ry0 = (int)cpl_vector_get(region,1);
766  x1 = (int)cpl_vector_get(region,2);
767  ry1 = (int)cpl_vector_get(region,3);*/
768 
769  if (x1 - x0 +1 > 0){
770  *ox = omega_iter_stat_opts(image,region,threshold, iter);
771  }
772  freevector(region);
773 
774  iscan = 3; /*OVSCY*/
775  region = omega_get_scan_coord(plist, iscan);
776  x0 = (int)cpl_vector_get(region,0);
777  ry0 = (int)cpl_vector_get(region,1);
778  x1 = (int)cpl_vector_get(region,2);
779  ry1 = (int)cpl_vector_get(region,3);
780 
781  /* region = GetOverscanYRegion(frame, extnum);
782  x0 = (int)cpl_vector_get(region,0);
783  ry0 = (int)cpl_vector_get(region,1);
784  x1 = (int)cpl_vector_get(region,2);
785  ry1 = (int)cpl_vector_get(region,3);*/
786 
787  if (ry1 - ry0 +1 > 0){
788  *oy = omega_iter_stat_opts(image,region,threshold, iter);
789  }
790 
791  freevector(region);
792  freeplist(plist);
793 
794  return;
795 
796 }
797 
811 void omega_scan_stats (omega_fits *ofits,cpl_stats **px,cpl_stats **py,
812  cpl_stats **ox,cpl_stats **oy)
813 {
814  int x0 = 0;
815  int ry0 = 0;
816  int x1 = 0;
817  int ry1 = 0;
818  int iscan = 0;
819  double threshold = 1.0; /*FIXME: pass this as a parameter*/
820  int iter = 5; /*FIXME: pass this as a parameter*/
821  cpl_vector *region;
822  cpl_propertylist *xlist;
823 
824  if(ofits == NULL)
825  return;
826 
827  xlist = omega_fits_get_ehu(ofits);
828 
829  /*
830  Returns a vector of type x0,ry0,x1,ry1
831  which are the coordinates of the trim section
832  */
833  /*
834  * Get pre/overscan regions based on iscan index:
835  * 0 = Prescan X region
836  * 1 = Prescan Y region
837  * 2 = Overscan X region
838  * 3 = Overscan Y region
839  */
840  iscan = 0; /*PRSCX*/
841  region = omega_get_scan_coord(xlist, iscan);
842  x0 = (int)cpl_vector_get(region,0);
843  ry0 = (int)cpl_vector_get(region,1);
844  x1 = (int)cpl_vector_get(region,2);
845  ry1 = (int)cpl_vector_get(region,3);
846 
847 
848  if (x1 - x0 +1 > 0){
849  *px = omega_iter_stat_opts(omega_fits_get_image(ofits),region, threshold, iter);
850  }
851  freevector(region);
852 
853  iscan = 1; /*PRSCY*/
854  region = omega_get_scan_coord(xlist, iscan);
855  x0 = (int)cpl_vector_get(region,0);
856  ry0 = (int)cpl_vector_get(region,1);
857  x1 = (int)cpl_vector_get(region,2);
858  ry1 = (int)cpl_vector_get(region,3);
859 
860  if (ry1 - ry0 +1 > 0){
861  *py = omega_iter_stat_opts(omega_fits_get_image(ofits),region,threshold, iter);
862  }
863  freevector(region);
864 
865  iscan = 2; /*OVSCX*/
866  region = omega_get_scan_coord(xlist, iscan);
867  x0 = (int)cpl_vector_get(region,0);
868  ry0 = (int)cpl_vector_get(region,1);
869  x1 = (int)cpl_vector_get(region,2);
870  ry1 = (int)cpl_vector_get(region,3);
871 
872  if (x1 - x0 +1 > 0){
873  *ox = omega_iter_stat_opts(omega_fits_get_image(ofits),region,threshold, iter);
874  }
875  freevector(region);
876 
877  iscan = 3; /*OVSCY*/
878  region = omega_get_scan_coord(xlist, iscan);
879  x0 = (int)cpl_vector_get(region,0);
880  ry0 = (int)cpl_vector_get(region,1);
881  x1 = (int)cpl_vector_get(region,2);
882  ry1 = (int)cpl_vector_get(region,3);
883 
884  if (ry1 - ry0 +1 > 0){
885  *oy = omega_iter_stat_opts(omega_fits_get_image(ofits),region,threshold, iter);
886  }
887 
888  freevector(region);
889  xlist = NULL;
890 
891  return;
892 
893 }
894 
919 cpl_vector * omega_get_scan_coord(const cpl_propertylist *plist, int iscan)
920 {
921 
922  int naxis1 = 0;
923  int naxis2 = 0;
924  int ovscx = 0;
925  int ovscy = 0;
926  int prscx = 0;
927  int prscy = 0;
928  int x0 = 0;
929  int x1 = 0;
930  int ry0 = 0;
931  int ry1 = 0;
932  const char * chipid;
933  int chipornt=0;
934 
935  cpl_vector *region;
936  cpl_vector *sregion;
937 
938 
939  if(plist == NULL)
940  return NULL;
941 
942  naxis1 = cpl_propertylist_get_int(plist, "NAXIS1");
943  naxis2 = cpl_propertylist_get_int(plist, "NAXIS2");
944  if (naxis1 == 0 || naxis2 == 0){
945  cpl_msg_error(cpl_func,"NAXIS1 or NAXIS2 is 0");
946  return NULL;
947  }
948 
949  chipid = omega_pfits_get_chipid(plist);
950  chipornt = omega_pfits_get_orientation(chipid);
951 
952  if(chipornt == 0) chipornt = 1;
953 
954  /*Get pre and overscan regions from header*/
955  sregion = omega_pfits_get_preovscan(plist);
956 
957  prscx = (int)cpl_vector_get(sregion,0);
958  prscy = (int)cpl_vector_get(sregion,1);
959  ovscx = (int)cpl_vector_get(sregion,2);
960  ovscy = (int)cpl_vector_get(sregion,3);
961 
962 
963 
964  if(chipornt==1)
965  {
966  switch(iscan){
967  case 0:
968  /* FITS coordinates of prescan in X direction */
969  x0 = 1;
970  ry0 = prscy+1;
971  x1 = prscx;
972  ry1 = naxis2 - ovscy;
973  cpl_msg_debug(cpl_func,"px: x0:%5d ry0:%5d x1:%5d ry1:%5d", x0,ry0,x1,ry1 );
974  break;
975  case 1:
976  /* FITS coordinates of prescan in Y direction */
977  x0 = prscx+1;
978  ry0 = 1;
979  x1 = naxis1-ovscx;
980  ry1 = prscy;
981  cpl_msg_debug(cpl_func,"py: x0:%5d ry0:%5d x1:%5d ry1:%5d", x0,ry0,x1,ry1 );
982  break;
983  case 2:
984  /* FITS coordinates of overscan in X direction */
985  x0 = naxis1-ovscx+1;
986  ry0 = prscy+1;
987  x1 = naxis1;
988  ry1 = naxis2 - ovscy;
989  cpl_msg_debug(cpl_func,"ox: x0:%5d ry0:%5d x1:%5d ry1:%5d", x0,ry0,x1,ry1 );
990  break;
991  case 3:
992  /* FITS coordinates of overscan in Y direction */
993  x0 = prscx+1;
994  ry0 = naxis2-ovscy+1;
995  x1 = naxis1-ovscx;
996  ry1 = naxis2;
997  cpl_msg_debug(cpl_func,"oy: x0:%5d ry0:%5d x1:%5d ry1:%5d", x0,ry0,x1,ry1 );
998  break;
999  default:
1000  cpl_msg_error(cpl_func,"Unable to get pre/overscan coordinates");
1001  freevector(sregion);
1002  return NULL;
1003 
1004  }
1005  }
1006  if(chipornt==2)
1007  {
1008  switch(iscan){
1009  case 0:
1010  /* FITS coordinates of prescan in X direction */
1011  x0 = naxis1 - prscx + 1;
1012  ry0 = ovscy+1;
1013  x1 = naxis1;
1014  ry1 = naxis2-prscy;
1015  cpl_msg_debug(cpl_func,"px: x0:%5d ry0:%5d x1:%5d ry1:%5d", x0,ry0,x1,ry1 );
1016  break;
1017  case 1:
1018  /* FITS coordinates of prescan in Y direction */
1019  x0 = ovscx+1;
1020  ry0 = naxis2-prscy;
1021  x1 = naxis1-prscx;
1022  ry1 = naxis2;
1023  cpl_msg_debug(cpl_func,"py: x0:%5d ry0:%5d x1:%5d ry1:%5d", x0,ry0,x1,ry1 );
1024  break;
1025  case 2:
1026  /* FITS coordinates of overscan in X direction */
1027  x0 = 1;
1028  ry0 = ovscy+1;
1029  x1 = ovscx;
1030  ry1 = naxis2 - prscy;
1031  cpl_msg_debug(cpl_func,"ox: x0:%5d ry0:%5d x1:%5d ry1:%5d", x0,ry0,x1,ry1 );
1032  break;
1033  case 3:
1034  /* FITS coordinates of overscan in Y direction */
1035  x0 = ovscx+1;
1036  ry0 = 1;
1037  x1 = naxis1-prscx;
1038  ry1 = ovscy;
1039  cpl_msg_debug(cpl_func,"oy: x0:%5d ry0:%5d x1:%5d ry1:%5d", x0,ry0,x1,ry1 );
1040  break;
1041  default:
1042  cpl_msg_error(cpl_func,"Unable to get pre/overscan coordinates");
1043  freevector(sregion);
1044  return NULL;
1045 
1046  }
1047  }
1048 
1049  region = cpl_vector_new(4);
1050  cpl_vector_set(region, 0, x0);
1051  cpl_vector_set(region, 1, ry0);
1052  cpl_vector_set(region, 2, x1);
1053  cpl_vector_set(region, 3, ry1);
1054 
1055 
1056  freevector(sregion);
1057 
1058  return region;
1059 
1060 }
1061