local_contrast_normalization( geometric_layer_property const& glp, int batch_size, scalar alpha, scalar beta, boost::compute::context const& context) : glp_(glp), batch_size_(batch_size), alpha_(alpha), beta_(beta), output_width_(output_width(glp)), local_mean_(output_dim(glp)*batch_size, context), local_variance_(output_dim(glp)*batch_size, context), input_(input_dim(glp)*batch_size, context), output_(output_dim(glp)*batch_size, context), local_mean_kernel_(make_kernel(local_mean_kernel_source, "local_mean", context)), local_variance_kernel_(make_kernel(local_variance_kernel_source, "local_variance", context)), forward_kernel_(make_kernel( local_contrast_normalization_forward_kernel_source, "forward", context)), backward_kernel_(make_kernel( local_contrast_normalization_backward_kernel_source, "backward", context)) {}
void sph_simulation::simulate(int frame_count) { if (frame_count == 0) { frame_count = (int)ceil(parameters.simulation_time * parameters.target_fps); } cl_int cl_error; std::vector<cl::Device> device_array; check_cl_error(init_cl_single_device(&context_, device_array, "", "", true)); queue_ = cl::CommandQueue(context_, device_array[0], 0, &cl_error); check_cl_error(cl_error); running_device = &device_array[0]; std::string source = readKernelFile(BUFFER_KERNEL_FILE_NAME); cl::Program program; check_cl_error(make_program(&program, context_, device_array, source, true, "-I ./kernels/ -I ./common/")); kernel_density_pressure_ = make_kernel(program, "density_pressure"); kernel_advection_collision_ = make_kernel(program, "advection_collision"); kernel_forces_ = make_kernel(program, "forces"); kernel_locate_in_grid_ = make_kernel(program, "locate_in_grid"); kernel_sort_count_ = make_kernel(program, "sort_count"); kernel_sort_ = make_kernel(program, "sort"); front_buffer_ = cl::Buffer(context_, CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR, sizeof(particle) * parameters.particles_count); back_buffer_ = cl::Buffer(context_, CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR, sizeof(particle) * parameters.particles_count); sort_count_buffer_ = cl::Buffer(context_, CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR, sizeof(unsigned int) * kSortThreadCount * kBucketCount); particle* particles = new particle[parameters.particles_count]; init_particles(particles, parameters); for (int i = 0; i < frame_count; ++i) { if (pre_frame) { pre_frame(particles, parameters, true); } for (int j = 0; (float)j < (1.f / parameters.simulation_scale); ++j) { if (pre_frame) pre_frame(particles, parameters, false); simulate_single_frame(particles, particles); if (post_frame) post_frame(particles, parameters, false); } if (post_frame) { post_frame(particles, parameters, true); } } delete[] particles; }
Air::Air(Simulation & simulation): airMode(0), ambientAirTemp(295.15f), sim(simulation) { //Simulation should do this. make_kernel(); std::fill(&bmap_blockair[0][0], &bmap_blockairh[0][0]+((XRES/CELL)*(YRES/CELL)), 0); std::fill(&bmap_blockairh[0][0], &bmap_blockair[0][0]+((XRES/CELL)*(YRES/CELL)), 0); std::fill(&vx[0][0], &vx[0][0]+((XRES/CELL)*(YRES/CELL)), 0.0f); std::fill(&ovx[0][0], &ovx[0][0]+((XRES/CELL)*(YRES/CELL)), 0.0f); std::fill(&vy[0][0], &vy[0][0]+((XRES/CELL)*(YRES/CELL)), 0.0f); std::fill(&ovy[0][0], &ovy[0][0]+((XRES/CELL)*(YRES/CELL)), 0.0f); std::fill(&hv[0][0], &hv[0][0]+((XRES/CELL)*(YRES/CELL)), 0.0f); std::fill(&ohv[0][0], &ohv[0][0]+((XRES/CELL)*(YRES/CELL)), 0.0f); std::fill(&pv[0][0], &pv[0][0]+((XRES/CELL)*(YRES/CELL)), 0.0f); std::fill(&opv[0][0], &opv[0][0]+((XRES/CELL)*(YRES/CELL)), 0.0f); }
int main(int argc, char *argv[]) { float sum, sum2, vsize, fwhm, complex_kernel[512]; int i, nvoxels; prog_name = argv[0]; /* Check arguments */ if (argc != 4) { (void) fprintf(stderr, "Usage: %s fwhm(mm) vsize(mm) nvoxels\n", argv[0]); exit(EXIT_FAILURE); } fwhm = (float)atof(argv[1]); vsize = (float)atof(argv[2]); nvoxels= atoi(argv[3]); print ("for fwhm = %f, vsize = %f, nvoxels = %d\n", fwhm, vsize, nvoxels); make_kernel(complex_kernel,(float)vsize,fwhm,nvoxels,KERN_GAUSSIAN); sum2, sum = 0.0; for(i=0; i<nvoxels; i++) { print ("%f\n",complex_kernel[i*2 + 1]); sum += complex_kernel[i*2 + 1]; sum2 += complex_kernel[i*2 + 1] * complex_kernel[i*2 + 1]; } print ("sum = %f, sum^2 = %f\n",sum,sum2); exit(EXIT_SUCCESS); }
void GUI::apply_filter_from_menu(Controller &controller, Gtk::ImageMenuItem &imagemenuitem) { if ( ! controller.image_file_loaded ) { return ; } bool is_alpha = controller.current_image_to_process.channels() == 4 ; Sharpen sharpen_filter(3, "diamond") ; Sharpen sharpen_filter_more(3, "diamond") ; Find_Edges find_edges_filter(3) ; Mean mean_filter(3) ; Mean mean_filter_more(5) ; cv::Mat kernel = make_kernel("rect", 3) ; cv::Mat tmp = controller.current_image_to_process.clone() ; cv::Mat frame ; switch ( stoi(imagemenuitem.get_name()) ) { case 0 : // Pencil Sketch filter. pencil_sketch_filter(tmp, frame) ; break ; case 1 : // Stylisation filter. stylisation_filter(tmp, frame) ; break ; case 2 : // Detail Enhance filter. detail_enhance_filter(tmp, frame) ; break ; case 3 : // Edge Preserving filter. edge_preserving_filter(tmp, frame) ; break ; case 4 : // Stroke edges filter: stroke_edges(tmp, frame) ; break ; case 5 : // Invert Intensity filter: invert_intensity(tmp, frame) ; break ; case 6 : // Light Intensity filter: effect_light(tmp, frame) ; break ; case 7 : // Recolor-RC (Red-Cyan) filter: recolorRC(tmp, frame) ; break ; case 8 : // Recolor-RC (Red-Green-Value) filter: recolorRGV(tmp, frame) ; break ; case 9 : // Recolor-RC (Cyan-Magenta-Value) filter: recolorCMV(tmp, frame) ; break ; case 10 : // Extrema Maximal Filter: extrema(tmp, frame, "max") ; break ; case 11 : // Extrema Minimal Filter: extrema(tmp, frame, "min") ; break ; case 12 : // Sharpen filter: sharpen_filter.apply(tmp, frame) ; break ; case 13 : // Sharpen More filter: sharpen_filter_more.apply(tmp, frame) ; break ; case 14 : // Find Edges filter: if (is_alpha) { cv::Mat frame_rgb ; cv::Mat tmp_1 ; cvtColor(tmp, frame_rgb, cv::COLOR_BGRA2BGR) ; find_edges_filter.apply(frame_rgb, tmp_1) ; vector<cv::Mat> tmp_2 ; vector<cv::Mat> tmp_3 ; cv::split(tmp, tmp_2) ; cv::split(tmp_1, tmp_3) ; // Assign BGR channels. tmp_2[0] = tmp_3[0] ; tmp_2[1] = tmp_3[1] ; tmp_2[2] = tmp_3[2] ; // Final channels merging into result with alpha channel unchanged. cv::merge(tmp_2, frame) ; break ; } find_edges_filter.apply(tmp, frame) ; break ; case 15 : // Mean Blur filter: mean_filter.apply(tmp, frame) ; break ; case 16 : // Mean Blur More filter: mean_filter_more.apply(tmp, frame) ; break ; case 17 : // Blur filter: blur_filter(tmp, frame) ; break ; case 18 : // Median Blur filter: median_blur_filter(tmp, frame) ; break ; case 19 : // Gaussian Blur filter: gaussian_blur_filter(tmp, frame) ; break ; case 20 : denoising_filter(tmp, frame) ; break ; case 21 : // Erode filter: erode_filter(tmp, frame, kernel, 1) ; break ; case 22 : // Dilate filter: dilate_filter(tmp, frame, kernel, 1) ; break ; case 23 : // Wave Horizontally filter: wave(tmp, frame, -1) ; break ; case 24 : // Wave Vertically filter: wave(tmp, frame, 1) ; break ; case 25 : // Wave Twice (Horizontally and Vertically) filter: wave(tmp, frame, 0) ; break ; case 26 : // Contours Sobel White filter. sobel_drawning(tmp, frame, 3, false, 1) ; break ; case 27 : // Contours Sobel Black filter. sobel_drawning(tmp, frame, 3, false, -1) ; break ; case 28 : // Contours Sobel Emboss filter. sobel_drawning(tmp, frame, 3, false, 0) ; break ; case 29 : // Emboss Sobel filter: sobel_emboss(tmp, frame, 3) ; break ; case 30 : // Emboss Laplacian filter: laplacian_emboss(tmp, frame, 3) ; break ; case 31 : // Binary White OTSU filter: // Build a binary image (a black and white only image) with white background (@arg value true) // based on the OTSU threshold computing algorithm (@arg value -1). build_binary_image(tmp, frame, -1, true) ; break ; case 32 : // Binary White TRIANGLE filter: // Build a binary image (a black and white only image) with white background (@arg value true) // based on the TRIANGLE threshold computing algorithm (@arg value 1). build_binary_image(tmp, frame, 1, true) ; break ; case 33 : // Binary White AVERAGE filter: // Build a binary image (a black and white only image) with white background (@arg value true) // based on the AVERAGE threshold from OTSU and TRIANGLE (@arg value 0). build_binary_image(tmp, frame, 0, true) ; break ; case 34 : // Binary Black OTSU filter: // Build a binary image (a black and white only image) with black background (@arg value true) // based on the OTSU threshold computing algorithm (@arg value -1). build_binary_image(tmp, frame, -1, false) ; break ; case 35 : // Binary Black TRIANGLE filter: // Build a binary image (a black and white only image) with black background (@arg value true) // based on the TRIANGLE threshold computing algorithm (@arg value 1). build_binary_image(tmp, frame, 1, false) ; break ; case 36 : // Binary Black AVERAGE filter: // Build a binary image (a black and white only image) with black background (@arg value true) // based on the AVERAGE threshold from OTSU and TRIANGLE (@arg value 0). build_binary_image(tmp, frame, 0, false) ; break ; case 37 : // Binary Contours White filter: // Build a binary image (a black and white only image) with contours detction on white background (@arg value false). laplacian_zero_crossing(tmp, frame, 19, false) ; break ; case 38 : // Binary Contours Black filter: // Build a binary image (a black and white only image) with contours detction on black background (@arg value true). laplacian_zero_crossing(tmp, frame, 19, true) ; break ; #ifdef DEBUG default : // Cannot append due of the GUI interfacing. fprintf(stdout,"Error applying filter !!!\n") ; return ; #endif } // We register current frame in vector<cv::Mat> for undo-redo. controller.process_after_applying(frame) ; // It convert current_image_to_process as src to RGB(A) in dst current_image_to_display. set_img(frame, controller.current_image_to_display, controller) ; // It auto process conversion to RGB(A). // Reset some variables. after_applying_reset_settings(controller) ; }
void GUI::morphological_callback(Controller &controller) { if ( ! controller.image_file_loaded ) { return ; } string kernel_type ; switch (controller.morphological_kernel_type) { case 0 : kernel_type = "rect" ; break ; case 1 : kernel_type = "diamond" ; break ; case 2 : kernel_type = "cross" ; break ; case 3 : kernel_type = "X" ; break ; #ifdef DEBUG default : // This cannot append due of the GUI interfacing. fprintf(stderr,"Cannot identify kernel type !!!\n") ; return ; #endif } char center_value ; switch (controller.morphological_center_value) { case 0 : center_value = '1' ; break ; case 1 : center_value = '0' ; break ; case 2 : center_value = '+' ; break ; case 3 : center_value = '-' ; break ; #ifdef DEBUG default : // This cannot append due of the GUI interfacing. fprintf(stderr,"Cannot identify center value !!!\n") ; return ; #endif } int operator_type ; switch (controller.morphological_operator_type) { case 0 : operator_type = cv::MORPH_ERODE ; break ; case 1 : operator_type = cv::MORPH_DILATE ; break ; case 2 : operator_type = cv::MORPH_OPEN ; break ; case 3 : operator_type = cv::MORPH_CLOSE ; break ; case 4 : operator_type = cv::MORPH_TOPHAT ; break ; case 5 : operator_type = cv::MORPH_BLACKHAT ; break ; #ifdef DEBUG default : // This cannot append due of the GUI interfacing. fprintf(stderr,"Cannot identify operator !!!\n") ; return ; #endif } cv::Mat kernel = make_kernel(kernel_type, static_cast<int>(controller.morphological_kernel_size), center_value) ; cv::Mat tmp = controller.current_image_to_process.clone() ; cv::Mat frame ; morphological_filter(tmp, frame, operator_type, kernel, static_cast<int>(controller.morphological_iterations) ) ; // We register current frame in vector<cv::Mat> for undo-redo. controller.process_after_applying(frame) ; // It convert current_image_to_process as src to RGB(A) in dst current_image_to_display. set_img(frame, controller.current_image_to_display, controller) ; // It auto process conversion to RGB(A). // Reset some variables. after_applying_reset_settings(controller) ; }
/*--------------------------------------------------------*/ int main(int argc, char *argv[]) { char **newheader, *imffname, *parfname, *psffname, *reffname, *instrname, *fieldname, *outfname, *coofname, **diffiles, **imfiles, **kerfiles; int nx0, ny0, k, *sector, nim, iim, npsf, ipsf, hsize, isec_x, isec_y, *cx, *cy, psfn, kern, irad, // ofs, *vnum, i; float *im, *difim, *refim; double **wxy, *x, *y, *xs, *ys, **psfs, *psfim, *kerim, ratio; STAR **obj, *objp; PSF_STRUCT psf; KER_STRUCT ker; PAR_STRUCT par; /*** IO stuff ***/ if (argc != 7) { printf("\n\tUSAGE: phot parameter_file instrument_file"); printf(" ref_image psf_fit_file image_data_list field_name\n"); exit(1); } parfname= argv[1]; instrname=argv[2]; reffname= argv[3]; psffname= argv[4]; imffname= argv[5]; fieldname=argv[6]; get_params(parfname, instrname, &par); if (par.verbose) printf("\n\n*** Profile photometry with variable PSF and kernel ***\n\n"); if (!(outfname=(char *)calloc(strlen(fieldname)+5, sizeof(char)))) errmess("calloc(outfname)"); strcpy(outfname, fieldname); strcat(outfname, ".db"); if (!(coofname=(char *)calloc(strlen(fieldname)+5, sizeof(char)))) errmess("calloc(coofname)"); strcpy(coofname, fieldname); strcat(coofname, ".coo"); /*** read filenames for difference images, images and kernel fits ***/ nim=readfnames(imffname, &diffiles, &imfiles, &kerfiles); if (par.verbose) printf("%d images to process\n", nim); /*** read coordinates of variables ***/ npsf=readcoo(coofname, &vnum, &x, &y); if (par.verbose) printf("%d variables to measure\n\n", npsf); /*** read in psf fit and get a sample kernel from the first image ***/ read_psf(psffname, &psf); read_kernel(kerfiles[0], &ker, 1, par.verbose); psf.normrad = par.normrad; psf.hw += ker.hw; psfn = 2*psf.hw + 1; kern = 2*ker.hw + 1; /*** get memory ***/ if (!(ker.vecs = (double **)malloc(ker.nvecs*sizeof(double *)))) errmess("malloc(ker.vecs)"); for (k=0; k<ker.nvecs; k++) if (!(ker.vecs[k] = (double *)malloc(kern*kern*sizeof(double)))) errmess("malloc(ker.vecs[k])"); if (!(sector=(int *)malloc(npsf*sizeof(int)))) errmess("malloc(sector)"); if (!(cx= (int *)malloc(npsf*sizeof(int)))) errmess("malloc(cx)"); if (!(cy= (int *)malloc(npsf*sizeof(int)))) errmess("malloc(cy)"); if (!(xs= (double *)malloc(npsf*sizeof(double)))) errmess("malloc(xs)"); if (!(ys= (double *)malloc(npsf*sizeof(double)))) errmess("malloc(ys)"); if (!(wxy=(double **)malloc(npsf*sizeof(double *)))) errmess("malloc(wxy)"); if (!(psfs=(double **)malloc(npsf*sizeof(double *)))) errmess("malloc(psfs)"); if (!(kerim=(double *)malloc(kern*kern*sizeof(double)))) errmess("malloc(kerim)"); if (!(psfim=(double *)malloc(psfn*psfn*sizeof(double)))) errmess("malloc(psfim)"); if (!(obj=(STAR **)malloc(npsf*sizeof(STAR *)))) errmess("malloc(obj)"); /***********************************************************************/ /** get things that can be done once for all: spatial coeffs and psfs **/ /***********************************************************************/ for (ipsf=0; ipsf<npsf; ipsf++) { if (!(obj[ipsf]=(STAR *)malloc(nim*sizeof(STAR)))) errmess("malloc(obj[ipsf])"); if (!(wxy[ipsf]=(double *)malloc(ker.nwxy*sizeof(double)))) errmess("malloc(wxy[ipsf])"); if (!(psfs[ipsf]=(double *)malloc(psfn*psfn*sizeof(double)))) errmess("malloc(psfs[ipsf])"); /* offsets for image sectors from kernel fit */ cx[ipsf]=(int)floor(x[ipsf]+0.5); cy[ipsf]=(int)floor(y[ipsf]+0.5); isec_x=(cx[ipsf] - ker.hw)/(ker.nx - 2*ker.hw); isec_y=(cy[ipsf] - ker.hw)/(ker.ny - 2*ker.hw); xs[ipsf]=x[ipsf] - isec_x*(ker.nx - 2*ker.hw); ys[ipsf]=y[ipsf] - isec_y*(ker.ny - 2*ker.hw); sector[ipsf]=isec_y + ker.nsec_y*isec_x; spatial_coeffs(&ker, xs[ipsf], ys[ipsf], wxy[ipsf]); init_psf(&psf, x[ipsf], y[ipsf]); make_psf(&psf, x[ipsf], y[ipsf], cx[ipsf], cy[ipsf], psfs[ipsf]); } refim=read_FITS_2D1file(reffname, 's', &hsize, &newheader, &nx0, &ny0); for (i=0; i<hsize; i++) free(newheader[i]); free(newheader); make_vectors(&ker); par.nx0=nx0; par.ny0=ny0; par.psfhw=psf.hw; par.psfn=psfn; irad=(int)par.anrad2 + 2; /*******************************/ /*** main loop over images ***/ /*******************************/ for (iim=0; iim<nim; iim++) { if (par.verbose > 2) printf("%d: %s\n", iim, diffiles[iim]); difim=read_FITS_2D1file(diffiles[iim], 's', &hsize, &newheader, &nx0, &ny0); for (i=0; i<hsize; i++) free(newheader[i]); free(newheader); if ((nx0 != par.nx0) || (ny0 != par.ny0)) { printf("ERROR! phot: wrong size of the image %s\n", diffiles[iim]); exit(2); } im=read_FITS_2D1file(imfiles[iim], 's', &hsize, &newheader, &nx0, &ny0); for (i=0; i<hsize; i++) free(newheader[i]); free(newheader); if ((nx0 != par.nx0) || (ny0 != par.ny0)) { printf("ERROR! phot: wrong size of the image %s\n", imfiles[iim]); exit(3); } /* read kernel into tables allocated before */ read_kernel(kerfiles[iim], &ker, 0, par.verbose); /*** loop over stars ***/ for (ipsf=0; ipsf<npsf; ipsf++) { objp = &obj[ipsf][iim]; if ((cx[ipsf] < irad) || (cy[ipsf] < irad) || (cx[ipsf] >= nx0-irad) || (cy[ipsf] >= ny0-irad)) { if (par.verbose) printf("%s warning: object %4d too close to edge: ignored!\n", diffiles[iim], ipsf); objp->a_flux = par.bad_value; objp->a_err = par.bad_value; objp->p_flux = par.bad_value; objp->p_err = par.bad_value; objp->chi2_n = par.bad_value; objp->ker_chi2 = par.bad_value; objp->corr = par.bad_value; objp->nbad = -1; continue; } /*** prepare local psf ***/ make_kernel(&ker, wxy[ipsf], kerim, sector[ipsf]); im_convolve(psfs[ipsf], psfim, psfn, psfn, kerim, ker.hw); objp->ker_chi2=(float)ker.chi2_n[sector[ipsf]]; objp->fwhm=(float)get_fwhm(psfim, x[ipsf], y[ipsf], cx[ipsf], cy[ipsf], &par, &ratio); if (par.bkg_mode) objp->bg = bkg(difim, cx[ipsf], cy[ipsf], &par); else objp->bg = 0.0; /*** actual profile and aperture photometry ***/ get_phot(difim, im, refim, x[ipsf], y[ipsf], cx[ipsf], cy[ipsf], psfim, objp, &par); if (par.verbose > 1) printf("%s star: %5d flux= %9g +- %8g nbad: %d\n", diffiles[iim], ipsf, objp->p_flux, objp->p_err, objp->nbad); } free(difim); free(im); } /*** write photometry to the output file ***/ if (par.verbose) printf("\nWriting photometry to: %s\n", outfname); if (par.dbf == 'A') writedba(outfname, diffiles, nim, npsf, vnum, obj); else writedbb(outfname, diffiles, nim, npsf, vnum, obj); if (par.verbose) printf("\nPhotometry done!\n"); return(0); }
Air::Air(): airMode(0) { //Simulation should do this. make_kernel(); }
void processColorArray(double** colorInput, double** colorOutput, FilterParam filterParam) { int imageWidth = filterParam.getImageSize().width(); int imageHeight = filterParam.getImageSize().height(); int windowSize = filterParam.getWindowSize(); int patchSize = filterParam.getPatchSize(); double filterP = 0.4f; double fSigma2 = filterParam.getSigma() * filterParam.getSigma(); double fH2 = filterP*filterP; int halfWindowSize = windowSize/2; int halfPatchSize = patchSize/2; int m = imageWidth - windowSize; int n = imageHeight - windowSize; double** kernel = new double*[patchSize]; double** Patch1 = new double*[patchSize]; double** Patch2 = new double*[patchSize]; for (int i = 0; i < patchSize; i++) { Patch1[i] = new double[patchSize]; Patch2[i] = new double[patchSize]; kernel[i] = new double[patchSize]; } make_kernel(kernel,patchSize,filterParam.getSigma()); std::cout << imageWidth << " " << imageHeight << std::endl; for (int i = 0; i < imageWidth; i++) { for (int j = 0; j < imageHeight; j++) { // std::cout << i << " " << j << std::endl; int i1 = i + halfPatchSize; int j1 = j + halfPatchSize; for (int a = 0; a < patchSize; a++) { for (int b = 0; b < patchSize; b++) { Patch1[a][b] = colorInput[i1-halfPatchSize+a][j1-halfPatchSize+a]; } } double wmax = 0; double average = 0; double sweight = 0; int rmin = std::max(i1-halfWindowSize,halfPatchSize+1) - 1; int rmax = std::min(i1+halfWindowSize,m+halfPatchSize); int smin = std::max(j1-halfWindowSize,halfPatchSize+1) - 1; int smax = std::min(j1+halfWindowSize,n+halfPatchSize); for (int r = rmin; r < rmax; r++) { for (int s = smin; s < smax; s++) { if (r == i1 && s == j1) { continue; } for (int a = 0; a < patchSize; a++) { for (int b = 0; b < patchSize; b++) { Patch2[a][b] = colorInput[r-halfPatchSize+a][s-halfPatchSize+a]; } } double N = 0; for (int a = 0; a < patchSize; a++) { for (int b = 0; b < patchSize; b++) { N += kernel[a][b] * ((Patch1[a][b] - Patch2[a][b]) * (Patch1[a][b] - Patch2[a][b])); } } // double W = exp(-N/fSigma2); double W = exp(-N/((patchSize+1)*(patchSize+1)*fH2 * fSigma2)); if (W > wmax) { wmax = W; } sweight += W; average += W * colorInput[r][s]; } } average += wmax * colorInput[i1][j1]; sweight += wmax; if (sweight > 0) { colorOutput[i][j] = average / sweight; } else { colorOutput[i][j] = colorInput[i1][j1]; } } } for (int i = 0; i < patchSize; i++) { delete Patch1[i]; delete Patch2[i]; delete kernel[i]; } delete kernel; delete Patch1; delete Patch2; }
/*--------------------------------------------------------*/ int main(int argc, char *argv[]) { char **header, *imffname, *parfname, *psffname, *reffname, *instrname, *fieldname, *catname, *coofname, record[RECORD_LEN], **diffiles, **imfiles, **kerfiles; int nx0, ny0, k, nim, iim, hsize, cx, cy, psfn, kern, irad, ofs, nobj1, nobj1_max, nobj2, nobj2_max, i, j, flag, *index, nvar; float x0_off, y0_off, x_tmp, y_tmp, fwhm_limit, *fwhm, **im, **difim, *refim, *varim1, *varim2, *corrim, *tmpim; double *wxy, x, y, *psfs, *psfim, *kerim, ratio; FILE *outfcat, *outfcoo; STAR *obj1, *obj2, *objp; PSF_STRUCT psf; KER_STRUCT ker; PAR_STRUCT par; /*** IO stuff ***/ if (argc != 9) usage(); parfname = argv[1]; instrname= argv[2]; reffname = argv[3]; psffname = argv[4]; imffname = argv[5]; fieldname = argv[6]; x0_off = atof(argv[7]); y0_off = atof(argv[8]); puts("sssssss"); get_params(parfname, instrname, &par); puts("sssssss"); if (!(catname=(char *)calloc(strlen(fieldname)+5, sizeof(char)))) errmess("calloc(catname)"); strcpy(catname, fieldname); strcat(catname, ".cat"); if (!(coofname=(char *)calloc(strlen(fieldname)+5, sizeof(char)))) errmess("calloc(coofname)"); strcpy(coofname, fieldname); strcat(coofname, ".coo"); if (par.verbose > 2) { printf("parfname = %s\n", parfname); printf("instrname= %s\n", instrname); printf("reffname = %s\n", reffname); printf("psffname = %s\n", psffname); printf("imffname = %s\n", imffname); printf("fieldname= %s\n", fieldname); printf("x0_off = %g\n", x0_off); printf("y0_off = %g\n", y0_off); printf("--------\n"); printf("catname = %s\n", catname); printf("coofname = %s\n", coofname); printf("--------\n"); } nim=read_inp_list(imffname, &diffiles, &imfiles, &kerfiles); if (par.verbose) printf("%s: %d images\n", imffname, nim); // printf("%s %s %s\n", diffiles[0],imfiles[0],kerfiles[0]); // printf("%s %s %s\n", diffiles[1],imfiles[1],kerfiles[1]); //getchar(); /**********************************************************************/ /*** read in psf fit and get a sample kernel from the first image ***/ /**********************************************************************/ read_psf(psffname, &psf, par.verbose); printf("%f %f %f\n",psf.ax, psf.ay, psf.vec[0]); read_kernel(kerfiles[0], &ker, 1, par.verbose); printf("%f %d %f\n",*ker.sig, *ker.deg, *ker.vec[0]); //getchar(); psf.normrad = par.normrad; psf.hw += ker.hw; psfn = 2*psf.hw + 1; kern = 2*ker.hw + 1; /*** get memory ***/ if (!(ker.vecs = (double **)malloc(ker.nvecs*sizeof(double *)))) errmess("malloc(ker.vecs)"); for (k=0; k<ker.nvecs; k++) if (!(ker.vecs[k] = (double *)malloc(kern*kern*sizeof(double)))) errmess("malloc(ker.vecs[k])"); if (!(kerim=(double *)malloc(kern*kern*sizeof(double)))) errmess("malloc(kerim)"); if (!(psfim=(double *)malloc(psfn*psfn*sizeof(double)))) errmess("malloc(psfim)"); if (!(fwhm = (float *)malloc(nim*sizeof(float)))) errmess("malloc(fwhm)"); if (!(index = (int *)malloc(nim*sizeof(int)))) errmess("malloc(index)"); /***********************************************************************/ /** get things that can be done once for all: spatial coeffs and psfs **/ /***********************************************************************/ refim=read_FITS_2D1file(reffname, 's', &hsize, &header, &nx0, &ny0); for (i=0; i<hsize; i++) free(header[i]); free(header); par.nx0 = nx0; par.ny0 = ny0; par.psfhw = psf.hw; par.psfn = psfn; printf("nx0: %d %d %d %d\n",nx0,ny0,psf.hw,psfn); irad = (int)par.anrad2 + 2; /*** get even more memory ***/ if (!(psfs=(double *)malloc(psfn*psfn*sizeof(double)))) errmess("malloc(psfs)"); if (!(wxy=(double *)malloc(ker.nwxy*sizeof(double)))) errmess("malloc(wxy)"); if (!(im =(float **)malloc(nim*sizeof(float *)))) errmess("malloc(im)"); if (!(difim=(float **)malloc(nim*sizeof(float *)))) errmess("malloc(difim)"); if (!(varim1=(float *)malloc(nx0*ny0*sizeof(float)))) errmess("malloc(varim1)"); if (!(varim2=(float *)malloc(nx0*ny0*sizeof(float)))) errmess("malloc(varim2)"); if (!(corrim=(float *)malloc(nx0*ny0*sizeof(float)))) errmess("malloc(corrim)"); for (i=0; i<nx0*ny0; i++) varim1[i] = varim2[i] = 0.0; /*** make reference psf and spatial coeffs for kernel ***/ init_psf(&psf, (double)(nx0/2), (double)(ny0/2)); make_psf(&psf, (double)(nx0/2), (double)(ny0/2), nx0/2, ny0/2, psfs); make_vectors(&ker); spatial_coeffs(&ker, (double)(ker.nx/2), (double)(ker.ny/2), wxy); /*******************************/ /*** main loop over images ***/ /*******************************/ printf("MAIN:\n"); for (iim=0; iim<nim; iim++) { read_kernel(kerfiles[iim], &ker, 0, par.verbose); make_kernel(&ker, wxy, kerim, 0); im_convolve(psfs, psfim, psfn, psfn, kerim, ker.hw); fwhm[iim] = get_fwhm(psfim, 0.0, 0.0, 0, 0, &par, &ratio); } /*** reject bad seeing frames ***/ quick_sort(fwhm, index, nim); printf("fwhm = %f\n",fwhm[0]); printf("fwhm = %f\n",fwhm[1]); fwhm_limit = fwhm[index[(int)(nim*par.fwhm_frac)]]; printf("fwhmlim = %f\n",fwhm_limit); j = 0; for (iim=0; iim<nim; iim++) { if (fwhm[iim] <= fwhm_limit) { if (j != iim) { strcpy(diffiles[j], diffiles[iim]); strcpy( imfiles[j], imfiles[iim]); strcpy(kerfiles[j], kerfiles[iim]); } j++; } } nim = j; if (nim == 0) { printf("no images left ! (exit)\n"); exit(2); } /*** get all variability data at once ***/ for (iim=0; iim<nim; iim++) { difim[iim]=read_FITS_2D1file(diffiles[iim], 's', &hsize, &header, &nx0, &ny0); //printf("difim = %f\n",difim[iim]); for (i=0; i<hsize; i++) free(header[i]); free(header); if ((nx0 != par.nx0) || (ny0 != par.ny0)) { printf("ERROR! getvar: image %s has wrong size\n", diffiles[iim]); exit(3); } im[iim]=read_FITS_2D1file(imfiles[iim], 's', &hsize, &header, &nx0, &ny0); for (i=0; i<hsize; i++) free(header[i]); free(header); if ((nx0 != par.nx0) || (ny0 != par.ny0)) { printf("ERROR! getvar: image %s has wrong size\n", imfiles[iim]); exit(4); } } /*** prepare variability likelihood image ***/ get_repeaters(difim, im, varim1, varim2, &par, nim); /*** save variability images ***/ if (!(tmpim=(float *)malloc(nx0*ny0*sizeof(float)))) errmess("malloc(tmpim)"); memcpy(tmpim, varim1, nx0*ny0*sizeof(float)); writevar2fits("var1.fits", nx0, ny0, tmpim); memcpy(tmpim, varim2, nx0*ny0*sizeof(float)); writevar2fits("var2.fits", nx0, ny0, tmpim); free(tmpim); /*** find stellar looking things ***/ covar_sig(varim1, corrim, nx0, ny0, 0.0, par.mohw, psfs, par.psfhw); obj1 = find_stars(corrim, nx0, ny0, &nobj1, &nobj1_max, &par); if (par.verbose > 1) printf("find_stars -> %d variables of type 1\n", nobj1); center_stars(obj1, nobj1, difim, im, &par, nim); /***/ covar_sig(varim2, corrim, nx0, ny0, 0.0, par.mohw, psfs, par.psfhw); obj2 = find_stars(corrim, nx0, ny0, &nobj2, &nobj2_max, &par); if (par.verbose > 1) printf("find_stars -> %d variables of type 2\n", nobj2); center_stars(obj2, nobj2, difim, im, &par, nim); /*** some stars may be found in both: take only one type and flag ***/ cross_id(obj1, nobj1, obj2, nobj2, par.id_rad); if (par.verbose) printf("\nFound %d and %d candidates for variables of both types\n\n", nobj1, nobj2); /*********************************************************************/ /*** write results to a binary file and x,y to a temporary file ***/ /*********************************************************************/ if (!(outfcoo = fopen(coofname, "w"))) errmess(coofname); if (!(outfcat = fopen(catname, "w"))) errmess(catname); free(coofname); free(catname); fseek(outfcat, 0, SEEK_END); ofs = sizeof(float); /*** get sinusoidal variables out ***/ if (par.verbose) printf("var1:\n num X Y flux flag\n"); nvar=0; for (i=0; i<nobj1; i++) { if (par.verbose > 1) printf("i= %d\n", i); objp = &(obj1[i]); x = (double)(objp->x); y = (double)(objp->y); if (par.verbose > 1) printf("x= %g y= %g\n", x, y); if ((x < par.bad_margin) || (x >= par.nx0 - par.bad_margin)) continue; if ((y < par.bad_margin) || (y >= par.ny0 - par.bad_margin)) continue; cx = objp->cx; cy = objp->cy; if (par.verbose > 1) printf("cx= %d cy= %d\n", cx, cy); /*** append to a binary file ***/ if (objp->nframes > 0) { fprintf(outfcoo, "%5d %11.5f %11.5f\n", nvar, x, y); nvar++; /*** get template photometry ***/ init_psf(&psf, x, y); make_psf(&psf, x, y, cx, cy, psfs); objp->bg = bkg(refim, cx, cy, &par); get_phot(refim, refim, refim, x, y, cx, cy, psfs, objp, &par); flag = neighbor(refim, (int)x, (int)y, &par, objp->bg); if (par.verbose) printf("%4d %9.3f %9.3f %9.1f %5d\n", i, x, y, objp->p_flux, flag); x_tmp = (float)(x + x0_off); y_tmp = (float)(y + y0_off); if (par.verbose > 2) { printf("x_tmp= %g y_tmp= %g\n", x_tmp, y_tmp); printf("ofs= %d 13*ofs= %d\n", ofs, 13*ofs); } memcpy(&record[ 0*ofs], &x_tmp , ofs); memcpy(&record[ 1*ofs], &y_tmp , ofs); memcpy(&record[ 2*ofs], &objp->p_flux , ofs); memcpy(&record[ 3*ofs], &objp->p_err , ofs); memcpy(&record[ 4*ofs], &objp->a_flux , ofs); memcpy(&record[ 5*ofs], &objp->a_err , ofs); memcpy(&record[ 6*ofs], &objp->bg , ofs); memcpy(&record[ 7*ofs], &objp->chi2_n , ofs); memcpy(&record[ 8*ofs], &objp->corr , ofs); memcpy(&record[ 9*ofs], &objp->nbad , sizeof(int)); memcpy(&record[10*ofs], &objp->vtype , sizeof(int)); memcpy(&record[11*ofs], &objp->nframes, sizeof(int)); memcpy(&record[12*ofs], &flag , sizeof(int)); if (par.verbose > 2) printf("record prepared - writting...\n"); fwrite(record, 1, sizeof(record), outfcat); } } /*** the same for transients ***/ if (par.verbose) printf("var2:\n num X Y flux flag\n"); for (i=0; i<nobj2; i++) { if (par.verbose > 1) printf("i= %d\n", i); objp = &obj2[i]; x = (double)(objp->x); y = (double)(objp->y); if (par.verbose > 1) printf("x= %g y= %g\n", x, y); if ((x < par.bad_margin) || (x >= par.nx0 - par.bad_margin)) continue; if ((y < par.bad_margin) || (y >= par.ny0 - par.bad_margin)) continue; cx = objp->cx; cy = objp->cy; if (par.verbose > 1) printf("cx= %d cy= %d\n", cx, cy); /*** append to a binary file ***/ if (objp->nframes > 0) { fprintf(outfcoo, "%5d %11.5f %11.5f\n", nvar, x, y); nvar++; /*** get template photometry ***/ init_psf(&psf, x, y); make_psf(&psf, x, y, cx, cy, psfs); obj2[i].bg = bkg(refim, cx, cy, &par); get_phot(refim, refim, refim, x, y, cx, cy, psfs, objp, &par); flag = neighbor(refim, (int)x, (int)y, &par, objp->bg); if (par.verbose) printf("%4d %9.3f %9.3f %9.1f %5d\n", i+nobj1, x, y, objp->p_flux, flag); x_tmp = (float)(x + x0_off); y_tmp = (float)(y + y0_off); if (par.verbose > 2) { printf("x_tmp= %g y_tmp= %g\n", x_tmp, y_tmp); printf("ofs= %d 13*ofs= %d\n", ofs, 13*ofs); } memcpy(&record[ 0*ofs], &x_tmp , ofs); memcpy(&record[ 1*ofs], &y_tmp , ofs); memcpy(&record[ 2*ofs], &objp->p_flux , ofs); memcpy(&record[ 3*ofs], &objp->p_err , ofs); memcpy(&record[ 4*ofs], &objp->a_flux , ofs); memcpy(&record[ 5*ofs], &objp->a_err , ofs); memcpy(&record[ 6*ofs], &objp->bg , ofs); memcpy(&record[ 7*ofs], &objp->chi2_n , ofs); memcpy(&record[ 8*ofs], &objp->corr , ofs); memcpy(&record[ 9*ofs], &objp->nbad , sizeof(int)); memcpy(&record[10*ofs], &objp->vtype , sizeof(int)); memcpy(&record[11*ofs], &objp->nframes, sizeof(int)); memcpy(&record[12*ofs], &flag , sizeof(int)); if (par.verbose > 2) printf("record prepared - writting...\n"); fwrite(record, 1, sizeof(record), outfcat); } } fclose(outfcoo); fclose(outfcat); if (par.verbose) printf("\nVariability seach fihished!\n"); return(0); }