188 #define POSIX_SOURCE 1
189 #include "sinfo_vltPort.h"
198 #include "sinfo_pfits.h"
199 #include "sinfo_spectrum_ops.h"
200 #include "sinfo_resampling.h"
201 #include "sinfo_utilities.h"
202 #include "sinfo_utils_wrappers.h"
224 int sinfo_stectrum_ima2table(
225 const cpl_image* spc,
226 const char* filename,
229 const float* pidata=NULL;
240 cpl_propertylist* plist=NULL;
247 pidata = cpl_image_get_data_const(spc);
248 nx=cpl_image_get_size_x(spc);
249 ny=cpl_image_get_size_y(spc);
251 if((nx == 0) || (ny == 0)) {
255 if((nx > 1) && (ny > 1)) {
262 *tbl = cpl_table_new(nraw);
263 cpl_table_new_column(*tbl,
"WAVE",CPL_TYPE_DOUBLE);
264 cpl_table_new_column(*tbl,
"INT",CPL_TYPE_DOUBLE);
267 if ((cpl_error_code)((plist = cpl_propertylist_load(filename, 0)) == NULL)) {
269 cpl_propertylist_delete(plist) ;
275 step=sinfo_pfits_get_cdelt1(plist);
276 wc=sinfo_pfits_get_crval1(plist);
279 step=sinfo_pfits_get_cdelt2(plist);
280 wc=sinfo_pfits_get_crval2(plist);
286 sinfo_msg(
"ws=%f we=%f step=%f",ws,we,step);
287 cpl_table_set_double(*tbl,
"WAVE",0,wav);
288 cpl_table_set_double(*tbl,
"INT",0,pidata[i]);
290 for(i=1;i<nraw;i++) {
292 double amp=(double)pidata[i];
293 cpl_table_set_double(*tbl,
"WAVE",i,wav);
294 cpl_table_set_double(*tbl,
"INT",i,amp);
296 cpl_propertylist_delete(plist);
313 Vector * sinfo_new_vector( ulong32 n_elements )
315 Vector * local_new_vector ;
317 if ( n_elements <= 0 )
324 local_new_vector = (Vector *) cpl_malloc (
sizeof (Vector)) ;
325 local_new_vector -> n_elements = n_elements ;
326 local_new_vector -> data = (pixelvalue *) cpl_calloc (n_elements,
327 sizeof (pixelvalue)) ;
329 return local_new_vector ;
339 void sinfo_free_svector( Vector **svector )
341 if ( *svector != NULL )
344 if((*svector) -> data != NULL) {
345 cpl_free ( (*svector) -> data ) ;
346 (*svector)->data = NULL;
348 cpl_free ( *svector ) ;
361 void sinfo_new_destroy_vector( Vector *sinfo_vector )
363 if ( sinfo_vector == NULL )
369 cpl_free ( sinfo_vector -> data ) ;
370 cpl_free ( sinfo_vector ) ;
381 cpl_image * sinfo_new_vector_to_image( Vector * spectrum )
383 cpl_image * returnIm ;
389 if ( spectrum == NULL )
396 if ( NULL == (returnIm = cpl_image_new(1, spectrum->n_elements,
400 sinfo_new_destroy_vector(spectrum) ;
404 podata=cpl_image_get_data_float(returnIm);
405 for ( i = 0 ; i < spectrum->n_elements ; i++ )
407 podata[i] = spectrum -> data[i] ;
410 sinfo_new_destroy_vector (spectrum) ;
422 Vector * sinfo_new_image_to_vector( cpl_image * spectrum )
424 Vector * returnVector ;
431 if ( spectrum == NULL )
436 ilx=cpl_image_get_size_x(spectrum);
437 ily=cpl_image_get_size_y(spectrum);
440 if ( NULL == (returnVector = sinfo_new_vector(ilx*ily)) )
443 cpl_image_delete(spectrum) ;
447 pidata=cpl_image_get_data_float(spectrum);
448 for ( i = 0 ; i < (int) ilx*ily ; i++ )
450 returnVector -> data[i] = pidata[i] ;
453 cpl_image_delete (spectrum) ;
454 return returnVector ;
470 sinfo_new_extract_spectrum_from_resampled_flat( cpl_image * resflat,
485 if ( resflat == NULL )
490 ilx=cpl_image_get_size_x(resflat);
491 ily=cpl_image_get_size_y(resflat);
494 if ( NullVector == (spectrum = sinfo_new_vector(ily) ) )
500 array=cpl_calloc(ily,
sizeof(
float)) ;
502 pidata=cpl_image_get_data_float(resflat);
503 for ( row = 0 ; row < ily ; row++ )
506 for ( col = 0 ; col < ilx ; col++ )
508 if ( !isnan(pidata[col + row*ilx]) )
510 array[n] = pidata[col+row*ilx] ;
521 if ( FLT_MAX == (cleanMean = sinfo_new_clean_mean(array, n,
526 sinfo_new_destroy_vector(spectrum) ;
530 spectrum->data[row] = cleanMean ;
532 if ( NULL == ( retIm = sinfo_new_vector_to_image( spectrum ) ) )
535 sinfo_new_destroy_vector(spectrum) ;
557 sinfo_new_multiply_image_with_spectrum( cpl_image * image,
558 cpl_image * spectrum )
561 cpl_image * retImage ;
579 ilx=cpl_image_get_size_x(image);
580 ily=cpl_image_get_size_y(image);
582 if ( spectrum == NULL )
588 sly=cpl_image_get_size_y(spectrum);
596 if ( NULL == (retImage = cpl_image_duplicate(image)) )
602 pidata=cpl_image_get_data_float(image);
603 psdata=cpl_image_get_data_float(spectrum);
604 podata=cpl_image_get_data_float(retImage);
606 for ( col = 0 ; col < ilx ; col++ )
608 for ( row = 0 ; row < ily ; row++ )
610 if ( !isnan(pidata[col+row*ilx]) &&
611 !isnan(psdata[col+row*ilx]))
613 podata[col+row*ilx] = pidata[col+row*ilx] * psdata[row] ;
659 cpl_image * sinfo_new_optimal_extraction_from_cube( cpl_imagelist * cube,
670 cpl_table** spectrum,
675 cpl_image * averagedIm ;
681 double gderv_par[7] ;
686 double** weight=NULL ;
687 double** sinfo_psf=NULL ;
692 int first_col, last_col ;
693 int first_row, last_row ;
699 float dispersion = 0;
700 float lambda_start=0;
708 cpl_propertylist* plist=NULL;
721 ilx=cpl_image_get_size_x(cpl_imagelist_get(cube,0));
722 ily=cpl_image_get_size_y(cpl_imagelist_get(cube,0));
723 inp=cpl_imagelist_get_size(cube);
725 if ( llx < 0 || llx + 2*halfbox_x >= ilx ||
726 lly < 0 || lly + 2*halfbox_y >= ily )
728 sinfo_msg(
"llx=%d, lly=%d, llx + 2*halfbox_x=%d, "
729 "lly + 2*halfbox_y=%d",
730 llx,lly,llx + 2*halfbox_x,lly + 2*halfbox_y);
731 sinfo_msg(
"tresh_min_x=%d, tresh_min_y=%d, "
732 "tresh_max_x=%d, tresh_max_y=%d",0,0,ilx,ily);
736 if ( halfbox_x <= 0 || halfbox_y <= 0 ||
737 2*halfbox_x > ilx || 2*halfbox_y > ily )
742 if ( fwhm_factor <= 0. )
747 if ( backvariance < 0. )
752 if ( exptime <= 0. || exptime == FLAG )
759 if ( NULL == (retIm = cpl_image_new(1, inp,CPL_TYPE_FLOAT)) )
766 if ( NULL == (averagedIm = sinfo_new_average_cube_to_image(cube)) )
769 cpl_image_delete(retIm) ;
774 for ( i = 0 ; i < 7 ; i++ )
779 if ( -1 == (fitInd = sinfo_new_fit_2d_gaussian(averagedIm,
790 cpl_image_delete(retIm) ;
791 cpl_image_delete(averagedIm) ;
796 sinfo_psf=sinfo_new_2Ddoublearray(ilx,ily) ;
798 for ( row = 0 ; row < ily ; row++ )
800 for ( col = 0 ; col < ilx ; col++ )
802 xdat[0] = (double) col ;
803 xdat[1] = (double) row ;
804 sinfo_psf[col][row] = sinfo_new_gaussian_ellipse(xdat,fit_par) -
806 sum += sinfo_psf[col][row] ;
815 weight=sinfo_new_2Ddoublearray(ilx,ily) ;
817 padata=cpl_image_get_data_float(averagedIm);
818 for ( row = 0 ; row < ily ; row++ )
820 for ( col = 0 ; col < ilx ; col++ )
822 sinfo_psf[col][row] /= sum ;
823 sum_psf += sinfo_psf[col][row];
824 if ( !isnan(padata[col+row*ilx]) )
830 variance = padata[col+row*ilx] / gain ;
835 weight[col][row] = 0. ;
839 weight[col][row] = 0. ;
844 weight[col][row] = sinfo_psf[col][row]/variance ;
846 norm += weight[col][row] * weight[col][row] * variance ;
854 for ( row = 0 ; row < ily ; row++ )
856 for ( col = 0 ; col < ilx ; col++ )
858 weight[col][row] /= norm;
859 sum_wgt += weight[col][row]*sinfo_psf[col][row];
863 cpl_image_delete(averagedIm) ;
867 cpl_image_delete(retIm) ;
880 if(fabs(fit_par[6]) > PI_NUMB/4) {
883 first_col = (int) (fit_par[0] -
884 fwhm_factor*fit_par[4]*cos((
double)fit_par[6])) ;
885 first_col = (first_col > 2 ) ? first_col : 2;
887 last_col = (int) (fit_par[0] +
888 fwhm_factor*fit_par[4]*cos((
double)fit_par[6])) ;
889 last_col = (last_col < 63 ) ? last_col : 63;
891 first_row = (int) (fit_par[1] -
892 fwhm_factor*fit_par[5]*cos((
double)fit_par[6])) ;
893 first_row = (first_row > 2 ) ? first_row : 2;
894 last_row = (int) (fit_par[1] +
895 fwhm_factor*fit_par[5]*cos((
double)fit_par[6])) ;
896 last_row = (last_row < 63 ) ? last_row : 63;
899 if(first_col > last_col) {
905 if(first_row > last_row) {
910 if(abs(first_col- last_col) < 1) {
914 if(abs(first_row- last_row) < 1) {
919 if ( first_col < 0 || first_row < 0 || last_col >= ilx || last_row >= ily )
922 cpl_image_delete(retIm) ;
927 cpl_table_new_column(*spectrum,
"wavelength", CPL_TYPE_FLOAT);
929 cpl_table_new_column(*spectrum,
"counts_tot" , CPL_TYPE_FLOAT);
930 cpl_table_new_column(*spectrum,
"counts_bkg" , CPL_TYPE_FLOAT);
931 cpl_table_new_column(*spectrum,
"bkg_tot" , CPL_TYPE_FLOAT);
934 cpl_table_new_column(*spectrum,
"AMP" , CPL_TYPE_FLOAT);
935 cpl_table_new_column(*spectrum,
"XC" , CPL_TYPE_FLOAT);
936 cpl_table_new_column(*spectrum,
"YC" , CPL_TYPE_FLOAT);
937 cpl_table_new_column(*spectrum,
"BKG" , CPL_TYPE_FLOAT);
938 cpl_table_new_column(*spectrum,
"FWHMX" , CPL_TYPE_FLOAT);
939 cpl_table_new_column(*spectrum,
"FWHMY" , CPL_TYPE_FLOAT);
940 cpl_table_new_column(*spectrum,
"ANGLE" , CPL_TYPE_FLOAT);
942 plist=cpl_propertylist_load(name,0);
943 cenpix = sinfo_pfits_get_crpix3(plist);
944 cenLambda = sinfo_pfits_get_crval3(plist);
945 dispersion = sinfo_pfits_get_cdelt3(plist);
946 cpl_propertylist_delete(plist);
947 lambda_start=cenLambda-cenpix*dispersion;
950 first_row, last_row, first_col, last_col);
952 podata=cpl_image_get_data_float(retIm);
953 for ( z = 0 ; z < inp ; z++ )
955 cpl_image* i_img=cpl_imagelist_get(cube,z);
956 float* pidata=cpl_image_get_data_float(i_img);
957 float weighted_sum = 0. ;
964 sinfo_new_fit_2d_gaussian(i_img,gfit_par,
965 gderv_par,gmpar,llx,lly,
966 halfbox_x,halfbox_y,check2);
969 for ( row = first_row ; row <= last_row ; row++ )
971 for ( col = first_col ; col < last_col ; col++ )
973 if ( !isnan(pidata[col+row*ilx]) )
976 weighted_sum += weight[col][row] * (pidata[col+row*ilx] -
979 counts_bkg += (pidata[col+row*ilx] - fit_par[3]);
980 counts_tot += (pidata[col+row*ilx]);
981 bkg_tot += fit_par[3];
987 if (weighted_sum == 0.)
989 weighted_sum = ZERO ;
1004 podata[z] = weighted_sum ;
1005 float lambda=lambda_start+z*dispersion;
1006 cpl_table_set_float(*spectrum,
"wavelength" ,z,lambda);
1008 cpl_table_set_float(*spectrum,
"counts_tot" ,z,counts_tot);
1009 cpl_table_set_float(*spectrum,
"counts_bkg" ,z,counts_bkg);
1010 cpl_table_set_float(*spectrum,
"bkg_tot" ,z,bkg_tot);
1012 lambda,counts_tot,counts_bkg,bkg_tot);
1014 cpl_table_set_float(*spectrum,
"AMP" ,z,gfit_par[0]);
1015 cpl_table_set_float(*spectrum,
"XC" ,z,gfit_par[1]);
1016 cpl_table_set_float(*spectrum,
"YC" ,z,gfit_par[2]);
1017 cpl_table_set_float(*spectrum,
"BKG" ,z,gfit_par[3]);
1018 cpl_table_set_float(*spectrum,
"FWHMX" ,z,gfit_par[4]);
1019 cpl_table_set_float(*spectrum,
"FWHMY" ,z,gfit_par[5]);
1020 cpl_table_set_float(*spectrum,
"ANGLE" ,z,gfit_par[6]);
1025 sinfo_new_destroy_2Ddoublearray(&sinfo_psf,ilx) ;
1026 sinfo_new_destroy_2Ddoublearray(&weight,ilx) ;
1054 Vector * sinfo_new_extract_sky_from_cube( cpl_imagelist * cube,
1064 int x_low , x_high ;
1065 int y_low , y_high ;
1073 ilx=cpl_image_get_size_x(cpl_imagelist_get(cube,0));
1074 ily=cpl_image_get_size_y(cpl_imagelist_get(cube,0));
1075 inp=cpl_imagelist_get_size(cube);
1082 if ( loReject < 0. || hiReject < 0. || loReject + hiReject >= 90. )
1084 sinfo_msg_error(
"wrong or unrealistic loReject and hiReject values!") ;
1087 if ( position == NULL)
1092 if ( position[0] < 0 || position[1] < 0 ||
1093 position[0] > ilx || position[1] > ily )
1098 if ( tolerance < 0 || tolerance >= ilx )
1103 if ( posindicator == 0 )
1110 switch(posindicator)
1114 x_low = position[0] + tolerance ;
1117 y_high = position[1] - tolerance ;
1121 x_low = position[0] + tolerance ;
1123 y_low = position[1] + tolerance ;
1129 x_high = position[0] - tolerance ;
1130 y_low = position [1] + tolerance ;
1138 if ( x_low >= ilx || x_high < 1 || y_low >= ily || y_high < 1 )
1143 if ( x_high - x_low != y_high - y_low )
1151 n_sky = (x_high - x_low) * (x_high - x_low - 1) / 2 ;
1163 if ( NullVector == (spectrum = sinfo_new_vector(inp)) )
1170 for ( z = 0 ; z < inp ; z++ )
1172 cpl_image* i_img=cpl_imagelist_get(cube,z);
1173 pidata=cpl_image_get_data_float(i_img);
1175 float* to_average=NULL;
1176 if (NULL == (to_average = (
float*) cpl_calloc(n_sky,
sizeof (
float))))
1179 sinfo_new_destroy_vector(spectrum) ;
1183 switch(posindicator)
1188 for ( y = y_low ; y < y_high - 1 ; y++ )
1191 for ( x = lo_x ; x < x_high ; x++ )
1193 to_average[n] = pidata[x+y*ilx] ;
1201 for ( y = y_low ; y < y_high - 1 ; y++ )
1204 for ( x = x_low ; x < hi_x ; x++ )
1206 to_average[n] = pidata[x+y*ilx] ;
1214 for ( y = y_low+1 ; y < y_high ; y++ )
1217 for ( x = lo_x ; x < x_high ; x++ )
1219 to_average[n] = pidata[x+y*ilx] ;
1227 for ( y = y_low+1 ; y < y_high ; y++ )
1230 for ( x = x_low ; x < hi_x ; x++ )
1232 to_average[n] = pidata[x+y*ilx] ;
1239 cpl_free(to_average) ;
1246 "not equal number of computed sky pixels!") ;
1250 float cleanMean = sinfo_new_clean_mean (to_average, n, loReject, hiReject) ;
1251 if (cleanMean == FLT_MAX)
1254 sinfo_new_destroy_vector(spectrum) ;
1255 cpl_free(to_average) ;
1258 spectrum->data[z] = cleanMean ;
1259 cpl_free (to_average) ;
1278 Vector * sinfo_new_sum_rectangle_of_cube_spectra( cpl_imagelist * cube,
1294 ilx=cpl_image_get_size_x(cpl_imagelist_get(cube,0));
1295 ily=cpl_image_get_size_y(cpl_imagelist_get(cube,0));
1296 inp=cpl_imagelist_get_size(cube);
1298 if ( cube == NULL || inp < 1 )
1304 if ((llx<0) || (llx>=ilx) ||
1305 (urx<0) || (urx>=ilx) ||
1306 (lly<0) || (lly>=ily) ||
1307 (ury<0) || (ury>=ily) ||
1308 (llx>=urx) || (lly>=ury))
1312 llx, lly, urx, ury) ;
1316 recsize = (urx - llx + 1) * (ury - lly + 1) ;
1319 if (NULL == (sum = sinfo_new_vector (inp)) )
1329 for ( i = 0 ; i < inp ; i++ )
1331 cpl_image* i_img=cpl_imagelist_get(cube,i);
1332 float* pidata=cpl_image_get_data_float(i_img);
1334 pixelvalue* local_rectangle = (pixelvalue *) cpl_calloc (recsize,
1335 sizeof (pixelvalue*));
1337 for ( j = lly ; j <= ury ; j++ )
1339 for ( k = llx ; k <= urx ; k++ )
1341 local_rectangle[m] = pidata[k + j * ilx] ;
1345 for ( l = 0 ; l < recsize ; l++ )
1347 if ( isnan(local_rectangle[l]) )
1351 sum -> data[i] += local_rectangle[l] ;
1353 cpl_free ( local_rectangle ) ;
1369 Vector * sinfo_new_sum_circle_of_cube_spectra( cpl_imagelist * cube,
1383 ilx=cpl_image_get_size_x(cpl_imagelist_get(cube,0));
1384 ily=cpl_image_get_size_y(cpl_imagelist_get(cube,0));
1385 inp=cpl_imagelist_get_size(cube);
1387 if ( cube == NULL || inp < 1 )
1393 if ((centerx+radius>=ilx) ||
1394 (centery+radius>=ily) ||
1395 (centerx-radius<0) ||
1403 for ( j = centery - radius ; j <= centery + radius ; j++ )
1405 for ( k = centerx - radius ; k <= centerx + radius ; k++ )
1407 if ( (k-centerx)*(k-centerx)+(j-centery)*(j-centery) <=
1422 if (NULL == (sum = sinfo_new_vector (inp)) )
1432 for ( i = 0 ; i < inp ; i++ )
1434 cpl_image* i_img=cpl_imagelist_get(cube,i);
1435 pidata=cpl_image_get_data_float(i_img);
1437 pixelvalue* circle = (pixelvalue *) cpl_calloc (circsize,
sizeof (pixelvalue*));
1439 for ( j = centery - radius ; j <= centery + radius ; j++ )
1441 for ( k = centerx - radius ; k <= centerx + radius ; k++ )
1443 if ( (k-centerx)*(k-centerx)+(j-centery)*(j-centery) <=
1446 circle[m] = pidata[k + j * ilx] ;
1452 for ( l = 0 ; l < circsize ; l++ )
1454 if ( isnan(circle[l]) )
1458 sum -> data[i] += circle[l] ;
1478 Vector * sinfo_new_mean_rectangle_of_cube_spectra( cpl_imagelist * cube,
1492 ilx=cpl_image_get_size_x(cpl_imagelist_get(cube,0));
1493 ily=cpl_image_get_size_y(cpl_imagelist_get(cube,0));
1494 inp=cpl_imagelist_get_size(cube);
1496 if ( cube == NULL || inp < 1 )
1502 if ((llx<0) || (llx>=ilx) ||
1503 (urx<0) || (urx>=ilx) ||
1504 (lly<0) || (lly>=ily) ||
1505 (ury<0) || (ury>=ily) ||
1506 (llx>=urx) || (lly>=ury))
1510 llx, lly, urx, ury) ;
1514 recsize = (urx - llx + 1) * (ury - lly + 1) ;
1517 if (NULL == (mean = sinfo_new_vector (inp)) )
1527 for ( i = 0 ; i < inp ; i++ )
1529 cpl_image* i_img=cpl_imagelist_get(cube,i);
1530 pidata=cpl_image_get_data_float(i_img);
1532 pixelvalue* local_rectangle = (pixelvalue *) cpl_calloc (recsize,
1533 sizeof (pixelvalue*));
1535 for ( j = lly ; j <= ury ; j++ )
1537 for ( k = llx ; k <= urx ; k++ )
1539 local_rectangle[m] = pidata[k + j * ilx] ;
1544 for ( l = 0 ; l < recsize ; l++ )
1546 if ( isnan(local_rectangle[l]) )
1550 mean -> data[i] += local_rectangle[l] ;
1555 mean -> data[i] = ZERO ;
1559 mean -> data[i] /= nv ;
1561 cpl_free ( local_rectangle ) ;
1578 sinfo_new_mean_circle_of_cube_spectra( cpl_imagelist * cube,
1592 ilx=cpl_image_get_size_x(cpl_imagelist_get(cube,0));
1593 ily=cpl_image_get_size_y(cpl_imagelist_get(cube,0));
1594 inp=cpl_imagelist_get_size(cube);
1596 if ( cube == NULL || inp < 1 )
1602 if ((centerx+radius>=ilx) ||
1603 (centery+radius>=ily) ||
1604 (centerx-radius<0) ||
1612 for ( j = centery - radius ; j <= centery + radius ; j++ )
1614 for ( k = centerx - radius ; k <= centerx + radius ; k++ )
1616 if ( (k-centerx)*(k-centerx)+(j-centery)*(j-centery) <=
1631 if (NULL == (mean = sinfo_new_vector (inp)) )
1641 for ( i = 0 ; i < inp ; i++ )
1643 cpl_image* i_img=cpl_imagelist_get(cube,i);
1644 pidata=cpl_image_get_data_float(i_img);
1646 pixelvalue* circle = (pixelvalue *) cpl_calloc (circsize,
sizeof (pixelvalue*));
1648 for ( j = centery - radius ; j <= centery + radius ; j++ )
1650 for ( k = centerx - radius ; k <= centerx + radius ; k++ )
1652 if ( (k-centerx)*(k-centerx)+(j-centery)*(j-centery) <=
1655 circle[m] = pidata[k + j * ilx] ;
1662 for ( l = 0 ; l < circsize ; l++ )
1664 if ( isnan(circle[l]) )
1668 mean -> data[i] += circle[l] ;
1673 mean -> data[i] = ZERO ;
1677 mean -> data[i] /= nv ;
1694 Vector * sinfo_new_blackbody_spectrum(
char * templateSpec,
double temp )
1701 double firstLambda ;
1703 cpl_propertylist* plist=NULL;
1705 if ( NULL == templateSpec )
1716 if ((cpl_error_code)((plist=cpl_propertylist_load(templateSpec,0))==NULL)){
1718 cpl_propertylist_delete(plist) ;
1723 cenpix = sinfo_pfits_get_crpix2(plist);
1724 if(cpl_error_get_code() != CPL_ERROR_NONE) {
1726 sinfo_free_propertylist(&plist) ;
1730 cenLambda = sinfo_pfits_get_crval2(plist);
1731 if(cpl_error_get_code() != CPL_ERROR_NONE) {
1733 sinfo_free_propertylist(&plist) ;
1736 disp = sinfo_pfits_get_cdelt2(plist);
1737 if(cpl_error_get_code() != CPL_ERROR_NONE) {
1739 sinfo_free_propertylist(&plist) ;
1742 npix = sinfo_pfits_get_naxis2(plist);
1743 if(cpl_error_get_code() != CPL_ERROR_NONE) {
1745 sinfo_free_propertylist(&plist) ;
1748 sinfo_free_propertylist(&plist) ;
1751 if (NULL == (retSpec = sinfo_new_vector (npix)))
1760 firstLambda = cenLambda - cenpix * disp ;
1761 for ( n = 0 ; n < npix ; n++ )
1763 double lambda = firstLambda + disp * (double)n ;
1771 double denom = 1./(expm1(PLANCK*SPEED_OF_LIGHT/(lambda*BOLTZMANN*temp))) ;
1772 double intens = 2.*PI_NUMB*PLANCK*SPEED_OF_LIGHT*SPEED_OF_LIGHT /
1773 pow(lambda, 5) * denom ;
1774 retSpec->data[n] = intens ;
1776 double norm = retSpec->data[npix/2] ;
1777 for ( n = 0 ; n < npix ; n++ )
1779 retSpec->data[n] /= norm ;
1799 Vector * sinfo_new_median_rectangle_of_cube_spectra( cpl_imagelist * cube,
1813 ilx=cpl_image_get_size_x(cpl_imagelist_get(cube,0));
1814 ily=cpl_image_get_size_y(cpl_imagelist_get(cube,0));
1815 inp=cpl_imagelist_get_size(cube);
1817 if ( cube == NULL || inp < 1 )
1823 if ((llx<0) || (llx>=ilx) ||
1824 (urx<0) || (urx>=ilx) ||
1825 (lly<0) || (lly>=ily) ||
1826 (ury<0) || (ury>=ily) ||
1827 (llx>=urx) || (lly>=ury))
1831 llx, lly, urx, ury) ;
1835 recsize = (urx - llx + 1) * (ury - lly + 1) ;
1838 if (NULL == (med = sinfo_new_vector (inp)) )
1848 for ( i = 0 ; i < inp ; i++ )
1851 cpl_image* i_img=cpl_imagelist_get(cube,i);
1852 pidata=cpl_image_get_data_float(i_img);
1854 pixelvalue* local_rectangle=(pixelvalue *)cpl_calloc(recsize,
sizeof (pixelvalue*));
1856 for ( j = lly ; j <= ury ; j++ )
1858 for ( k = llx ; k <= urx ; k++ )
1860 if ( isnan(pidata[k+j*ilx]) )
1866 local_rectangle[m] = pidata[k + j * ilx] ;
1877 med->data[i] = sinfo_new_median(local_rectangle, m) ;
1879 cpl_free ( local_rectangle ) ;
1895 Vector * sinfo_new_median_circle_of_cube_spectra( cpl_imagelist * cube,
1908 ilx=cpl_image_get_size_x(cpl_imagelist_get(cube,0));
1909 ily=cpl_image_get_size_y(cpl_imagelist_get(cube,0));
1910 inp=cpl_imagelist_get_size(cube);
1912 if ( cube == NULL || inp < 1 )
1918 if ((centerx+radius>=ilx) ||
1919 (centery+radius>=ily) ||
1920 (centerx-radius<0) ||
1928 for ( j = centery - radius ; j <= centery + radius ; j++ )
1930 for ( k = centerx - radius ; k <= centerx + radius ; k++ )
1932 if ( (k-centerx)*(k-centerx)+(j-centery)*(j-centery) <=
1947 if (NULL == (med = sinfo_new_vector (inp)) )
1957 for ( i = 0 ; i < inp ; i++ )
1959 cpl_image* i_img=cpl_imagelist_get(cube,i);
1960 pidata=cpl_image_get_data_float(i_img);
1962 pixelvalue* circle = (pixelvalue *) cpl_calloc (circsize,
sizeof (pixelvalue*));
1964 for ( j = centery - radius ; j <= centery + radius ; j++ )
1966 for ( k = centerx - radius ; k <= centerx + radius ; k++ )
1968 if ( (k-centerx)*(k-centerx)+(j-centery)*(j-centery) <=
1971 circle[m] = pidata[k + j * ilx] ;
1978 for ( l = 0 ; l < circsize ; l++ )
1980 if ( isnan(circle[l]) )
1984 med -> data[i] += circle[l] ;
1993 med->data[i] = sinfo_new_median(circle, nv) ;
2014 sinfo_new_cleanmean_rectangle_of_cube_spectra( cpl_imagelist * cube,
2031 ilx=cpl_image_get_size_x(cpl_imagelist_get(cube,0));
2032 ily=cpl_image_get_size_y(cpl_imagelist_get(cube,0));
2033 inp=cpl_imagelist_get_size(cube);
2035 if ( cube == NULL || inp < 1 )
2041 if ((llx<0) || (llx>=ilx) ||
2042 (urx<0) || (urx>=ilx) ||
2043 (lly<0) || (lly>=ily) ||
2044 (ury<0) || (ury>=ily) ||
2045 (llx>=urx) || (lly>=ury))
2049 llx, lly, urx, ury) ;
2053 recsize = (urx - llx + 1) * (ury - lly + 1) ;
2056 if (NULL == (clean = sinfo_new_vector (inp)) )
2066 for ( i = 0 ; i < inp ; i++ )
2068 cpl_image* i_img=cpl_imagelist_get(cube,i);
2069 pidata=cpl_image_get_data_float(i_img);
2071 pixelvalue* local_rectangle=(pixelvalue *) cpl_calloc(recsize,
sizeof (pixelvalue*));
2073 for ( j = lly ; j <= ury ; j++ )
2075 for ( k = llx ; k <= urx ; k++ )
2077 if ( isnan(pidata[k+j*ilx]) )
2083 local_rectangle[m] = pidata[k + j * ilx] ;
2090 clean->data[i] = 0. ;
2094 clean->data[i] = sinfo_new_clean_mean(local_rectangle, m,
2095 lo_reject, hi_reject) ;
2097 cpl_free ( local_rectangle ) ;
2114 sinfo_new_cleanmean_circle_of_cube_spectra( cpl_imagelist * cube,
2129 ilx=cpl_image_get_size_x(cpl_imagelist_get(cube,0));
2130 ily=cpl_image_get_size_y(cpl_imagelist_get(cube,0));
2131 inp=cpl_imagelist_get_size(cube);
2133 if ( cube == NULL || inp < 1 )
2139 if ((centerx+radius>=ilx) ||
2140 (centery+radius>=ily) ||
2141 (centerx-radius<0) ||
2149 for ( j = centery - radius ; j <= centery + radius ; j++ )
2151 for ( k = centerx - radius ; k <= centerx + radius ; k++ )
2153 if ( (k-centerx)*(k-centerx)+(j-centery)*(j-centery) <=
2168 if (NULL == (clean = sinfo_new_vector (inp)) )
2178 for ( i = 0 ; i < inp ; i++ )
2180 cpl_image* i_img=cpl_imagelist_get(cube,i);
2181 pidata=cpl_image_get_data_float(i_img);
2183 pixelvalue* circle = (pixelvalue *) cpl_calloc (circsize,
sizeof (pixelvalue*));
2185 for ( j = centery - radius ; j <= centery + radius ; j++ )
2187 for ( k = centerx - radius ; k <= centerx + radius ; k++ )
2189 if ( (k-centerx)*(k-centerx)+(j-centery)*(j-centery) <=
2192 circle[m] = pidata[k + j * ilx] ;
2199 for ( l = 0 ; l < circsize ; l++ )
2201 if ( isnan(circle[l]) )
2205 clean -> data[i] += circle[l] ;
2210 clean->data[i] = 0. ;
2214 clean->data[i] = sinfo_new_clean_mean(circle, nv,
2215 lo_reject, hi_reject) ;
2234 sinfo_new_shift_array (
float * input,
int n_elements,
2235 float shift,
double * ker )
2238 int samples = KERNEL_SAMPLES ;
2243 register float * pix ;
2253 sinfo_msg_error(
" wrong number of elements in input array given!\n") ;
2257 shifted = (
float*) cpl_calloc(n_elements,
sizeof(
float)) ;
2260 if ((fabs(shift)<1e-2))
2262 for (i = 0 ; i < n_elements ; i++ )
2264 shifted[i] = input[i] ;
2269 int mid = (int)samples/(
int)2 ;
2271 for (i=1 ; i< n_elements-2 ; i++)
2273 float fx = (float)i+shift ;
2274 int px = sinfo_new_nint(fx) ;
2275 float rx = fx - (float)px ;
2278 if ((px>=1) && (px<(n_elements-2)))
2280 tabx = (int)(fabs((
float)mid * rx)) ;
2288 if (isnan(pix[i-1]))
2292 if (isnan(pix[i+1]))
2296 if (isnan(pix[i+2]))
2305 value = pix[i-1] * ker[mid+tabx] +
2306 pix[i] * ker[tabx] +
2307 pix[i+1] * ker[mid-tabx] +
2308 pix[i+2] * ker[samples-tabx-1] ;
2313 float norm = ker[mid+tabx] +
2316 ker[samples-tabx-1] ;
2317 if (fabs(norm) > 1e-4)
2333 shifted[i] = value ;
2354 sinfo_new_div_image_by_spectrum( cpl_image * image, cpl_image * spectrum )
2357 cpl_image * retImage ;
2368 if ( image == NULL )
2373 ilx=cpl_image_get_size_x(image);
2374 ily=cpl_image_get_size_y(image);
2377 if ( spectrum == NULL )
2383 sly=cpl_image_get_size_y(spectrum);
2390 if ( NULL == (retImage = cpl_image_duplicate(image)) )
2395 pidata=cpl_image_get_data_float(image);
2396 psdata=cpl_image_get_data_float(spectrum);
2397 podata=cpl_image_get_data_float(retImage);
2399 for ( col = 0 ; col < ilx ; col++ )
2401 for ( row = 0 ; row < ily ; row++ )
2403 if ( !isnan(pidata[col+row*ilx]) &&
2404 !isnan(psdata[col+row*ilx]))
2406 podata[col+row*ilx] = pidata[col+row*ilx] / psdata[row] ;
2424 Vector * sinfo_new_clean_mean_circle_of_cube_spectra(cpl_imagelist * cube,
2438 cpl_image* img=NULL;
2440 lz=cpl_imagelist_get_size(cube);
2442 if ( cube == NULL || lz < 1 )
2447 img=cpl_imagelist_get(cube,0);
2448 lx=cpl_image_get_size_x(img);
2449 ly=cpl_image_get_size_y(img);
2451 if ((centerx+radius>=lx) ||
2452 (centery+radius>=ly) ||
2453 (centerx-radius<0) ||
2461 for ( j = centery - radius ; j <= centery + radius ; j++ )
2463 for ( k = centerx - radius ; k <= centerx + radius ; k++ )
2465 if ( (k-centerx)*(k-centerx)+(j-centery)*(j-centery) <=
2480 if (NULL == (clean = sinfo_new_vector (lz)) )
2490 for ( i = 0 ; i < lz ; i++ )
2492 img=cpl_imagelist_get(cube,i);
2493 pidata=cpl_image_get_data(img);
2495 pixelvalue* circle = (pixelvalue *) cpl_calloc (circsize,
sizeof (pixelvalue*));
2497 for ( j = centery - radius ; j <= centery + radius ; j++ )
2499 for ( k = centerx - radius ; k <= centerx + radius ; k++ )
2501 if ( (k-centerx)*(k-centerx)+(j-centery)*(j-centery) <=
2504 circle[m] = pidata[k + j * lx] ;
2511 for ( l = 0 ; l < circsize ; l++ )
2513 if ( isnan(circle[l]) )
2517 clean -> data[i] += circle[l] ;
2522 clean->data[i] = 0. ;
2526 clean->data[i] = sinfo_new_clean_mean(circle, nv,
2527 lo_reject, hi_reject) ;
2547 Vector * sinfo_new_clean_mean_rectangle_of_cube_spectra( cpl_imagelist * cube,
2562 cpl_image* img=NULL;
2564 lz=cpl_imagelist_get_size(cube);
2566 if ( cube == NULL || lz < 1 )
2571 img=cpl_imagelist_get(cube,0);
2572 lx=cpl_image_get_size_x(img);
2573 ly=cpl_image_get_size_y(img);
2575 if ((llx<0) || (llx>=lx) ||
2576 (urx<0) || (urx>=lx) ||
2577 (lly<0) || (lly>=ly) ||
2578 (ury<0) || (ury>=ly) ||
2579 (llx>=urx) || (lly>=ury))
2583 llx, lly, urx, ury) ;
2587 recsize = (urx - llx + 1) * (ury - lly + 1) ;
2590 if (NULL == (clean = sinfo_new_vector (lz)) )
2600 for ( i = 0 ; i < lz ; i++ )
2603 pixelvalue* rectangle = (pixelvalue *) cpl_calloc (recsize,
sizeof (pixelvalue*));
2604 img=cpl_imagelist_get(cube,i);
2605 pidata=cpl_image_get_data(img);
2606 for ( j = lly ; j <= ury ; j++ )
2608 for ( k = llx ; k <= urx ; k++ )
2610 if ( isnan(pidata[k+j*lx]) )
2616 rectangle[m] = pidata[k + j * lx] ;
2623 clean->data[i] = 0. ;
2627 clean->data[i] = sinfo_new_clean_mean(rectangle, m,
2628 lo_reject, hi_reject) ;
2630 cpl_free ( rectangle ) ;
#define sinfo_msg_debug(...)
Print a debug message.
#define sinfo_msg_error(...)
Print an error message.
#define sinfo_msg_warning(...)
Print an warning message.