void read_or_make_image(const char* filename, double value, size_t width, size_t height, cl_float** image) { // if file is given if(filename) { // file width and height size_t w, h; // read FITS file read_fits(filename, TFLOAT, &w, &h, (void**)image); // make sure dimensions agree if(w != width || h != height) errorf(filename, 0, "wrong dimensions: %zu x %zu (should be %zu x %zu)", w, h, width, height); } else { // total size of image size_t size = width*height; // make array for image cl_float* x = malloc(size*sizeof(cl_float)); if(!x) errori(NULL); // set value for each pixel for(size_t i = 0; i < size; ++i) x[i] = value; // output image *image = x; } }
/***********************************************************************//** * @brief Read spectrum from FITS file * * @param[in] fits FITS file. * @param[in] extno Extension number of spectrum. * * @exception GMWLException::file_open_error * No table found in file. * * Read the spectrum from a FITS table found in the specified extension. * In no extension number if specified (or if extno=0) then the spectrum * is loaded from the first table extension that is found in the file. ***************************************************************************/ void GMWLSpectrum::read(const GFits& fits, const int& extno) { // Clear object clear(); // Initialise extension number int extension = extno; // If the extension number is 0 then load first FITS table in file. if (extension == 0) { for (int i = 0; i < fits.size(); ++i) { if (fits.at(i)->exttype() == GFitsHDU::HT_ASCII_TABLE || fits.at(i)->exttype() == GFitsHDU::HT_BIN_TABLE) { extension = i; break; } } } // If we found no table then throw an exception if (extension == 0) { throw GMWLException::file_open_error(G_READ, fits.filename().url(), "No table found in file."); } // Get table pointer const GFitsTable& table = *fits.table(extension); // Read spectrum from table read_fits(table); // Return return; }
void read_image(const char* filename, size_t* width, size_t* height, cl_float** image) { // read FITS file read_fits(filename, TFLOAT, width, height, (void**)image); // TODO process depending on format }
int main(int argc, char* argv[]) { unsigned char* a; unsigned char* b; unsigned char* c; long nelements; int i; int errors = 0; unsigned char maxi = 0; unsigned char delta; if (argc != 4){ printf("Usage: %s <raw> <offset or dark> <result>\n",argv[0]); return -1; } if (read_fits(argv[1],&a) == 0){ if (read_fits(argv[2],&b) == 0){ nelements = width*height; c = malloc(sizeof(unsigned char)*nelements); for(i=0;i<nelements;i++){ if (a[i] >= b[i]) c[i] = a[i] - b[i]; else { c[i] = 0; delta = b[i] - a[i]; if (delta > maxi) maxi = delta; errors++; } } printf("%d errors : maxi =%d\n",errors,maxi); remove(argv[3]); if (write_fits(argv[3],c) == 0){ free(a); free(b); free(c); return 0; } } } free(a); free(b); free(c); return -1; }
int main(int argc,char *argv[]) { int i; struct image img; img=read_fits(argv[1]); write_composite_pgm("avg.pgm",img); write_pgm("f1.pgm",img); return 0; }
void read_mask(const char* maskname, const char* imagename, const pcsdata* pcs, size_t width, size_t height, int** mask) { // mask width and height size_t msk_w = width, msk_h = height; #ifdef LENSED_REGIONS // check if file is FITS if(is_fits(maskname)) read_fits(maskname, TINT, &msk_w, &msk_h, (void**)mask); // else try to read region file else if(read_regions_mask(maskname, imagename, pcs, width, height, mask)) errorf(maskname, 0, "file does not contain a recognized mask format"); #else // read mask from FITS file read_fits(maskname, TINT, &msk_w, &msk_h, (void**)mask); #endif // make sure dimensions agree if(msk_w != width || msk_h != height) errorf(maskname, 0, "wrong dimensions %zu x %zu for mask (should be %zu x %zu)", msk_w, msk_h, width, height); }
/***********************************************************************//** * @brief Read spectrum from FITS file * * @param[in] fits FITS file. * @param[in] extname FITS extension name. ***************************************************************************/ void GMWLSpectrum::read(const GFits& fits, const std::string& extname) { // Clear object clear(); // Get table pointer const GFitsTable& table = *fits.table(extname); // Read spectrum from table read_fits(table); // Return return; }
void read_psf(const char* filename, size_t* width, size_t* height, cl_float** psf) { // normalisation double norm; size_t size, i; // read PSF from FITS file read_fits(filename, TFLOAT, width, height, (void**)psf); // normalise PSF norm = 0; size = (*width)*(*height); for(i = 0; i < size; ++i) norm += (*psf)[i]; for(i = 0; i < size; ++i) (*psf)[i] /= norm; }
int main(int argc,char *argv[]) { int i,j,k,l,m; struct catalog c; struct transformation t; double ra0,de0; float rmsmin; float x[NMAX],y[NMAX],rx[NMAX],ry[NMAX]; struct image img; char filename[128]; if (argc==1) strcpy(filename,"test.fits"); else if (argc==2) strcpy(filename,argv[1]); img=read_fits(filename); printf("files read\n"); c=read_catalog("out.dat"); printf("files read\n"); // Initial fit t.ra0=c.ra[0]; t.de0=c.de[0]; t.x0=(float) img.naxis1/2.0; t.y0=(float) img.naxis2/2.0; for (l=0;l<10;l++) { for (j=0;j<5;j++) { // Transform for (i=0;i<c.n;i++) forward(t.ra0,t.de0,c.ra[i],c.de[i],&c.rx[i],&c.ry[i]); // Select for (i=0,k=0;i<c.n;i++) { if (c.usage[i]==1) { x[k]=c.x[i]; y[k]=c.y[i]; rx[k]=c.rx[i]; ry[k]=c.ry[i]; k++; } } // Fit lfit2d(x,y,rx,k,t.a); lfit2d(x,y,ry,k,t.b); printf("%f %f %f %f %f %f %f %f\n",t.ra0,t.de0,t.a[0],t.a[1],t.a[2],t.b[0],t.b[1],t.b[2]); // Move reference point reverse(t.ra0,t.de0,t.a[0],t.b[0],&ra0,&de0); t.ra0=ra0; t.de0=de0; } // Compute and plot residuals for (i=0,c.xrms=0.0,c.yrms=0.0,m=0;i<c.n;i++) { if (c.usage[i]==1) { c.xres[i]=c.rx[i]-(t.a[0]+t.a[1]*c.x[i]+t.a[2]*c.y[i]); c.yres[i]=c.ry[i]-(t.b[0]+t.b[1]*c.x[i]+t.b[2]*c.y[i]); printf("%12.4f %12.4f %12.4f %12.4f %10.4f %10.4f\n",c.x[i],c.y[i],c.rx[i],c.ry[i],c.xres[i],c.yres[i]); c.res[i]=sqrt(c.xres[i]*c.xres[i]+c.yres[i]*c.yres[i]); c.xrms+=c.xres[i]*c.xres[i]; c.yrms+=c.yres[i]*c.yres[i]; c.rms+=c.xres[i]*c.xres[i]+c.yres[i]*c.yres[i]; m++; } } c.xrms=sqrt(c.xrms/(float) m); c.yrms=sqrt(c.yrms/(float) m); c.rms=sqrt(c.rms/(float) m); // Deselect outliers for (i=0;i<c.n;i++) { if (c.res[i]>2*c.rms) c.usage[i]=0; } } printf("%12.8lf %10.6lf %10.6lf %8.4f %8.4f %8.4f %8.4f\n",img.mjd,t.ra0,t.de0,t.a[1],t.a[2],t.b[1],t.b[2]); printf("%d/%d %f %f %f\n",m,c.n,c.xrms,c.yrms,c.rms); // add_fits_keywords(t,"test.fits"); modify_fits_keywords(t,filename); return 0; }
int main(int argc, char* argv[]) { unsigned char* c; long nelements; unsigned char factor; unsigned char thresholda; unsigned char thresholdb; unsigned long ul; unsigned char mini; unsigned char maxi; int i,j,k,l; int n; int na, nb; unsigned char us; int va,vb,vc,size,index; Tpivot pivota[4]; Tpivot pivotb[4]; Px* pxa; Px* pxb; Px* pxl; int* a2; int* b2; int iter; int dx,dy,distance,distmini; int x,y; int x0, y0; int x1, y1; int upper; int lista[10]; int listb[10]; int pointa[20]; int pointb[20]; int point[20]; int la,lb; int go; int nsample; int ii; nsample = 20; x=0; y=0; x0=0; y0=0; xmini = 0; ymini = 0; unsigned int* score; if (argc != 5){ printf("Usage: %s <image1> <image2> <result> <factor>\n",argv[0]); return -1; } factor = atoi(argv[4]); if (read_fits(argv[1],&a) == 0){ if (read_fits(argv[2],&b) == 0){ nelements = height * width; // detect maxi/mini mini = UCHAR_MAX; maxi = 0; for (i=0;i<nelements;i++){ us = a[i]; if ( us < mini ) mini = us; if ( us > maxi ) maxi = us; } ul = (unsigned long)(maxi-mini)*(unsigned long)factor; thresholda = (unsigned char)(ul/100L)+mini; printf("mini=%d - maxi=%d - threshold=%d\n",mini,maxi,thresholda); // // detect stars aa = malloc(sizeof(unsigned char)*nelements); for (i=0;i<nelements;i++){ aa[i] = 0; } n = 0; for (y=1;y<height-1;y++) for(x=1;x<width-1;x++){ i = y*width+x; us = a[i]; aa[i] = us; for(y0=y-1;y0<=y+1;y0++){ for(x0=x-1;x0<=x+1;x0++){ if (a[y0*width+x0] > us) { aa[i] = 0; break; } } } if (aa[i]!=0){ //printf("(%d,%d)\n",x,y); n++; } } printf("detect %d/%ld\n",n,nelements); na = n; pxa = malloc(sizeof(Px)*na); // filter upper = INT_MAX; j = 0; for (i=0;i<nsample;i++){ //printf(". %d\n",upper); ii = get_max_uchar(aa,nelements,&upper); //printf(".. %d\n",upper); if (upper == 0) break; pxa[j].x = ii % width; pxa[j].y = ii / width; j++; } na = j; a2 = malloc(sizeof(int)*na*na); for(i=0;i<na;i++) for(j=0;j<na;j++){ dx=pxa[i].x-pxa[j].x; dy=pxa[i].y-pxa[j].y; distance=dx*dx+dy*dy; distance = (int)sqrt((double)distance); //printf("%d:%d -> %d\n",i,j,distance); a2[j*na+i]=distance; } // detect maxi/mini mini = UCHAR_MAX; maxi = 0; for (i=0;i<nelements;i++){ us = b[i]; if ( us < mini ) mini = us; if ( us > maxi ) maxi = us; } ul = (unsigned long)(maxi-mini)*(unsigned long)factor; thresholdb = (unsigned char)(ul/100L)+mini; printf("mini=%d - maxi=%d - threshold=%d\n",mini,maxi,thresholdb); // detect stars bb = malloc(sizeof(unsigned char)*nelements); for (i=0;i<nelements;i++){ bb[i] = 0; } n = 0; for (y=1;y<height-1;y++) for(x=1;x<width-1;x++){ i = y*width+x; us = b[i]; bb[i] = us; for(y0=y-1;y0<=y+1;y0++){ for(x0=x-1;x0<=x+1;x0++){ if (b[y0*width+x0] > us) { bb[i] = 0; break; } } } if (bb[i]!=0){ //printf("(%d,%d)\n",x,y); n++; } } printf("detect %d/%ld\n",n,nelements); nb = n; pxb = malloc(sizeof(Px)*nb); // filter upper = INT_MAX; j = 0; for (i=0;i<nsample;i++){ ii = get_max_uchar(bb,nelements,&upper); //printf(".. %d\n",upper); if (upper == 0) break; pxb[j].x = ii % width; pxb[j].y = ii / width; j++; } nb = j; b2 = malloc(sizeof(int)*nb*nb); for(i=0;i<nb;i++) for(j=0;j<nb;j++){ dx=pxb[i].x-pxb[j].x; dy=pxb[i].y-pxb[j].y; distance=dx*dx+dy*dy; distance = (int)sqrt((double)distance); //printf("%d:%d -> %d\n",i,j,distance); b2[j*nb+i]=distance; } // segment correspondance upper = INT_MAX; go = 1; i = 0; while ( go ) { la = get_max(a2,na*na,&upper); if (la>=0) { //printf("upper=%d a=%d\n",upper,la); lb = get_index(b2,nb*nb,upper); if (lb>=0) { //printf("found b=%d\n",lb); lista[i] = la; listb[i++] = lb; if (i==10) go=0; } } else { go = 0; } } size = i; printf("distance match=%d\n",size); if (size == 0) { printf ("No matching found!\n"); exit(0); } for(j=0;j<size;j++){ la = lista[j]; pointa[2*j ]=la%na; pointa[2*j+1]=la/na; lb = listb[j]; pointb[2*j ]=lb%nb; pointb[2*j+1]=lb/nb; } pxl = malloc(sizeof(Px)*size); // middle of segments for(j=0;j<size;j++){ x0 = pointa[2*j]; x1 = pointa[2*j+1]; y0 = pointb[2*j]; y1 = pointb[2*j+1]; //printf("a (%d,%d) - (%d,%d)\n",pxa[x0].x,pxa[x0].y,pxa[x1].x,pxa[x1].y); x = (pxa[x0].x+pxa[x1].x) - (pxb[y0].x+pxb[y1].x); x = x/2; //printf("b (%d,%d) - (%d,%d)\n",pxb[y0].x,pxb[y0].y,pxb[y1].x,pxb[y1].y); y = (pxa[x0].y+pxa[x1].y) - (pxb[y0].y+pxb[y1].y); y = y/2; pxl[j].x = x; pxl[j].y = y; //printf("dx=%d , dy=%d\n",x,y); } score = malloc(sizeof(unsigned int)*size); for (i=0;i<size;i++){ score[i] = 0; for(j=i;j<size;j++) if ((pxl[i].x == pxl[j].x) && (pxl[i].y == pxl[j].y)) score[i]++; } maxi = 0; index = 0; for (i=0;i<size;i++){ //printf("%d:%d (%d,%d)\n",i,score[i],pxl[i].x,pxl[i].y); if (score[i] > maxi){ maxi = score[i]; index = i; } } printf("max score= %d index=%d\n",maxi,index); if (size == 1) { index = 0; } else { if (maxi < 2) { printf("No enough matching\n"); free(pxl); free(score); exit(0); } } x = (pxl[index].x); y = (pxl[index].y); free(pxl); free(score); xmini = -x; ymini = -y; printf("translate: x:%d y:%d\n",xmini,ymini); // output fits is the image2 translated ( relative to image1) c = malloc(sizeof(unsigned char)*nelements); for(y=0;y< height;y++) for(x=0;x< width; x++){ x0=x+xmini; y0=y+ymini; if ( ( x0 >= width ) || ( x0 < 0) || ( y0 >= height ) || (y0 < 0) ){ c[x+y*width] = 0; } else { c[x+y*width] = b[x0+y0*width]; } } remove(argv[3]); if (write_fits(argv[3],c) == 0){ free(a); free(b); free(c); return 0; } } } free(a); free(b); free(c); return -1; }
int main(int argc,char *argv[]) { int i,j,k,l,m; struct transformation t; struct image img; char *fitsfile=NULL,*reffile=NULL,catfile[128],calfile[128]; FILE *outfile; struct catalog c; float mmin=10.0,rmin=10.0; double mjd0=51544.5,ra0,de0,ra1,de1; float q0,q1; float rmsmin; float x[NMAX],y[NMAX],rx[NMAX],ry[NMAX]; int arg=0,plot=0,add=0,track=0; char *env,starfile[128]; // Environment variables env=getenv("ST_DATADIR"); sprintf(starfile,"%s/data/tycho2.dat",env); // Decode options if (argc>1) { while ((arg=getopt(argc,argv,"f:r:m:R:hpnta"))!=-1) { switch (arg) { case 'f': fitsfile=optarg; break; case 'r': reffile=optarg; break; case 'm': mmin=atof(optarg); break; case 't': track=1; break; case 'R': rmin=atof(optarg); break; case 'p': plot=1; break; case 'a': add=1; break; case 'h': usage(mmin,rmin); return 0; default: usage(mmin,rmin); return 0; } } } else { usage(mmin,rmin); return 0; } // Check if minimum input is provided if (fitsfile==NULL || reffile==NULL) { usage(mmin,rmin); return 0; } // Check this is indeed a FITS file if (is_fits_file(fitsfile)!=1) { printf("%s is not a FITS file\n",fitsfile); return -1 ; } // Check this is indeed a FITS file if (is_fits_file(reffile)!=1) { printf("%s is not a FITS file\n",reffile); return -1 ; } // Read fits file img=read_fits(fitsfile); sprintf(catfile,"%s.cat",fitsfile); sprintf(calfile,"%s.cal",fitsfile); // Read reference transformation t=reference(reffile); // Correct astrometry for fixed or tracked setup if (track==0) { precess(mjd0,t.ra0,t.de0,t.mjd,&ra1,&de1); ra1=modulo(ra1+gmst(img.mjd)-gmst(t.mjd),360.0); precess(img.mjd,ra1,de1,mjd0,&t.ra0,&t.de0); } // Match catalog c=match_catalogs(catfile,starfile,t,img,rmin,mmin); // Plot if (plot==1) plot_image(img,t,c,catfile,mmin); // Do fit if (c.n>10) { for (l=0;l<10;l++) { for (j=0;j<5;j++) { // Transform for (i=0;i<c.n;i++) forward(t.ra0,t.de0,c.ra[i],c.de[i],&c.rx[i],&c.ry[i]); // Select for (i=0,k=0;i<c.n;i++) { if (c.usage[i]==1) { x[k]=c.x[i]; y[k]=c.y[i]; rx[k]=(float) c.rx[i]; ry[k]=(float) c.ry[i]; k++; } } // Fit lfit2d(x,y,rx,k,t.a); lfit2d(x,y,ry,k,t.b); // Move reference point reverse(t.ra0,t.de0,t.a[0],t.b[0],&ra0,&de0); t.ra0=ra0; t.de0=de0; } // Compute and plot residuals for (i=0,t.xrms=0.0,t.yrms=0.0,m=0;i<c.n;i++) { if (c.usage[i]==1) { c.xres[i]=c.rx[i]-(t.a[0]+t.a[1]*c.x[i]+t.a[2]*c.y[i]); c.yres[i]=c.ry[i]-(t.b[0]+t.b[1]*c.x[i]+t.b[2]*c.y[i]); c.res[i]=sqrt(c.xres[i]*c.xres[i]+c.yres[i]*c.yres[i]); t.xrms+=c.xres[i]*c.xres[i]; t.yrms+=c.yres[i]*c.yres[i]; t.rms+=c.xres[i]*c.xres[i]+c.yres[i]*c.yres[i]; m++; } } t.xrms=sqrt(t.xrms/(float) m); t.yrms=sqrt(t.yrms/(float) m); t.rms=sqrt(t.rms/(float) m); // Deselect outliers for (i=0;i<c.n;i++) { if (c.res[i]>2*t.rms) c.usage[i]=0; } } } else { t.xrms=0.0; t.yrms=0.0; t.rms=0.0; } // Print results outfile=fopen(calfile,"w"); for (i=0;i<c.n;i++) if (c.usage[i]==1) fprintf(outfile,"%10.4f %10.4f %10.6f %10.6f %8.3f %8.3f %8.3f %8.3f %8.3f\n",c.x[i],c.y[i],c.ra[i],c.de[i],c.vmag[i],c.imag[i],c.fb[i],c.fm[i],c.bg[i]); fclose(outfile); printf("%s %8.4lf %8.4lf ",fitsfile,t.ra0,t.de0); printf("%3d/%3d %6.1f %6.1f %6.1f\n",m,c.n,t.xrms,t.yrms,t.rms); // Add keywords if (add==1) add_fits_keywords(t,fitsfile); else modify_fits_keywords(t,fitsfile); return 0; }