/* Routine to compute mosaic weigthed sky images. Sept 2003 Jérôme Chenevez */ #include #include #include #include #include #include #include #define NB_IMAGES 999 /* Maximum number of input images here (may be increased) */ #define XD 600 /* X dim. of input images (NL: 255) */ #define YD 600 /* Y dim. of input images (CBJ: New:525, Old:262) */ #define MOSAICSIZE 1000000 /* Declare big arrays as static ! */ int mark[MOSAICSIZE]; double pval_o[MOSAICSIZE], var_o[MOSAICSIZE], exp_o[MOSAICSIZE], var_s[MOSAICSIZE], exp_s[MOSAICSIZE], t_exp[MOSAICSIZE], weight[MOSAICSIZE], weigh2[MOSAICSIZE]; double pvalue[XD*YD], variance[XD*YD]; double pval_i[XD][YD], var_i[XD][YD], exp_i[XD][YD]; double vign[XD][YD], vigntmp[XD][YD]; float value_in[XD*YD], value_out[XD*YD]; float pvalcor_f[525*525]; /* CBJ's new correction array */ /* ********************************************************** */ #define sky_xdim 255 #define sky_ydim 255 #define skydim sky_xdim*sky_ydim void clean(float sky_r[skydim], float skyim[skydim], int bol); /* ********************************************************** */ main () { int i=0, j=0, k=0, loop=0; int xdim=XD, ydim=YD, dimx=(int)(sqrt(MOSAICSIZE)), dimy=(int)(sqrt(MOSAICSIZE)); int NL=0, CBJ=0; int numHDU=2 , numAxes=2; int p=0, q=0, pix[2]={0, 0}; int index1=0, index2=0; int lin=0, status=ISDC_OK; int isub, nsub, psub=0, qsub=0; int ext[NB_IMAGES]; int numImages = 0; /* Number of images found */ int pvalcor[262*262]; /* CBJ's correction array */ int nex=1; long startValues[2] = {1L, 1L}; long endValues[2] = {(long)(XD), (long)(YD)}; long numValues=0L; double pi, dr; float dsub, reduc, dp=0., dq=0.; float wei=1., radius=5., diamout=15.; double ra_o=0., de_o=0., ra_x=0., de_x=0.; double t1[3][3], t2[3][3], t[3][3]; double t1_o[3][3], t2_o[3][3], t_o[3][3], o_t[3][3]; double NaN = NAN; double ra=0., dec=0.; double centerA=-1.0, centerD=0.0; double exp_t[NB_IMAGES]; double crpix1[NB_IMAGES], crpix2[NB_IMAGES]; double cd1_1[NB_IMAGES], cd2_1[NB_IMAGES],cd1_2[NB_IMAGES], cd2_2[NB_IMAGES]; double crval1[NB_IMAGES], crval2[NB_IMAGES]; double crval1_min=0., crval1_max=0., crval2_min=0., crval2_max=0., crval1_o=0., crval2_o=0.; double crpix1_o=0., crpix2_o=0.; double cdelt=0., cd1_1_o=0., cd1_2_o=0., cd2_1_o=0., cd2_2_o=0.; double yz[2], ayz[2], vi[3], ve[3], vi_o[3]; double maxvign=0.; char DOLstring[NB_IMAGES][99], DOLstring_1[NB_IMAGES][99], DOLstring_2[99]; /* DAL_FILENAME_STRING */ char fitstring[99]; char parameter[99]; char mosaic[] = "MOSAIC"; char vignef[] = "VIGNET"; char template[] = "JMX2-SKY.-IMA.tpl"; char skymaps[99]; char vign_s[99]; char filexpstring[NB_IMAGES][99]; char useA='N', plot_T='N', plot_I='N', plot_E='N', plot_V='N', plot_W='N', plot_W2='N', plot_SoE='N', plot_RVE='N'; void *arrayPtr = NULL; void *arrayBuff = NULL; FILE *vignet; FILE *fp1; dal_dataType DALtype = DAL_DOUBLE; dal_element *dolPtr = NULL; dal_element *skyPtr = NULL; dal_element *pvlPtr = NULL; dal_element *expPtr = NULL; dal_element *varPtr = NULL; dal_element *sw_Ptr = NULL; dal_element *sw2Ptr = NULL; dal_element *mapPtr = NULL; dal_element *sqtPtr = NULL; dal_element *texPtr = NULL; fitsfile *fitsinPtr = NULL; /* Statements begin */ pi = acos (-1.); dr = pi/180.; isub = 2; nsub = 2*isub + 1; dsub = 1./nsub; reduc = dsub*dsub; /* Read input parameters */ if( (fp1 = fopen("mosaic_weight.par", "rt")) == NULL ) { printf("*** Could not open the necessary parameters file named mosaic_weight.par STOP! ***\n"); return; } for (i=0; i<100; i++){ fgets(parameter, 99, fp1); if (feof(fp1)) goto x_end; sscanf(parameter, "%d", &lin); if (lin == 1) sscanf(parameter, "%d %s", &lin, &skymaps); if (lin == 2) sscanf(parameter, "%d %f", &lin, &radius); if (lin == 3) sscanf(parameter, "%d %f", &lin, &diamout); if (lin == 31) sscanf(parameter, "%d %lf", &lin, ¢erA); if (lin == 32) sscanf(parameter, "%d %lf", &lin, ¢erD); if (lin == 101) sscanf(parameter, "%d %c", &lin, &useA); if (lin==201) sscanf(parameter, "%d %c", &lin, &plot_T); if (lin==202) sscanf(parameter, "%d %c", &lin, &plot_I); if (lin==203) sscanf(parameter, "%d %c", &lin, &plot_E); if (lin==204) sscanf(parameter, "%d %c", &lin, &plot_V); if (lin==205) sscanf(parameter, "%d %c", &lin, &plot_W); if (lin==206) sscanf(parameter, "%d %c", &lin, &plot_W2); if (lin==207) sscanf(parameter, "%d %c", &lin, &plot_SoE); if (lin==208) sscanf(parameter, "%d %c", &lin, &plot_RVE); } x_end: fclose (fp1); /* Look for number and size of input images */ if( (fp1 = fopen(skymaps, "rt")) == NULL ) { printf("Could not open skymaps list file\n"); return; } if(1 != fscanf(fp1, "%d", &numImages)){ fprintf(stderr, "The skymaps list file must start with the number of input fits files to be read! \n"); return; } if(1 != fscanf(fp1, "%s", fitstring)){ fprintf(stderr, "Can't read fits file name in skymaps list file\n"); return 1; } strcat(fitstring, "[1]"); fits_open_file(&fitsinPtr, fitstring, READONLY, &status); /* Open input fits file to get Ptr */ fits_read_key_lng(fitsinPtr, "NAXIS1", &numValues, NULL, &status); /* Get total number of extensions in the input fits */ fits_close_file(fitsinPtr, &status); /* Close input fits file */ rewind(fp1); fscanf(fp1, "%d", &numImages); if (numImages > NB_IMAGES){ printf("Please set the parameter NB_IMAGES > %d and recompile the program\n", numImages); return; } else printf("There are %d input images of dimensions %ld * %ld pixels\n", numImages, numValues, numValues); /* Initialisations */ ydim = xdim = (int)(numValues); /* Assuming that the input skymaps have always square dimensions */ if (xdim == 255) NL = 1; else if ((xdim == 262) || (xdim==525)) CBJ = 1; endValues[0] = numValues; endValues[1] = numValues; for (i=0; i maxvign) maxvign = vigntmp[i][j]; } for (i=0; i crval1_max) crval1_max = crval1[loop]; if (crval2[loop] > crval2_max) crval2_max = crval2[loop]; } if ((crval1_max-crval1_min) >= 180.) crval1_max -= 360.; if (diamout > 0.) if ( sqrt((crval1_max-crval1_min)*(crval1_max-crval1_min) + (crval2_max-crval2_min)*(crval2_max-crval2_min)) >= diamout) printf("*** Warning: total separation angle larger than %f degrees, some images may not be included ***\n", diamout); if (centerA < 0.){ crval1_o = 0.5 * (crval1_min + crval1_max); if (crval1_o < 0.) crval1_o+= 360.; crval2_o = 0.5 * (crval2_min + crval2_max); } else { crval1_o = centerA; crval2_o = centerD; } printf("Center of output mosaic skyimage: RA=%f, DEC=%f\n", crval1_o, crval2_o); /* Define transformation from sky coord. to FITS */ cdelt = 0.03 ; /* degrees per pixel*/ if (diamout > 0.){ dimx = (int)(diamout / cdelt); dimy = (int)(diamout / cdelt); } crpix1_o = (float)(dimx)/2.; crpix2_o = (float)(dimy)/2.; cd1_1_o = - cdelt; cd1_2_o = 0.0; cd2_1_o = 0.0; cd2_2_o = cdelt; ra_o = crval1_o; de_o = crval2_o; t1_o[0][0] = cos(ra_o*dr); t1_o[1][0] = sin(ra_o*dr); t1_o[2][0] = 0.; t1_o[0][1] = -sin(ra_o*dr); t1_o[1][1] = cos(ra_o*dr); t1_o[2][1] = 0.; t1_o[0][2] = 0.; t1_o[1][2] = 0.; t1_o[2][2] = 1.; t2_o[0][0] = cos(de_o*dr); t2_o[1][0] = 0.; t2_o[2][0] = sin(de_o*dr); t2_o[0][1] = 0.; t2_o[1][1] = 1.; t2_o[2][1] = 0.; t2_o[0][2] = -sin(de_o*dr); t2_o[1][2] = 0.; t2_o[2][2] = cos(de_o*dr); for (i=0; i<3; i++) for (j=0; j<3; j++){ t_o[i][j] = 0.; for (k=0; k<3; k++) t_o[i][j] += t1_o[i][k]*t2_o[k][j]; } for (i=0; i<3; i++) for (j=0; j<3; j++) o_t[i][j] = t_o[j][i]; /* COMPOSE MOSAIC BY LOOPING OVER SKY IMAGES TO ADD */ for (loop=0; loop 0.) if ((sqrt((crval1[loop]-crval1_o)*(crval1[loop]-crval1_o)+(crval2[loop]-crval2_o)*(crval2[loop]-crval2_o))-radius) >= diamout/2.){ printf(" Excluding %s\n", DOLstring[loop]); continue; } for (i=0; i radius) continue; /* Loop over pixels subdivisions */ for (psub=-isub; psub<=isub; psub++){ for (qsub=-isub; qsub<=isub; qsub++){ dp = psub*dsub; dq = qsub*dsub; yz[0] = cd1_1[loop]*(p+dp+1-crpix1[loop]) + cd1_2[loop]*(q+dq+1-crpix2[loop]); yz[1] = cd2_1[loop]*(p+dp+1-crpix1[loop]) + cd2_2[loop]*(q+dq+1-crpix2[loop]); for(k=0; k<2; k++) ayz[k] = atan(yz[k]*dr); for(k=1; k<3; k++) vi[k] = sin(ayz[k-1]); vi[0] = sqrt(1. -vi[1]*vi[1]-vi[2]*vi[2]); for (i=0; i<3; i++){ ve[i] = 0.; for (j=0; j<3; j++) ve[i] += t[i][j]*vi[j]; } /* dec = asin(ve[2])/dr; ra = atan2(ve[1],ve[0])/dr + 360.; if (ra > 360.) ra -= 360.; */ /* Where does this position belong in mosaic image? */ for (i=0; i<3; i++){ vi_o[i] = 0.; for (j=0; j<3; j++) vi_o[i] += o_t[i][j]*ve[j]; } for (k=0; k<2; k++){ ayz[k] = asin(vi_o[k+1]); yz[k] = tan(ayz[k]) / dr; } pix[0] = (int)((-1./cdelt)*yz[0]+ crpix1_o -1.); pix[1] = (int)((1./cdelt)*yz[1] + crpix2_o -1.); index2 = pix[1]*dimx+pix[0]; if ((pix[0]>=0) && (pix[0]=0) && (pix[1] 0.) wei = (exp_i[p][q]/var_i[p][q]) / (exp_o[index2]/var_o[index2]); else wei = 1.0; if (wei <= 1.0){ pval_o[index2] += wei * pval_i[p][q]; exp_o[index2] += wei * exp_i[p][q]; var_o[index2] += wei * wei * var_i[p][q]; } else{ wei = 1./wei; pval_o[index2] = pval_i[p][q] + wei * pval_o[index2]; if ((exp_i[p][q]+wei*exp_o[index2]) > exp_o[index2]) exp_o[index2] = exp_i[p][q] + wei * exp_o[index2]; var_o[index2] = var_i[p][q] + wei * wei * var_o[index2]; } weight[index2] += wei; weigh2[index2] += wei*wei; */ wei = (exp_i[p][q]/var_i[p][q]); pval_o[index2] += wei * pval_i[p][q]; exp_o[index2] += wei * exp_i[p][q]; var_o[index2] += wei * wei * var_i[p][q]; weight[index2] += wei; weigh2[index2] += wei*wei; exp_s[index2] += exp_i[p][q]; var_s[index2] += var_i[p][q]; } } } } } } /* End loop over total number of sky images to add */ for (index2=0; index2 max[0]) { max[1] = max[0]; max_j[1] = max_j[0]; max[0] = v; max_j[0] = i+indx[j]; } else { if (v > max[1]) { max[1] = v; max_j[1] = i+indx[j]; } } if (v < min[0]) { min[1] = min[0]; min_j[1] = min_j[0]; min[0] = v; min_j[0] = i+indx[j]; } else { if (v < min[1]) { min[1] = v; min_j[1] = i+indx[j]; } } v = sky_r[i-indx[j]]; if (v > max[0]) { max[1] = max[0]; max_j[1] = max_j[0]; max[0] = v; max_j[0] = i-indx[j]; } else { if (v > max[1]) { max[1] = v; max_j[1] = i-indx[j]; } } if (v < min[0]) { min[1] = min[0]; min_j[1] = min_j[0]; min[0] = v; min_j[0] = i+indx[j]; } else { if (v < min[1]) { min[1] = v; min_j[1] = i+indx[j]; } } } b = 0.0; n = 0; for (j=0; j<10; j++) { k = i+indx[j]; if (k == max_j[0]) goto try2; if (k == max_j[1]) goto try2; if (k == min_j[0]) goto try2; if (k == min_j[1]) goto try2; b += sky_r[k]; n++; try2: k = i-indx[j]; if (k == max_j[0]) continue; if (k == max_j[1]) continue; if (k == min_j[0]) continue; if (k == min_j[1]) continue; b += sky_r[k]; n++; } b /= (float)(n); if (bol == 1) skyim[i] = sky_r[i] - b; else if (bol == 0) skyim[i] = b; else printf("ERROR: bol = %d\n", bol); } return; } /* ********************************************************** */