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") ;
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);
506 sinfo_free_imagelist(&c2);
537 cpl_image* i_img=NULL;
539 inp1=cpl_imagelist_get_size(c1);
540 i_img=cpl_imagelist_get(c1,0);
541 ilx1=cpl_image_get_size_x(i_img);
542 ily1=cpl_image_get_size_y(i_img);
545 inp2=cpl_imagelist_get_size(c2);
546 i_img=cpl_imagelist_get(c2,0);
547 ilx2=cpl_image_get_size_x(i_img);
548 ily2=cpl_image_get_size_y(i_img);
550 if ((ilx1 != ilx2) ||
557 if ((inp2 != inp1) &&
564 if ( NULL == (c3 = cpl_imagelist_new()) )
569 cpl_image* img3=NULL;
570 for (np=0 ; np < inp1 ; np++)
572 img3=cpl_image_new(ilx1,ily1,CPL_TYPE_FLOAT);
573 cpl_imagelist_set(c3,img3,np);
576 for (np=0 ; np < inp1 ; np++)
578 cpl_image* img1=cpl_imagelist_get(c1,np);
579 float* p1data=cpl_image_get_data_float(img1);
580 cpl_image* img2=cpl_imagelist_get(c2,np);
581 float* p2data=cpl_image_get_data_float(img2);
582 img3=cpl_imagelist_get(c3,np);
583 float* p3data=cpl_image_get_data_float(img3);
585 for (i=0 ; i< (ulong32)ilx1*ily1 ; i++)
587 p3data[i] = p1data[i] - p2data[i] ;
619 cpl_image* i_img=NULL;
620 cpl_image* img3=NULL;
622 inp1=cpl_imagelist_get_size(c1);
623 i_img=cpl_imagelist_get(c1,0);
624 ilx1=cpl_image_get_size_x(i_img);
625 ily1=cpl_image_get_size_y(i_img);
627 inp2=cpl_imagelist_get_size(c2);
628 i_img=cpl_imagelist_get(c2,0);
629 ilx2=cpl_image_get_size_x(i_img);
630 ily2=cpl_image_get_size_y(i_img);
631 if ((ilx1 != ilx2) || (ily1 != ily2))
636 if ((inp2 != inp1) && (inp2 != 1))
642 if (NULL == (c3 = cpl_imagelist_new()) )
648 for (np=0 ; np < inp1 ; np++)
650 img3=cpl_image_new(ilx1,ily1,CPL_TYPE_FLOAT);
651 cpl_imagelist_set(c3,img3,np);
655 for (np=0 ; np < inp1 ; np++)
657 cpl_image* img1=cpl_imagelist_get(c1,np);
658 float* p1data=cpl_image_get_data_float(img1);
659 cpl_image* img2=cpl_imagelist_get(c2,np);
660 float* p2data=cpl_image_get_data_float(img2);
661 img3=cpl_imagelist_get(c3,np);
662 float* p3data=cpl_image_get_data_float(img3);
663 for (i=0 ; i< (ulong32)ilx1*ily1 ; i++)
665 p3data[i] = p1data[i] + p2data[i] ;
696 cpl_image* i_img=NULL;
698 inp1=cpl_imagelist_get_size(c1);
699 i_img=cpl_imagelist_get(c1,0);
700 ilx1=cpl_image_get_size_x(i_img);
701 ily1=cpl_image_get_size_y(i_img);
703 inp2=cpl_imagelist_get_size(c2);
704 i_img=cpl_imagelist_get(c2,0);
705 ilx2=cpl_image_get_size_x(i_img);
706 ily2=cpl_image_get_size_y(i_img);
708 if ((ilx1 != ilx2) || (ily1 != ily2))
714 if ((inp2 != inp1) && (inp2 != 1))
720 if ( NULL == (c3 = cpl_imagelist_new()) )
726 cpl_image* img3=NULL;
727 for (np=0 ; np < inp1 ; np++)
729 img3=cpl_image_new(ilx1,ily1,CPL_TYPE_FLOAT);
730 cpl_imagelist_set(c3,img3,np);
734 for (np=0 ; np < inp1 ; np++)
736 cpl_image* img1=cpl_imagelist_get(c1,np);
737 float* p1data=cpl_image_get_data_float(img1);
738 cpl_image* img2=cpl_imagelist_get(c2,np);
739 float* p2data=cpl_image_get_data_float(img2);
740 img3=cpl_imagelist_get(c3,np);
741 float* p3data=cpl_image_get_data_float(img3);
742 for (i=0 ; i< (ulong32)ilx1*ilx2 ; i++)
744 p3data[i] = p1data[i] * p2data[i] ;
775 cpl_image* i_img=NULL;
777 inp1=cpl_imagelist_get_size(c1);
778 i_img=cpl_imagelist_get(c1,0);
779 ilx1=cpl_image_get_size_x(i_img);
780 ily1=cpl_image_get_size_y(i_img);
782 inp2=cpl_imagelist_get_size(c2);
783 i_img=cpl_imagelist_get(c2,0);
784 ilx2=cpl_image_get_size_x(i_img);
785 ily2=cpl_image_get_size_y(i_img);
787 if ((ilx1 != ilx2) ||
794 if ((inp2 != inp1) && (inp2 != 1))
800 if (NULL == (c3 = cpl_imagelist_new()) )
806 cpl_image* img3=NULL;
807 for (np=0 ; np < inp1 ; np++)
809 img3=cpl_image_new(ilx1,ily1,CPL_TYPE_FLOAT);
810 cpl_imagelist_set(c3,img3,np);
813 for (np=0 ; np < inp1 ; np++)
815 cpl_image* img1=cpl_imagelist_get(c1,np);
816 float* p1data=cpl_image_get_data_float(img1);
817 cpl_image* img2=cpl_imagelist_get(c2,np);
818 float* p2data=cpl_image_get_data_float(img2);
819 img3=cpl_imagelist_get(c3,np);
820 float* p3data=cpl_image_get_data_float(img3);
823 for (i=0 ; i< (ulong32) ilx1*ily1 ; i++)
825 if (fabs((
double)p2data[i]) < 1e-10)
831 p3data[i] = p1data[i] / p2data[i] ;
849 sinfo_new_add_image_to_cube(cpl_imagelist * cu, cpl_image * im)
851 cpl_imagelist * cube ;
860 cpl_image* i_img=NULL;
862 if (cu==NULL || im==NULL)
867 cnp=cpl_imagelist_get_size(cu);
868 i_img=cpl_imagelist_get(cu,0);
869 clx=cpl_image_get_size_x(i_img);
870 cly=cpl_image_get_size_y(i_img);
872 ilx=cpl_image_get_size_x(im);
873 ily=cpl_image_get_size_y(im);
875 if ((clx != ilx) || (cly != ily))
881 cube = cpl_imagelist_duplicate (cu) ;
883 for (i=0 ; i<cnp ; i++)
888 cpl_image_add(cpl_imagelist_get(cube,i), im) ;
902 sinfo_new_sub_image_from_cube (cpl_imagelist * cu, cpl_image * im)
904 cpl_imagelist * cube ;
913 cpl_image* i_img=NULL;
915 if (cu==NULL || im==NULL)
920 cnp=cpl_imagelist_get_size(cu);
921 i_img=cpl_imagelist_get(cu,0);
922 clx=cpl_image_get_size_x(i_img);
923 cly=cpl_image_get_size_y(i_img);
925 ilx=cpl_image_get_size_x(im);
926 ily=cpl_image_get_size_y(im);
928 if ((clx != ilx) || (cly != ily))
931 sinfo_msg_error(
"incompatible size: cannot subtract image from cube") ;
935 cube = cpl_imagelist_duplicate (cu) ;
937 for (i=0 ; i<cnp ; i++)
942 cpl_image_subtract(cpl_imagelist_get(cube,i), im) ;
955 sinfo_new_mul_image_to_cube(cpl_imagelist * cu, cpl_image * im)
957 cpl_imagelist * cube ;
966 cpl_image* i_img=NULL;
968 if (cu==NULL || im==NULL)
973 cnp=cpl_imagelist_get_size(cu);
974 i_img=cpl_imagelist_get(cu,0);
975 clx=cpl_image_get_size_x(i_img);
976 cly=cpl_image_get_size_y(i_img);
978 ilx=cpl_image_get_size_x(im);
979 ily=cpl_image_get_size_y(im);
981 if ((clx != ilx) || (cly != ily))
987 cube = cpl_imagelist_duplicate (cu) ;
989 for (i=0 ; i<cnp ; i++)
994 cpl_image_multiply(cpl_imagelist_get(cube,i), im) ;
1008 sinfo_new_div_cube_by_image(cpl_imagelist * cu, cpl_image * im)
1010 cpl_imagelist * cube ;
1019 cpl_image* i_img=NULL;
1021 if (cu==NULL || im==NULL)
1026 cnp=cpl_imagelist_get_size(cu);
1027 i_img=cpl_imagelist_get(cu,0);
1028 clx=cpl_image_get_size_x(i_img);
1029 cly=cpl_image_get_size_y(i_img);
1031 ilx=cpl_image_get_size_x(im);
1032 ily=cpl_image_get_size_y(im);
1034 if ((clx != ilx) || (cly != ily))
1040 cube = cpl_imagelist_duplicate (cu) ;
1042 for (i=0 ; i<cnp ; i++)
1047 cpl_image_divide(cpl_imagelist_get(cube,i), im) ;
1063 sinfo_new_add_spectrum_to_cube(cpl_imagelist *cu, Vector *spec)
1065 cpl_imagelist * cube ;
1072 cpl_image* i_img=NULL;
1073 cpl_image* o_img=NULL;
1075 if (cu == NULL || spec == NULL)
1080 inp=cpl_imagelist_get_size(cu);
1081 i_img=cpl_imagelist_get(cu,0);
1082 ilx=cpl_image_get_size_x(i_img);
1083 ily=cpl_image_get_size_y(i_img);
1085 if ( inp != spec -> n_elements )
1087 sinfo_msg_error(
"cube length and spectrum length are not compatible") ;
1091 if ( NULL == (cube = cpl_imagelist_new ()) )
1096 for ( i = 0; i < inp; i++)
1098 o_img=cpl_image_new(ilx,ily,CPL_TYPE_FLOAT);
1099 cpl_imagelist_set(cube,o_img,i);
1103 for ( i = 0; i < inp; i++)
1105 i_img=cpl_imagelist_get(cu,i);
1106 pidata=cpl_image_get_data_float(i_img);
1107 o_img=cpl_imagelist_get(cube,i);
1108 float* podata=cpl_image_get_data_float(o_img);
1109 for ( j = 0; j < (int) ilx*ily; j++)
1111 podata[j] = pidata[j] + spec -> data[i] ;
1127 sinfo_new_sub_spectrum_from_cube(cpl_imagelist *cu, Vector *spec)
1129 cpl_imagelist * cube ;
1136 cpl_image* i_img=NULL;
1137 cpl_image* o_img=NULL;
1139 if (cu == NULL || spec == NULL)
1144 inp=cpl_imagelist_get_size(cu);
1145 i_img=cpl_imagelist_get(cu,0);
1146 ilx=cpl_image_get_size_x(i_img);
1147 ily=cpl_image_get_size_y(i_img);
1149 if ( inp != spec -> n_elements )
1151 sinfo_msg_error(
"cube length and spectrum length are not compatible") ;
1155 if ( NULL == (cube = cpl_imagelist_new()) )
1160 for ( i = 0; i < inp; i++)
1162 o_img=cpl_image_new(ilx,ily,CPL_TYPE_FLOAT);
1163 cpl_imagelist_set(cube,o_img,i);
1167 for ( i = 0; i < inp; i++)
1169 i_img=cpl_imagelist_get(cu,i);
1170 pidata=cpl_image_get_data_float(i_img);
1171 o_img=cpl_imagelist_get(cube,i);
1172 float* podata=cpl_image_get_data_float(o_img);
1173 for ( j = 0; j < (int) ilx*ily; j++)
1175 if ( isnan(pidata[j]) || isnan(spec -> data[i]) )
1181 podata[j] = pidata[j] - spec -> data[i] ;
1199 sinfo_new_mul_spectrum_to_cube(cpl_imagelist *cu, Vector *spec)
1201 cpl_imagelist * cube ;
1208 cpl_image* i_img=NULL;
1209 cpl_image* o_img=NULL;
1211 if (cu == NULL || spec == NULL)
1216 inp=cpl_imagelist_get_size(cu);
1217 i_img=cpl_imagelist_get(cu,0);
1218 ilx=cpl_image_get_size_x(i_img);
1219 ily=cpl_image_get_size_y(i_img);
1221 if ( inp != spec -> n_elements )
1223 sinfo_msg_error(
"cube length and spectrum length are not compatible") ;
1227 if ( NULL == (cube = cpl_imagelist_new ()) )
1233 for ( i = 0; i < inp; i++)
1235 o_img=cpl_image_new(ilx,ily,CPL_TYPE_FLOAT);
1236 cpl_imagelist_set(cube,o_img,i);
1240 for ( i = 0; i < inp; i++)
1242 i_img=cpl_imagelist_get(cu,i);
1243 pidata=cpl_image_get_data_float(i_img);
1244 o_img=cpl_imagelist_get(cube,i);
1245 float* podata=cpl_image_get_data_float(o_img);
1246 for ( j = 0; j < (int) ilx*ily; j++)
1248 if ( isnan(pidata[j]) || isnan(spec->data[i]) )
1254 podata[j] = pidata[j] * spec -> data[i] ;
1272 sinfo_new_div_cube_by_spectrum(cpl_imagelist *cu, Vector *spec)
1274 cpl_imagelist * cube ;
1282 cpl_image* i_img=NULL;
1283 cpl_image* o_img=NULL;
1285 if (cu == NULL || spec == NULL)
1290 inp=cpl_imagelist_get_size(cu);
1291 i_img=cpl_imagelist_get(cu,0);
1292 ilx=cpl_image_get_size_x(i_img);
1293 ily=cpl_image_get_size_y(i_img);
1295 if ( inp != spec -> n_elements )
1297 sinfo_msg_error(
"cube length and spectrum length are not compatible") ;
1301 if (NULL == (cube = cpl_imagelist_new ()) )
1307 for ( i = 0; i < inp; i++)
1309 o_img=cpl_image_new(ilx,ily,CPL_TYPE_FLOAT);
1310 cpl_imagelist_set(cube,o_img,i);
1314 for ( i = 0; i < inp; i++)
1317 i_img=cpl_imagelist_get(cu,i);
1318 pidata=cpl_image_get_data_float(i_img);
1319 o_img=cpl_imagelist_get(cube,i);
1320 float* podata=cpl_image_get_data_float(o_img);
1321 for ( j = 0; j < (int) ilx*ily; j++)
1323 if (!isnan(spec->data[i]) && spec->data[i] != 0.)
1325 help = 1/spec->data[i] ;
1326 if ( help > THRESH )
1336 if ( isnan(help) || isnan(pidata[j]) )
1342 podata[j] = pidata[j] * help ;
1364 sinfo_new_clean_mean_of_spectra(cpl_imagelist * cube,
1375 int recsize, lo_n, hi_n;
1380 cpl_image* i_img=NULL;
1382 if ( cube == NULL || cpl_imagelist_get_size(cube) < 1 )
1387 inp=cpl_imagelist_get_size(cube);
1388 i_img=cpl_imagelist_get(cube,0);
1389 ilx=cpl_image_get_size_x(i_img);
1390 ily=cpl_image_get_size_y(i_img);
1392 if ((llx<1) || (llx>ilx) ||
1393 (urx<1) || (urx>ilx) ||
1394 (lly<1) || (lly>ily) ||
1395 (ury<1) || (ury>ily) ||
1396 (llx>=urx) || (lly>=ury))
1400 llx, lly, urx, ury) ;
1404 if ((lo_reject + hi_reject) > 0.9)
1407 lo_reject, hi_reject) ;
1409 " aborting average") ;
1419 recsize = (urx - llx + 1) * (ury - lly + 1) ;
1421 lo_n = (int) (recsize * lo_reject + 0.5) ;
1422 hi_n = (int) (recsize * hi_reject + 0.5) ;
1424 if (lo_n + hi_n >= recsize)
1431 if (NULL == (mean = sinfo_new_vector (inp)) )
1442 for ( i = 0 ; i < inp ; i++ )
1444 i_img=cpl_imagelist_get(cube,i);
1445 pidata=cpl_image_get_data_float(i_img);
1447 pixelvalue* local_rectangle=(pixelvalue *)cpl_calloc(recsize,
sizeof (pixelvalue*));
1449 for ( j = lly ; j <= ury ; j++ )
1451 for ( k = llx ; k <= urx ; k++ )
1453 local_rectangle[m] = pidata[k + j * ilx] ;
1458 sinfo_pixel_qsort (local_rectangle, recsize) ;
1461 for ( l = lo_n ; l < (recsize - hi_n) ; l++ )
1463 mean -> data[i] += local_rectangle[l] ;
1466 mean -> data[i] /= nv ;
1468 cpl_free ( local_rectangle ) ;
1485 sinfo_new_median_cube(cpl_imagelist * cube)
1488 pixelvalue * buffer ;
1495 cpl_image* i_img=NULL;
1502 inp=cpl_imagelist_get_size(cube);
1503 i_img=cpl_imagelist_get(cube,0);
1504 ilx=cpl_image_get_size_x(i_img);
1505 ily=cpl_image_get_size_y(i_img);
1508 if (NULL == (im = cpl_image_new (ilx, ily, CPL_TYPE_FLOAT )) )
1519 podata=cpl_image_get_data_float(im);
1520 for ( i = 0 ; i < (int) ilx*ily ; i++ )
1522 buffer = (pixelvalue *) cpl_calloc (inp,
sizeof (pixelvalue *));
1524 for ( j = 0 ; j < inp ; j ++ )
1526 i_img=cpl_imagelist_get(cube,j);
1527 pidata=cpl_image_get_data_float(i_img);
1528 if ( !isnan(pidata[i]) )
1530 buffer[k] = pidata[i] ;
1539 podata[i] = sinfo_new_median ( buffer, nz ) ;
1543 podata[i] = (buffer[0] + buffer[1]) / 2. ;
1547 podata[i] = buffer[0] ;
1554 cpl_free ( buffer ) ;
1572 sinfo_new_average_cube_to_image(cpl_imagelist * cube)
1581 cpl_image* i_img=NULL;
1588 inp=cpl_imagelist_get_size(cube);
1589 i_img=cpl_imagelist_get(cube,0);
1590 ilx=cpl_image_get_size_x(i_img);
1591 ily=cpl_image_get_size_y(i_img);
1594 if (NULL == (im = cpl_image_new (ilx, ily,CPL_TYPE_FLOAT )) )
1605 podata=cpl_image_get_data_float(im);
1606 for ( i = 0 ; i < (int) ilx*ily ; i++ )
1609 for ( j = 0 ; j < inp ; j ++ )
1611 i_img=cpl_imagelist_get(cube,j);
1612 pidata=cpl_image_get_data_float(i_img);
1613 if ( !isnan(pidata[i]) )
1616 podata[i] += pidata[i] ;
1645 sinfo_new_sum_cube_to_image(cpl_imagelist * cube)
1654 cpl_image* i_img=NULL;
1661 inp=cpl_imagelist_get_size(cube);
1662 i_img=cpl_imagelist_get(cube,0);
1663 ilx=cpl_image_get_size_x(i_img);
1664 ily=cpl_image_get_size_y(i_img);
1667 if (NULL == (im = cpl_image_new (ilx, ily, CPL_TYPE_FLOAT )) )
1678 podata=cpl_image_get_data_float(im);
1679 for ( i = 0 ; i < (int) ilx*ily ; i++ )
1682 for ( j = 0 ; j < inp ; j ++ )
1684 i_img=cpl_imagelist_get(cube,j);
1685 pidata=cpl_image_get_data_float(i_img);
1686 if ( !isnan(pidata[i]) )
1689 podata[i] += pidata[i] ;
1724 sinfo_new_average_cube_to_image_between_waves (cpl_imagelist * cube,
1727 float initialLambda,
1740 cpl_image* i_img=NULL;
1747 i_img=cpl_imagelist_get(cube,0);
1748 ilx=cpl_image_get_size_x(i_img);
1749 ily=cpl_image_get_size_y(i_img);
1751 inp=cpl_imagelist_get_size(cube);
1753 minWave = centralWave - (float) (inp / 2)*dispersion ;
1755 if ( dispersion <= 0. || minWave <= 0. )
1761 if ( initialLambda < minWave ||
1762 (initialLambda >= minWave + dispersion * inp) )
1768 if ( finalLambda <= minWave ||
1769 (finalLambda > minWave + dispersion * inp) )
1776 if (NULL == (im = cpl_image_new (ilx, ily, CPL_TYPE_FLOAT )) )
1783 firstPlane = sinfo_new_nint ((
double) ((initialLambda - minWave) /
1785 lastPlane = sinfo_new_nint ((
double) ((finalLambda - minWave) /
1788 if ( firstPlane < 0 || firstPlane >= inp ||
1789 lastPlane < 0 || lastPlane > inp )
1792 cpl_image_delete(im);
1803 podata=cpl_image_get_data_float(im);
1804 for ( i = 0 ; i < (int) ilx*ily ; i++ )
1808 for ( j = firstPlane ; j <= lastPlane ; j ++ )
1810 i_img=cpl_imagelist_get(cube,j);
1811 pidata=cpl_image_get_data_float(i_img);
1812 if ( !isnan(pidata[i]) )
1815 podata[i] += pidata[i] ;
1843 sinfo_new_extract_image_from_cube(cpl_imagelist * cube,
int plane_index)
1851 if ( plane_index < 0 || plane_index >= cpl_imagelist_get_size(cube) )
1857 return cpl_imagelist_get(cube,plane_index) ;
1871 sinfo_new_extract_spectrum_from_cube(cpl_imagelist * cube,
1872 int x_pos,
int y_pos)
1874 Vector * returnedSpectrum ;
1880 cpl_image* i_img=NULL;
1887 i_img=cpl_imagelist_get(cube,0);
1888 ilx=cpl_image_get_size_x(i_img);
1889 ily=cpl_image_get_size_y(i_img);
1890 inp=cpl_imagelist_get_size(cube);
1892 if ( x_pos < 0 || x_pos >= ilx )
1898 if ( y_pos < 0 || y_pos >= ily )
1905 if ( NULL == (returnedSpectrum = sinfo_new_vector ( inp )) )
1911 for ( i = 0 ; i < inp ; i++ )
1913 i_img=cpl_imagelist_get(cube,i);
1914 pidata=cpl_image_get_data_float(i_img);
1915 returnedSpectrum -> data[i] = pidata[x_pos + ilx*y_pos] ;
1918 return returnedSpectrum ;
1963 sinfo_new_combine_jittered_cubes ( cpl_imagelist ** cubes,
1964 cpl_imagelist * mergedCube,
1969 char * kernel_type )
1981 cpl_imagelist * mask=NULL;
1982 double * kernel=NULL;
1988 float* sub_offsetx=NULL ;
1989 float* sub_offsety=NULL ;
1991 cpl_imagelist ** tmpcubes=NULL ;
2007 cpl_image* i_img=NULL;
2008 cpl_image* o_img=NULL;
2009 cpl_image* m_img=NULL;
2010 cpl_image* t_img=NULL;
2013 if ( cubes == NULL )
2023 if ( cumoffsetx == NULL || cumoffsety == NULL )
2028 if ( exptimes == NULL )
2034 o_img=cpl_imagelist_get(mergedCube,0);
2035 olx=cpl_image_get_size_x(o_img);
2036 oly=cpl_image_get_size_y(o_img);
2037 onp=cpl_imagelist_get_size(mergedCube);
2038 if ( NULL == (mask = cpl_imagelist_new()) )
2044 o_img=cpl_image_new(olx,oly,CPL_TYPE_FLOAT);
2045 cpl_imagelist_set(mergedCube,o_img,i);
2048 i_img=cpl_imagelist_get(cubes[0],0);
2049 ilx=cpl_image_get_size_x(i_img);
2050 ily=cpl_image_get_size_y(i_img);
2052 inp=cpl_imagelist_get_size(cubes[0]);
2060 llx0 = olx/2 - ilx/2 ;
2061 lly0 = oly/2 - ily/2 ;
2069 llx=cpl_calloc(n_cubes,
sizeof(
int)); ;
2070 lly=cpl_calloc(n_cubes,
sizeof(
int)) ;
2072 sub_offsetx=cpl_calloc(n_cubes,
sizeof(
float)) ;
2073 sub_offsety=cpl_calloc(n_cubes,
sizeof(
float)) ;
2075 for ( i = 0 ; i < n_cubes ; i++ )
2077 llx[i] = llx0 - sinfo_new_nint(cumoffsetx[i]) ;
2078 sub_offsetx[i] = (float)sinfo_new_nint(cumoffsetx[i]) - cumoffsetx[i] ;
2079 lly[i] = lly0 - sinfo_new_nint(cumoffsety[i]) ;
2080 sub_offsety[i] = (float)sinfo_new_nint(cumoffsety[i]) - cumoffsety[i] ;
2089 if ( NULL == (kernel = sinfo_generate_interpolation_kernel(kernel_type)))
2092 " or no kernel_typ was given, the default kernel"
2098 tmpcubes=(cpl_imagelist**)cpl_calloc(n_cubes,
sizeof(cpl_imagelist*)) ;
2100 for ( i = 0 ; i < n_cubes ; i++ )
2102 pixelvalue* tmpspace = cpl_calloc(ilx, ily*
sizeof(pixelvalue)) ;
2103 tmpcubes[i] = cpl_imagelist_new();
2105 for ( z = 0 ; z < inp ; z++ )
2109 t_img=sinfo_new_shift_image(cpl_imagelist_get(cubes[i],z),
2110 sub_offsetx[i], sub_offsety[i], kernel);
2115 " in cube no %d!", z, i) ;
2116 cpl_imagelist_delete(mergedCube) ;
2117 cpl_imagelist_delete(mask) ;
2121 cpl_imagelist_set(tmpcubes[i],t_img,z);
2133 for ( i = 0 ; i < n_cubes ; i++ )
2137 for ( y = 0 ; y < oly ; y++ )
2139 for ( x = 0 ; x < olx ; x++ )
2143 if ( y >= lly[i] && y < lly[i]+ily &&
2144 x >= llx[i] && x < llx[i]+ilx )
2148 for ( z = 0 ; z < onp ; z++ )
2150 t_img=cpl_imagelist_get(tmpcubes[i],z);
2151 ptdata=cpl_image_get_data_float(t_img);
2152 m_img=cpl_imagelist_get(mask,z);
2153 pmdata=cpl_image_get_data_float(m_img);
2154 if (!isnan(ptdata[posx+posy*ilx]) &&
2155 ptdata[posx+posy*ilx] != 0.)
2157 pmdata[x+y*mlx] += exptimes[i] ;
2173 for ( i = 0 ; i < n_cubes ; i++ )
2177 for ( y = 0 ; y < oly ; y++ )
2180 for ( x = 0 ; x < olx ; x++ )
2185 if ( y >= lly[i] && y < lly[i]+ily &&
2186 x >= llx[i] && x < llx[i]+ilx )
2191 for ( z = 0 ; z < onp ; z++ )
2194 t_img=cpl_imagelist_get(tmpcubes[i],z);
2195 ptdata=cpl_image_get_data_float(t_img);
2196 m_img=cpl_imagelist_get(mask,z);
2197 pmdata=cpl_image_get_data_float(m_img);
2198 mlx=cpl_image_get_size_x(m_img);
2200 o_img=cpl_imagelist_get(mergedCube,z);
2201 podata=cpl_image_get_data_float(o_img);
2203 if (!isnan(ptdata[posx+posy*ilx]))
2205 if (pmdata[x+y*mlx] != 0.)
2209 weight = exptimes[0] / pmdata[x+y*mlx] ;
2216 weight*ptdata[posx+posy*ilx] ;
2230 for( i = 0 ; i < n_cubes ; i++ )
2232 cpl_imagelist_delete (tmpcubes[i]) ;
2235 cpl_free(tmpcubes); ;
2239 cpl_free(sub_offsetx) ;
2240 cpl_free(sub_offsety) ;
2282 sinfo_build_mask_cube(
const int z_min,
2290 cpl_imagelist** cubes,
2291 cpl_imagelist** tmpcubes,
2292 cpl_imagelist* mask)
2300 cpl_image* i_img=NULL;
2301 cpl_image* t_img=NULL;
2309 cpl_image* m_img=NULL;
2312 for ( z = z_min, m=0 ; z < z_max ; z++, m++ ) {
2315 for ( y = 0 ; y < oly ; y++ ) {
2316 for ( x = 0 ; x < olx ; x++ ) {
2317 for ( i = 0 ; i < n_cubes ; i++ ) {
2319 i_img=cpl_imagelist_get(cubes[i],0);
2320 ilx=cpl_image_get_size_x(i_img);
2321 ily=cpl_image_get_size_y(i_img);
2326 if ( y >= lly[i] && y < lly[i]+ily &&
2327 x >= llx[i] && x < llx[i]+ilx )
2333 t_img=cpl_imagelist_get(tmpcubes[i],m);
2334 ptdata=cpl_image_get_data_float(t_img);
2335 m_img=cpl_imagelist_get(mask,z);
2336 pmdata=cpl_image_get_data_float(m_img);
2337 mlx=cpl_image_get_size_x(m_img);
2339 if (!isnan(ptdata[posx+posy*ilx]) &&
2340 ptdata[posx+posy*ilx] != 0.)
2342 pmdata[x+y*mlx] += (float)exptimes[i] ;
2343 }
else if (isnan(ptdata[posx+posy*ilx])) {
2345 }
else if (ptdata[posx+posy*ilx] == 0.) {
2364 sinfo_build_mask_cube_thomas(
const int z_min,
2372 cpl_imagelist** cubes,
2373 cpl_imagelist** tmpcubes,
2374 cpl_imagelist* mask)
2382 cpl_image* t_img=NULL;
2392 for ( i = 0 ; i < n_cubes ; i++ ) {
2394 cpl_image* i_img=cpl_imagelist_get(cubes[i],0);
2395 int ilx=cpl_image_get_size_x(i_img);
2396 int ily=cpl_image_get_size_y(i_img);
2400 for ( y = 0 ; y < oly ; y++ ){
2401 for ( x = 0 ; x < olx ; x++ ){
2404 if ( y >= lly[i] && y < lly[i]+ily &&
2405 x >= llx[i] && x < llx[i]+ilx ) {
2408 for ( z = z_min,m=0 ; z < z_max ; z++,m++ ) {
2409 t_img=cpl_imagelist_get(tmpcubes[i],m);
2410 ptdata=cpl_image_get_data_float(t_img);
2411 cpl_image* m_img=cpl_imagelist_get(mask,z);
2412 pmdata=cpl_image_get_data_float(m_img);
2413 mlx=cpl_image_get_size_x(m_img);
2415 if (!isnan(ptdata[posx+posy*ilx]) &&
2416 ptdata[posx+posy*ilx] != 0.) {
2417 pmdata[x+y*mlx] += (float)exptimes[i] ;
2482 sinfo_new_combine_jittered_cubes_range ( cpl_imagelist ** cubes,
2483 cpl_imagelist * mergedCube,
2484 cpl_imagelist * mask,
2490 const int z_min,
const int z_max )
2495 cpl_imagelist ** tmpcubes=NULL ;
2498 float* sub_offsetx=NULL ;
2499 float* sub_offsety=NULL ;
2508 cpl_image* i_img=NULL;
2509 cpl_image* o_img=NULL;
2512 if(sinfo_check_input(cubes,n_cubes,cumoffsetx,cumoffsety,exptimes) == -1) {
2516 o_img=cpl_imagelist_get(mergedCube,z_min);
2517 olx=cpl_image_get_size_x(o_img);
2518 oly=cpl_image_get_size_y(o_img);
2519 i_img=cpl_imagelist_get(cubes[0],0);
2520 ilx=cpl_image_get_size_x(i_img);
2521 ily=cpl_image_get_size_y(i_img);
2532 llx0 = olx/2 - ilx/2 ;
2533 lly0 = oly/2 - ily/2 ;
2542 llx=cpl_calloc(n_cubes,
sizeof(
int)) ;
2543 lly=cpl_calloc(n_cubes,
sizeof(
int)) ;
2544 sub_offsetx=cpl_calloc(n_cubes,
sizeof(
float)) ;
2545 sub_offsety=cpl_calloc(n_cubes,
sizeof(
float)) ;
2547 for ( i = 0 ; i < n_cubes ; i++ )
2549 llx[i] = llx0 - sinfo_new_nint(cumoffsetx[i]) ;
2550 sub_offsetx[i] = (float)sinfo_new_nint(cumoffsetx[i]) - cumoffsetx[i] ;
2551 lly[i] = lly0 - sinfo_new_nint(cumoffsety[i]) ;
2552 sub_offsety[i] = (float)sinfo_new_nint(cumoffsety[i]) - cumoffsety[i] ;
2555 tmpcubes=(cpl_imagelist**)cpl_calloc(n_cubes,
sizeof(cpl_imagelist*)) ;
2561 if(sinfo_shift_cubes(tmpcubes,kernel_type,n_cubes,cubes,z_min, z_max,
2562 sub_offsetx,sub_offsety,mlx,mly,mask) == -1) {
2577 sinfo_build_mask_cube(z_min,z_max,olx,oly,n_cubes,llx,lly,exptimes,
2578 cubes,tmpcubes,mask);
2584 sinfo_compute_weight_average(z_min,z_max,ilx,ily,n_cubes,mergedCube,mask,
2585 tmpcubes,exptimes,llx,lly);
2590 for( i = 0 ; i < n_cubes ; i++ )
2592 cpl_imagelist_delete (tmpcubes[i]) ;
2596 cpl_free(tmpcubes) ;
2599 cpl_free(sub_offsetx) ;
2600 cpl_free(sub_offsety) ;
2623 sinfo_check_input(cpl_imagelist** cubes,
2629 if ( cubes == NULL )
2639 if ( cumoffsetx == NULL || cumoffsety == NULL )
2644 if ( exptimes == NULL )
2679 sinfo_compute_weight_average(
const int z_min,
2684 cpl_imagelist* mergedCube,
2685 cpl_imagelist* mask,
2686 cpl_imagelist** tmpcubes,
2703 cpl_image* o_img=NULL;
2705 cpl_image* t_img=NULL;
2714 o_img=cpl_imagelist_get(mergedCube,z_min);
2715 olx=cpl_image_get_size_x(o_img);
2716 oly=cpl_image_get_size_y(o_img);
2724 for ( z = z_min, m = 0 ; z < z_max ; z++, m++ ) {
2725 o_img=cpl_imagelist_get(mergedCube,z);
2726 float* podata=cpl_image_get_data_float(o_img);
2727 cpl_image* m_img=cpl_imagelist_get(mask,z);
2728 float* pmdata=cpl_image_get_data_float(m_img);
2729 mlx=cpl_image_get_size_x(m_img);
2732 for ( y = 0 ; y < oly ; y++ ) {
2733 for ( x = 0 ; x < olx ; x++ ) {
2738 for ( i = 0 ; i < n_cubes ; i++ ) {
2740 if ( y >= lly[i] && y < lly[i]+ily &&
2741 x >= llx[i] && x < llx[i]+ilx ) {
2745 t_img=cpl_imagelist_get(tmpcubes[i],m);
2746 ptdata=cpl_image_get_data_float(t_img);
2750 if (!isnan(ptdata[posx+posy*ilx])) {
2751 if (pmdata[x+y*mlx] != 0.) {
2754 weight = exptimes[0] / pmdata[x+y*mlx] ;
2758 podata[x+y*olx] += weight*ptdata[posx+posy*ilx] ;
2810 sinfo_shift_cubes(cpl_imagelist** tmpcubes,
2813 cpl_imagelist** cubes,
2820 cpl_imagelist* mask)
2829 cpl_image* t_img=NULL;
2830 cpl_image* m_img=NULL;
2839 if ( NULL == (kernel = sinfo_generate_interpolation_kernel(kernel_type)) )
2842 "or no kernel_typ was given, the default kernel"
2846 for ( i = 0 ; i < n_cubes ; i++ )
2849 cpl_image* i_img=cpl_imagelist_get(cubes[i],0);
2850 int ilx=cpl_image_get_size_x(i_img);
2851 int ily=cpl_image_get_size_y(i_img);
2853 pixelvalue *tmpspace = cpl_calloc(ilx, ily*
sizeof(pixelvalue)) ;
2854 tmpcubes[i]=cpl_imagelist_new();
2856 for ( z = z_min, m=0 ; z < z_max ; z++, m++ )
2858 t_img=sinfo_new_shift_image(cpl_imagelist_get(cubes[i],z),
2866 "in cube no %d!", z, i) ;
2871 cpl_imagelist_set(tmpcubes[i],t_img,m);
2872 m_img=cpl_image_new(mlx,mly,CPL_TYPE_FLOAT);
2873 cpl_imagelist_set(mask,m_img,z);
2879 if(kernel != NULL) cpl_free(kernel) ;
3086 sinfo_new_combine_jittered_cubes_thomas_range(cpl_imagelist ** cubes,
3087 cpl_imagelist * mergedCube,
3088 cpl_imagelist * mask,
3096 const double kappa )
3098 const int VERY_BIG_INT = 268431360;
3103 float* sub_offsetx=NULL ;
3104 float* sub_offsety=NULL ;
3105 cpl_imagelist ** tmpcubes=NULL ;
3106 const int z_siz=z_max-z_min;
3114 cpl_image* i_img=NULL;
3115 cpl_image* o_img=NULL;
3116 int min_lx = VERY_BIG_INT;
3117 int min_ly = VERY_BIG_INT;
3120 if(sinfo_check_input(cubes,n_cubes,cumoffsetx,cumoffsety,exptimes) == -1) {
3129 i_img=cpl_imagelist_get(cubes[0],0);
3130 o_img=cpl_imagelist_get(mergedCube,0);
3131 ilx=cpl_image_get_size_x(i_img);
3132 ily=cpl_image_get_size_y(i_img);
3133 olx=cpl_image_get_size_x(o_img);
3134 oly=cpl_image_get_size_y(o_img);
3144 llx0 = (1.0 * olx- 1.0 * ilx)/2.0 ;
3145 lly0 = (1.0 * oly - 1.0 * ily)/2.0 ;
3153 llx=cpl_calloc(n_cubes,
sizeof(
int));
3154 lly=cpl_calloc(n_cubes,
sizeof(
int)) ;
3155 sub_offsetx=cpl_calloc(n_cubes,
sizeof(
float)) ;
3156 sub_offsety=cpl_calloc(n_cubes,
sizeof(
float)) ;
3158 for ( i = 0 ; i < n_cubes ; i++ ) {
3159 llx[i] = llx0 - sinfo_new_nint(cumoffsetx[i]) ;
3161 sub_offsetx[i] = (float)sinfo_new_nint(cumoffsetx[i]) - cumoffsetx[i] ;
3162 lly[i] = lly0 - sinfo_new_nint(cumoffsety[i]) ;
3163 sub_offsety[i] = (float)sinfo_new_nint(cumoffsety[i]) - cumoffsety[i] ;
3167 if (llx[i] < min_lx)
3171 if (lly[i] < min_ly)
3181 for (i = 0 ; i < n_cubes ; i++ )
3183 llx[i] = llx[i] - min_lx;
3188 for (i = 0 ; i < n_cubes ; i++ )
3190 lly[i] = lly[i] - min_ly;
3200 tmpcubes=(cpl_imagelist**)cpl_calloc(n_cubes,
sizeof(cpl_imagelist*)) ;
3202 if(sinfo_shift_cubes(tmpcubes,kernel_type,n_cubes,cubes,z_min, z_max,
3203 sub_offsetx,sub_offsety,mlx,mly,mask) == -1) {
3220 o_img=cpl_imagelist_get(mergedCube,0);
3221 olx=cpl_image_get_size_x(o_img);
3222 oly=cpl_image_get_size_y(o_img);
3225 if(-1 == sinfo_build_mask_cube_thomas(z_min,z_max,olx,oly,n_cubes,llx,lly,
3226 exptimes,cubes,tmpcubes,mask) ) {
3237 check_nomsg(sinfo_coadd_with_ks_clip2(z_min,z_max,ilx,ily,n_cubes,kappa,llx,lly,
3238 exptimes,mask,mergedCube,tmpcubes));
3248 for( i = 0 ; i < n_cubes ; i++ ) {
3249 cpl_imagelist_delete (tmpcubes[i]) ;
3255 cpl_free(sub_offsetx) ;
3256 cpl_free(sub_offsety) ;
3257 sinfo_print_rec_status(0);
3276 sinfo_new_interpol_cube_simple( cpl_imagelist * cube,
3277 cpl_imagelist * badcube,
3280 cpl_imagelist * intercube ;
3281 float* goodNeighbors=NULL ;
3285 int zi, coli, rowi ;
3298 cpl_image* bzi_img=NULL;
3299 cpl_image* czi_img=NULL;
3303 if ( cube == NULL || badcube == NULL )
3313 intercube = cpl_imagelist_duplicate(cube) ;
3315 goodNeighbors=cpl_calloc((2*maxdist+1)*(2*maxdist+1)*(2*maxdist+1) -1,
3318 cnp=cpl_imagelist_get_size(cube);
3319 float* pczidata=NULL;
3320 float* pbzidata=NULL;
3321 for ( z = 0 ; z < cnp ; z++ )
3323 cpl_image* b_img=cpl_imagelist_get(badcube,z);
3324 cpl_image* i_img=cpl_imagelist_get(intercube,z);
3325 float* pbdata=cpl_image_get_data_float(b_img);
3326 float* pidata=cpl_image_get_data_float(i_img);
3327 int blx=cpl_image_get_size_x(b_img);
3330 cpl_image* c_img=cpl_imagelist_get(cube,z);
3331 int clx=cpl_image_get_size_x(c_img);
3332 int cly=cpl_image_get_size_y(c_img);
3334 for ( row = 0 ; row < cly ; row++ )
3336 for ( col = 0 ; col < clx ; col++ )
3338 if ( pbdata[col+row*clx] == 0 )
3341 llx = col - maxdist ;
3348 if ( llx + nx > clx )
3350 nx -= (llx + nx - clx) ;
3353 lly = row - maxdist ;
3360 if ( lly + ny > cly )
3362 ny -= (lly + ny - cly) ;
3372 if ( llz + nz > cnp )
3374 nz -= (llz + nz - cnp) ;
3377 for ( zi = llz ; zi < llz+nz ; zi++ )
3379 bzi_img=cpl_imagelist_get(badcube,zi);
3380 czi_img=cpl_imagelist_get(cube,zi);
3381 pbzidata=cpl_image_get_data_float(bzi_img);
3382 pczidata=cpl_image_get_data_float(czi_img);
3384 for ( rowi = lly ; rowi < lly+ny ; rowi++ )
3386 for ( coli = llx ; coli < llx+nx ; coli++ )
3388 if ( pbzidata[coli+rowi*blx] == 1 )
3390 goodNeighbors[n] = pczidata[coli+rowi*clx] ;
3398 pidata[col+row*clx]=sinfo_new_median(goodNeighbors,n);
3399 pbdata[col+row*clx]=1 ;
3409 cpl_free(goodNeighbors) ;
3470 sinfo_new_combine_cubes ( cpl_imagelist ** cubes,
3471 cpl_imagelist * mergedCube,
3476 char * kernel_type )
3486 cpl_imagelist * mask=NULL ;
3487 double * kernel=NULL ;
3488 cpl_image * shiftedImage=NULL ;
3496 cpl_imagelist ** tmpcubes=NULL ;
3501 float* sub_offsetx=NULL ;
3502 float* sub_offsety=NULL ;
3503 float* cubedata=NULL ;
3517 cpl_image* tmp_img=NULL;
3518 cpl_image* o_img=NULL;
3519 cpl_image* m_img=NULL;
3520 cpl_image* c_img=NULL;
3521 cpl_image* t_img=NULL;
3526 if ( cubes == NULL )
3533 if ( mergedCube == NULL )
3545 if ( cumoffsetx == NULL || cumoffsety == NULL )
3557 m_img=cpl_imagelist_get(mergedCube,0);
3558 mlx=cpl_image_get_size_x(m_img);
3559 mly=cpl_image_get_size_y(m_img);
3560 cnp=cpl_imagelist_get_size(cubes[0]);
3561 c_img=cpl_imagelist_get(cubes[0],0);
3562 clx=cpl_image_get_size_x(c_img);
3563 cly=cpl_image_get_size_y(c_img);
3566 tmpcubes=(cpl_imagelist**)cpl_calloc(n_cubes,
sizeof(cpl_imagelist*)) ;
3588 tmpcubes[0]=cpl_imagelist_duplicate(cubes[0]);
3596 llx0 = mlx/2 - clx/2 ;
3597 lly0 = mly/2 - cly/2 ;
3606 llx=cpl_calloc(n_cubes,
sizeof(
int)) ;
3607 lly=cpl_calloc(n_cubes,
sizeof(
int)) ;
3608 sub_offsetx=cpl_calloc(n_cubes,
sizeof(
float)) ;
3609 sub_offsety=cpl_calloc(n_cubes,
sizeof(
float)) ;
3611 for ( i = 0 ; i < n_cubes ; i++ )
3613 llx[i] = llx0 - sinfo_new_nint(cumoffsetx[i]) ;
3614 sub_offsetx[i] = (float)sinfo_new_nint(cumoffsetx[i]) - cumoffsetx[i] ;
3615 lly[i] = lly0 - sinfo_new_nint(cumoffsety[i]) ;
3616 sub_offsety[i] = (float)sinfo_new_nint(cumoffsety[i]) - cumoffsety[i] ;
3624 if ( NULL == (kernel = sinfo_generate_interpolation_kernel(kernel_type)) )
3627 " or no kernel_typ was given, the default kernel"
3631 for ( i = 0 ; i < n_cubes ; i++ )
3635 for ( z = 0 ; z < cnp ; z++ )
3637 tmp_img=cpl_imagelist_get(cubes[i],z);
3638 if ( NULL == (shiftedImage = sinfo_new_shift_image(tmp_img,
3644 "in cube no %d!", z, i) ;
3645 cpl_imagelist_delete(mergedCube) ;
3646 cpl_imagelist_delete(mask) ;
3648 for( i = 0 ; i < n_cubes ; i++ ) {
3649 cpl_imagelist_delete (tmpcubes[i]) ;
3654 cpl_imagelist_set(tmpcubes[i],shiftedImage,z);
3658 cubedata=cpl_calloc(n_cubes,
sizeof(
float)) ;
3660 for ( y = 0 ; y < mly ; y++ )
3662 for ( x = 0 ; x < mlx ; x++ )
3664 for ( z = 0 ; z < mnp ; z++ )
3669 for ( i = 0 ; i < n_cubes ; i++ )
3671 c_img=cpl_imagelist_get(cubes[i],z);
3673 clx=cpl_image_get_size_x(c_img);
3674 cly=cpl_image_get_size_y(c_img);
3676 t_img=cpl_imagelist_get(tmpcubes[i],z);
3677 ptdata=cpl_image_get_data_float(t_img);
3679 m_img=cpl_imagelist_get(mergedCube,z);
3680 pmdata=cpl_image_get_data_float(m_img);
3681 o_img=cpl_imagelist_get(mask,z);
3682 podata=cpl_image_get_data_float(o_img);
3687 if ( y >= lly[i] && y < lly[i]+cly &&
3688 x >= llx[i] && x < llx[i]+clx )
3692 if (!isnan(ptdata[posx+posy*clx]))
3694 sum += ptdata[posx+posy*clx] ;
3695 sum2 += (ptdata[posx+posy*clx] *
3696 ptdata[posx+posy*clx]) ;
3697 cubedata[n] = ptdata[posx+posy*clx] ;
3707 pmdata[x+y*mlx] = 0. ;
3708 podata[x+y*mlx] = 0 ;
3714 pmdata[x+y*mlx] = mean ;
3715 podata[x+y*mlx] = 1 ;
3719 mean = sum/(double)n ;
3720 sigma = sqrt( (sum2 - sum*mean) / (
double)(n - 1) ) ;
3722 for ( i = 0 ; i < n ; i++ )
3724 if ( cubedata[i] > mean+factor*sigma ||
3725 cubedata[i] < mean-factor*sigma )
3731 pmdata[x+y*mlx] += cubedata[i] ;
3737 pmdata[x+y*mlx] = 0. ;
3741 pmdata[x+y*mlx] /= (float)ns ;
3743 podata[x+y*mlx] = (float)ns ;
3749 for( i = 0 ; i < n_cubes ; i++ )
3751 cpl_imagelist_delete (tmpcubes[i]) ;
3756 cpl_free(sub_offsetx);
3757 cpl_free(sub_offsety);
3761 sinfo_new_convert_0_to_ZERO_for_cubes(mergedCube) ;
3767 sinfo_new_bin_cube(cpl_imagelist *cu,
3776 cpl_imagelist * cube;
3785 cpl_image* i_img=NULL;
3796 inp=cpl_imagelist_get_size(cu);
3797 i_img=cpl_imagelist_get(cu,0);
3798 ilx=cpl_image_get_size_x(i_img);
3804 cube=cpl_imagelist_new();
3805 cpl_image* o_img=NULL;
3806 for ( i = 0 ; i < inp ; i++ ) {
3807 o_img = cpl_image_new(olx,oly,CPL_TYPE_FLOAT);
3808 cpl_imagelist_set(cube,o_img,i);
3812 for (i=0;i<inp;i++){
3813 cpl_image* i_img=cpl_imagelist_get(cu,i);
3814 float* pidata=cpl_image_get_data_float(i_img);
3815 o_img=cpl_imagelist_get(cube,i);
3816 float* podata=cpl_image_get_data_float(o_img);
3817 for (j=0 ; j < olx ; j++) {
3818 for (k=0 ; k< oly ; k++) {
3819 podata[j+k*olx]=pidata[((int) (j+xmin)/xscale)+
3820 ((
int) (k+ymin)/yscale)*ilx]/
3831 sinfo_new_scale_cube(cpl_imagelist *cu,
3836 cpl_imagelist * cube ;
3837 int i=0, j=0, k=0, l=0 ;
3838 int lx_out, ly_out ;
3840 double * invert_transform ;
3841 double neighbors[16] ;
3855 cpl_image* in_img=NULL;
3872 invert_transform = sinfo_invert_linear_transform(param) ;
3873 if (invert_transform == NULL) {
3875 "aborting warping") ;
3880 kernel = sinfo_generate_interpolation_kernel(kernel_type) ;
3881 if (kernel == NULL) {
3888 ilx=cpl_image_get_size_x(cpl_imagelist_get(cu,0));
3889 ily=cpl_image_get_size_y(cpl_imagelist_get(cu,0));
3890 inp=cpl_imagelist_get_size(cu);
3892 lx_out = (int) ilx*xscale ;
3893 ly_out = (int) ily*yscale ;
3895 cube=cpl_imagelist_new();
3896 for ( l = 0 ; l < inp ; l++ ) {
3897 in_img = cpl_image_new(ilx,ily,CPL_TYPE_FLOAT);
3898 cpl_imagelist_set(cube,in_img,l);
3909 for (l=0;l<inp;l++){
3910 in_img=cpl_imagelist_get(cu,l);
3911 cpl_image* ou_img=cpl_imagelist_get(cube,l);
3912 int tlx=cpl_image_get_size_x(in_img);
3913 int tly=cpl_image_get_size_y(in_img);
3914 float* podata=cpl_image_get_data_float(ou_img);
3916 leaps[0] = -1 - tlx ;
3918 leaps[2] = 1 - tlx ;
3919 leaps[3] = 2 - tlx ;
3926 leaps[8] = -1 + tlx ;
3928 leaps[10]= 1 + tlx ;
3929 leaps[11]= 2 + tlx ;
3931 leaps[12]= -1 + 2*tlx ;
3933 leaps[14]= 1 + 2*tlx ;
3934 leaps[15]= 2 + 2*tlx ;
3937 for (j=0 ; j < ly_out ; j++) {
3938 for (i=0 ; i< lx_out ; i++) {
3941 x = invert_transform[0] * (double)i +
3942 invert_transform[1] * (
double)j +
3943 invert_transform[2] ;
3945 y = invert_transform[3] * (
double)i +
3946 invert_transform[4] * (
double)j +
3947 invert_transform[5] ;
3957 podata[i+j*lx_out] = (pixelvalue)0.0 ;
3960 pos = px + py * tlx ;
3961 for (k=0 ; k<16 ; k++){
3962 if(!isnan(podata[(
int)(pos+leaps[k])])) neighbors[k] =
3963 (
double)(podata[(int)(pos+leaps[k])]) ;
3964 else neighbors[k]=0;
3968 tabx = (x - (double)px) * (double)(TABSPERPIX) ;
3969 taby = (y - (double)py) * (double)(TABSPERPIX) ;
3974 rsc[0] = kernel[TABSPERPIX + tabx] ;
3975 rsc[1] = kernel[tabx] ;
3976 rsc[2] = kernel[TABSPERPIX - tabx] ;
3977 rsc[3] = kernel[2 * TABSPERPIX - tabx] ;
3978 rsc[4] = kernel[TABSPERPIX + taby] ;
3979 rsc[5] = kernel[taby] ;
3980 rsc[6] = kernel[TABSPERPIX - taby] ;
3981 rsc[7] = kernel[2 * TABSPERPIX - taby] ;
3983 sumrs = (rsc[0]+rsc[1]+rsc[2]+rsc[3]) *
3984 (rsc[4]+rsc[5]+rsc[6]+rsc[7]) ;
3987 cur = rsc[4] * ( rsc[0]*neighbors[0] +
3988 rsc[1]*neighbors[1] +
3989 rsc[2]*neighbors[2] +
3990 rsc[3]*neighbors[3] ) +
3991 rsc[5] * ( rsc[0]*neighbors[4] +
3992 rsc[1]*neighbors[5] +
3993 rsc[2]*neighbors[6] +
3994 rsc[3]*neighbors[7] ) +
3995 rsc[6] * ( rsc[0]*neighbors[8] +
3996 rsc[1]*neighbors[9] +
3997 rsc[2]*neighbors[10] +
3998 rsc[3]*neighbors[11] ) +
3999 rsc[7] * ( rsc[0]*neighbors[12] +
4000 rsc[1]*neighbors[13] +
4001 rsc[2]*neighbors[14] +
4002 rsc[3]*neighbors[15] ) ;
4005 podata[i+j*lx_out] = (pixelvalue)(cur/sumrs) ;
4012 cpl_free(invert_transform) ;
4027 sinfo_cube_zshift(
const cpl_imagelist * cube_inp,
4032 cpl_imagelist * cube_out=NULL ;
4033 const cpl_image* img_inp=NULL;
4034 cpl_image* img_out=NULL;
4045 const float* pidata=NULL;
4048 cknull(cube_inp,
"no input cube given!") ;
4049 check_nomsg(img_inp=cpl_imagelist_get_const(cube_inp,0));
4050 check_nomsg(ilx=cpl_image_get_size_x(img_inp));
4051 check_nomsg(ily=cpl_image_get_size_y(img_inp));
4052 check_nomsg(ilz=cpl_imagelist_get_size(cube_inp));
4058 int_shift = sinfo_new_nint(shift) ;
4059 *sub_shift = shift - (double) int_shift ;
4060 if ( int_shift == 0 )
4062 cube_out =cpl_imagelist_duplicate(cube_inp) ;
4068 cknull(cube_out = cpl_imagelist_new(),
"could not allocate memory!") ;
4069 for ( i = 0 ; i < olz ; i++ ) {
4070 check_nomsg(img_out=cpl_image_new(olx,oly,CPL_TYPE_FLOAT));
4071 check_nomsg(cpl_imagelist_set(cube_out,img_out,i));
4075 for(z=0; z< ilz; z++) {
4076 if ( (z-int_shift >= 0 ) && (z - int_shift < olz) ) {
4077 check_nomsg(img_inp=cpl_imagelist_get_const(cube_inp,z));
4078 check_nomsg(img_out=cpl_imagelist_get(cube_out,z-int_shift));
4079 check_nomsg(pidata=cpl_image_get_data_float_const(img_inp));
4080 check_nomsg(podata=cpl_image_get_data_float(img_out));
4081 for ( col = 0 ; col < ilx ; col++ ) {
4082 for ( row = 0 ; row < ily ; row++ ) {
4083 podata[col+row*olx] = pidata[col+row*olx] ;
4091 sinfo_free_imagelist(&cube_out);
4105 sinfo_cube_zshift_poly(
const cpl_imagelist * cube_inp,
4106 const double sub_shift,
4109 cpl_imagelist * cube_out ;
4112 float* corrected_spec=NULL ;
4118 float * imageptr=NULL ;
4134 const float* pidata=NULL;
4136 const cpl_image* img_inp=NULL;
4137 cpl_image* img_out=NULL;
4139 if ( cube_inp == NULL ) {
4144 img_inp=cpl_imagelist_get_const(cube_inp,0);
4146 ilx=cpl_image_get_size_x(img_inp);
4147 ily=cpl_image_get_size_y(img_inp);
4148 ilz=cpl_imagelist_get_size(cube_inp);
4161 if ( NULL == (cube_out = cpl_imagelist_new()) ) {
4165 for ( i = 0 ; i < ilz ; i++ ) {
4166 img_out=cpl_image_new(olx,oly,CPL_TYPE_FLOAT);
4167 cpl_imagelist_set(cube_out,img_out,i);
4172 n_points = order + 1 ;
4173 if ( n_points % 2 == 0 ) {
4174 firstpos = (int)(n_points/2) - 1 ;
4176 firstpos = (int)(n_points/2) ;
4179 spec=cpl_calloc(ilz,
sizeof(
float)) ;
4180 corrected_spec=cpl_calloc(ilz,
sizeof(
float)) ;
4181 xnum=cpl_calloc(order+1,
sizeof(
float)) ;
4185 for ( i = 0 ; i < n_points ; i++ ) {
4189 for ( col = 0 ; col < ilx ; col++ ) {
4190 for ( row = 0 ; row < ily ; row++ ) {
4191 for( z=0; z< ilz; z++) {
4192 corrected_spec[z] = 0. ;
4195 for ( z = 0 ; z < ilz ; z++ ) {
4196 img_inp=cpl_imagelist_get_const(cube_inp,z);
4197 pidata=cpl_image_get_data_float_const(img_inp);
4198 spec[z] = pidata[col + row*ilx] ;
4199 if (isnan(spec[z]) ) {
4202 for ( i = z - firstpos ; i < z-firstpos+n_points ; i++ ) {
4203 if ( i < 0 ) continue ;
4204 if ( i >= ilz) continue ;
4205 corrected_spec[i] = ZERO ;
4208 if ( z != 0 && z != ilz - 1 ) {
4215 for ( z = 0 ; z < ilz ; z++ ) {
4223 if (isnan(corrected_spec[z])) continue ;
4224 if ( z - firstpos < 0 ) {
4225 imageptr = &spec[0] ;
4226 eval = sub_shift + z ;
4227 }
else if ( z - firstpos + n_points >= ilz ) {
4228 imageptr = &spec[ilz - n_points] ;
4229 eval = sub_shift + z + n_points - ilz ;
4231 imageptr = &spec[z-firstpos] ;
4232 eval = sub_shift + firstpos ;
4236 corrected_spec[z]=sinfo_new_nev_ille(xnum,imageptr,order,eval,&flag);
4237 if ( z != 0 && z != ilz - 1 ) {
4238 new_sum += corrected_spec[z] ;
4243 for (z = 0 ; z < ilz ; z++ )
4245 img_out=cpl_imagelist_get(cube_out,z);
4246 podata=cpl_image_get_data_float(img_out);
4247 if ( new_sum == 0. ) {
4251 podata[col+row*olx] = ZERO ;
4252 }
else if ( z == ilz - 1 ) {
4253 podata[col+row*olx] = ZERO ;
4254 }
else if ( isnan(corrected_spec[z]) ) {
4255 podata[col+row*olx] = ZERO ;
4257 corrected_spec[z] *= sum / new_sum ;
4258 podata[col+row*olx] = corrected_spec[z] ;
4266 cpl_free(corrected_spec) ;
4282 sinfo_cube_zshift_spline3(
const cpl_imagelist * cube_inp,
4283 const double sub_shift)
4286 cpl_imagelist * cube_out=NULL ;
4288 float* corrected_spec=NULL ;
4305 const float* pidata=NULL;
4307 const cpl_image* img_inp=NULL;
4308 cpl_image* img_out=NULL;
4310 if ( cube_inp == NULL ) {
4315 img_inp=cpl_imagelist_get_const(cube_inp,0);
4316 ilx=cpl_image_get_size_x(img_inp);
4317 ily=cpl_image_get_size_y(img_inp);
4318 ilz=cpl_imagelist_get_size(cube_inp);
4325 if ( NULL == (cube_out = cpl_imagelist_new()) ) {
4329 for ( i = 0 ; i < ilz ; i++ ) {
4330 img_out=cpl_image_new(olx,oly,CPL_TYPE_FLOAT);
4331 cpl_imagelist_set(cube_out,img_out,i);
4335 xnum=cpl_calloc(ilz,
sizeof(
float)) ;
4337 for ( i = 0 ; i < ilz ; i++ ) {
4341 spec=cpl_calloc(ilz,
sizeof(
float)) ;
4342 corrected_spec=cpl_calloc(ilz,
sizeof(
float)) ;
4343 eval=cpl_calloc(ilz,
sizeof(
float)) ;
4345 for ( col = 0 ; col < ilx ; col++ ) {
4346 for ( row = 0 ; row < ily ; row++ ) {
4348 for ( z = 0 ; z < ilz ; z++ ) {
4349 img_inp=cpl_imagelist_get_const(cube_inp,z);
4350 pidata=cpl_image_get_data_float_const(img_inp);
4351 spec[z] = pidata[col + row*ilx] ;
4352 if (isnan(spec[z]) ) {
4353 for ( i = z-1 ; i <= z+1 ; i++ ) {
4354 if ( i < 0 ) continue ;
4355 if ( i >= ilz) continue ;
4356 corrected_spec[i] = ZERO ;
4361 eval[z] = (float)sub_shift+(
float)z ;
4364 if ( -1 == sinfo_function1d_natural_spline( xnum, spec, ilz, eval,
4365 corrected_spec, ilz ) )
4372 for ( z = 0 ; z < ilz ; z++ ) {
4373 if ( isnan(corrected_spec[z]) ) {
4376 new_sum += corrected_spec[z] ;
4379 for ( z = 0 ; z < ilz ; z++ ) {
4380 img_out=cpl_imagelist_get(cube_out,z);
4381 podata=cpl_image_get_data_float(img_out);
4382 if ( new_sum == 0. ) new_sum =1. ;
4384 if ( isnan(corrected_spec[z]) ) {
4385 podata[col+row*olx] = ZERO ;
4387 corrected_spec[z] *= sum / new_sum ;
4388 podata[col+row*olx] = corrected_spec[z] ;
4396 cpl_free(corrected_spec) ;
4412 typedef struct _CubeData CubeData;
4414 struct _CubeDataVector
4419 typedef struct _CubeDataVector CubeDataVector;
4437 static int sinfo_kappa_sigma_offset_with_mask(
4441 cpl_imagelist** inputCubes,
4442 const double* exptimes,
4443 cpl_imagelist* imResult,
4446 cpl_imagelist* sky_mask,
4450 sinfo_kappa_sigma_CubeDataVector(
4453 CubeDataVector* pCubeDataVector,
4454 cpl_imagelist* imlistResult,
4455 cpl_imagelist** input_cubes,
4456 cpl_imagelist* sky_mask,
4460 const double* exptimes
4464 sinfo_kappa_sigma_array_with_mask(cpl_array* parray,
int szArray,
4465 const double kappa,cpl_image* imMask,
4466 const double* exptimes,
int x,
int y,
4470 int nInvalidPoints = 0;
4471 const double EPS = 1E-10;
4473 double mask_adjustment = mask_delta;
4481 check_nomsg(median = cpl_array_get_median(parray));
4482 check_nomsg(sig = cpl_array_get_stdev(parray));
4483 for (z = 0; z < szArray; z++)
4487 check_nomsg(value = cpl_array_get(parray, z, &isnull));
4490 if (fabs(value - median) > (kappa * sig))
4500 cpl_array_fill_window_invalid(parray, z, 1);
4501 mask_adjustment += exptimes[z];
4512 while (nInvalidPoints);
4513 if(imMask && fabs(mask_adjustment) > EPS)
4516 int px_rejected = 0;
4517 double msk_value = 0;
4518 check_nomsg(msk_value = cpl_image_get(imMask, x, y, &px_rejected));
4519 check_nomsg(cpl_image_set(imMask, x,y, msk_value - mask_adjustment));
4522 check_nomsg(result = cpl_array_get_mean(parray));
4525 sinfo_msg(
"Error in sinfo_kappa_sigma_array_with_mask");
4530 int sinfo_coadd_with_ks_clip_optimized(
4538 cpl_imagelist* sky_mask,
4539 cpl_imagelist* mergedCube,
4540 cpl_imagelist** tmpcubes
4548 check_nomsg(result=sinfo_kappa_sigma_offset_with_mask(z_min, z_max, n_cubes, tmpcubes, exptimes, mergedCube, llx, lly, sky_mask, kappa));
4556 static int sinfo_kappa_sigma_offset_with_mask(
4560 cpl_imagelist** inputCubes,
4561 const double* exptimes,
4562 cpl_imagelist* imResult,
4563 int* global_offsetX,
4564 int* global_offsetY,
4565 cpl_imagelist* sky_mask,
4569 const int BIG_ENOUGH_INT = 65535;
4570 CubeDataVector*** indexX = 0;
4574 int iPlanesNumber = z_max - z_min;
4575 int nIndexXbytes = 0;
4576 int globalSizeX = 0 ;
4577 int globalSizeY = 0;
4579 int xmax = -BIG_ENOUGH_INT;
4580 int ymax = -BIG_ENOUGH_INT;
4581 int xmin = BIG_ENOUGH_INT;
4582 int ymin = BIG_ENOUGH_INT;
4587 sinfo_check_rec_status(0);
4588 for (z = 0; z < nCubes; z++)
4591 cpl_imagelist* pCube = inputCubes[z];
4592 cpl_image* pImage = 0;
4598 pImage = cpl_imagelist_get(pCube, 0);
4600 localMaxX = cpl_image_get_size_x(pImage) + global_offsetX[z];
4601 localMaxY = cpl_image_get_size_y(pImage) + global_offsetY[z];
4602 localMinX = global_offsetX[z];
4603 localMinY = global_offsetY[z];
4605 if(localMaxX > xmax) xmax = localMaxX;
4606 if(localMaxY > ymax) ymax = localMaxY;
4608 if(localMinX < xmin) xmin = localMinX;
4609 if(localMinY < ymin) ymin = localMinY;
4611 sinfo_check_rec_status(1);
4618 cpl_image * pmaskimage = cpl_imagelist_get(sky_mask, 0);
4619 msize_x = cpl_image_get_size_x(pmaskimage);
4620 msize_y = cpl_image_get_size_y(pmaskimage);
4621 xmax = msize_x < xmax ? msize_x : xmax;
4622 ymax = msize_y < ymax ? msize_y : ymax;
4629 check_nomsg(offsetX = cpl_malloc(
sizeof(offsetX[0]) * nCubes));
4630 check_nomsg(offsetY = cpl_malloc(
sizeof(offsetY[0]) * nCubes));
4631 sinfo_check_rec_status(2);
4632 for (z = 0; z < nCubes; z++)
4634 offsetX[z] = global_offsetX[z];
4635 offsetY[z] = global_offsetY[z];
4638 sinfo_check_rec_status(3);
4640 nIndexXbytes =
sizeof(CubeDataVector**) * (globalSizeX+1 );
4642 indexX = cpl_malloc(nIndexXbytes);
4643 memset(&indexX[0], 0, (globalSizeX+1 )*
sizeof(indexX[0]));
4647 for (z = 0; z < nCubes; z++)
4654 cpl_imagelist* pCube = inputCubes[z];
4655 cpl_image* pImage = 0;
4656 pImage = cpl_imagelist_get(pCube, 0);
4658 iCubeSizeX = cpl_image_get_size_x(pImage);
4659 iCubeSizeY = cpl_image_get_size_y(pImage);
4660 iOffsetX = offsetX[z];
4661 iOffsetY = offsetY[z];
4663 for (x = 1; x <= iCubeSizeX; x++)
4665 int iGlobalX = x + iOffsetX;
4667 CubeDataVector** ppVector = 0;
4668 if (indexX[iGlobalX - 1] == 0)
4671 int nBytes =
sizeof(CubeDataVector*) * (globalSizeY+1 );
4672 ppVector= cpl_malloc(nBytes);
4673 memset(&ppVector[0],0,(globalSizeY+1) *
sizeof(ppVector[0]));
4674 indexX[iGlobalX - 1] = ppVector;
4678 ppVector = indexX[iGlobalX - 1];
4680 for (y = 1; y <=iCubeSizeY; y++)
4682 CubeData* pCubeData = 0;
4683 int iGlobalY = y + iOffsetY;
4684 CubeDataVector* pVector = ppVector[iGlobalY - 1];
4687 int nbytes =
sizeof(CubeDataVector);
4688 check_nomsg(pVector = cpl_malloc(nbytes));
4689 ppVector[iGlobalY - 1] = pVector;
4691 nbytes =
sizeof(CubeData*) * nCubes;
4692 pVector->pdata = cpl_malloc(nbytes);
4695 pCubeData = cpl_malloc(
sizeof(CubeData));
4696 pVector->pdata[(pVector->size)++] = pCubeData;
4697 pCubeData->iCubeNumber = z;
4698 pCubeData->iLocalX = x;
4699 pCubeData->iLocalY = y;
4703 sinfo_check_rec_status(4);
4706 for (x = 1; x <= globalSizeX; x++)
4708 CubeDataVector** pDataX = indexX[x - 1];
4711 for (y = 1; y <= globalSizeY; y++)
4713 CubeDataVector* pDataY = pDataX[y - 1];
4714 if (pDataY && pDataY->size)
4716 sinfo_kappa_sigma_CubeDataVector(x, y, pDataY, imResult,
4717 inputCubes, sky_mask, iPlanesNumber, z_min,
4722 check_nomsg(cpl_free(pDataY->pdata));
4723 check_nomsg(cpl_free(pDataY));
4726 check_nomsg(cpl_free(pDataX));
4729 sinfo_check_rec_status(5);
4739 sinfo_kappa_sigma_CubeDataVector(
4742 CubeDataVector* pCubeDataVector,
4743 cpl_imagelist* imlistResult,
4744 cpl_imagelist** input_cubes,
4745 cpl_imagelist* sky_mask,
4749 const double* exptimes
4756 cpl_array* pArray = 0;
4757 check_nomsg(pArray = cpl_array_new(pCubeDataVector->size, CPL_TYPE_DOUBLE));
4760 for (plane = z_min; plane < z_min + iPlanesNumber; plane++)
4764 cpl_image* imResult = 0;
4765 cpl_image* imMask = 0;
4766 double mask_adjustment = 0;
4767 int nValidPoints = 0;
4768 cpl_array_fill_window_invalid(pArray, 0, pCubeDataVector->size);
4769 check_nomsg(imMask = cpl_imagelist_get(sky_mask, plane - z_min));
4771 for (z = 0; z < pCubeDataVector->size; z++)
4774 cpl_imagelist* pCube = 0;
4775 CubeData* pCubeData = pCubeDataVector->pdata[z];
4776 pCube = input_cubes[pCubeData->iCubeNumber];
4779 cpl_image* pImage = cpl_imagelist_get(pCube, plane - z_min);
4783 int is_rejected = 0;
4785 check_nomsg(value = cpl_image_get(pImage, pCubeData->iLocalX,
4786 pCubeData->iLocalY, &is_rejected));
4789 check_nomsg(cpl_array_set(pArray, z, value));
4794 mask_adjustment += exptimes[z];
4799 sinfo_msg(
"pImage is null");
4805 sinfo_kappa_sigma_array_with_mask(pArray, pCubeDataVector->size,
4806 kappa, imMask, exptimes, globalX, globalY,
4808 check_nomsg(imResult = cpl_imagelist_get(imlistResult, plane));
4811 check_nomsg(cpl_image_set(imResult, globalX, globalY,
4812 cpl_array_get_mean(pArray)));
4816 sinfo_msg(
"imResult is null");
4821 check_nomsg(cpl_image_set(imMask, globalX,globalY, 0));
4824 for (z = 0; z < pCubeDataVector->size; z++)
4826 CubeData* pCubeData = pCubeDataVector->pdata[z];
4827 cpl_free(pCubeData);
4829 cpl_array_delete(pArray);
4838 sinfo_coadd_with_ks_clip(
const int z_min,
4847 cpl_imagelist* mask,
4848 cpl_imagelist* mergedCube,
4849 cpl_imagelist** tmpcubes)
4872 float val_msk_sum=0;
4878 cpl_image* t_img=NULL;
4879 cpl_image* v_img=NULL;
4882 cpl_vector* val=NULL;
4883 cpl_vector* msk=NULL;
4886 cpl_image* o_img=cpl_imagelist_get(mergedCube,0);
4887 olx=cpl_image_get_size_x(o_img);
4888 oly=cpl_image_get_size_y(o_img);
4891 for ( z = z_min; z < z_max ; z++ ) {
4892 cpl_image* m_img=cpl_imagelist_get(mask,z);
4893 pmdata=cpl_image_get_data_float(m_img);
4894 o_img=cpl_imagelist_get(mergedCube,z);
4895 float* podata=cpl_image_get_data_float(o_img);
4896 int mlx=cpl_image_get_size_x(m_img);
4899 for ( y = 0 ; y < oly ; y++ ) {
4900 for ( x = 0 ; x < olx ; x++ ) {
4904 for ( i = 0 ; i < n_cubes ; i++ ) {
4905 t_img=cpl_imagelist_get(tmpcubes[i],m);
4906 ptdata=cpl_image_get_data_float(t_img);
4907 if ( y >= lly[i] && y < lly[i]+ily &&
4908 x >= llx[i] && x < llx[i]+ilx ) {
4911 if (!isnan(ptdata[posx+posy*ilx]) &&
4912 ptdata[posx+posy*ilx] != 0.) {
4920 msk=cpl_vector_new(n_cubes);
4921 for (i=0;i<n_cubes;i++) {
4922 cpl_vector_set(msk,i,1);
4929 for (ks=0;ks<nc;ks++) {
4934 val=cpl_vector_new(nc-nclip);
4937 for ( i = 0 ; i < n_cubes ; i++ ) {
4938 t_img=cpl_imagelist_get(tmpcubes[i],m);
4939 ptdata=cpl_image_get_data_float(t_img);
4940 if ( y >= lly[i] && y < lly[i]+ily &&
4941 x >= llx[i] && x < llx[i]+ilx ) {
4944 if (!isnan(ptdata[posx+posy*ilx]) &&
4945 ptdata[posx+posy*ilx] != 0. &&
4946 (cpl_vector_get(msk,i) != 0)) {
4947 cpl_vector_set(val,ovr,(
double)ptdata[posx+posy*ilx]);
4956 med=cpl_vector_get_median_const(val);
4958 sig=cpl_vector_get_stdev(val);
4962 cpl_vector_delete(val);
4965 for ( i = 0 ; i < n_cubes ; i++ ) {
4966 t_img=cpl_imagelist_get(tmpcubes[i],m);
4967 ptdata=cpl_image_get_data_float(t_img);
4969 if ( y >= lly[i] && y < lly[i]+ily &&
4970 x >= llx[i] && x < llx[i]+ilx ) {
4973 if (!isnan(ptdata[posx+posy*ilx]) &&
4974 ptdata[posx+posy*ilx] != 0. &&
4975 (cpl_vector_get(msk,i) != 0)) {
4976 if(abs((ptdata[posx+posy*ilx]-med))> kappa*sig) {
4977 ptdata[posx+posy*ilx]=0;
4978 pmdata[x+y*mlx] -= exptimes[i] ;
4979 cpl_vector_set(msk,i,0);
4989 for ( i = 0 ; i < n_cubes ; i++ ) {
4990 v_img=cpl_imagelist_get(tmpcubes[i],m);
4991 pvdata=cpl_image_get_data_float(v_img);
4993 if ( y >= lly[i] && y < lly[i]+ily &&
4994 x >= llx[i] && x < llx[i]+ilx ) {
4999 if (!isnan(pvdata[posx+posy*ilx]) &&
5000 pvdata[posx+posy*ilx] != 0. &&
5001 (cpl_vector_get(msk,i) != 0)) {
5002 msk_sum+=pmdata[x+y*mlx];
5003 val_msk_sum+=pvdata[posx+posy*ilx]*
5008 podata[x+y*olx]=val_msk_sum/msk_sum;
5009 cpl_vector_delete(msk);
5031 sinfo_compute_contributes_at_pos(cpl_imagelist** tmpcubes,
5033 const int x,
const int y,
5034 const int ilx,
const int ily,
5035 const int m,
const int n_cubes)
5047 for ( i = 0 ; i < n_cubes ; i++ ) {
5048 cpl_image* t_img=cpl_imagelist_get(tmpcubes[i],m);
5049 float* ptdata=cpl_image_get_data_float(t_img);
5055 if ( y >= loy && y < upy && x >= lox && x < upx ) {
5058 post = posx+posy*ilx;
5060 if (!isnan(ptdata[post]) && ptdata[post] != 0.) {
5075 sinfo_cubes_coadd_with_ks_clip(cpl_imagelist** tmpcubes,
5076 const int n_cubes,
const int nc,
5077 const int x,
const int y,
const int m,
5079 const int ilx,
const int ily,
5081 double* exptimes,
float** pmdata,
5082 cpl_vector** msk,
const int mlx)
5088 cpl_vector* val=NULL;
5089 cpl_image* t_img=NULL;
5117 for (ks=0;ks<nc;ks++) {
5122 check_nomsg(val=cpl_vector_new(nc-nclip));
5125 for ( i = 0 ; i < n_cubes ; i++ ) {
5126 check_nomsg(t_img=cpl_imagelist_get(tmpcubes[i],m));
5127 check_nomsg(ptdata=cpl_image_get_data_float(t_img));
5134 if ( y >= loy && y < upy && x >= lox && x < upx ) {
5139 if (!isnan(ptdata[post]) && ptdata[post] != 0. &&
5140 (cpl_vector_get(*msk,i) != 0)) {
5141 cpl_vector_set(val,ovr,(
double)ptdata[post]);
5150 med=cpl_vector_get_median_const(val);
5152 sig=cpl_vector_get_stdev(val);
5156 cpl_vector_delete(val);
5159 for ( i = 0 ; i < n_cubes ; i++ ) {
5160 t_img=cpl_imagelist_get(tmpcubes[i],m);
5161 ptdata=cpl_image_get_data_float(t_img);
5169 if ( y >= loy && y < upy && x >= lox && x < upx ) {
5172 post = posx+posy*ilx;
5173 if (!isnan(ptdata[post]) && ptdata[post] != 0. &&
5174 (cpl_vector_get(*msk,i) != 0)) {
5175 if( abs( (ptdata[post]-med) ) > kappa*sig ) {
5177 (*pmdata)[x+y*mlx] -= exptimes[i] ;
5178 check_nomsg(cpl_vector_set(*msk,i,0));
5214 sinfo_coadd_with_ks_clip2(
const int z_min,
5223 cpl_imagelist* mask,
5224 cpl_imagelist* mergedCube,
5225 cpl_imagelist** tmpcubes)
5243 float val_msk_sum=0;
5249 cpl_image* o_img=NULL;
5250 cpl_image* v_img=NULL;
5253 cpl_vector* msk=NULL;
5256 o_img=cpl_imagelist_get(mergedCube,0);
5257 olx=cpl_image_get_size_x(o_img);
5258 oly=cpl_image_get_size_y(o_img);
5261 for ( z = z_min; z < z_max ; z++ ) {
5262 cpl_image* m_img=cpl_imagelist_get(mask,z);
5263 pmdata=cpl_image_get_data_float(m_img);
5264 o_img=cpl_imagelist_get(mergedCube,z);
5265 float* podata=cpl_image_get_data_float(o_img);
5266 int mlx=cpl_image_get_size_x(m_img);
5269 for ( y = 0 ; y < oly ; y++ ) {
5270 for ( x = 0 ; x < olx ; x++ ) {
5275 nc=sinfo_compute_contributes_at_pos(tmpcubes,llx,lly,x,y,
5281 msk=cpl_vector_new(n_cubes);
5282 for (i=0;i<n_cubes;i++) {
5283 cpl_vector_set(msk,i,1);
5287 sinfo_cubes_coadd_with_ks_clip(tmpcubes, n_cubes,nc,x,y,m,
5288 llx,lly,ilx,ily,kappa,
5289 exptimes,&pmdata, &msk,mlx);
5293 for ( i = 0 ; i < n_cubes ; i++ ) {
5294 v_img=cpl_imagelist_get(tmpcubes[i],m);
5295 pvdata=cpl_image_get_data_float(v_img);
5297 if ( y >= lly[i] && y < lly[i]+ily &&
5298 x >= llx[i] && x < llx[i]+ilx ) {
5303 if (!isnan(pvdata[posx+posy*ilx]) &&
5304 pvdata[posx+posy*ilx] != 0. &&
5305 (cpl_vector_get(msk,i) != 0)) {
5306 msk_sum+=pmdata[x+y*mlx];
5307 val_msk_sum+=pvdata[posx+posy*ilx]*
5312 podata[x+y*olx]=val_msk_sum/msk_sum;
5313 cpl_vector_delete(msk);
5332 sinfo_imagelist_reject_value(cpl_imagelist* iml,cpl_value value)
5334 int sz=cpl_imagelist_get_size(iml);
5337 cpl_image* ima=cpl_imagelist_get(iml,i);
5338 cpl_image_reject_value(ima, CPL_VALUE_NAN);
5340 return cpl_error_get_code();
#define sinfo_msg_debug(...)
Print a debug message.
#define sinfo_msg_error(...)
Print an error message.
#define sinfo_msg_warning(...)
Print an warning message.