238 #include "sinfo_vltPort.h"
244 #include <sys/types.h>
245 #include <sys/times.h>
250 #include "sinfo_dfs.h"
251 #include "sinfo_new_cube_ops.h"
252 #include "sinfo_resampling.h"
253 #include "sinfo_function_1d.h"
254 #include "sinfo_error.h"
255 #include "sinfo_globals.h"
256 #include "sinfo_utils_wrappers.h"
258 #include <cpl_vector.h>
265 sinfo_shift_cubes(cpl_imagelist** tmpcubes,
268 cpl_imagelist** cubes,
275 cpl_imagelist* mask);
278 sinfo_build_mask_cube(
const int z_min,
286 cpl_imagelist** cubes,
287 cpl_imagelist** tmpcubes,
288 cpl_imagelist* mask);
291 sinfo_build_mask_cube_thomas(
const int z_min,
299 cpl_imagelist** cubes,
300 cpl_imagelist** tmpcubes,
301 cpl_imagelist* mask);
303 sinfo_compute_weight_average(
const int z_min,
308 cpl_imagelist* mergedCube,
310 cpl_imagelist** tmpcubes,
316 sinfo_check_input(cpl_imagelist** cubes,
322 sinfo_coadd_with_ks_clip2(
const int z_min,
332 cpl_imagelist* mergedCube,
333 cpl_imagelist** tmpcubes);
361 sinfo_coadd_with_ks_clip(
const int z_min,
371 cpl_imagelist* mergedCube,
372 cpl_imagelist** tmpcubes);
401 cpl_imagelist * cube1,
402 cpl_imagelist * cube2,
406 if (cube1==NULL || cube2==NULL)
415 return sinfo_new_cube_add(cube1, cube2) ;
418 return sinfo_new_cube_sub(cube1, cube2) ;
422 return sinfo_new_cube_mul(cube1, cube2) ;
426 return sinfo_new_cube_div(cube1, cube2) ;
430 sinfo_msg_error(
"illegal requested operation: aborting cube arithmetic") ;
453 sinfo_new_cube_const_ops(
463 cpl_imagelist* c2=NULL;
482 if ((constant == 0.0) && (operation ==
'/'))
485 "in cube/constant operation") ;
489 if ( NULL == (c2 = cpl_imagelist_new()) )
495 c2=cpl_imagelist_duplicate(c1);
496 if(operation ==
'+') {
497 cpl_imagelist_add_scalar(c2,constant);
498 }
else if (operation ==
'-') {
499 cpl_imagelist_subtract_scalar(c2,constant);
500 }
else if (operation ==
'*') {
501 cpl_imagelist_multiply_scalar(c2,constant);
502 }
else if (operation ==
'/') {
503 cpl_imagelist_divide_scalar(c2,constant);
537 cpl_image* i_img=NULL;
538 cpl_image* img1=NULL;
539 cpl_image* img2=NULL;
540 cpl_image* img3=NULL;
547 inp1=cpl_imagelist_get_size(c1);
548 i_img=cpl_imagelist_get(c1,0);
549 ilx1=cpl_image_get_size_x(i_img);
550 ily1=cpl_image_get_size_y(i_img);
553 inp2=cpl_imagelist_get_size(c2);
554 i_img=cpl_imagelist_get(c2,0);
555 ilx2=cpl_image_get_size_x(i_img);
556 ily2=cpl_image_get_size_y(i_img);
558 if ((ilx1 != ilx2) ||
565 if ((inp2 != inp1) &&
572 if ( NULL == (c3 = cpl_imagelist_new()) )
578 for (np=0 ; np < inp1 ; np++)
580 img3=cpl_image_new(ilx1,ily1,CPL_TYPE_FLOAT);
581 cpl_imagelist_set(c3,img3,np);
585 for (np=0 ; np < inp1 ; np++)
587 img1=cpl_imagelist_get(c1,np);
588 p1data=cpl_image_get_data_float(img1);
589 img2=cpl_imagelist_get(c2,np);
590 p2data=cpl_image_get_data_float(img2);
591 img3=cpl_imagelist_get(c3,np);
592 p3data=cpl_image_get_data_float(img3);
594 for (i=0 ; i< (ulong32)ilx1*ily1 ; i++)
596 p3data[i] = p1data[i] - p2data[i] ;
628 cpl_image* i_img=NULL;
629 cpl_image* img1=NULL;
630 cpl_image* img2=NULL;
631 cpl_image* img3=NULL;
638 inp1=cpl_imagelist_get_size(c1);
639 i_img=cpl_imagelist_get(c1,0);
640 ilx1=cpl_image_get_size_x(i_img);
641 ily1=cpl_image_get_size_y(i_img);
644 inp2=cpl_imagelist_get_size(c2);
645 i_img=cpl_imagelist_get(c2,0);
646 ilx2=cpl_image_get_size_x(i_img);
647 ily2=cpl_image_get_size_y(i_img);
648 if ((ilx1 != ilx2) || (ily1 != ily2))
653 if ((inp2 != inp1) && (inp2 != 1))
659 if (NULL == (c3 = cpl_imagelist_new()) )
665 for (np=0 ; np < inp1 ; np++)
667 img3=cpl_image_new(ilx1,ily1,CPL_TYPE_FLOAT);
668 cpl_imagelist_set(c3,img3,np);
671 for (np=0 ; np < inp1 ; np++)
673 img1=cpl_imagelist_get(c1,np);
674 p1data=cpl_image_get_data_float(img1);
675 img2=cpl_imagelist_get(c2,np);
676 p2data=cpl_image_get_data_float(img2);
677 img3=cpl_imagelist_get(c3,np);
678 p3data=cpl_image_get_data_float(img3);
679 for (i=0 ; i< (ulong32)ilx1*ily1 ; i++)
681 p3data[i] = p1data[i] + p2data[i] ;
712 cpl_image* i_img=NULL;
713 cpl_image* img1=NULL;
714 cpl_image* img2=NULL;
715 cpl_image* img3=NULL;
723 inp1=cpl_imagelist_get_size(c1);
724 i_img=cpl_imagelist_get(c1,0);
725 ilx1=cpl_image_get_size_x(i_img);
726 ily1=cpl_image_get_size_y(i_img);
729 inp2=cpl_imagelist_get_size(c2);
730 i_img=cpl_imagelist_get(c2,0);
731 ilx2=cpl_image_get_size_x(i_img);
732 ily2=cpl_image_get_size_y(i_img);
734 if ((ilx1 != ilx2) || (ily1 != ily2))
740 if ((inp2 != inp1) && (inp2 != 1))
746 if ( NULL == (c3 = cpl_imagelist_new()) )
753 for (np=0 ; np < inp1 ; np++)
755 img3=cpl_image_new(ilx1,ily1,CPL_TYPE_FLOAT);
756 cpl_imagelist_set(c3,img3,np);
759 for (np=0 ; np < inp1 ; np++)
761 img1=cpl_imagelist_get(c1,np);
762 p1data=cpl_image_get_data_float(img1);
763 img2=cpl_imagelist_get(c2,np);
764 p2data=cpl_image_get_data_float(img2);
765 img3=cpl_imagelist_get(c3,np);
766 p3data=cpl_image_get_data_float(img3);
767 for (i=0 ; i< (ulong32)ilx1*ilx2 ; i++)
769 p3data[i] = p1data[i] * p2data[i] ;
801 cpl_image* i_img=NULL;
802 cpl_image* img1=NULL;
803 cpl_image* img2=NULL;
804 cpl_image* img3=NULL;
810 inp1=cpl_imagelist_get_size(c1);
811 i_img=cpl_imagelist_get(c1,0);
812 ilx1=cpl_image_get_size_x(i_img);
813 ily1=cpl_image_get_size_y(i_img);
816 inp2=cpl_imagelist_get_size(c2);
817 i_img=cpl_imagelist_get(c2,0);
818 ilx2=cpl_image_get_size_x(i_img);
819 ily2=cpl_image_get_size_y(i_img);
821 if ((ilx1 != ilx2) ||
828 if ((inp2 != inp1) && (inp2 != 1))
834 if (NULL == (c3 = cpl_imagelist_new()) )
840 for (np=0 ; np < inp1 ; np++)
842 img3=cpl_image_new(ilx1,ily1,CPL_TYPE_FLOAT);
843 cpl_imagelist_set(c3,img3,np);
846 for (np=0 ; np < inp1 ; np++)
848 img1=cpl_imagelist_get(c1,np);
849 p1data=cpl_image_get_data_float(img1);
850 img2=cpl_imagelist_get(c2,np);
851 p2data=cpl_image_get_data_float(img2);
852 img3=cpl_imagelist_get(c3,np);
853 p3data=cpl_image_get_data_float(img3);
856 for (i=0 ; i< (ulong32) ilx1*ily1 ; i++)
858 if (fabs((
double)p2data[i]) < 1e-10)
864 p3data[i] = p1data[i] / p2data[i] ;
882 sinfo_new_add_image_to_cube(cpl_imagelist * cu, cpl_image * im)
884 cpl_imagelist * cube ;
893 cpl_image* i_img=NULL;
895 if (cu==NULL || im==NULL)
900 cnp=cpl_imagelist_get_size(cu);
901 i_img=cpl_imagelist_get(cu,0);
902 clx=cpl_image_get_size_x(i_img);
903 cly=cpl_image_get_size_y(i_img);
905 ilx=cpl_image_get_size_x(im);
906 ily=cpl_image_get_size_y(im);
908 if ((clx != ilx) || (cly != ily))
914 cube = cpl_imagelist_duplicate (cu) ;
916 for (i=0 ; i<cnp ; i++)
921 cpl_image_add(cpl_imagelist_get(cube,i), im) ;
935 sinfo_new_sub_image_from_cube (cpl_imagelist * cu, cpl_image * im)
937 cpl_imagelist * cube ;
946 cpl_image* i_img=NULL;
948 if (cu==NULL || im==NULL)
953 cnp=cpl_imagelist_get_size(cu);
954 i_img=cpl_imagelist_get(cu,0);
955 clx=cpl_image_get_size_x(i_img);
956 cly=cpl_image_get_size_y(i_img);
958 ilx=cpl_image_get_size_x(im);
959 ily=cpl_image_get_size_y(im);
961 if ((clx != ilx) || (cly != ily))
964 sinfo_msg_error(
"incompatible size: cannot subtract image from cube") ;
968 cube = cpl_imagelist_duplicate (cu) ;
970 for (i=0 ; i<cnp ; i++)
975 cpl_image_subtract(cpl_imagelist_get(cube,i), im) ;
988 sinfo_new_mul_image_to_cube(cpl_imagelist * cu, cpl_image * im)
990 cpl_imagelist * cube ;
999 cpl_image* i_img=NULL;
1001 if (cu==NULL || im==NULL)
1006 cnp=cpl_imagelist_get_size(cu);
1007 i_img=cpl_imagelist_get(cu,0);
1008 clx=cpl_image_get_size_x(i_img);
1009 cly=cpl_image_get_size_y(i_img);
1011 ilx=cpl_image_get_size_x(im);
1012 ily=cpl_image_get_size_y(im);
1014 if ((clx != ilx) || (cly != ily))
1020 cube = cpl_imagelist_duplicate (cu) ;
1022 for (i=0 ; i<cnp ; i++)
1027 cpl_image_multiply(cpl_imagelist_get(cube,i), im) ;
1041 sinfo_new_div_cube_by_image(cpl_imagelist * cu, cpl_image * im)
1043 cpl_imagelist * cube ;
1052 cpl_image* i_img=NULL;
1054 if (cu==NULL || im==NULL)
1059 cnp=cpl_imagelist_get_size(cu);
1060 i_img=cpl_imagelist_get(cu,0);
1061 clx=cpl_image_get_size_x(i_img);
1062 cly=cpl_image_get_size_y(i_img);
1064 ilx=cpl_image_get_size_x(im);
1065 ily=cpl_image_get_size_y(im);
1067 if ((clx != ilx) || (cly != ily))
1073 cube = cpl_imagelist_duplicate (cu) ;
1075 for (i=0 ; i<cnp ; i++)
1080 cpl_image_divide(cpl_imagelist_get(cube,i), im) ;
1096 sinfo_new_add_spectrum_to_cube(cpl_imagelist *cu, Vector *spec)
1098 cpl_imagelist * cube ;
1105 cpl_image* i_img=NULL;
1106 cpl_image* o_img=NULL;
1108 if (cu == NULL || spec == NULL)
1113 inp=cpl_imagelist_get_size(cu);
1114 i_img=cpl_imagelist_get(cu,0);
1115 ilx=cpl_image_get_size_x(i_img);
1116 ily=cpl_image_get_size_y(i_img);
1118 if ( inp != spec -> n_elements )
1120 sinfo_msg_error(
"cube length and spectrum length are not compatible") ;
1124 if ( NULL == (cube = cpl_imagelist_new ()) )
1129 for ( i = 0; i < inp; i++)
1131 o_img=cpl_image_new(ilx,ily,CPL_TYPE_FLOAT);
1132 cpl_imagelist_set(cube,o_img,i);
1136 for ( i = 0; i < inp; i++)
1138 i_img=cpl_imagelist_get(cu,i);
1139 pidata=cpl_image_get_data_float(i_img);
1140 o_img=cpl_imagelist_get(cube,i);
1141 podata=cpl_image_get_data_float(o_img);
1142 for ( j = 0; j < (int) ilx*ily; j++)
1144 podata[j] = pidata[j] + spec -> data[i] ;
1160 sinfo_new_sub_spectrum_from_cube(cpl_imagelist *cu, Vector *spec)
1162 cpl_imagelist * cube ;
1169 cpl_image* i_img=NULL;
1170 cpl_image* o_img=NULL;
1172 if (cu == NULL || spec == NULL)
1177 inp=cpl_imagelist_get_size(cu);
1178 i_img=cpl_imagelist_get(cu,0);
1179 ilx=cpl_image_get_size_x(i_img);
1180 ily=cpl_image_get_size_y(i_img);
1182 if ( inp != spec -> n_elements )
1184 sinfo_msg_error(
"cube length and spectrum length are not compatible") ;
1188 if ( NULL == (cube = cpl_imagelist_new()) )
1193 for ( i = 0; i < inp; i++)
1195 o_img=cpl_image_new(ilx,ily,CPL_TYPE_FLOAT);
1196 cpl_imagelist_set(cube,o_img,i);
1199 for ( i = 0; i < inp; i++)
1201 i_img=cpl_imagelist_get(cu,i);
1202 pidata=cpl_image_get_data_float(i_img);
1203 o_img=cpl_imagelist_get(cube,i);
1204 podata=cpl_image_get_data_float(o_img);
1205 for ( j = 0; j < (int) ilx*ily; j++)
1207 if ( isnan(pidata[j]) || isnan(spec -> data[i]) )
1213 podata[j] = pidata[j] - spec -> data[i] ;
1231 sinfo_new_mul_spectrum_to_cube(cpl_imagelist *cu, Vector *spec)
1233 cpl_imagelist * cube ;
1240 cpl_image* i_img=NULL;
1241 cpl_image* o_img=NULL;
1243 if (cu == NULL || spec == NULL)
1248 inp=cpl_imagelist_get_size(cu);
1249 i_img=cpl_imagelist_get(cu,0);
1250 ilx=cpl_image_get_size_x(i_img);
1251 ily=cpl_image_get_size_y(i_img);
1253 if ( inp != spec -> n_elements )
1255 sinfo_msg_error(
"cube length and spectrum length are not compatible") ;
1259 if ( NULL == (cube = cpl_imagelist_new ()) )
1265 for ( i = 0; i < inp; i++)
1267 o_img=cpl_image_new(ilx,ily,CPL_TYPE_FLOAT);
1268 cpl_imagelist_set(cube,o_img,i);
1271 for ( i = 0; i < inp; i++)
1273 i_img=cpl_imagelist_get(cu,i);
1274 pidata=cpl_image_get_data_float(i_img);
1275 o_img=cpl_imagelist_get(cube,i);
1276 podata=cpl_image_get_data_float(o_img);
1277 for ( j = 0; j < (int) ilx*ily; j++)
1279 if ( isnan(pidata[j]) || isnan(spec->data[i]) )
1285 podata[j] = pidata[j] * spec -> data[i] ;
1303 sinfo_new_div_cube_by_spectrum(cpl_imagelist *cu, Vector *spec)
1305 cpl_imagelist * cube ;
1313 cpl_image* i_img=NULL;
1314 cpl_image* o_img=NULL;
1316 if (cu == NULL || spec == NULL)
1321 inp=cpl_imagelist_get_size(cu);
1322 i_img=cpl_imagelist_get(cu,0);
1323 ilx=cpl_image_get_size_x(i_img);
1324 ily=cpl_image_get_size_y(i_img);
1326 if ( inp != spec -> n_elements )
1328 sinfo_msg_error(
"cube length and spectrum length are not compatible") ;
1332 if (NULL == (cube = cpl_imagelist_new ()) )
1338 for ( i = 0; i < inp; i++)
1340 o_img=cpl_image_new(ilx,ily,CPL_TYPE_FLOAT);
1341 cpl_imagelist_set(cube,o_img,i);
1344 for ( i = 0; i < inp; i++)
1347 i_img=cpl_imagelist_get(cu,i);
1348 pidata=cpl_image_get_data_float(i_img);
1349 o_img=cpl_imagelist_get(cube,i);
1350 podata=cpl_image_get_data_float(o_img);
1351 for ( j = 0; j < (int) ilx*ily; j++)
1353 if (!isnan(spec->data[i]) && spec->data[i] != 0.)
1355 help = 1/spec->data[i] ;
1356 if ( help > THRESH )
1366 if ( isnan(help) || isnan(pidata[j]) )
1372 podata[j] = pidata[j] * help ;
1394 sinfo_new_clean_mean_of_spectra(cpl_imagelist * cube,
1403 pixelvalue *local_rectangle ;
1405 int recsize, lo_n, hi_n, nv ;
1410 cpl_image* i_img=NULL;
1412 if ( cube == NULL || cpl_imagelist_get_size(cube) < 1 )
1417 inp=cpl_imagelist_get_size(cube);
1418 i_img=cpl_imagelist_get(cube,0);
1419 ilx=cpl_image_get_size_x(i_img);
1420 ily=cpl_image_get_size_y(i_img);
1422 if ((llx<1) || (llx>ilx) ||
1423 (urx<1) || (urx>ilx) ||
1424 (lly<1) || (lly>ily) ||
1425 (ury<1) || (ury>ily) ||
1426 (llx>=urx) || (lly>=ury))
1430 llx, lly, urx, ury) ;
1434 if ((lo_reject + hi_reject) > 0.9)
1437 lo_reject, hi_reject) ;
1439 " aborting average") ;
1449 recsize = (urx - llx + 1) * (ury - lly + 1) ;
1451 lo_n = (int) (recsize * lo_reject + 0.5) ;
1452 hi_n = (int) (recsize * hi_reject + 0.5) ;
1454 if (lo_n + hi_n >= recsize)
1461 if (NULL == (mean = sinfo_new_vector (inp)) )
1471 for ( i = 0 ; i < inp ; i++ )
1473 i_img=cpl_imagelist_get(cube,i);
1474 pidata=cpl_image_get_data_float(i_img);
1476 local_rectangle=(pixelvalue *)cpl_calloc(recsize,
sizeof (pixelvalue*));
1478 for ( j = lly ; j <= ury ; j++ )
1480 for ( k = llx ; k <= urx ; k++ )
1482 local_rectangle[m] = pidata[k + j * ilx] ;
1487 sinfo_pixel_qsort (local_rectangle, recsize) ;
1490 for ( l = lo_n ; l < (recsize - hi_n) ; l++ )
1492 mean -> data[i] += local_rectangle[l] ;
1495 mean -> data[i] /= nv ;
1497 cpl_free ( local_rectangle ) ;
1514 sinfo_new_median_cube(cpl_imagelist * cube)
1517 pixelvalue * buffer ;
1524 cpl_image* i_img=NULL;
1531 inp=cpl_imagelist_get_size(cube);
1532 i_img=cpl_imagelist_get(cube,0);
1533 ilx=cpl_image_get_size_x(i_img);
1534 ily=cpl_image_get_size_y(i_img);
1537 if (NULL == (im = cpl_image_new (ilx, ily, CPL_TYPE_FLOAT )) )
1548 podata=cpl_image_get_data_float(im);
1549 for ( i = 0 ; i < (int) ilx*ily ; i++ )
1551 buffer = (pixelvalue *) cpl_calloc (inp,
sizeof (pixelvalue *));
1553 for ( j = 0 ; j < inp ; j ++ )
1555 i_img=cpl_imagelist_get(cube,j);
1556 pidata=cpl_image_get_data_float(i_img);
1557 if ( !isnan(pidata[i]) )
1559 buffer[k] = pidata[i] ;
1568 podata[i] = sinfo_new_median ( buffer, nz ) ;
1572 podata[i] = (buffer[0] + buffer[1]) / 2. ;
1576 podata[i] = buffer[0] ;
1583 cpl_free ( buffer ) ;
1601 sinfo_new_average_cube_to_image(cpl_imagelist * cube)
1610 cpl_image* i_img=NULL;
1617 inp=cpl_imagelist_get_size(cube);
1618 i_img=cpl_imagelist_get(cube,0);
1619 ilx=cpl_image_get_size_x(i_img);
1620 ily=cpl_image_get_size_y(i_img);
1623 if (NULL == (im = cpl_image_new (ilx, ily,CPL_TYPE_FLOAT )) )
1634 podata=cpl_image_get_data_float(im);
1635 for ( i = 0 ; i < (int) ilx*ily ; i++ )
1638 for ( j = 0 ; j < inp ; j ++ )
1640 i_img=cpl_imagelist_get(cube,j);
1641 pidata=cpl_image_get_data_float(i_img);
1642 if ( !isnan(pidata[i]) )
1645 podata[i] += pidata[i] ;
1674 sinfo_new_sum_cube_to_image(cpl_imagelist * cube)
1683 cpl_image* i_img=NULL;
1690 inp=cpl_imagelist_get_size(cube);
1691 i_img=cpl_imagelist_get(cube,0);
1692 ilx=cpl_image_get_size_x(i_img);
1693 ily=cpl_image_get_size_y(i_img);
1696 if (NULL == (im = cpl_image_new (ilx, ily, CPL_TYPE_FLOAT )) )
1707 podata=cpl_image_get_data_float(im);
1708 for ( i = 0 ; i < (int) ilx*ily ; i++ )
1711 for ( j = 0 ; j < inp ; j ++ )
1713 i_img=cpl_imagelist_get(cube,j);
1714 pidata=cpl_image_get_data_float(i_img);
1715 if ( !isnan(pidata[i]) )
1718 podata[i] += pidata[i] ;
1753 sinfo_new_average_cube_to_image_between_waves (cpl_imagelist * cube,
1756 float initialLambda,
1769 cpl_image* i_img=NULL;
1776 i_img=cpl_imagelist_get(cube,0);
1777 ilx=cpl_image_get_size_x(i_img);
1778 ily=cpl_image_get_size_y(i_img);
1780 inp=cpl_imagelist_get_size(cube);
1782 minWave = centralWave - (float) (inp / 2)*dispersion ;
1784 if ( dispersion <= 0. || minWave <= 0. )
1790 if ( initialLambda < minWave ||
1791 (initialLambda >= minWave + dispersion * inp) )
1797 if ( finalLambda <= minWave ||
1798 (finalLambda > minWave + dispersion * inp) )
1805 if (NULL == (im = cpl_image_new (ilx, ily, CPL_TYPE_FLOAT )) )
1812 firstPlane = sinfo_new_nint ((
double) ((initialLambda - minWave) /
1814 lastPlane = sinfo_new_nint ((
double) ((finalLambda - minWave) /
1817 if ( firstPlane < 0 || firstPlane >= inp ||
1818 lastPlane < 0 || lastPlane > inp )
1831 podata=cpl_image_get_data_float(im);
1832 for ( i = 0 ; i < (int) ilx*ily ; i++ )
1836 for ( j = firstPlane ; j <= lastPlane ; j ++ )
1838 i_img=cpl_imagelist_get(cube,j);
1839 pidata=cpl_image_get_data_float(i_img);
1840 if ( !isnan(pidata[i]) )
1843 podata[i] += pidata[i] ;
1869 sinfo_new_extract_image_from_cube(cpl_imagelist * cube,
int plane_index)
1877 if ( plane_index < 0 || plane_index >= cpl_imagelist_get_size(cube) )
1883 return cpl_imagelist_get(cube,plane_index) ;
1895 sinfo_new_extract_spectrum_from_cube(cpl_imagelist * cube,
1896 int x_pos,
int y_pos)
1898 Vector * returnedSpectrum ;
1904 cpl_image* i_img=NULL;
1911 i_img=cpl_imagelist_get(cube,0);
1912 ilx=cpl_image_get_size_x(i_img);
1913 ily=cpl_image_get_size_y(i_img);
1914 inp=cpl_imagelist_get_size(cube);
1916 if ( x_pos < 0 || x_pos >= ilx )
1922 if ( y_pos < 0 || y_pos >= ily )
1929 if ( NULL == (returnedSpectrum = sinfo_new_vector ( inp )) )
1935 for ( i = 0 ; i < inp ; i++ )
1937 i_img=cpl_imagelist_get(cube,i);
1938 pidata=cpl_image_get_data_float(i_img);
1939 returnedSpectrum -> data[i] = pidata[x_pos + ilx*y_pos] ;
1942 return returnedSpectrum ;
1985 sinfo_new_combine_jittered_cubes ( cpl_imagelist ** cubes,
1986 cpl_imagelist * mergedCube,
1991 char * kernel_type )
2003 cpl_imagelist * mask=NULL;
2004 double * kernel=NULL;
2010 float* sub_offsetx=NULL ;
2011 float* sub_offsety=NULL ;
2013 cpl_imagelist ** tmpcubes=NULL ;
2014 pixelvalue * tmpspace=NULL;
2031 cpl_image* i_img=NULL;
2032 cpl_image* o_img=NULL;
2033 cpl_image* m_img=NULL;
2034 cpl_image* t_img=NULL;
2037 if ( cubes == NULL )
2047 if ( cumoffsetx == NULL || cumoffsety == NULL )
2052 if ( exptimes == NULL )
2058 o_img=cpl_imagelist_get(mergedCube,0);
2059 olx=cpl_image_get_size_x(o_img);
2060 oly=cpl_image_get_size_y(o_img);
2061 onp=cpl_imagelist_get_size(mergedCube);
2062 if ( NULL == (mask = cpl_imagelist_new()) )
2068 o_img=cpl_image_new(olx,oly,CPL_TYPE_FLOAT);
2069 cpl_imagelist_set(mergedCube,o_img,i);
2072 i_img=cpl_imagelist_get(cubes[0],0);
2073 ilx=cpl_image_get_size_x(i_img);
2074 ily=cpl_image_get_size_y(i_img);
2076 inp=cpl_imagelist_get_size(cubes[0]);
2084 llx0 = olx/2 - ilx/2 ;
2085 lly0 = oly/2 - ily/2 ;
2093 llx=cpl_calloc(n_cubes,
sizeof(
int)); ;
2094 lly=cpl_calloc(n_cubes,
sizeof(
int)) ;
2096 sub_offsetx=cpl_calloc(n_cubes,
sizeof(
float)) ;
2097 sub_offsety=cpl_calloc(n_cubes,
sizeof(
float)) ;
2099 for ( i = 0 ; i < n_cubes ; i++ )
2101 llx[i] = llx0 - sinfo_new_nint(cumoffsetx[i]) ;
2102 sub_offsetx[i] = (float)sinfo_new_nint(cumoffsetx[i]) - cumoffsetx[i] ;
2103 lly[i] = lly0 - sinfo_new_nint(cumoffsety[i]) ;
2104 sub_offsety[i] = (float)sinfo_new_nint(cumoffsety[i]) - cumoffsety[i] ;
2113 if ( NULL == (kernel = sinfo_generate_interpolation_kernel(kernel_type)))
2116 " or no kernel_typ was given, the default kernel"
2122 tmpcubes=(cpl_imagelist**)cpl_calloc(n_cubes,
sizeof(cpl_imagelist*)) ;
2124 for ( i = 0 ; i < n_cubes ; i++ )
2126 tmpspace = cpl_calloc(ilx, ily*
sizeof(pixelvalue)) ;
2127 tmpcubes[i] = cpl_imagelist_new();
2129 for ( z = 0 ; z < inp ; z++ )
2133 t_img=sinfo_new_shift_image(cpl_imagelist_get(cubes[i],z),
2134 sub_offsetx[i], sub_offsety[i], kernel);
2139 " in cube no %d!", z, i) ;
2140 cpl_imagelist_delete(mergedCube) ;
2141 cpl_imagelist_delete(mask) ;
2145 cpl_imagelist_set(tmpcubes[i],t_img,z);
2157 for ( i = 0 ; i < n_cubes ; i++ )
2161 for ( y = 0 ; y < oly ; y++ )
2163 for ( x = 0 ; x < olx ; x++ )
2167 if ( y >= lly[i] && y < lly[i]+ily &&
2168 x >= llx[i] && x < llx[i]+ilx )
2172 for ( z = 0 ; z < onp ; z++ )
2174 t_img=cpl_imagelist_get(tmpcubes[i],z);
2175 ptdata=cpl_image_get_data_float(t_img);
2176 m_img=cpl_imagelist_get(mask,z);
2177 pmdata=cpl_image_get_data_float(m_img);
2178 if (!isnan(ptdata[posx+posy*ilx]) &&
2179 ptdata[posx+posy*ilx] != 0.)
2181 pmdata[x+y*mlx] += exptimes[i] ;
2197 for ( i = 0 ; i < n_cubes ; i++ )
2201 for ( y = 0 ; y < oly ; y++ )
2204 for ( x = 0 ; x < olx ; x++ )
2209 if ( y >= lly[i] && y < lly[i]+ily &&
2210 x >= llx[i] && x < llx[i]+ilx )
2215 for ( z = 0 ; z < onp ; z++ )
2218 t_img=cpl_imagelist_get(tmpcubes[i],z);
2219 ptdata=cpl_image_get_data_float(t_img);
2220 m_img=cpl_imagelist_get(mask,z);
2221 pmdata=cpl_image_get_data_float(m_img);
2222 mlx=cpl_image_get_size_x(m_img);
2224 o_img=cpl_imagelist_get(mergedCube,z);
2225 podata=cpl_image_get_data_float(o_img);
2227 if (!isnan(ptdata[posx+posy*ilx]))
2229 if (pmdata[x+y*mlx] != 0.)
2233 weight = exptimes[0] / pmdata[x+y*mlx] ;
2240 weight*ptdata[posx+posy*ilx] ;
2254 for( i = 0 ; i < n_cubes ; i++ )
2256 cpl_imagelist_delete (tmpcubes[i]) ;
2259 cpl_free(tmpcubes); ;
2263 cpl_free(sub_offsetx) ;
2264 cpl_free(sub_offsety) ;
2306 sinfo_build_mask_cube(
const int z_min,
2314 cpl_imagelist** cubes,
2315 cpl_imagelist** tmpcubes,
2316 cpl_imagelist* mask)
2324 cpl_image* i_img=NULL;
2325 cpl_image* t_img=NULL;
2333 cpl_image* m_img=NULL;
2336 for ( z = z_min, m=0 ; z < z_max ; z++, m++ ) {
2339 for ( y = 0 ; y < oly ; y++ ) {
2340 for ( x = 0 ; x < olx ; x++ ) {
2341 for ( i = 0 ; i < n_cubes ; i++ ) {
2343 i_img=cpl_imagelist_get(cubes[i],0);
2344 ilx=cpl_image_get_size_x(i_img);
2345 ily=cpl_image_get_size_y(i_img);
2350 if ( y >= lly[i] && y < lly[i]+ily &&
2351 x >= llx[i] && x < llx[i]+ilx )
2357 t_img=cpl_imagelist_get(tmpcubes[i],m);
2358 ptdata=cpl_image_get_data_float(t_img);
2359 m_img=cpl_imagelist_get(mask,z);
2360 pmdata=cpl_image_get_data_float(m_img);
2361 mlx=cpl_image_get_size_x(m_img);
2363 if (!isnan(ptdata[posx+posy*ilx]) &&
2364 ptdata[posx+posy*ilx] != 0.)
2366 pmdata[x+y*mlx] += (float)exptimes[i] ;
2367 }
else if (isnan(ptdata[posx+posy*ilx])) {
2369 }
else if (ptdata[posx+posy*ilx] == 0.) {
2388 sinfo_build_mask_cube_thomas(
const int z_min,
2396 cpl_imagelist** cubes,
2397 cpl_imagelist** tmpcubes,
2398 cpl_imagelist* mask)
2407 cpl_image* i_img=NULL;
2408 cpl_image* t_img=NULL;
2416 cpl_image* m_img=NULL;
2418 for ( i = 0 ; i < n_cubes ; i++ ) {
2420 i_img=cpl_imagelist_get(cubes[i],0);
2421 ilx=cpl_image_get_size_x(i_img);
2422 ily=cpl_image_get_size_y(i_img);
2426 for ( y = 0 ; y < oly ; y++ ){
2427 for ( x = 0 ; x < olx ; x++ ){
2430 if ( y >= lly[i] && y < lly[i]+ily &&
2431 x >= llx[i] && x < llx[i]+ilx ) {
2435 for ( z = z_min,m=0 ; z < z_max ; z++,m++ ) {
2436 t_img=cpl_imagelist_get(tmpcubes[i],m);
2437 ptdata=cpl_image_get_data_float(t_img);
2438 m_img=cpl_imagelist_get(mask,z);
2439 pmdata=cpl_image_get_data_float(m_img);
2440 mlx=cpl_image_get_size_x(m_img);
2442 if (!isnan(ptdata[posx+posy*ilx]) &&
2443 ptdata[posx+posy*ilx] != 0.) {
2444 pmdata[x+y*mlx] += (float)exptimes[i] ;
2509 sinfo_new_combine_jittered_cubes_range ( cpl_imagelist ** cubes,
2510 cpl_imagelist * mergedCube,
2511 cpl_imagelist * mask,
2517 const int z_min,
const int z_max )
2522 cpl_imagelist ** tmpcubes=NULL ;
2525 float* sub_offsetx=NULL ;
2526 float* sub_offsety=NULL ;
2535 cpl_image* i_img=NULL;
2536 cpl_image* o_img=NULL;
2539 if(sinfo_check_input(cubes,n_cubes,cumoffsetx,cumoffsety,exptimes) == -1) {
2543 o_img=cpl_imagelist_get(mergedCube,z_min);
2544 olx=cpl_image_get_size_x(o_img);
2545 oly=cpl_image_get_size_y(o_img);
2546 i_img=cpl_imagelist_get(cubes[0],0);
2547 ilx=cpl_image_get_size_x(i_img);
2548 ily=cpl_image_get_size_y(i_img);
2559 llx0 = olx/2 - ilx/2 ;
2560 lly0 = oly/2 - ily/2 ;
2569 llx=cpl_calloc(n_cubes,
sizeof(
int)) ;
2570 lly=cpl_calloc(n_cubes,
sizeof(
int)) ;
2571 sub_offsetx=cpl_calloc(n_cubes,
sizeof(
float)) ;
2572 sub_offsety=cpl_calloc(n_cubes,
sizeof(
float)) ;
2574 for ( i = 0 ; i < n_cubes ; i++ )
2576 llx[i] = llx0 - sinfo_new_nint(cumoffsetx[i]) ;
2577 sub_offsetx[i] = (float)sinfo_new_nint(cumoffsetx[i]) - cumoffsetx[i] ;
2578 lly[i] = lly0 - sinfo_new_nint(cumoffsety[i]) ;
2579 sub_offsety[i] = (float)sinfo_new_nint(cumoffsety[i]) - cumoffsety[i] ;
2582 tmpcubes=(cpl_imagelist**)cpl_calloc(n_cubes,
sizeof(cpl_imagelist*)) ;
2588 if(sinfo_shift_cubes(tmpcubes,kernel_type,n_cubes,cubes,z_min, z_max,
2589 sub_offsetx,sub_offsety,mlx,mly,mask) == -1) {
2602 sinfo_build_mask_cube(z_min,z_max,olx,oly,n_cubes,llx,lly,exptimes,
2603 cubes,tmpcubes,mask);
2609 sinfo_compute_weight_average(z_min,z_max,ilx,ily,n_cubes,mergedCube,mask,
2610 tmpcubes,exptimes,llx,lly);
2615 for( i = 0 ; i < n_cubes ; i++ )
2617 cpl_imagelist_delete (tmpcubes[i]) ;
2621 cpl_free(tmpcubes) ;
2624 cpl_free(sub_offsetx) ;
2625 cpl_free(sub_offsety) ;
2648 sinfo_check_input(cpl_imagelist** cubes,
2654 if ( cubes == NULL )
2664 if ( cumoffsetx == NULL || cumoffsety == NULL )
2669 if ( exptimes == NULL )
2704 sinfo_compute_weight_average(
const int z_min,
2709 cpl_imagelist* mergedCube,
2710 cpl_imagelist* mask,
2711 cpl_imagelist** tmpcubes,
2728 cpl_image* o_img=NULL;
2729 cpl_image* m_img=NULL;
2730 cpl_image* t_img=NULL;
2741 o_img=cpl_imagelist_get(mergedCube,z_min);
2742 olx=cpl_image_get_size_x(o_img);
2743 oly=cpl_image_get_size_y(o_img);
2749 for ( z = z_min, m = 0 ; z < z_max ; z++, m++ ) {
2750 o_img=cpl_imagelist_get(mergedCube,z);
2751 podata=cpl_image_get_data_float(o_img);
2752 m_img=cpl_imagelist_get(mask,z);
2753 pmdata=cpl_image_get_data_float(m_img);
2754 mlx=cpl_image_get_size_x(m_img);
2757 for ( y = 0 ; y < oly ; y++ ) {
2758 for ( x = 0 ; x < olx ; x++ ) {
2763 for ( i = 0 ; i < n_cubes ; i++ ) {
2765 if ( y >= lly[i] && y < lly[i]+ily &&
2766 x >= llx[i] && x < llx[i]+ilx ) {
2770 t_img=cpl_imagelist_get(tmpcubes[i],m);
2771 ptdata=cpl_image_get_data_float(t_img);
2775 if (!isnan(ptdata[posx+posy*ilx])) {
2776 if (pmdata[x+y*mlx] != 0.) {
2779 weight = exptimes[0] / pmdata[x+y*mlx] ;
2783 podata[x+y*olx] += weight*ptdata[posx+posy*ilx] ;
2835 sinfo_shift_cubes(cpl_imagelist** tmpcubes,
2838 cpl_imagelist** cubes,
2845 cpl_imagelist* mask)
2850 cpl_image* i_img=NULL;
2854 pixelvalue * tmpspace;
2856 cpl_image* t_img=NULL;
2857 cpl_image* m_img=NULL;
2866 if ( NULL == (kernel = sinfo_generate_interpolation_kernel(kernel_type)) )
2869 "or no kernel_typ was given, the default kernel"
2874 for ( i = 0 ; i < n_cubes ; i++ )
2877 i_img=cpl_imagelist_get(cubes[i],0);
2878 ilx=cpl_image_get_size_x(i_img);
2879 ily=cpl_image_get_size_y(i_img);
2881 tmpspace = cpl_calloc(ilx, ily*
sizeof(pixelvalue)) ;
2882 tmpcubes[i]=cpl_imagelist_new();
2884 for ( z = z_min, m=0 ; z < z_max ; z++, m++ )
2886 t_img=sinfo_new_shift_image(cpl_imagelist_get(cubes[i],z),
2894 "in cube no %d!", z, i) ;
2899 cpl_imagelist_set(tmpcubes[i],t_img,m);
2900 m_img=cpl_image_new(mlx,mly,CPL_TYPE_FLOAT);
2901 cpl_imagelist_set(mask,m_img,z);
2907 if(kernel != NULL) cpl_free(kernel) ;
3114 sinfo_new_combine_jittered_cubes_thomas_range(cpl_imagelist ** cubes,
3115 cpl_imagelist * mergedCube,
3116 cpl_imagelist * mask,
3124 const double kappa )
3126 const int VERY_BIG_INT = 268431360;
3131 float* sub_offsetx=NULL ;
3132 float* sub_offsety=NULL ;
3133 cpl_imagelist ** tmpcubes=NULL ;
3134 const int z_siz=z_max-z_min;
3142 cpl_image* i_img=NULL;
3143 cpl_image* o_img=NULL;
3144 int min_lx = VERY_BIG_INT;
3145 int min_ly = VERY_BIG_INT;
3148 if(sinfo_check_input(cubes,n_cubes,cumoffsetx,cumoffsety,exptimes) == -1) {
3157 i_img=cpl_imagelist_get(cubes[0],0);
3158 o_img=cpl_imagelist_get(mergedCube,0);
3159 ilx=cpl_image_get_size_x(i_img);
3160 ily=cpl_image_get_size_y(i_img);
3161 olx=cpl_image_get_size_x(o_img);
3162 oly=cpl_image_get_size_y(o_img);
3172 llx0 = (1.0 * olx- 1.0 * ilx)/2.0 ;
3173 lly0 = (1.0 * oly - 1.0 * ily)/2.0 ;
3181 llx=cpl_calloc(n_cubes,
sizeof(
int));
3182 lly=cpl_calloc(n_cubes,
sizeof(
int)) ;
3183 sub_offsetx=cpl_calloc(n_cubes,
sizeof(
float)) ;
3184 sub_offsety=cpl_calloc(n_cubes,
sizeof(
float)) ;
3186 for ( i = 0 ; i < n_cubes ; i++ ) {
3187 llx[i] = llx0 - sinfo_new_nint(cumoffsetx[i]) ;
3189 sub_offsetx[i] = (float)sinfo_new_nint(cumoffsetx[i]) - cumoffsetx[i] ;
3190 lly[i] = lly0 - sinfo_new_nint(cumoffsety[i]) ;
3191 sub_offsety[i] = (float)sinfo_new_nint(cumoffsety[i]) - cumoffsety[i] ;
3195 if (llx[i] < min_lx)
3199 if (lly[i] < min_ly)
3209 for (i = 0 ; i < n_cubes ; i++ )
3211 llx[i] = llx[i] - min_lx;
3216 for (i = 0 ; i < n_cubes ; i++ )
3218 lly[i] = lly[i] - min_ly;
3228 tmpcubes=(cpl_imagelist**)cpl_calloc(n_cubes,
sizeof(cpl_imagelist*)) ;
3230 if(sinfo_shift_cubes(tmpcubes,kernel_type,n_cubes,cubes,z_min, z_max,
3231 sub_offsetx,sub_offsety,mlx,mly,mask) == -1) {
3246 o_img=cpl_imagelist_get(mergedCube,0);
3247 olx=cpl_image_get_size_x(o_img);
3248 oly=cpl_image_get_size_y(o_img);
3251 if(-1 == sinfo_build_mask_cube_thomas(z_min,z_max,olx,oly,n_cubes,llx,lly,
3252 exptimes,cubes,tmpcubes,mask) ) {
3263 check_nomsg(sinfo_coadd_with_ks_clip2(z_min,z_max,ilx,ily,n_cubes,kappa,llx,lly,
3264 exptimes,mask,mergedCube,tmpcubes));
3274 for( i = 0 ; i < n_cubes ; i++ ) {
3275 cpl_imagelist_delete (tmpcubes[i]) ;
3281 cpl_free(sub_offsetx) ;
3282 cpl_free(sub_offsety) ;
3283 sinfo_print_rec_status(0);
3302 sinfo_new_interpol_cube_simple( cpl_imagelist * cube,
3303 cpl_imagelist * badcube,
3306 cpl_imagelist * intercube ;
3307 float* goodNeighbors=NULL ;
3311 int zi, coli, rowi ;
3327 float* pbzidata=NULL;
3328 float* pczidata=NULL;
3330 cpl_image* c_img=NULL;
3331 cpl_image* b_img=NULL;
3332 cpl_image* i_img=NULL;
3334 cpl_image* bzi_img=NULL;
3335 cpl_image* czi_img=NULL;
3339 if ( cube == NULL || badcube == NULL )
3349 intercube = cpl_imagelist_duplicate(cube) ;
3351 goodNeighbors=cpl_calloc((2*maxdist+1)*(2*maxdist+1)*(2*maxdist+1) -1,
3354 cnp=cpl_imagelist_get_size(cube);
3355 for ( z = 0 ; z < cnp ; z++ )
3357 b_img=cpl_imagelist_get(badcube,z);
3358 i_img=cpl_imagelist_get(intercube,z);
3359 pbdata=cpl_image_get_data_float(b_img);
3360 pidata=cpl_image_get_data_float(i_img);
3361 blx=cpl_image_get_size_x(b_img);
3364 c_img=cpl_imagelist_get(cube,z);
3365 clx=cpl_image_get_size_x(c_img);
3366 cly=cpl_image_get_size_y(c_img);
3368 for ( row = 0 ; row < cly ; row++ )
3370 for ( col = 0 ; col < clx ; col++ )
3372 if ( pbdata[col+row*clx] == 0 )
3375 llx = col - maxdist ;
3382 if ( llx + nx > clx )
3384 nx -= (llx + nx - clx) ;
3387 lly = row - maxdist ;
3394 if ( lly + ny > cly )
3396 ny -= (lly + ny - cly) ;
3406 if ( llz + nz > cnp )
3408 nz -= (llz + nz - cnp) ;
3411 for ( zi = llz ; zi < llz+nz ; zi++ )
3413 bzi_img=cpl_imagelist_get(badcube,zi);
3414 czi_img=cpl_imagelist_get(cube,zi);
3415 pbzidata=cpl_image_get_data_float(bzi_img);
3416 pczidata=cpl_image_get_data_float(czi_img);
3418 for ( rowi = lly ; rowi < lly+ny ; rowi++ )
3420 for ( coli = llx ; coli < llx+nx ; coli++ )
3422 if ( pbzidata[coli+rowi*blx] == 1 )
3424 goodNeighbors[n] = pczidata[coli+rowi*clx] ;
3432 pidata[col+row*clx]=sinfo_new_median(goodNeighbors,n);
3433 pbdata[col+row*clx]=1 ;
3443 cpl_free(goodNeighbors) ;
3504 sinfo_new_combine_cubes ( cpl_imagelist ** cubes,
3505 cpl_imagelist * mergedCube,
3510 char * kernel_type )
3520 cpl_imagelist * mask=NULL ;
3521 double * kernel=NULL ;
3522 cpl_image * shiftedImage=NULL ;
3530 cpl_imagelist ** tmpcubes=NULL ;
3535 float* sub_offsetx=NULL ;
3536 float* sub_offsety=NULL ;
3537 float* cubedata=NULL ;
3551 cpl_image* tmp_img=NULL;
3552 cpl_image* o_img=NULL;
3553 cpl_image* m_img=NULL;
3554 cpl_image* c_img=NULL;
3555 cpl_image* t_img=NULL;
3560 if ( cubes == NULL )
3567 if ( mergedCube == NULL )
3579 if ( cumoffsetx == NULL || cumoffsety == NULL )
3591 m_img=cpl_imagelist_get(mergedCube,0);
3592 mlx=cpl_image_get_size_x(m_img);
3593 mly=cpl_image_get_size_y(m_img);
3594 cnp=cpl_imagelist_get_size(cubes[0]);
3595 c_img=cpl_imagelist_get(cubes[0],0);
3596 clx=cpl_image_get_size_x(c_img);
3597 cly=cpl_image_get_size_y(c_img);
3600 tmpcubes=(cpl_imagelist**)cpl_calloc(n_cubes,
sizeof(cpl_imagelist*)) ;
3622 tmpcubes[0]=cpl_imagelist_duplicate(cubes[0]);
3630 llx0 = mlx/2 - clx/2 ;
3631 lly0 = mly/2 - cly/2 ;
3640 llx=cpl_calloc(n_cubes,
sizeof(
int)) ;
3641 lly=cpl_calloc(n_cubes,
sizeof(
int)) ;
3642 sub_offsetx=cpl_calloc(n_cubes,
sizeof(
float)) ;
3643 sub_offsety=cpl_calloc(n_cubes,
sizeof(
float)) ;
3645 for ( i = 0 ; i < n_cubes ; i++ )
3647 llx[i] = llx0 - sinfo_new_nint(cumoffsetx[i]) ;
3648 sub_offsetx[i] = (float)sinfo_new_nint(cumoffsetx[i]) - cumoffsetx[i] ;
3649 lly[i] = lly0 - sinfo_new_nint(cumoffsety[i]) ;
3650 sub_offsety[i] = (float)sinfo_new_nint(cumoffsety[i]) - cumoffsety[i] ;
3658 if ( NULL == (kernel = sinfo_generate_interpolation_kernel(kernel_type)) )
3661 " or no kernel_typ was given, the default kernel"
3665 for ( i = 0 ; i < n_cubes ; i++ )
3669 for ( z = 0 ; z < cnp ; z++ )
3671 tmp_img=cpl_imagelist_get(cubes[i],z);
3672 if ( NULL == (shiftedImage = sinfo_new_shift_image(tmp_img,
3678 "in cube no %d!", z, i) ;
3679 cpl_imagelist_delete(mergedCube) ;
3680 cpl_imagelist_delete(mask) ;
3684 cpl_imagelist_set(tmpcubes[i],shiftedImage,z);
3688 cubedata=cpl_calloc(n_cubes,
sizeof(
float)) ;
3690 for ( y = 0 ; y < mly ; y++ )
3692 for ( x = 0 ; x < mlx ; x++ )
3694 for ( z = 0 ; z < mnp ; z++ )
3699 for ( i = 0 ; i < n_cubes ; i++ )
3701 c_img=cpl_imagelist_get(cubes[i],z);
3703 clx=cpl_image_get_size_x(c_img);
3704 cly=cpl_image_get_size_y(c_img);
3706 t_img=cpl_imagelist_get(tmpcubes[i],z);
3707 ptdata=cpl_image_get_data_float(t_img);
3709 m_img=cpl_imagelist_get(mergedCube,z);
3710 pmdata=cpl_image_get_data_float(m_img);
3711 o_img=cpl_imagelist_get(mask,z);
3712 podata=cpl_image_get_data_float(o_img);
3717 if ( y >= lly[i] && y < lly[i]+cly &&
3718 x >= llx[i] && x < llx[i]+clx )
3722 if (!isnan(ptdata[posx+posy*clx]))
3724 sum += ptdata[posx+posy*clx] ;
3725 sum2 += (ptdata[posx+posy*clx] *
3726 ptdata[posx+posy*clx]) ;
3727 cubedata[n] = ptdata[posx+posy*clx] ;
3737 pmdata[x+y*mlx] = 0. ;
3738 podata[x+y*mlx] = 0 ;
3744 pmdata[x+y*mlx] = mean ;
3745 podata[x+y*mlx] = 1 ;
3749 mean = sum/(double)n ;
3750 sigma = sqrt( (sum2 - sum*mean) / (
double)(n - 1) ) ;
3752 for ( i = 0 ; i < n ; i++ )
3754 if ( cubedata[i] > mean+factor*sigma ||
3755 cubedata[i] < mean-factor*sigma )
3761 pmdata[x+y*mlx] += cubedata[i] ;
3767 pmdata[x+y*mlx] = 0. ;
3771 pmdata[x+y*mlx] /= (float)ns ;
3773 podata[x+y*mlx] = (float)ns ;
3779 for( i = 0 ; i < n_cubes ; i++ )
3781 cpl_imagelist_delete (tmpcubes[i]) ;
3786 cpl_free(sub_offsetx);
3787 cpl_free(sub_offsety);
3791 sinfo_new_convert_0_to_ZERO_for_cubes(mergedCube) ;
3797 sinfo_new_bin_cube(cpl_imagelist *cu,
3806 cpl_imagelist * cube;
3815 cpl_image* i_img=NULL;
3816 cpl_image* o_img=NULL;
3826 inp=cpl_imagelist_get_size(cu);
3827 i_img=cpl_imagelist_get(cu,0);
3828 ilx=cpl_image_get_size_x(i_img);
3834 cube=cpl_imagelist_new();
3835 for ( i = 0 ; i < inp ; i++ ) {
3836 o_img = cpl_image_new(olx,oly,CPL_TYPE_FLOAT);
3837 cpl_imagelist_set(cube,o_img,i);
3841 for (i=0;i<inp;i++){
3842 i_img=cpl_imagelist_get(cu,i);
3843 pidata=cpl_image_get_data_float(i_img);
3844 o_img=cpl_imagelist_get(cube,i);
3845 podata=cpl_image_get_data_float(o_img);
3846 for (j=0 ; j < olx ; j++) {
3847 for (k=0 ; k< oly ; k++) {
3848 podata[j+k*olx]=pidata[((int) (j+xmin)/xscale)+
3849 ((
int) (k+ymin)/yscale)*ilx]/
3860 sinfo_new_scale_cube(cpl_imagelist *cu,
3865 cpl_imagelist * cube ;
3866 int i=0, j=0, k=0, l=0 ;
3867 int lx_out, ly_out ;
3869 double * invert_transform ;
3870 double neighbors[16] ;
3886 cpl_image* in_img=NULL;
3887 cpl_image* ou_img=NULL;
3904 invert_transform = sinfo_invert_linear_transform(param) ;
3905 if (invert_transform == NULL) {
3907 "aborting warping") ;
3912 kernel = sinfo_generate_interpolation_kernel(kernel_type) ;
3913 if (kernel == NULL) {
3920 ilx=cpl_image_get_size_x(cpl_imagelist_get(cu,0));
3921 ily=cpl_image_get_size_y(cpl_imagelist_get(cu,0));
3922 inp=cpl_imagelist_get_size(cu);
3924 lx_out = (int) ilx*xscale ;
3925 ly_out = (int) ily*yscale ;
3927 cube=cpl_imagelist_new();
3928 for ( l = 0 ; l < inp ; l++ ) {
3929 in_img = cpl_image_new(ilx,ily,CPL_TYPE_FLOAT);
3930 cpl_imagelist_set(cube,in_img,l);
3941 for (l=0;l<inp;l++){
3942 in_img=cpl_imagelist_get(cu,l);
3943 ou_img=cpl_imagelist_get(cube,l);
3944 tlx=cpl_image_get_size_x(in_img);
3945 tly=cpl_image_get_size_y(in_img);
3946 podata=cpl_image_get_data_float(ou_img);
3948 leaps[0] = -1 - tlx ;
3950 leaps[2] = 1 - tlx ;
3951 leaps[3] = 2 - tlx ;
3958 leaps[8] = -1 + tlx ;
3960 leaps[10]= 1 + tlx ;
3961 leaps[11]= 2 + tlx ;
3963 leaps[12]= -1 + 2*tlx ;
3965 leaps[14]= 1 + 2*tlx ;
3966 leaps[15]= 2 + 2*tlx ;
3969 for (j=0 ; j < ly_out ; j++) {
3970 for (i=0 ; i< lx_out ; i++) {
3973 x = invert_transform[0] * (double)i +
3974 invert_transform[1] * (
double)j +
3975 invert_transform[2] ;
3977 y = invert_transform[3] * (
double)i +
3978 invert_transform[4] * (
double)j +
3979 invert_transform[5] ;
3989 podata[i+j*lx_out] = (pixelvalue)0.0 ;
3992 pos = px + py * tlx ;
3993 for (k=0 ; k<16 ; k++){
3994 if(!isnan(podata[(
int)(pos+leaps[k])])) neighbors[k] =
3995 (
double)(podata[(int)(pos+leaps[k])]) ;
3996 else neighbors[k]=0;
4000 tabx = (x - (double)px) * (double)(TABSPERPIX) ;
4001 taby = (y - (double)py) * (double)(TABSPERPIX) ;
4006 rsc[0] = kernel[TABSPERPIX + tabx] ;
4007 rsc[1] = kernel[tabx] ;
4008 rsc[2] = kernel[TABSPERPIX - tabx] ;
4009 rsc[3] = kernel[2 * TABSPERPIX - tabx] ;
4010 rsc[4] = kernel[TABSPERPIX + taby] ;
4011 rsc[5] = kernel[taby] ;
4012 rsc[6] = kernel[TABSPERPIX - taby] ;
4013 rsc[7] = kernel[2 * TABSPERPIX - taby] ;
4015 sumrs = (rsc[0]+rsc[1]+rsc[2]+rsc[3]) *
4016 (rsc[4]+rsc[5]+rsc[6]+rsc[7]) ;
4019 cur = rsc[4] * ( rsc[0]*neighbors[0] +
4020 rsc[1]*neighbors[1] +
4021 rsc[2]*neighbors[2] +
4022 rsc[3]*neighbors[3] ) +
4023 rsc[5] * ( rsc[0]*neighbors[4] +
4024 rsc[1]*neighbors[5] +
4025 rsc[2]*neighbors[6] +
4026 rsc[3]*neighbors[7] ) +
4027 rsc[6] * ( rsc[0]*neighbors[8] +
4028 rsc[1]*neighbors[9] +
4029 rsc[2]*neighbors[10] +
4030 rsc[3]*neighbors[11] ) +
4031 rsc[7] * ( rsc[0]*neighbors[12] +
4032 rsc[1]*neighbors[13] +
4033 rsc[2]*neighbors[14] +
4034 rsc[3]*neighbors[15] ) ;
4037 podata[i+j*lx_out] = (pixelvalue)(cur/sumrs) ;
4044 cpl_free(invert_transform) ;
4059 sinfo_cube_zshift(
const cpl_imagelist * cube_inp,
4064 cpl_imagelist * cube_out=NULL ;
4065 const cpl_image* img_inp=NULL;
4066 cpl_image* img_out=NULL;
4077 const float* pidata=NULL;
4080 cknull(cube_inp,
"no input cube given!") ;
4081 check_nomsg(img_inp=cpl_imagelist_get_const(cube_inp,0));
4082 check_nomsg(ilx=cpl_image_get_size_x(img_inp));
4083 check_nomsg(ily=cpl_image_get_size_y(img_inp));
4084 check_nomsg(ilz=cpl_imagelist_get_size(cube_inp));
4090 int_shift = sinfo_new_nint(shift) ;
4091 *sub_shift = shift - (double) int_shift ;
4092 if ( int_shift == 0 )
4094 cube_out =cpl_imagelist_duplicate(cube_inp) ;
4100 cknull(cube_out = cpl_imagelist_new(),
"could not allocate memory!") ;
4101 for ( i = 0 ; i < olz ; i++ ) {
4102 check_nomsg(img_out=cpl_image_new(olx,oly,CPL_TYPE_FLOAT));
4103 check_nomsg(cpl_imagelist_set(cube_out,img_out,i));
4107 for(z=0; z< ilz; z++) {
4108 if ( (z-int_shift >= 0 ) && (z - int_shift < olz) ) {
4109 check_nomsg(img_inp=cpl_imagelist_get_const(cube_inp,z));
4110 check_nomsg(img_out=cpl_imagelist_get(cube_out,z-int_shift));
4111 check_nomsg(pidata=cpl_image_get_data_float_const(img_inp));
4112 check_nomsg(podata=cpl_image_get_data_float(img_out));
4113 for ( col = 0 ; col < ilx ; col++ ) {
4114 for ( row = 0 ; row < ily ; row++ ) {
4115 podata[col+row*olx] = pidata[col+row*olx] ;
4123 sinfo_free_imagelist(&cube_out);
4137 sinfo_cube_zshift_poly(
const cpl_imagelist * cube_inp,
4138 const double sub_shift,
4141 cpl_imagelist * cube_out ;
4144 float* corrected_spec=NULL ;
4150 float * imageptr=NULL ;
4166 const float* pidata=NULL;
4168 const cpl_image* img_inp=NULL;
4169 cpl_image* img_out=NULL;
4171 if ( cube_inp == NULL ) {
4176 img_inp=cpl_imagelist_get_const(cube_inp,0);
4178 ilx=cpl_image_get_size_x(img_inp);
4179 ily=cpl_image_get_size_y(img_inp);
4180 ilz=cpl_imagelist_get_size(cube_inp);
4193 if ( NULL == (cube_out = cpl_imagelist_new()) ) {
4197 for ( i = 0 ; i < ilz ; i++ ) {
4198 img_out=cpl_image_new(olx,oly,CPL_TYPE_FLOAT);
4199 cpl_imagelist_set(cube_out,img_out,i);
4204 n_points = order + 1 ;
4205 if ( n_points % 2 == 0 ) {
4206 firstpos = (int)(n_points/2) - 1 ;
4208 firstpos = (int)(n_points/2) ;
4211 spec=cpl_calloc(ilz,
sizeof(
float)) ;
4212 corrected_spec=cpl_calloc(ilz,
sizeof(
float)) ;
4213 xnum=cpl_calloc(order+1,
sizeof(
float)) ;
4217 for ( i = 0 ; i < n_points ; i++ ) {
4221 for ( col = 0 ; col < ilx ; col++ ) {
4222 for ( row = 0 ; row < ily ; row++ ) {
4223 for( z=0; z< ilz; z++) {
4224 corrected_spec[z] = 0. ;
4227 for ( z = 0 ; z < ilz ; z++ ) {
4228 img_inp=cpl_imagelist_get_const(cube_inp,z);
4229 pidata=cpl_image_get_data_float_const(img_inp);
4230 spec[z] = pidata[col + row*ilx] ;
4231 if (isnan(spec[z]) ) {
4234 for ( i = z - firstpos ; i < z-firstpos+n_points ; i++ ) {
4235 if ( i < 0 ) continue ;
4236 if ( i >= ilz) continue ;
4237 corrected_spec[i] = ZERO ;
4240 if ( z != 0 && z != ilz - 1 ) {
4247 for ( z = 0 ; z < ilz ; z++ ) {
4255 if (isnan(corrected_spec[z])) continue ;
4256 if ( z - firstpos < 0 ) {
4257 imageptr = &spec[0] ;
4258 eval = sub_shift + z ;
4259 }
else if ( z - firstpos + n_points >= ilz ) {
4260 imageptr = &spec[ilz - n_points] ;
4261 eval = sub_shift + z + n_points - ilz ;
4263 imageptr = &spec[z-firstpos] ;
4264 eval = sub_shift + firstpos ;
4268 corrected_spec[z]=sinfo_new_nev_ille(xnum,imageptr,order,eval,&flag);
4269 if ( z != 0 && z != ilz - 1 ) {
4270 new_sum += corrected_spec[z] ;
4275 for (z = 0 ; z < ilz ; z++ )
4277 img_out=cpl_imagelist_get(cube_out,z);
4278 podata=cpl_image_get_data_float(img_out);
4279 if ( new_sum == 0. ) {
4283 podata[col+row*olx] = ZERO ;
4284 }
else if ( z == ilz - 1 ) {
4285 podata[col+row*olx] = ZERO ;
4286 }
else if ( isnan(corrected_spec[z]) ) {
4287 podata[col+row*olx] = ZERO ;
4289 corrected_spec[z] *= sum / new_sum ;
4290 podata[col+row*olx] = corrected_spec[z] ;
4298 cpl_free(corrected_spec) ;
4314 sinfo_cube_zshift_spline3(
const cpl_imagelist * cube_inp,
4315 const double sub_shift)
4318 cpl_imagelist * cube_out=NULL ;
4320 float* corrected_spec=NULL ;
4337 const float* pidata=NULL;
4339 const cpl_image* img_inp=NULL;
4340 cpl_image* img_out=NULL;
4342 if ( cube_inp == NULL ) {
4347 img_inp=cpl_imagelist_get_const(cube_inp,0);
4348 ilx=cpl_image_get_size_x(img_inp);
4349 ily=cpl_image_get_size_y(img_inp);
4350 ilz=cpl_imagelist_get_size(cube_inp);
4357 if ( NULL == (cube_out = cpl_imagelist_new()) ) {
4361 for ( i = 0 ; i < ilz ; i++ ) {
4362 img_out=cpl_image_new(olx,oly,CPL_TYPE_FLOAT);
4363 cpl_imagelist_set(cube_out,img_out,i);
4367 xnum=cpl_calloc(ilz,
sizeof(
float)) ;
4369 for ( i = 0 ; i < ilz ; i++ ) {
4373 spec=cpl_calloc(ilz,
sizeof(
float)) ;
4374 corrected_spec=cpl_calloc(ilz,
sizeof(
float)) ;
4375 eval=cpl_calloc(ilz,
sizeof(
float)) ;
4377 for ( col = 0 ; col < ilx ; col++ ) {
4378 for ( row = 0 ; row < ily ; row++ ) {
4380 for ( z = 0 ; z < ilz ; z++ ) {
4381 img_inp=cpl_imagelist_get_const(cube_inp,z);
4382 pidata=cpl_image_get_data_float_const(img_inp);
4383 spec[z] = pidata[col + row*ilx] ;
4384 if (isnan(spec[z]) ) {
4385 for ( i = z-1 ; i <= z+1 ; i++ ) {
4386 if ( i < 0 ) continue ;
4387 if ( i >= ilz) continue ;
4388 corrected_spec[i] = ZERO ;
4393 eval[z] = (float)sub_shift+(
float)z ;
4396 if ( -1 == sinfo_function1d_natural_spline( xnum, spec, ilz, eval,
4397 corrected_spec, ilz ) )
4404 for ( z = 0 ; z < ilz ; z++ ) {
4405 if ( isnan(corrected_spec[z]) ) {
4408 new_sum += corrected_spec[z] ;
4411 for ( z = 0 ; z < ilz ; z++ ) {
4412 img_out=cpl_imagelist_get(cube_out,z);
4413 podata=cpl_image_get_data_float(img_out);
4414 if ( new_sum == 0. ) new_sum =1. ;
4416 if ( isnan(corrected_spec[z]) ) {
4417 podata[col+row*olx] = ZERO ;
4419 corrected_spec[z] *= sum / new_sum ;
4420 podata[col+row*olx] = corrected_spec[z] ;
4428 cpl_free(corrected_spec) ;
4444 typedef struct _CubeData CubeData;
4446 struct _CubeDataVector
4451 typedef struct _CubeDataVector CubeDataVector;
4469 static int sinfo_kappa_sigma_offset_with_mask(
4473 cpl_imagelist** inputCubes,
4474 const double* exptimes,
4475 cpl_imagelist* imResult,
4478 cpl_imagelist* sky_mask,
4482 sinfo_kappa_sigma_CubeDataVector(
4485 CubeDataVector* pCubeDataVector,
4486 cpl_imagelist* imlistResult,
4487 cpl_imagelist** input_cubes,
4488 cpl_imagelist* sky_mask,
4492 const double* exptimes
4496 sinfo_kappa_sigma_array_with_mask(cpl_array* parray,
int szArray,
4497 const double kappa,cpl_image* imMask,
4498 const double* exptimes,
int x,
int y,
4502 int nInvalidPoints = 0;
4503 const double EPS = 1E-10;
4505 double mask_adjustment = mask_delta;
4513 check_nomsg(median = cpl_array_get_median(parray));
4514 check_nomsg(sig = cpl_array_get_stdev(parray));
4515 for (z = 0; z < szArray; z++)
4519 check_nomsg(value = cpl_array_get(parray, z, &isnull));
4522 if (fabs(value - median) > (kappa * sig))
4532 cpl_array_fill_window_invalid(parray, z, 1);
4533 mask_adjustment += exptimes[z];
4544 while (nInvalidPoints);
4545 if(imMask && fabs(mask_adjustment) > EPS)
4548 int px_rejected = 0;
4549 double msk_value = 0;
4550 check_nomsg(msk_value = cpl_image_get(imMask, x, y, &px_rejected));
4551 check_nomsg(cpl_image_set(imMask, x,y, msk_value - mask_adjustment));
4554 check_nomsg(result = cpl_array_get_mean(parray));
4557 sinfo_msg(
"Error in sinfo_kappa_sigma_array_with_mask");
4562 int sinfo_coadd_with_ks_clip_optimized(
4570 cpl_imagelist* sky_mask,
4571 cpl_imagelist* mergedCube,
4572 cpl_imagelist** tmpcubes
4580 check_nomsg(result=sinfo_kappa_sigma_offset_with_mask(z_min, z_max, n_cubes, tmpcubes, exptimes, mergedCube, llx, lly, sky_mask, kappa));
4588 static int sinfo_kappa_sigma_offset_with_mask(
4592 cpl_imagelist** inputCubes,
4593 const double* exptimes,
4594 cpl_imagelist* imResult,
4595 int* global_offsetX,
4596 int* global_offsetY,
4597 cpl_imagelist* sky_mask,
4601 const int BIG_ENOUGH_INT = 65535;
4602 CubeDataVector*** indexX = 0;
4606 int iPlanesNumber = z_max - z_min;
4607 int nIndexXbytes = 0;
4608 int globalSizeX = 0 ;
4609 int globalSizeY = 0;
4611 int xmax = -BIG_ENOUGH_INT;
4612 int ymax = -BIG_ENOUGH_INT;
4613 int xmin = BIG_ENOUGH_INT;
4614 int ymin = BIG_ENOUGH_INT;
4619 sinfo_check_rec_status(0);
4620 for (z = 0; z < nCubes; z++)
4623 cpl_imagelist* pCube = inputCubes[z];
4624 cpl_image* pImage = 0;
4630 pImage = cpl_imagelist_get(pCube, 0);
4632 localMaxX = cpl_image_get_size_x(pImage) + global_offsetX[z];
4633 localMaxY = cpl_image_get_size_y(pImage) + global_offsetY[z];
4634 localMinX = global_offsetX[z];
4635 localMinY = global_offsetY[z];
4637 if(localMaxX > xmax) xmax = localMaxX;
4638 if(localMaxY > ymax) ymax = localMaxY;
4640 if(localMinX < xmin) xmin = localMinX;
4641 if(localMinY < ymin) ymin = localMinY;
4643 sinfo_check_rec_status(1);
4650 cpl_image * pmaskimage = cpl_imagelist_get(sky_mask, 0);
4651 msize_x = cpl_image_get_size_x(pmaskimage);
4652 msize_y = cpl_image_get_size_y(pmaskimage);
4653 xmax = msize_x < xmax ? msize_x : xmax;
4654 ymax = msize_y < ymax ? msize_y : ymax;
4661 check_nomsg(offsetX = cpl_malloc(
sizeof(offsetX[0]) * nCubes));
4662 check_nomsg(offsetY = cpl_malloc(
sizeof(offsetY[0]) * nCubes));
4663 sinfo_check_rec_status(2);
4664 for (z = 0; z < nCubes; z++)
4666 offsetX[z] = global_offsetX[z];
4667 offsetY[z] = global_offsetY[z];
4670 sinfo_check_rec_status(3);
4672 nIndexXbytes =
sizeof(CubeDataVector**) * (globalSizeX+1 );
4674 indexX = cpl_malloc(nIndexXbytes);
4675 memset(&indexX[0], 0, (globalSizeX+1 )*
sizeof(indexX[0]));
4679 for (z = 0; z < nCubes; z++)
4686 cpl_imagelist* pCube = inputCubes[z];
4687 cpl_image* pImage = 0;
4688 pImage = cpl_imagelist_get(pCube, 0);
4690 iCubeSizeX = cpl_image_get_size_x(pImage);
4691 iCubeSizeY = cpl_image_get_size_y(pImage);
4692 iOffsetX = offsetX[z];
4693 iOffsetY = offsetY[z];
4695 for (x = 1; x <= iCubeSizeX; x++)
4697 int iGlobalX = x + iOffsetX;
4699 CubeDataVector** ppVector = 0;
4700 if (indexX[iGlobalX - 1] == 0)
4703 int nBytes =
sizeof(CubeDataVector*) * (globalSizeY+1 );
4704 ppVector= cpl_malloc(nBytes);
4705 memset(&ppVector[0],0,(globalSizeY+1) *
sizeof(ppVector[0]));
4706 indexX[iGlobalX - 1] = ppVector;
4710 ppVector = indexX[iGlobalX - 1];
4712 for (y = 1; y <=iCubeSizeY; y++)
4714 CubeData* pCubeData = 0;
4715 int iGlobalY = y + iOffsetY;
4716 CubeDataVector* pVector = ppVector[iGlobalY - 1];
4719 int nbytes =
sizeof(CubeDataVector);
4720 check_nomsg(pVector = cpl_malloc(nbytes));
4721 ppVector[iGlobalY - 1] = pVector;
4723 nbytes =
sizeof(CubeData*) * nCubes;
4724 pVector->pdata = cpl_malloc(nbytes);
4727 pCubeData = cpl_malloc(
sizeof(CubeData));
4728 pVector->pdata[(pVector->size)++] = pCubeData;
4729 pCubeData->iCubeNumber = z;
4730 pCubeData->iLocalX = x;
4731 pCubeData->iLocalY = y;
4735 sinfo_check_rec_status(4);
4738 for (x = 1; x <= globalSizeX; x++)
4740 CubeDataVector** pDataX = indexX[x - 1];
4743 for (y = 1; y <= globalSizeY; y++)
4745 CubeDataVector* pDataY = pDataX[y - 1];
4746 if (pDataY && pDataY->size)
4748 sinfo_kappa_sigma_CubeDataVector(x, y, pDataY, imResult,
4749 inputCubes, sky_mask, iPlanesNumber, z_min,
4754 check_nomsg(cpl_free(pDataY->pdata));
4755 check_nomsg(cpl_free(pDataY));
4758 check_nomsg(cpl_free(pDataX));
4761 sinfo_check_rec_status(5);
4771 sinfo_kappa_sigma_CubeDataVector(
4774 CubeDataVector* pCubeDataVector,
4775 cpl_imagelist* imlistResult,
4776 cpl_imagelist** input_cubes,
4777 cpl_imagelist* sky_mask,
4781 const double* exptimes
4788 cpl_array* pArray = 0;
4789 check_nomsg(pArray = cpl_array_new(pCubeDataVector->size, CPL_TYPE_DOUBLE));
4792 for (plane = z_min; plane < z_min + iPlanesNumber; plane++)
4796 cpl_image* imResult = 0;
4797 cpl_image* imMask = 0;
4798 double mask_adjustment = 0;
4799 int nValidPoints = 0;
4800 cpl_array_fill_window_invalid(pArray, 0, pCubeDataVector->size);
4801 check_nomsg(imMask = cpl_imagelist_get(sky_mask, plane - z_min));
4803 for (z = 0; z < pCubeDataVector->size; z++)
4806 cpl_imagelist* pCube = 0;
4807 CubeData* pCubeData = pCubeDataVector->pdata[z];
4808 pCube = input_cubes[pCubeData->iCubeNumber];
4811 cpl_image* pImage = cpl_imagelist_get(pCube, plane - z_min);
4815 int is_rejected = 0;
4817 check_nomsg(value = cpl_image_get(pImage, pCubeData->iLocalX,
4818 pCubeData->iLocalY, &is_rejected));
4821 check_nomsg(cpl_array_set(pArray, z, value));
4826 mask_adjustment += exptimes[z];
4831 sinfo_msg(
"pImage is null");
4837 sinfo_kappa_sigma_array_with_mask(pArray, pCubeDataVector->size,
4838 kappa, imMask, exptimes, globalX, globalY,
4840 check_nomsg(imResult = cpl_imagelist_get(imlistResult, plane));
4843 check_nomsg(cpl_image_set(imResult, globalX, globalY,
4844 cpl_array_get_mean(pArray)));
4848 sinfo_msg(
"imResult is null");
4853 check_nomsg(cpl_image_set(imMask, globalX,globalY, 0));
4856 for (z = 0; z < pCubeDataVector->size; z++)
4858 CubeData* pCubeData = pCubeDataVector->pdata[z];
4859 cpl_free(pCubeData);
4861 cpl_array_delete(pArray);
4870 sinfo_coadd_with_ks_clip(
const int z_min,
4879 cpl_imagelist* mask,
4880 cpl_imagelist* mergedCube,
4881 cpl_imagelist** tmpcubes)
4905 float val_msk_sum=0;
4913 cpl_image* m_img=NULL;
4914 cpl_image* o_img=NULL;
4915 cpl_image* t_img=NULL;
4916 cpl_image* v_img=NULL;
4919 cpl_vector* val=NULL;
4920 cpl_vector* msk=NULL;
4923 o_img=cpl_imagelist_get(mergedCube,0);
4924 olx=cpl_image_get_size_x(o_img);
4925 oly=cpl_image_get_size_y(o_img);
4928 for ( z = z_min; z < z_max ; z++ ) {
4929 m_img=cpl_imagelist_get(mask,z);
4930 pmdata=cpl_image_get_data_float(m_img);
4931 o_img=cpl_imagelist_get(mergedCube,z);
4932 podata=cpl_image_get_data_float(o_img);
4933 mlx=cpl_image_get_size_x(m_img);
4936 for ( y = 0 ; y < oly ; y++ ) {
4937 for ( x = 0 ; x < olx ; x++ ) {
4941 for ( i = 0 ; i < n_cubes ; i++ ) {
4942 t_img=cpl_imagelist_get(tmpcubes[i],m);
4943 ptdata=cpl_image_get_data_float(t_img);
4944 if ( y >= lly[i] && y < lly[i]+ily &&
4945 x >= llx[i] && x < llx[i]+ilx ) {
4948 if (!isnan(ptdata[posx+posy*ilx]) &&
4949 ptdata[posx+posy*ilx] != 0.) {
4957 msk=cpl_vector_new(n_cubes);
4958 for (i=0;i<n_cubes;i++) {
4959 cpl_vector_set(msk,i,1);
4966 for (ks=0;ks<nc;ks++) {
4971 val=cpl_vector_new(nc-nclip);
4974 for ( i = 0 ; i < n_cubes ; i++ ) {
4975 t_img=cpl_imagelist_get(tmpcubes[i],m);
4976 ptdata=cpl_image_get_data_float(t_img);
4977 if ( y >= lly[i] && y < lly[i]+ily &&
4978 x >= llx[i] && x < llx[i]+ilx ) {
4981 if (!isnan(ptdata[posx+posy*ilx]) &&
4982 ptdata[posx+posy*ilx] != 0. &&
4983 (cpl_vector_get(msk,i) != 0)) {
4984 cpl_vector_set(val,ovr,(
double)ptdata[posx+posy*ilx]);
4993 med=cpl_vector_get_median_const(val);
4995 sig=cpl_vector_get_stdev(val);
4999 cpl_vector_delete(val);
5002 for ( i = 0 ; i < n_cubes ; i++ ) {
5003 t_img=cpl_imagelist_get(tmpcubes[i],m);
5004 ptdata=cpl_image_get_data_float(t_img);
5006 if ( y >= lly[i] && y < lly[i]+ily &&
5007 x >= llx[i] && x < llx[i]+ilx ) {
5010 if (!isnan(ptdata[posx+posy*ilx]) &&
5011 ptdata[posx+posy*ilx] != 0. &&
5012 (cpl_vector_get(msk,i) != 0)) {
5013 if(abs((ptdata[posx+posy*ilx]-med))> kappa*sig) {
5014 ptdata[posx+posy*ilx]=0;
5015 pmdata[x+y*mlx] -= exptimes[i] ;
5016 cpl_vector_set(msk,i,0);
5026 for ( i = 0 ; i < n_cubes ; i++ ) {
5027 v_img=cpl_imagelist_get(tmpcubes[i],m);
5028 pvdata=cpl_image_get_data_float(v_img);
5030 if ( y >= lly[i] && y < lly[i]+ily &&
5031 x >= llx[i] && x < llx[i]+ilx ) {
5036 if (!isnan(pvdata[posx+posy*ilx]) &&
5037 pvdata[posx+posy*ilx] != 0. &&
5038 (cpl_vector_get(msk,i) != 0)) {
5039 msk_sum+=pmdata[x+y*mlx];
5040 val_msk_sum+=pvdata[posx+posy*ilx]*
5045 podata[x+y*olx]=val_msk_sum/msk_sum;
5046 cpl_vector_delete(msk);
5068 sinfo_compute_contributes_at_pos(cpl_imagelist** tmpcubes,
5070 const int x,
const int y,
5071 const int ilx,
const int ily,
5072 const int m,
const int n_cubes)
5086 cpl_image* t_img=NULL;
5092 for ( i = 0 ; i < n_cubes ; i++ ) {
5093 t_img=cpl_imagelist_get(tmpcubes[i],m);
5094 ptdata=cpl_image_get_data_float(t_img);
5100 if ( y >= loy && y < upy && x >= lox && x < upx ) {
5103 post = posx+posy*ilx;
5105 if (!isnan(ptdata[post]) && ptdata[post] != 0.) {
5120 sinfo_cubes_coadd_with_ks_clip(cpl_imagelist** tmpcubes,
5121 const int n_cubes,
const int nc,
5122 const int x,
const int y,
const int m,
5124 const int ilx,
const int ily,
5126 double* exptimes,
float** pmdata,
5127 cpl_vector** msk,
const int mlx)
5133 cpl_vector* val=NULL;
5134 cpl_image* t_img=NULL;
5162 for (ks=0;ks<nc;ks++) {
5167 check_nomsg(val=cpl_vector_new(nc-nclip));
5170 for ( i = 0 ; i < n_cubes ; i++ ) {
5171 check_nomsg(t_img=cpl_imagelist_get(tmpcubes[i],m));
5172 check_nomsg(ptdata=cpl_image_get_data_float(t_img));
5179 if ( y >= loy && y < upy && x >= lox && x < upx ) {
5184 if (!isnan(ptdata[post]) && ptdata[post] != 0. &&
5185 (cpl_vector_get(*msk,i) != 0)) {
5186 cpl_vector_set(val,ovr,(
double)ptdata[post]);
5195 med=cpl_vector_get_median_const(val);
5197 sig=cpl_vector_get_stdev(val);
5201 cpl_vector_delete(val);
5204 for ( i = 0 ; i < n_cubes ; i++ ) {
5205 t_img=cpl_imagelist_get(tmpcubes[i],m);
5206 ptdata=cpl_image_get_data_float(t_img);
5214 if ( y >= loy && y < upy && x >= lox && x < upx ) {
5217 post = posx+posy*ilx;
5218 if (!isnan(ptdata[post]) && ptdata[post] != 0. &&
5219 (cpl_vector_get(*msk,i) != 0)) {
5220 if( abs( (ptdata[post]-med) ) > kappa*sig ) {
5222 (*pmdata)[x+y*mlx] -= exptimes[i] ;
5223 check_nomsg(cpl_vector_set(*msk,i,0));
5259 sinfo_coadd_with_ks_clip2(
const int z_min,
5268 cpl_imagelist* mask,
5269 cpl_imagelist* mergedCube,
5270 cpl_imagelist** tmpcubes)
5289 float val_msk_sum=0;
5296 cpl_image* m_img=NULL;
5297 cpl_image* o_img=NULL;
5298 cpl_image* v_img=NULL;
5301 cpl_vector* msk=NULL;
5304 o_img=cpl_imagelist_get(mergedCube,0);
5305 olx=cpl_image_get_size_x(o_img);
5306 oly=cpl_image_get_size_y(o_img);
5309 for ( z = z_min; z < z_max ; z++ ) {
5310 m_img=cpl_imagelist_get(mask,z);
5311 pmdata=cpl_image_get_data_float(m_img);
5312 o_img=cpl_imagelist_get(mergedCube,z);
5313 podata=cpl_image_get_data_float(o_img);
5314 mlx=cpl_image_get_size_x(m_img);
5317 for ( y = 0 ; y < oly ; y++ ) {
5318 for ( x = 0 ; x < olx ; x++ ) {
5323 nc=sinfo_compute_contributes_at_pos(tmpcubes,llx,lly,x,y,
5329 msk=cpl_vector_new(n_cubes);
5330 for (i=0;i<n_cubes;i++) {
5331 cpl_vector_set(msk,i,1);
5335 sinfo_cubes_coadd_with_ks_clip(tmpcubes, n_cubes,nc,x,y,m,
5336 llx,lly,ilx,ily,kappa,
5337 exptimes,&pmdata, &msk,mlx);
5341 for ( i = 0 ; i < n_cubes ; i++ ) {
5342 v_img=cpl_imagelist_get(tmpcubes[i],m);
5343 pvdata=cpl_image_get_data_float(v_img);
5345 if ( y >= lly[i] && y < lly[i]+ily &&
5346 x >= llx[i] && x < llx[i]+ilx ) {
5351 if (!isnan(pvdata[posx+posy*ilx]) &&
5352 pvdata[posx+posy*ilx] != 0. &&
5353 (cpl_vector_get(msk,i) != 0)) {
5354 msk_sum+=pmdata[x+y*mlx];
5355 val_msk_sum+=pvdata[posx+posy*ilx]*
5360 podata[x+y*olx]=val_msk_sum/msk_sum;
5361 cpl_vector_delete(msk);