static int wcstools_radec2pixelxy(const struct WorldCoor* wcs, double ra, double dec, double* px, double* py) { int offscl; wcs2pix(wcs, ra, dec, px, py, &offscl); return offscl; }
/** * Function: radec_to_ij * Returns the detector pixel coordinates corresponding to the * point radec in the sky sgiven the WCS wcs. * * Parameter: * @param wcs a pointer to a WorldCoor WCS structure * @param radec a point with celestial coordinates * * Return: * @return a pointer to a d_point structure containing the image * pixel coordinates. */ d_point * radec_to_ij (struct WorldCoor * wcs, sky_coord radec) { d_point *pos; int offscl; pos = malloc (sizeof (d_point)); wcs2pix (wcs, radec.ra, radec.dec, &pos->x, &pos->y, &offscl); return pos; }
int main(int argc, char **argv) { int i; Fun fun; struct WorldCoor *wcs; /* WCS info */ double x,y,ra,dec,xr,yr; if(argc == 1){ fprintf(stderr, "usage: twcs iname\n"); exit(1); } /* open Funtools file */ /* Funopen makes initial WCS library call: wcs = wcsinit(header_string) */ if( !(fun = FunOpen(argv[1], "r ", NULL)) ){ fprintf(stderr, "ERROR can't open file: %s\n", argv[1]); exit(1); } /* get wcs structure */ FunInfoGet(fun,FUN_WCS,&wcs,0); if( !wcs || !iswcs(wcs) ){ fprintf(stderr,"No WCS data"); return(1); } /* read input, convert pixels to wcs and back */ while(1){ fprintf(stdout,"\nInput x y: "); if(scanf("%lf %lf", &x, &y) != EOF){ if(x <= -999) break; /* convert image pixels to sky coords */ pix2wcs(wcs, x, y, &ra, &dec); fprintf(stdout,"Convert from pixels to ra,dec using pix2wcs()\n"); fprintf(stdout, "x=%.10g y=%.10g -> ra=%.10g dec=%.10g\n", x, y, ra, dec); /* convert sky coords to image pixels */ fprintf(stdout,"Convert from ra,dec -> pixels using wcs2pix()\n"); wcs2pix(wcs, ra, dec, &xr, &yr, &i); fprintf(stdout, "ra=%.10g dec=%.10g -> x=%.10g y=%.10g offscale=%d\n", ra, dec, xr, yr, i); } else break; } /* clean up */ /* FunClose makes final WCS library call: wcsfree(wcs) */ FunClose(fun); return(0); }
/* convert pixels to wcs and return string */ char *wcs2pixstr(int n, double ra, double dec){ Info info = getinfo(n); char str[SZ_LINE]; double xpix, ypix; int offscale; if( info->wcs ){ wcs2pix(info->wcs, ra, dec, &xpix, &ypix, &offscale); // convert to 1-indexed image coords snprintf(str, SZ_LINE-1, "%.3f %.3f", xpix+1, ypix+1); nowhite(str, info->str); return info->str; } else { return NULL; } }
/////////////////////////////////////////////////////////////////////////////// // calculates and returns the input pixel coordinates of the outut // coordinates xOut / yOut PixelCoord PixToPix::CalcInPixels(double xOut, double yOut) { double xPos, yPos; // sky coordinates double xIn, yIn; int offscl = 0; pix2wcs(m_outWcs, xOut, yOut, &xPos, &yPos); gal2fk5(&xPos, &yPos); wcs2pix(m_inWcs, xPos, yPos, &xIn, &yIn, &offscl); return PixelCoord(xIn, yIn, true); }
/////////////////////////////////////////////////////////////////////////////// // calculates and returns the output pixel coordinates of the input // coordinates xIn / yIn // PixelCoord.m_valid is set to false if the coordinates outside of the // of the output area PixelCoord PixToPix::CalcOutPixels(double xIn, double yIn) { double xPos, yPos; // sky coordinates double xOut, yOut; bool valid = true; int offscl = 0; /* Pixels in fits images don't start at 0, but 1. C images start at 0...*/ pix2wcs(m_inWcs, xIn, yIn, &xPos, &yPos); /* Now, xPos and yPos are in equatorial (ie, RA, DEC). This is because this is what our input is in, ie, if you have something not integral this won't work */ /* Convert to Glactic */ fk52gal(&xPos,&yPos); // test if we are inside the current sky area if (yPos < m_lowB || yPos > m_highB) valid = false; if (m_lowL < m_highL) { // normal case if (xPos < m_lowL || xPos > m_highL) valid = false; } else { // we crossed the 0/360 line if (xPos < m_lowL && xPos > m_highL) valid = false; } if (valid) { wcs2pix(m_outWcs, xPos, yPos, &xOut, &yOut, &offscl); if (offscl) valid = false; } return PixelCoord(xOut, yOut, valid); }
int readTemplate(char *filename) { int i; FILE *fp; char line[MAXSTR]; char *header[2]; int sys; double epoch; double x, y; double ix, iy; double xpos, ypos; int offscl; header[0] = malloc(32768); header[1] = (char *)NULL; /********************************************************/ /* Open the template file, read and parse all the lines */ /********************************************************/ fp = fopen(filename, "r"); if(fp == (FILE *)NULL) printError("Template file not found"); while(1) { if(fgets(line, MAXSTR, fp) == (char *)NULL) break; if(line[strlen(line)-1] == '\n') line[strlen(line)-1] = '\0'; if(debug >= 2) { printf("Template line: [%s]\n", line); fflush(stdout); } for(i=strlen(line); i<80; ++i) line[i] = ' '; line[80] = '\0'; strcat(header[0], line); parseLine(line); } if(debug >= 2) { printf("\nheader ----------------------------------------\n"); printf("%s\n", header[0]); printf("-----------------------------------------------\n\n"); } /****************************************/ /* Initialize the WCS transform library */ /****************************************/ output.wcs = wcsinit(header[0]); if(output.wcs == (struct WorldCoor *)NULL) { printf("[struct stat=\"ERROR\", msg=\"Output wcsinit() failed.\"]\n"); exit(0); } /* Kludge to get around bug in WCS library: */ /* 360 degrees sometimes added to pixel coord */ ix = 0.5; iy = 0.5; offscl = 0; pix2wcs(output.wcs, ix, iy, &xpos, &ypos); wcs2pix(output.wcs, xpos, ypos, &x, &y, &offscl); xcorrection = x-ix; ycorrection = y-iy; /*************************************/ /* Set up the coordinate transform */ /*************************************/ if(output.wcs->syswcs == WCS_J2000) { sys = EQUJ; epoch = 2000.; if(output.wcs->equinox == 1950.) epoch = 1950; } else if(output.wcs->syswcs == WCS_B1950) { sys = EQUB; epoch = 1950.; if(output.wcs->equinox == 2000.) epoch = 2000; } else if(output.wcs->syswcs == WCS_GALACTIC) { sys = GAL; epoch = 2000.; } else if(output.wcs->syswcs == WCS_ECLIPTIC) { sys = ECLJ; epoch = 2000.; if(output.wcs->equinox == 1950.) { sys = ECLB; epoch = 1950.; } } else { sys = EQUJ; epoch = 2000.; } output.sys = sys; output.epoch = epoch; free(header[0]); return 0; }
int main(int argc, char **argv) { int i, j, l, m, c, count, ismag, ncol; int dl, dm, width, haveFlux, isImg, csys; int side, ibegin, iend, nstep, useCenter; int racol, deccol, fluxcol; int ra1col, dec1col; int ra2col, dec2col; int ra3col, dec3col; int ra4col, dec4col; long fpixel, nelements; double rac, decc; double ra[4], dec[4]; double x, y, z; double x0, y0, z0; double x1, y1, z1; double oxpix, oypix, equinox; int offscl; double ilon; double ilat; double pixscale, len, sideLength, dtr; double offset; double xn, yn, zn; double ran, decn; double sina, cosa; double sind, cosd; double a11, a12, a13; double a21, a22, a23; double a31, a32, a33; double x0p, y0p, z0p; double x1p, y1p, z1p; double lon0, lon1; double xp, yp; double lon; double pixel_value; double weights[5][5]; double weights3[5][5] = {{0.0, 0.0, 0.0, 0.0, 0.0}, {0.0, 0.1, 0.2, 0.1, 0.0}, {0.0, 0.2, 1.0, 0.2, 0.0}, {0.0, 0.1, 0.2, 0.1, 0.0}, {0.0, 0.0, 0.0, 0.0, 0.0}}; double weights5[5][5] = {{0.0, 0.1, 0.2, 0.1, 0.0}, {0.1, 0.3, 0.5, 0.3, 0.1}, {0.2, 0.5, 1.0, 0.5, 0.2}, {0.1, 0.3, 0.5, 0.3, 0.1}, {0.0, 0.1, 0.2, 0.1, 0.0}}; double **data; int status = 0; char input_file [MAXSTR]; char colname [MAXSTR]; char output_file [MAXSTR]; char template_file[MAXSTR]; int bitpix = DOUBLE_IMG; long naxis = 2; double sumweights; double refmag; dtr = atan(1.0)/45.; /***************************************/ /* Process the command-line parameters */ /***************************************/ ismag = 0; opterr = 0; useCenter = 0; strcpy(colname, ""); while ((c = getopt(argc, argv, "pm:d:w:c:")) != EOF) { switch (c) { case 'm': ismag = 1; refmag = atof(optarg); break; case 'd': debug = debugCheck(optarg); break; case 'w': width = atoi(optarg); break; case 'c': strcpy(colname, optarg); break; case 'p': useCenter = 1; break; default: printf("[struct stat=\"ERROR\", msg=\"Usage: %s [-c column][-m refmag][-d level][-w size] in.tbl out.fits hdr.template\"]\n", argv[0]); exit(1); break; } } if (argc - optind < 3) { printf("[struct stat=\"ERROR\", msg=\"Usage: %s [-c column][-m refmag][-d level][-w size] in.tbl out.fits hdr.template\"]\n", argv[0]); exit(1); } strcpy(input_file, argv[optind]); strcpy(output_file, argv[optind+1]); strcpy(template_file, argv[optind+2]); if(debug >= 1) { printf("input_file = [%s]\n", input_file); printf("colname = [%s]\n", colname); printf("output_file = [%s]\n", output_file); printf("template_file = [%s]\n", template_file); printf("width = %d\n", width); printf("ismag = %d\n", ismag); fflush(stdout); } /********************************************/ /* Set the weights for spreading the points */ /********************************************/ sumweights = 0.; for(i=0; i<5; ++i) { for(j=0; j<5; ++j) { if(width == 3) sumweights += weights3[i][j]; else if(width == 5) sumweights += weights5[i][j]; } } for(i=0; i<5; ++i) { for(j=0; j<5; ++j) { if(width == 3) weights[i][j] = weights3[i][j]/sumweights; else if(width == 5) weights[i][j] = weights5[i][j]/sumweights; else weights[i][j] = 0.; } } if(width != 3 && width != 5) weights[2][2] = 1.; /************************************************/ /* Open the table file and find the data column */ /************************************************/ ncol = topen(input_file); if(ncol <= 0) { printf("[struct stat=\"ERROR\", msg=\"Can't open input table %s\"]\n", input_file); exit(0); } racol = tcol( "ra"); deccol = tcol("dec"); ra1col = tcol( "ra1"); dec1col = tcol("dec1"); ra2col = tcol( "ra2"); dec2col = tcol("dec2"); ra3col = tcol( "ra3"); dec3col = tcol("dec3"); ra4col = tcol( "ra4"); dec4col = tcol("dec4"); haveFlux = 1; if(strlen(colname) == 0) haveFlux = 0; else fluxcol = tcol(colname); isImg = 1; if(ra1col < 0 || dec1col < 0 || ra2col < 0 || dec2col < 0 || ra3col < 0 || dec3col < 0 || ra4col < 0 || dec4col < 0) isImg = 0; if(useCenter) isImg = 0; if(!isImg) { if(racol < 0) { printf("[struct stat=\"ERROR\", msg=\"Can't find column 'ra'\"]\n"); exit(0); } if(deccol < 0) { printf("[struct stat=\"ERROR\", msg=\"Can't find column 'dec'\"]\n"); exit(0); } } if(haveFlux && fluxcol < 0) { printf("[struct stat=\"ERROR\", msg=\"Can't find column '%s'\"]\n", colname); exit(0); } /*************************************************/ /* Process the output header template to get the */ /* image size, coordinate system and projection */ /*************************************************/ readTemplate(template_file); pixscale = fabs(output.wcs->xinc); if(fabs(output.wcs->yinc) > pixscale) pixscale = fabs(output.wcs->yinc); csys = EQUJ; if(strncmp(output.wcs->c1type, "RA", 2) == 0) csys = EQUJ; if(strncmp(output.wcs->c1type, "GLON", 4) == 0) csys = GAL; if(strncmp(output.wcs->c1type, "ELON", 4) == 0) csys = ECLJ; equinox = output.wcs->equinox; if(debug >= 1) { printf("output.naxes[0] = %ld\n", output.naxes[0]); printf("output.naxes[1] = %ld\n", output.naxes[1]); printf("output.sys = %d\n", output.sys); printf("output.epoch = %-g\n", output.epoch); printf("output proj = %s\n", output.wcs->ptype); printf("output crval[0] = %-g\n", output.wcs->crval[0]); printf("output crval[1] = %-g\n", output.wcs->crval[1]); printf("output crpix[0] = %-g\n", output.wcs->crpix[0]); printf("output crpix[1] = %-g\n", output.wcs->crpix[1]); printf("output cdelt[0] = %-g\n", output.wcs->cdelt[0]); printf("output cdelt[1] = %-g\n", output.wcs->cdelt[1]); fflush(stdout); } /***********************************************/ /* Allocate memory for the output image pixels */ /***********************************************/ data = (double **)malloc(output.naxes[1] * sizeof(double *)); data[0] = (double *)malloc(output.naxes[0] * output.naxes[1] * sizeof(double)); if(debug >= 1) { printf("%ld bytes allocated for image pixels\n", output.naxes[0] * output.naxes[1] * sizeof(double)); fflush(stdout); } /**********************************************************/ /* Initialize pointers to the start of each row of pixels */ /**********************************************************/ for(i=1; i<output.naxes[1]; i++) data[i] = data[i-1] + output.naxes[0]; if(debug >= 1) { printf("pixel line pointers populated\n"); fflush(stdout); } for (j=0; j<output.naxes[1]; ++j) { for (i=0; i<output.naxes[0]; ++i) { data[j][i] = 0.; } } /************************/ /* Create the FITS file */ /************************/ remove(output_file); if(fits_create_file(&output.fptr, output_file, &status)) printFitsError(status); /*********************************************************/ /* Create the FITS image. All the required keywords are */ /* handled automatically. */ /*********************************************************/ if (fits_create_img(output.fptr, bitpix, naxis, output.naxes, &status)) printFitsError(status); if(debug >= 1) { printf("FITS image created (not yet populated)\n"); fflush(stdout); } /*****************************/ /* Loop over the input files */ /*****************************/ time(&currtime); start = currtime; /*******************/ /* For each source */ /*******************/ count = 0; while(tread() >= 0) { ++count; if(isImg) { if(debug && count/1000*1000 == count) { printf("%9d image outlines processed\n", count); fflush(stdout); } ra [0] = atof(tval( ra1col)); dec[0] = atof(tval(dec1col)); ra [1] = atof(tval( ra2col)); dec[1] = atof(tval(dec2col)); ra [2] = atof(tval( ra3col)); dec[2] = atof(tval(dec3col)); ra [3] = atof(tval( ra4col)); dec[3] = atof(tval(dec4col)); for(side=0; side<4; ++side) { ibegin = side; iend = (side+1)%4; x0 = cos(ra[ibegin]*dtr) * cos(dec[ibegin]*dtr); y0 = sin(ra[ibegin]*dtr) * cos(dec[ibegin]*dtr); z0 = sin(dec[ibegin]*dtr); x1 = cos(ra[iend]*dtr) * cos(dec[iend]*dtr); y1 = sin(ra[iend]*dtr) * cos(dec[iend]*dtr); z1 = sin(dec[iend]*dtr); xn = y0*z1 - z0*y1; yn = z0*x1 - x0*z1; zn = x0*y1 - y0*x1; len = sqrt(xn*xn + yn*yn + zn*zn); xn = xn / len; yn = yn / len; zn = zn / len; ran = atan2(yn, xn); decn = asin(zn); sina = sin(ran); cosa = cos(ran); sind = sin(decn); cosd = cos(decn); a11 = cosa*sind; a12 = sina*sind; a13 = -cosd; a21 = -sina; a22 = cosa; a23 = 0.; a31 = cosa*cosd; a32 = sina*cosd; a33 = sind; x0p = a11*x0 + a12*y0 + a13*z0; y0p = a21*x0 + a22*y0 + a23*z0; z0p = a31*x0 + a32*y0 + a33*z0; x1p = a11*x1 + a12*y1 + a13*z1; y1p = a21*x1 + a22*y1 + a23*z1; z1p = a31*x1 + a32*y1 + a33*z1; lon0 = atan2(y0p, x0p); lon1 = atan2(y1p, x1p); if(fabs(lon1-lon0)/dtr > 180.) { if(lon0 < 0.) lon0 += 360.*dtr; if(lon1 < 0.) lon1 += 360.*dtr; } sideLength = acos(x0*x1 + y0*y1 + z0*z1) / dtr; offset = pixscale/2.*dtr; if(lon0 > lon1) offset = -offset; nstep = (lon1 - lon0)/offset; lon = lon0; for(i=0; i<nstep; ++i) { lon += offset; xp = cos(lon); yp = sin(lon); x = a11*xp + a21*yp; y = a12*xp + a22*yp; z = a13*xp + a23*yp; rac = atan2(y,x)/dtr; decc = asin(z)/dtr; convertCoordinates (EQUJ, 2000., rac, decc, csys, equinox, &ilon, &ilat, 0.); offscl = 0; wcs2pix(output.wcs, ilon, ilat, &oxpix, &oypix, &offscl); fixxy(&oxpix, &oypix, &offscl); if(haveFlux) pixel_value = atof(tval(fluxcol)); else pixel_value = 1; l = (int)(oxpix + 0.5) - 1; m = (int)(oypix + 0.5) - 1; if(!offscl) data[m][l] = pixel_value; } } } else { if(debug && count/1000*1000 == count) { printf("%9d sources processed\n", count); fflush(stdout); } rac = atof(tval(racol)); decc = atof(tval(deccol)); convertCoordinates (EQUJ, 2000., rac, decc, csys, equinox, &ilon, &ilat, 0.); if(haveFlux) pixel_value = atof(tval(fluxcol)); else pixel_value = 1; if(ismag) pixel_value = pow(10., 0.4 * (refmag - pixel_value)); wcs2pix(output.wcs, ilon, ilat, &oxpix, &oypix, &offscl); if(pixel_value < 1.e10) { if(debug >= 3) { printf(" value = %11.3e at coord = (%12.8f,%12.8f)", pixel_value, ilon, ilat); if(offscl) { printf(" -> opix = (%7.1f,%7.1f) OFF SCALE\n", oxpix, oypix); fflush(stdout); } else { printf(" -> opix = (%7.1f,%7.1f)\n", oxpix, oypix); fflush(stdout); } } else if(pixel_value > 1000000.) { printf(" value = %11.3e at coord = (%12.8f,%12.8f)", pixel_value, ilon, ilat); if(offscl) { printf(" -> opix = (%7.1f,%7.1f) OFF SCALE\n", oxpix, oypix); fflush(stdout); } else { printf(" -> opix = (%7.1f,%7.1f)\n", oxpix, oypix); fflush(stdout); } } if(!offscl) { l = (int)(oxpix + 0.5) - 1; m = (int)(oypix + 0.5) - 1; if(l < 0 || m < 0 || l >= output.naxes[0] || m >= output.naxes[1]) { /* printf("ERROR: l=%d, m=%d\n", l, m); fflush(stdout); */ } else { for(dl=-2; dl<=2; ++dl) { if(l+dl < 0 || l+dl>= output.naxes[0]) continue; for(dm=-2; dm<=2; ++dm) { if(m+dm < 0 || m+dm>= output.naxes[1]) continue; data[m+dm][l+dl] += weights[dm+2][dl+2] * pixel_value; } } } } } } } /************************/ /* Write the image data */ /************************/ fpixel = 1; nelements = output.naxes[0] * output.naxes[1]; if (fits_write_img(output.fptr, TDOUBLE, fpixel, nelements, data[0], &status)) printFitsError(status); if(debug >= 1) { printf("Data array written to FITS image\n"); fflush(stdout); } /*************************************/ /* Add keywords from a template file */ /*************************************/ if(fits_write_key_template(output.fptr, template_file, &status)) printFitsError(status); if(debug >= 1) { printf("Template keywords written to FITS image\n"); fflush(stdout); } /***********************/ /* Close the FITS file */ /***********************/ if(fits_close_file(output.fptr, &status)) printFitsError(status); if(debug >= 1) { printf("FITS image finalized\n"); fflush(stdout); } time(&currtime); printf("[struct stat=\"OK\", time=%d]\n", (int)(currtime - start)); fflush(stdout); exit(0); }
struct mExamineReturn * mExamine(char *infile, int areaMode, int hdu, int plane3, int plane4, double ra, double dec, double radius, int locinpix, int radinpix, int debug) { int i, j, offscl, nullcnt; int status, clockwise, nfound; int npix, nnull, first; int ixpix, iypix; int maxi, maxj; char *header; char tmpstr[32768]; char proj[32]; int csys; char csys_str[64]; char ctype1[256]; char ctype2[256]; double equinox; long naxis; long naxes[10]; double naxis1; double naxis2; double crval1; double crval2; double crpix1; double crpix2; double cdelt1; double cdelt2; double crota2; double lon, lat; double lonc, latc; double rac, decc; double racp, deccp; double ra1, dec1, ra2, dec2, ra3, dec3, ra4, dec4; double xpix, ypix, rpix, rap; double x, y, z; double xp, yp, zp; double rot, beta, dtr; double r; double sumflux, sumflux2, sumn, mean, background, oldbackground, rms, dot; double sigmaref, sigmamax, sigmamin; double val, valx, valy, valra, valdec; double min, minx, miny, minra, mindec; double max, maxx, maxy, maxra, maxdec; double x0, y0, z0; double fluxMin, fluxMax, fluxRef, sigma; struct WorldCoor *wcs; fitsfile *fptr; int ibegin, iend, jbegin, jend; long fpixel[4], nelements; double *data = (double *)NULL; struct apPhoto *ap = (struct apPhoto *)NULL; int nflux, maxflux; struct mExamineReturn *returnStruct; returnStruct = (struct mExamineReturn *)malloc(sizeof(struct mExamineReturn)); memset((void *)returnStruct, 0, sizeof(returnStruct)); returnStruct->status = 1; strcpy(returnStruct->msg, ""); nflux = 0; maxflux = MAXFLUX; /************************************************/ /* Make a NaN value to use setting blank pixels */ /************************************************/ union { double d; char c[8]; } value; double nan; for(i=0; i<8; ++i) value.c[i] = (char)255; nan = value.d; dtr = atan(1.)/45.; /* Process basic command-line arguments */ if(debug) { printf("DEBUG> areaMode = %d \n", areaMode); printf("DEBUG> infile = %s \n", infile); printf("DEBUG> ra = %-g\n", ra); printf("DEBUG> dec = %-g\n", dec); printf("DEBUG> radius = %-g\n", radius); fflush(stdout); } rpix = 0.; if(areaMode == APPHOT) ap = (struct apPhoto *)malloc(maxflux * sizeof(struct apPhoto)); /* Open the FITS file and initialize the WCS transform */ status = 0; if(fits_open_file(&fptr, infile, READONLY, &status)) { if(ap) free(ap); sprintf(returnStruct->msg, "Cannot open FITS file %s", infile); return returnStruct; } if(hdu > 0) { status = 0; if(fits_movabs_hdu(fptr, hdu+1, NULL, &status)) { if(ap) free(ap); sprintf(returnStruct->msg, "Can't find HDU %d", hdu); return returnStruct; } } status = 0; if(fits_get_image_wcs_keys(fptr, &header, &status)) { if(ap) free(ap); sprintf(returnStruct->msg, "Cannot find WCS keys in FITS file %s", infile); return returnStruct; } status = 0; if(fits_read_key_lng(fptr, "NAXIS", &naxis, (char *)NULL, &status)) { if(ap) free(ap); sprintf(returnStruct->msg, "Cannot find NAXIS keyword in FITS file %s", infile); return returnStruct; } status = 0; if(fits_read_keys_lng(fptr, "NAXIS", 1, naxis, naxes, &nfound, &status)) { if(ap) free(ap); sprintf(returnStruct->msg, "Cannot find NAXIS1,2 keywords in FITS file %s", infile); return returnStruct; } wcs = wcsinit(header); if(wcs == (struct WorldCoor *)NULL) { if(ap) free(ap); sprintf(returnStruct->msg, "WCS initialization failed."); return returnStruct; } /* A bunch of the parameters we want are in the WCS structure */ clockwise = 0; strcpy(ctype1, wcs->ctype[0]); strcpy(ctype2, wcs->ctype[1]); naxis1 = wcs->nxpix; naxis2 = wcs->nypix; crval1 = wcs->xref; crval2 = wcs->yref; crpix1 = wcs->xrefpix; crpix2 = wcs->yrefpix; cdelt1 = wcs->xinc; cdelt2 = wcs->yinc; crota2 = wcs->rot; if((cdelt1 < 0 && cdelt2 < 0) || (cdelt1 > 0 && cdelt2 > 0)) clockwise = 1; strcpy(proj, ""); if(strlen(ctype1) > 5) strcpy (proj, ctype1+5); /* We get the Equinox from the WCS. If not */ /* there we take the command-line value. We */ /* then infer Julian/Besselian as best we can. */ equinox = wcs->equinox; csys = EQUJ; strcpy(csys_str, "EQUJ"); if(strncmp(ctype1, "RA--", 4) == 0) { csys = EQUJ; strcpy(csys_str, "EQUJ"); if(equinox < 1975.) { csys = EQUB; strcpy(csys_str, "EQUB"); } } if(strncmp(ctype1, "LON-", 4) == 0) { csys = GAL; strcpy(csys_str, "GAL"); } if(strncmp(ctype1, "GLON", 4) == 0) { csys = GAL; strcpy(csys_str, "GAL"); } if(strncmp(ctype1, "ELON", 4) == 0) { csys = ECLJ; strcpy(csys_str, "ECLJ"); if(equinox < 1975.) { csys = ECLB; strcpy(csys_str, "ECLB"); } } if(debug) { printf("DEBUG> proj = [%s]\n", proj); printf("DEBUG> csys = %d\n", csys); printf("DEBUG> clockwise = %d\n", clockwise); printf("DEBUG> ctype1 = [%s]\n", ctype1); printf("DEBUG> ctype2 = [%s]\n", ctype2); printf("DEBUG> equinox = %-g\n", equinox); printf("\n"); fflush(stdout); } /* To get corners and rotation in EQUJ we need the */ /* locations of the center pixel and the one above it. */ pix2wcs(wcs, wcs->nxpix/2.+0.5, wcs->nypix/2.+0.5, &lonc, &latc); convertCoordinates (csys, equinox, lonc, latc, EQUJ, 2000., &rac, &decc, 0.); pix2wcs(wcs, wcs->nxpix/2.+0.5, wcs->nypix/2.+1.5, &lonc, &latc); convertCoordinates (csys, equinox, lonc, latc, EQUJ, 2000., &racp, &deccp, 0.); /* Use spherical trig to get the Equatorial rotation angle */ x = cos(decc*dtr) * cos(rac*dtr); y = cos(decc*dtr) * sin(rac*dtr); z = sin(decc*dtr); xp = cos(deccp*dtr) * cos(racp*dtr); yp = cos(deccp*dtr) * sin(racp*dtr); zp = sin(deccp*dtr); beta = acos(x*xp + y*yp + z*zp) / dtr; rot = asin(cos(deccp*dtr) * sin((rac-racp)*dtr)/sin(beta*dtr)) / dtr; /* And for the four corners we want them uniformly clockwise */ if(!clockwise) { pix2wcs(wcs, -0.5, -0.5, &lon, &lat); convertCoordinates (csys, equinox, lon, lat, EQUJ, 2000., &ra1, &dec1, 0.); pix2wcs(wcs, wcs->nxpix+0.5, -0.5, &lon, &lat); convertCoordinates (csys, equinox, lon, lat, EQUJ, 2000., &ra2, &dec2, 0.); pix2wcs(wcs, wcs->nxpix+0.5, wcs->nypix+0.5, &lon, &lat); convertCoordinates (csys, equinox, lon, lat, EQUJ, 2000., &ra3, &dec3, 0.); pix2wcs(wcs, -0.5, wcs->nypix+0.5, &lon, &lat); convertCoordinates (csys, equinox, lon, lat, EQUJ, 2000., &ra4, &dec4, 0.); } else { pix2wcs(wcs, -0.5, -0.5, &lon, &lat); convertCoordinates (csys, equinox, lon, lat, EQUJ, 2000., &ra2, &dec2, 0.); pix2wcs(wcs, wcs->nxpix+0.5, -0.5, &lon, &lat); convertCoordinates (csys, equinox, lon, lat, EQUJ, 2000., &ra1, &dec1, 0.); pix2wcs(wcs, wcs->nxpix+0.5, wcs->nypix+0.5, &lon, &lat); convertCoordinates (csys, equinox, lon, lat, EQUJ, 2000., &ra4, &dec4, 0.); pix2wcs(wcs, -0.5, wcs->nypix+0.5, &lon, &lat); convertCoordinates (csys, equinox, lon, lat, EQUJ, 2000., &ra3, &dec3, 0.); } // Treat the default region (ALL) as a circle // containing the whole image if(areaMode == ALL) { locinpix = 1; radinpix = 1; ra = wcs->nxpix / 2.; dec = wcs->nypix / 2.; radius = sqrt(wcs->nxpix*wcs->nxpix + wcs->nypix*wcs->nypix)/2.; } /* Get region statistics */ ixpix = 1; iypix = 1; if(radinpix) { rpix = radius; radius = rpix * fabs(cdelt2); } else rpix = radius / fabs(cdelt2); if(locinpix) { xpix = ra; ypix = dec; pix2wcs(wcs, xpix, ypix, &lon, &lat); if(wcs->offscl) { if(ap) free(ap); sprintf(returnStruct->msg, "Location off the image."); return returnStruct; } convertCoordinates (csys, equinox, lon, lat, EQUJ, 2000., &ra, &dec, 0); } else { convertCoordinates (EQUJ, 2000., ra, dec, csys, equinox, &lon, &lat, 0.); wcs2pix(wcs, lon, lat, &xpix, &ypix, &offscl); if(offscl) { if(ap) free(ap); sprintf(returnStruct->msg, "Location off the image."); return returnStruct; } } x0 = cos(ra*dtr) * cos(dec*dtr); y0 = sin(ra*dtr) * cos(dec*dtr); z0 = sin(dec*dtr); ixpix = (int)(xpix+0.5); iypix = (int)(ypix+0.5); if(debug) { printf("DEBUG> Region statististics for %-g pixels around (%-g,%-g) [%d,%d] [Equatorial (%-g, %-g)\n", rpix, xpix, ypix, ixpix, iypix, ra, dec); fflush(stdout); } // Then read the data jbegin = iypix - rpix - 1; jend = iypix + rpix + 1; ibegin = ixpix - rpix - 1; iend = ixpix + rpix + 1; if(ibegin < 1 ) ibegin = 1; if(iend > wcs->nxpix) iend = wcs->nxpix; nelements = iend - ibegin + 1; if(jbegin < 1 ) jbegin = 1; if(jend > wcs->nypix) jend = wcs->nxpix; fpixel[0] = ibegin; fpixel[1] = jbegin; fpixel[2] = 1; fpixel[3] = 1; fpixel[2] = plane3; fpixel[3] = plane4; data = (double *)malloc(nelements * sizeof(double)); status = 0; sumflux = 0.; sumflux2 = 0.; npix = 0; nnull = 0; val = 0.; valx = 0.; valy = 0.; valra = 0.; valdec = 0.; max = 0.; maxx = 0.; maxy = 0.; maxra = 0.; maxdec = 0.; min = 0.; minx = 0.; miny = 0.; minra = 0.; mindec = 0.; first = 1; if(radius > 0.) { if(debug) { printf("\nDEBUG> Location: (%.6f %.6f) -> (%d,%d)\n\n", xpix, ypix, ixpix, iypix); printf("DEBUG> Radius: %.6f\n\n", rpix); printf("DEBUG> i: %d to %d\n", ibegin, iend); printf("DEBUG> j: %d to %d\n", jbegin, jend); } for (j=jbegin; j<=jend; ++j) { status = 0; if(fits_read_pix(fptr, TDOUBLE, fpixel, nelements, &nan, (void *)data, &nullcnt, &status)) { ++fpixel[1]; continue; } for(i=0; i<nelements; ++i) { if(mNaN(data[i])) { if(debug) printf("%10s ", "NULL"); ++nnull; continue; } sumflux += data[i]; sumflux2 += data[i]*data[i]; ++npix; x = ibegin + i; y = j; pix2wcs(wcs, x, y, &lon, &lat); convertCoordinates(csys, equinox, lon, lat, EQUJ, 2000., &ra, &dec, 0.); xp = cos(ra*dtr) * cos(dec*dtr); yp = sin(ra*dtr) * cos(dec*dtr); zp = sin(dec*dtr); dot = xp*x0 + yp*y0 + zp*z0; if(dot > 1.) dot = 1.; r = acos(dot)/dtr / fabs(cdelt2); if(debug) { if(r <= rpix) printf("%10.3f ", data[i]); else printf("%10s ", "."); } if(r > rpix) continue; if(x == ixpix && y == iypix) { val = data[i]; valx = x; valy = y; } if(first) { first = 0; min = data[i]; minx = x; miny = y; max = data[i]; maxx = x; maxy = y; maxi = i+ibegin; maxj = j; } if(data[i] > max) { max = data[i]; maxx = x; maxy = y; maxi = i+ibegin; maxj = j; } if(data[i] < min) { min = data[i]; minx = x; miny = y; } } pix2wcs(wcs, valx, valy, &lon, &lat); convertCoordinates(csys, equinox, lon, lat, EQUJ, 2000., &valra, &valdec, 0.); pix2wcs(wcs, minx, miny, &lon, &lat); convertCoordinates(csys, equinox, lon, lat, EQUJ, 2000., &minra, &mindec, 0.); pix2wcs(wcs, maxx, maxy, &lon, &lat); convertCoordinates(csys, equinox, lon, lat, EQUJ, 2000., &maxra, &maxdec, 0.); if(debug) printf("\n"); ++fpixel[1]; } mean = 0.; rms = 0.; sigmaref = 0.; sigmamin = 0.; sigmamax = 0.; if(npix > 0) { mean = sumflux / npix; rms = sqrt(sumflux2/npix - mean*mean); sigmaref = (val - mean) / rms; sigmamin = (min - mean) / rms; sigmamax = (max - mean) / rms; } } /* Finally, print out parameters */ strcpy(montage_json, "{"); strcpy(montage_msgstr, ""); if(areaMode == ALL || areaMode == REGION) { sprintf(tmpstr, "\"proj\":\"%s\",", proj); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"csys\":\"%s\",", csys_str); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"equinox\":%.1f,", equinox); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"naxis\":%ld,", naxis); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"naxis1\":%d,", (int)naxis1); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"naxis2\":%d,", (int)naxis2); strcat(montage_json, tmpstr); if(naxis > 2) { sprintf(tmpstr, " \"naxis3\":%ld,", naxes[2]); strcat(montage_json, tmpstr); } if(naxis > 3) { sprintf(tmpstr, " \"naxis4\":%ld,", naxes[3]); strcat(montage_json, tmpstr); } sprintf(tmpstr, " \"crval1\":%.7f,", crval1); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"crval2\":%.7f,", crval2); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"crpix1\":%-g,", crpix1); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"crpix2\":%-g,", crpix2); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"cdelt1\":%.7f,", cdelt1); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"cdelt2\":%.7f,", cdelt2); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"crota2\":%.4f,", crota2); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"lonc\":%.7f,", lonc); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"latc\":%.7f,", latc); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"ximgsize\":%.6f,", fabs(naxis1*cdelt1)); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"yimgsize\":%.6f,", fabs(naxis1*cdelt2)); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"rotequ\":%.4f,", rot); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"rac\":%.7f,", rac); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"decc\":%.7f,", decc); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"ra1\":%.7f,", ra1); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"dec1\":%.7f,", dec1); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"ra2\":%.7f,", ra2); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"dec2\":%.7f,", dec2); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"ra3\":%.7f,", ra3); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"dec3\":%.7f,", dec3); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"ra4\":%.7f,", ra4); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"dec4\":%.7f,", dec4); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"radius\":%.7f,", radius); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"radpix\":%.2f,", rpix); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"npixel\":%d,", npix); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"nnull\":%d,", nnull); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"aveflux\":%-g,", mean); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"rmsflux\":%-g,", rms); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"fluxref\":%-g,", val); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"sigmaref\":%-g,", sigmaref); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"xref\":%.0f,", valx); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"yref\":%.0f,", valy); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"raref\":%.7f,", valra); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"decref\":%.7f,", valdec); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"fluxmin\":%-g,", min); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"sigmamin\":%-g,", sigmamin); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"xmin\":%.0f,", minx); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"ymin\":%.0f,", miny); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"ramin\":%.7f,", minra); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"decmin\":%.7f,", mindec); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"fluxmax\":%-g,", max); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"sigmamax\":%-g,", sigmamax); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"xmax\":%.0f,", maxx); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"ymax\":%.0f,", maxy); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"ramax\":%.7f,", maxra); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"decmax\":%.7f", maxdec); strcat(montage_json, tmpstr); sprintf(tmpstr, "proj=\"%s\",", proj); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " csys=\"%s\",", csys_str); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " equinox=%.1f,", equinox); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " naxis=%ld,", naxis); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " naxis1=%d,", (int)naxis1); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " naxis2=%d,", (int)naxis2); strcat(montage_msgstr, tmpstr); if(naxis > 2) { sprintf(tmpstr, " naxis3=%ld,", naxes[2]); strcat(montage_msgstr, tmpstr); } if(naxis > 3) { sprintf(tmpstr, " naxis4=%ld,", naxes[3]); strcat(montage_msgstr, tmpstr); } sprintf(tmpstr, " crval1=%.7f,", crval1); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " crval2=%.7f,", crval2); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " crpix1=%-g,", crpix1); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " crpix2=%-g,", crpix2); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " cdelt1=%.7f,", cdelt1); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " cdelt2=%.7f,", cdelt2); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " crota2=%.4f,", crota2); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " lonc=%.7f,", lonc); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " latc=%.7f,", latc); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " ximgsize=%.6f,", fabs(naxis1*cdelt1)); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " yimgsize=%.6f,", fabs(naxis1*cdelt2)); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " rotequ=%.4f,", rot); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " rac=%.7f,", rac); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " decc=%.7f,", decc); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " ra1=%.7f,", ra1); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " dec1=%.7f,", dec1); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " ra2=%.7f,", ra2); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " dec2=%.7f,", dec2); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " ra3=%.7f,", ra3); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " dec3=%.7f,", dec3); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " ra4=%.7f,", ra4); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " dec4=%.7f,", dec4); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " radius=%.7f,", radius); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " radpix=%.2f,", rpix); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " npixel=%d,", npix); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " nnull=%d,", nnull); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " aveflux=%-g,", mean); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " rmsflux=%-g,", rms); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " fluxref=%-g,", val); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " sigmaref=%-g,", sigmaref); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " xref=%.0f,", valx); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " yref=%.0f,", valy); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " raref=%.7f,", valra); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " decref=%.7f,", valdec); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " fluxmin=%-g,", min); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " sigmamin=%-g,", sigmamin); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " xmin=%.0f,", minx); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " ymin=%.0f,", miny); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " ramin=%.7f,", minra); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " decmin=%.7f,", mindec); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " fluxmax=%-g,", max); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " sigmamax=%-g,", sigmamax); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " xmax=%.0f,", maxx); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " ymax=%.0f,", maxy); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " ramax=%.7f,", maxra); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " decmax=%.7f", maxdec); strcat(montage_msgstr, tmpstr); } else if(areaMode == APPHOT) { // For aperture photometry mode we process the flux vs. radius data // to get the integrated source flux. rap = radius; jbegin = maxj - rap - 1; jend = maxj + rap + 1; ibegin = maxi - rap - 1; iend = maxi + rap + 1; nelements = iend - ibegin + 1; fpixel[0] = ibegin; fpixel[1] = jbegin; fpixel[2] = 1; fpixel[3] = 1; for (j=jbegin; j<=jend; ++j) { status = 0; if(fits_read_pix(fptr, TDOUBLE, fpixel, nelements, &nan, (void *)data, &nullcnt, &status)) { if(ap) free(ap); free(data); sprintf(returnStruct->msg, "Error reading FITS data."); return returnStruct; } for(i=0; i<nelements; ++i) { if(mNaN(data[i])) continue; r = sqrt(((double)i+ibegin-maxi)*((double)i+ibegin-maxi) + ((double)j-maxj)*((double)j-maxj)); if(r > rap) continue; ap[nflux].rad = r; ap[nflux].flux = data[i]; ap[nflux].fit = 0.; ++nflux; if(nflux >= maxflux) { maxflux += MAXFLUX; ap = (struct apPhoto *)realloc(ap, maxflux * sizeof(struct apPhoto)); } } ++fpixel[1]; } // Sort the data by radius qsort(ap, (size_t)nflux, sizeof(struct apPhoto), mExamine_radCompare); // Find the peak flux and the minimum flux. // The max is constrained to be in the first quarter of the // data, just to be careful. fluxMax = -1.e99; fluxMin = 1.e99; for(i=0; i<nflux; ++i) { if(i < nflux/4 && ap[i].flux > fluxMax) fluxMax = ap[i].flux; if(ap[i].flux < fluxMin) fluxMin = ap[i].flux; } // Fit the minimum a little more carefully, iterating // and excluding points more than 3 sigma above the // minimum. background = fluxMin; sigma = fluxMax - fluxMin; for(j=0; j<1000; ++j) { oldbackground = background; sumn = 0.; sumflux = 0.; sumflux2 = 0.; for(i=0; i<nflux; ++i) { if(fabs(ap[i].flux - background) > 3.*sigma) continue; sumn += 1.; sumflux += ap[i].flux; sumflux2 += ap[i].flux * ap[i].flux; } background = sumflux / sumn; sigma = sqrt(sumflux2/sumn - background*background); if(fabs((background - oldbackground) / oldbackground) < 1.e-3) break; } fluxRef = (fluxMax-fluxMin) / exp(1.); for(i=0; i<nflux; ++i) { if(ap[i].flux < fluxRef) { sigma = ap[i].rad; break; } } for(i=0; i<nflux; ++i) { ap[i].fit = (fluxMax-background) * exp(-(ap[i].rad * ap[i].rad) / (sigma * sigma)) + background; if(i == 0) ap[i].sum = ap[i].flux - background; else ap[i].sum = ap[i-1].sum + (ap[i].flux - background); } if(debug) { printf("| rad | flux | fit | sum |\n"); fflush(stdout); for(i=0; i<nflux; ++i) { printf("%12.6f %12.6f %12.6f %12.6f \n", ap[i].rad, ap[i].flux, ap[i].fit, ap[i].sum); fflush(stdout); } } sprintf(tmpstr, "\"proj\":\"%s\",", proj); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"csys\":\"%s\",", csys_str); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"equinox\":%.1f,", equinox); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"naxis\":%ld,", naxis); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"naxis1\":%d,", (int)naxis1); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"naxis2\":%d,", (int)naxis2); strcat(montage_json, tmpstr); if(naxis > 2) { sprintf(tmpstr, " \"naxis3\":%ld,", naxes[2]); strcat(montage_json, tmpstr); } if(naxis > 3) { sprintf(tmpstr, " \"naxis4\":%ld,", naxes[3]); strcat(montage_json, tmpstr); } sprintf(tmpstr, " \"crval1\":%.7f,", crval1); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"crval2\":%.7f,", crval2); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"crpix1\":%-g,", crpix1); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"crpix2\":%-g,", crpix2); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"cdelt1\":%.7f,", cdelt1); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"cdelt2\":%.7f,", cdelt2); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"crota2\":%.4f,", crota2); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"lonc\":%.7f,", lonc); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"latc\":%.7f,", latc); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"ximgsize\":%.6f,", fabs(naxis1*cdelt1)); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"yimgsize\":%.6f,", fabs(naxis1*cdelt2)); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"rotequ\":%.4f,", rot); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"rac\":%.7f,", rac); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"decc\":%.7f,", decc); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"ra1\":%.7f,", ra1); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"dec1\":%.7f,", dec1); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"ra2\":%.7f,", ra2); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"dec2\":%.7f,", dec2); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"ra3\":%.7f,", ra3); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"dec3\":%.7f,", dec3); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"ra4\":%.7f,", ra4); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"dec4\":%.7f,", dec4); strcat(montage_json, tmpstr); sprintf(tmpstr, " \"totalflux\":%.7e", ap[nflux/2].sum); strcat(montage_json, tmpstr); sprintf(tmpstr, "proj=\"%s\",", proj); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " csys=\"%s\",", csys_str); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " equinox=%.1f,", equinox); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " naxis=%ld,", naxis); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " naxis1=%d,", (int)naxis1); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " naxis2=%d,", (int)naxis2); strcat(montage_msgstr, tmpstr); if(naxis > 2) { sprintf(tmpstr, " naxis3=%ld,", naxes[2]); strcat(montage_msgstr, tmpstr); } if(naxis > 3) { sprintf(tmpstr, " naxis4=%ld,", naxes[3]); strcat(montage_msgstr, tmpstr); } sprintf(tmpstr, " crval1=%.7f,", crval1); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " crval2=%.7f,", crval2); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " crpix1=%-g,", crpix1); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " crpix2=%-g,", crpix2); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " cdelt1=%.7f,", cdelt1); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " cdelt2=%.7f,", cdelt2); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " crota2=%.4f,", crota2); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " lonc=%.7f,", lonc); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " latc=%.7f,", latc); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " ximgsize=%.6f,", fabs(naxis1*cdelt1)); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " yimgsize=%.6f,", fabs(naxis1*cdelt2)); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " rotequ=%.4f,", rot); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " rac=%.7f,", rac); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " decc=%.7f,", decc); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " ra1=%.7f,", ra1); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " dec1=%.7f,", dec1); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " ra2=%.7f,", ra2); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " dec2=%.7f,", dec2); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " ra3=%.7f,", ra3); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " dec3=%.7f,", dec3); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " ra4=%.7f,", ra4); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " dec4=%.7f,", dec4); strcat(montage_msgstr, tmpstr); sprintf(tmpstr, " totalflux=%.7e", ap[nflux/2].sum); strcat(montage_msgstr, tmpstr); } strcat(montage_json, "}"); if(ap) free(ap); free(data); returnStruct->status = 0; strcpy(returnStruct->msg, montage_msgstr); strcpy(returnStruct->json, montage_json); strcpy(returnStruct->proj, proj); strcpy(returnStruct->csys, csys_str); returnStruct->equinox = equinox; returnStruct->naxis = naxis; returnStruct->naxis1 = naxis1; returnStruct->naxis2 = naxis2; returnStruct->naxis3 = naxes[2]; returnStruct->naxis4 = naxes[3]; returnStruct->crval1 = crval1; returnStruct->crval2 = crval2; returnStruct->crpix1 = crpix1; returnStruct->crpix2 = crpix2; returnStruct->cdelt1 = cdelt1; returnStruct->cdelt2 = cdelt2; returnStruct->crota2 = crota2; returnStruct->lonc = lonc; returnStruct->latc = latc; returnStruct->ximgsize = fabs(naxis1*cdelt1); returnStruct->yimgsize = fabs(naxis2*cdelt2); returnStruct->rotequ = rot; returnStruct->rac = rac; returnStruct->decc = decc; returnStruct->ra1 = ra1; returnStruct->dec1 = dec1; returnStruct->ra2 = ra2; returnStruct->dec2 = dec2; returnStruct->ra3 = ra3; returnStruct->dec3 = dec3; returnStruct->ra4 = ra4; returnStruct->dec4 = dec4; returnStruct->radius = radius; returnStruct->radpix = rpix; returnStruct->npixel = npix; returnStruct->nnull = nnull; returnStruct->aveflux = mean; returnStruct->rmsflux = rms; returnStruct->fluxref = val; returnStruct->sigmaref = sigmaref; returnStruct->xref = valx; returnStruct->yref = valy; returnStruct->raref = valra; returnStruct->decref = valdec; returnStruct->fluxmin = min; returnStruct->sigmamin = sigmamin; returnStruct->xmin = minx; returnStruct->ymin = miny; returnStruct->ramin = minra; returnStruct->decmin = mindec; returnStruct->fluxmax = max; returnStruct->sigmamax = sigmamax; returnStruct->xmax = maxx; returnStruct->ymax = maxy; returnStruct->ramax = maxra; returnStruct->decmax = maxdec; if(areaMode == APPHOT) returnStruct->totalflux = ap[nflux/2].sum; else returnStruct->totalflux = 0.; return returnStruct; }
int main(int argc, char **argv) { int c; char header[32768]; char temp [MAXSTR]; char fmt [MAXSTR]; char rfmt [MAXSTR]; char pfmt [MAXSTR]; char cfmt [MAXSTR]; char ofile [MAXSTR]; char scale [MAXSTR]; int i, j; int namelen, nimages, ntotal, stat; double xpos, ypos; double lon, lat; double oxpix, oypix; int oxpixMin, oypixMin; int oxpixMax, oypixMax; int offscl, mode; int ncols; FILE *fraw; FILE *fproj; FILE *fcorr; /***************************************/ /* Process the command-line parameters */ /***************************************/ debug = 0; opterr = 0; fstatus = stdout; while ((c = getopt(argc, argv, "ds:")) != EOF) { switch (c) { case 'd': debug = 1; break; case 's': if((fstatus = fopen(optarg, "w+")) == (FILE *)NULL) { printf("[struct stat=\"ERROR\", msg=\"Cannot open status file: %s\"]\n", optarg); exit(1); } break; default: printf("[struct stat=\"ERROR\", msg=\"Usage: %s [-d][-s statusfile] images.tbl hdr.template raw.tbl projected.tbl corrected.tbl\"]\n", argv[0]); exit(1); break; } } if (argc - optind < 5) { printf("[struct stat=\"ERROR\", msg=\"Usage: %s [-d][-s statusfile] images.tbl hdr.template raw.tbl projected.tbl corrected.tbl\"]\n", argv[0]); exit(1); } strcpy(origimg_file, argv[optind]); strcpy(template_file, argv[optind + 1]); strcpy(rawimg_file, argv[optind + 2]); strcpy(projimg_file, argv[optind + 3]); strcpy(corrimg_file, argv[optind + 4]); checkHdr(template_file, 1, 0); if(debug) { printf("\norigimg_file = [%s]\n", origimg_file); printf("template_file = [%s]\n\n", template_file); printf("rawimg_file = [%s]\n", rawimg_file); printf("projimg_file = [%s]\n", projimg_file); printf("corrimg_file = [%s]\n", corrimg_file); fflush(stdout); } /*************************************************/ /* Process the output header template to get the */ /* image size, coordinate system and projection */ /*************************************************/ readTemplate(template_file); if(debug) { printf("\noutput.sys = %d\n", output.sys); printf("output.epoch = %-g\n", output.epoch); printf("output proj = %s\n", output.wcs->ptype); fflush(stdout); } /*********************************************/ /* Open the image header metadata table file */ /*********************************************/ ncols = topen(origimg_file); if(ncols <= 0) { fprintf(fstatus, "[struct stat=\"ERROR\", msg=\"Invalid image metadata file: %s\"]\n", origimg_file); exit(1); } icntr = tcol("cntr"); ictype1 = tcol("ctype1"); ictype2 = tcol("ctype2"); iequinox = tcol("equinox"); inl = tcol("nl"); ins = tcol("ns"); icrval1 = tcol("crval1"); icrval2 = tcol("crval2"); icrpix1 = tcol("crpix1"); icrpix2 = tcol("crpix2"); icdelt1 = tcol("cdelt1"); icdelt2 = tcol("cdelt2"); icrota2 = tcol("crota2"); iepoch = tcol("epoch"); ifname = tcol("fname"); iscale = tcol("scale"); icd11 = tcol("cd1_1"); icd12 = tcol("cd1_2"); icd21 = tcol("cd2_1"); icd22 = tcol("cd2_2"); if(ins < 0) ins = tcol("naxis1"); if(inl < 0) inl = tcol("naxis2"); if(ifname < 0) ifname = tcol("file"); if(icd11 >= 0 && icd12 >= 0 && icd21 >= 0 && icd12 >= 0) mode = CD; else if(icdelt1 >= 0 && icdelt2 >= 0 && icrota2 >= 0) mode = CDELT; else { fprintf(fstatus, "[struct stat=\"ERROR\", msg=\"Not enough information to determine coverages (CDELTs or CD matrix)\"]\n"); exit(1); } if(icntr < 0 || ictype1 < 0 || ictype2 < 0 || inl < 0 || ins < 0 || icrval1 < 0 || icrval2 < 0 || icrpix1 < 0 || icrpix2 < 0 || ifname < 0) { fprintf(fstatus, "[struct stat=\"ERROR\", msg=\"Need columns: cntr ctype1 ctype2 nl ns crval1 crval2 crpix1 crpix2 cdelt1 cdelt2 crota2 fname (equinox optional)\"]\n"); exit(1); } /******************************************************/ /* Scan the table to get the true 'file' column width */ /******************************************************/ namelen = 0; while(1) { stat = tread(); if(stat < 0) break; strcpy(input.fname, fileName(tval(ifname))); if(strlen(input.fname) > namelen) namelen = strlen(input.fname); } tseek(0); /*************************************/ /* Write headers to the output files */ /*************************************/ if((fraw = (FILE *)fopen(rawimg_file, "w+")) == (FILE *)NULL) { fprintf(fstatus, "[struct stat=\"ERROR\", msg=\"Invalid output metadata file: %s\"]\n", rawimg_file); exit(1); } fprintf(fraw, "\\datatype=fitshdr\n"); if(iscale >= 0) { sprintf(fmt, "|%%5s|%%8s|%%8s|%%6s|%%6s|%%10s|%%10s|%%10s|%%10s|%%11s|%%11s|%%8s|%%7s|%%10s|%%%ds|\n", namelen+2); fprintf(fraw, fmt, "cntr", "ctype1", "ctype2", "naxis1", "naxis2", "crval1", "crval2", "crpix1", "crpix2", "cdelt1", "cdelt2", "crota2", "equinox", "scale", "file"); fprintf(fraw, fmt, "int", "char", "char", "int", "int", "double", "double", "double", "double", "double", "double", "double", "int", "double", "char"); } else { sprintf(fmt, "|%%5s|%%8s|%%8s|%%6s|%%6s|%%10s|%%10s|%%10s|%%10s|%%11s|%%11s|%%8s|%%7s|%%%ds|\n", namelen+2); fprintf(fraw, fmt, "cntr", "ctype1", "ctype2", "naxis1", "naxis2", "crval1", "crval2", "crpix1", "crpix2", "cdelt1", "cdelt2", "crota2", "equinox", "file"); fprintf(fraw, fmt, "int", "char", "char", "int", "int", "double", "double", "double", "double", "double", "double", "double", "int", "char"); } if((fproj = (FILE *)fopen(projimg_file, "w+")) == (FILE *)NULL) { fprintf(fstatus, "[struct stat=\"ERROR\", msg=\"Invalid output metadata file: %s\"]\n", projimg_file); exit(1); } fprintf(fproj, "\\datatype=fitshdr\n"); sprintf(fmt, "|%%5s|%%8s|%%8s|%%6s|%%6s|%%10s|%%10s|%%10s|%%10s|%%11s|%%11s|%%8s|%%7s|%%%ds|\n", namelen+2); fprintf(fproj, fmt, "cntr", "ctype1", "ctype2", "naxis1", "naxis2", "crval1", "crval2", "crpix1", "crpix2", "cdelt1", "cdelt2", "crota2", "equinox", "file"); fprintf(fproj, fmt, "int", "char", "char", "int", "int", "double", "double", "double", "double", "double", "double", "double", "int", "char"); if((fcorr = (FILE *)fopen(corrimg_file, "w+")) == (FILE *)NULL) { fprintf(fstatus, "[struct stat=\"ERROR\", msg=\"Invalid output metadata file: %s\"]\n", corrimg_file); exit(1); } fprintf(fcorr, "\\datatype=fitshdr\n"); fprintf(fcorr, fmt, "cntr", "ctype1", "ctype2", "naxis1", "naxis2", "crval1", "crval2", "crpix1", "crpix2", "cdelt1", "cdelt2", "crota2", "equinox", "file"); fprintf(fcorr, fmt, "int", "char", "char", "int", "int", "double", "double", "double", "double", "double", "double", "double", "int", "char"); /************************************************/ /* Read the metadata and process each image WCS */ /************************************************/ namelen = 0; nimages = 0; ntotal = 0; if(iscale >= 0) sprintf(rfmt, " %%5d %%8s %%8s %%6d %%6d %%10.6f %%10.6f %%10.2f %%10.2f %%11.8f %%11.8f %%8.5f %%7.0f %%10s %%%ds\n", namelen+2); else sprintf(rfmt, " %%5d %%8s %%8s %%6d %%6d %%10.6f %%10.6f %%10.2f %%10.2f %%11.8f %%11.8f %%8.5f %%7.0f %%%ds\n", namelen+2); sprintf(pfmt, " %%5d %%8s %%8s %%6d %%6d %%10.6f %%10.6f %%10.2f %%10.2f %%11.8f %%11.8f %%8.5f %%7.0f p%%%ds\n", namelen+2); sprintf(cfmt, " %%5d %%8s %%8s %%6d %%6d %%10.6f %%10.6f %%10.2f %%10.2f %%11.8f %%11.8f %%8.5f %%7.0f c%%%ds\n", namelen+2); while(1) { stat = tread(); if(stat < 0) break; ++ntotal; strcpy(input.ctype1, tval(ictype1)); strcpy(input.ctype2, tval(ictype2)); input.cntr = atoi(tval(icntr)); input.naxis1 = atoi(tval(ins)); input.naxis2 = atoi(tval(inl)); input.crpix1 = atof(tval(icrpix1)); input.crpix2 = atof(tval(icrpix2)); input.crval1 = atof(tval(icrval1)); input.crval2 = atof(tval(icrval2)); if(mode == CDELT) { input.cdelt1 = atof(tval(icdelt1)); input.cdelt2 = atof(tval(icdelt2)); input.crota2 = atof(tval(icrota2)); } else { input.cd11 = atof(tval(icd11)); input.cd12 = atof(tval(icd12)); input.cd21 = atof(tval(icd21)); input.cd22 = atof(tval(icd22)); } input.epoch = 2000; strcpy(header, ""); sprintf(temp, "SIMPLE = T" ); stradd(header, temp); sprintf(temp, "BITPIX = -64" ); stradd(header, temp); sprintf(temp, "NAXIS = 2" ); stradd(header, temp); sprintf(temp, "NAXIS1 = %d", input.naxis1 ); stradd(header, temp); sprintf(temp, "NAXIS2 = %d", input.naxis2 ); stradd(header, temp); sprintf(temp, "CTYPE1 = '%s'", input.ctype1 ); stradd(header, temp); sprintf(temp, "CTYPE2 = '%s'", input.ctype2 ); stradd(header, temp); sprintf(temp, "CRVAL1 = %11.6f", input.crval1 ); stradd(header, temp); sprintf(temp, "CRVAL2 = %11.6f", input.crval2 ); stradd(header, temp); sprintf(temp, "CRPIX1 = %11.6f", input.crpix1 ); stradd(header, temp); sprintf(temp, "CRPIX2 = %11.6f", input.crpix2 ); stradd(header, temp); if(mode == CDELT) { sprintf(temp, "CDELT1 = %11.6f", input.cdelt1 ); stradd(header, temp); sprintf(temp, "CDELT2 = %11.6f", input.cdelt2 ); stradd(header, temp); sprintf(temp, "CROTA2 = %11.6f", input.crota2 ); stradd(header, temp); } else { sprintf(temp, "CD1_1 = %11.6f", input.cd11 ); stradd(header, temp); sprintf(temp, "CD1_2 = %11.6f", input.cd12 ); stradd(header, temp); sprintf(temp, "CD2_1 = %11.6f", input.cd21 ); stradd(header, temp); sprintf(temp, "CD2_2 = %11.6f", input.cd22 ); stradd(header, temp); } sprintf(temp, "EQUINOX = %d", input.equinox); stradd(header, temp); sprintf(temp, "END" ); stradd(header, temp); if(iequinox >= 0) input.equinox = atoi(tval(iequinox)); strcpy(input.fname, fileName(tval(ifname))); if(iscale >= 0) strcpy(scale, tval(iscale)); if(strlen(input.fname) > namelen) namelen = strlen(input.fname); if(debug) { printf("Image header to wcsinit():\n%s\n", header); fflush(stdout); } input.wcs = wcsinit(header); checkWCS(input.wcs, 0); if(input.wcs == (struct WorldCoor *)NULL) { fprintf(fstatus, "[struct stat=\"ERROR\", msg=\"Bad WCS for image %d\"]\n", nimages); exit(1); } /***************************************************/ /* Check the boundaries of the input image against */ /* the output region of interest */ /***************************************************/ oxpixMin = 100000000; oxpixMax = -100000000; oypixMin = 100000000; oypixMax = -100000000; /* Check input left and right */ for (j=0; j<input.naxis2+1; ++j) { pix2wcs(input.wcs, 0.5, j+0.5, &xpos, &ypos); convertCoordinates(input.sys, input.epoch, xpos, ypos, output.sys, output.epoch, &lon, &lat, 0.0); wcs2pix(output.wcs, lon, lat, &oxpix, &oypix, &offscl); if(!offscl) { if(oxpix < oxpixMin) oxpixMin = oxpix; if(oxpix > oxpixMax) oxpixMax = oxpix; if(oypix < oypixMin) oypixMin = oypix; if(oypix > oypixMax) oypixMax = oypix; } pix2wcs(input.wcs, input.naxis1+0.5, j+0.5, &xpos, &ypos); convertCoordinates(input.sys, input.epoch, xpos, ypos, output.sys, output.epoch, &lon, &lat, 0.0); wcs2pix(output.wcs, lon, lat, &oxpix, &oypix, &offscl); if(!offscl) { if(oxpix < oxpixMin) oxpixMin = oxpix; if(oxpix > oxpixMax) oxpixMax = oxpix; if(oypix < oypixMin) oypixMin = oypix; if(oypix > oypixMax) oypixMax = oypix; } } /* Check input top and bottom */ for (i=0; i<input.naxis1+1; ++i) { pix2wcs(input.wcs, i+0.5, 0.5, &xpos, &ypos); convertCoordinates(input.sys, input.epoch, xpos, ypos, output.sys, output.epoch, &lon, &lat, 0.0); wcs2pix(output.wcs, lon, lat, &oxpix, &oypix, &offscl); if(!offscl) { if(oxpix < oxpixMin) oxpixMin = oxpix; if(oxpix > oxpixMax) oxpixMax = oxpix; if(oypix < oypixMin) oypixMin = oypix; if(oypix > oypixMax) oypixMax = oypix; } pix2wcs(input.wcs, i+0.5, input.naxis2+0.5, &xpos, &ypos); convertCoordinates(input.sys, input.epoch, xpos, ypos, output.sys, output.epoch, &lon, &lat, 0.0); wcs2pix(output.wcs, lon, lat, &oxpix, &oypix, &offscl); if(!offscl) { if(oxpix < oxpixMin) oxpixMin = oxpix; if(oxpix > oxpixMax) oxpixMax = oxpix; if(oypix < oypixMin) oypixMin = oypix; if(oypix > oypixMax) oypixMax = oypix; } } /***************************************************/ /* Check the boundaries of the region of interest */ /* against the input image */ /***************************************************/ /* Check ouput left and right */ for (j=0; j<output.wcs->nypix+1; ++j) { pix2wcs(output.wcs, 0.5, j+0.5, &xpos, &ypos); convertCoordinates(output.sys, output.epoch, xpos, ypos, input.sys, input.epoch, &lon, &lat, 0.0); wcs2pix(input.wcs, lon, lat, &oxpix, &oypix, &offscl); if(!offscl) { if(0.5 < oxpixMin) oxpixMin = 0.5; if(0.5 > oxpixMax) oxpixMax = 0.5; if(j+0.5 < oypixMin) oypixMin = j+0.5; if(j+0.5 > oypixMax) oypixMax = j+0.5; } pix2wcs(output.wcs, output.wcs->nxpix+0.5, j+0.5, &xpos, &ypos); convertCoordinates(output.sys, output.epoch, xpos, ypos, input.sys, input.epoch, &lon, &lat, 0.0); wcs2pix(input.wcs, lon, lat, &oxpix, &oypix, &offscl); if(!offscl) { if(output.wcs->nxpix+0.5 < oxpixMin) oxpixMin = output.wcs->nxpix+0.5; if(output.wcs->nxpix+0.5 > oxpixMax) oxpixMax = output.wcs->nxpix+0.5; if(j+0.5 < oypixMin) oypixMin = j+0.5; if(j+0.5 > oypixMax) oypixMax = j+0.5; } } /* Check input top and bottom */ for (i=0; i<output.wcs->nxpix+1; ++i) { pix2wcs(output.wcs, i+0.5, 0.5, &xpos, &ypos); convertCoordinates(output.sys, output.epoch, xpos, ypos, input.sys, input.epoch, &lon, &lat, 0.0); wcs2pix(input.wcs, lon, lat, &oxpix, &oypix, &offscl); if(!offscl) { if(i+0.5 < oxpixMin) oxpixMin = i+0.5; if(i+0.5 > oxpixMax) oxpixMax = i+0.5; if(0.5 < oypixMin) oypixMin = 0.5 ; if(0.5 > oypixMax) oypixMax = 0.5 ; } pix2wcs(output.wcs, i+0.5, output.wcs->nypix+0.5, &xpos, &ypos); convertCoordinates(output.sys, output.epoch, xpos, ypos, input.sys, input.epoch, &lon, &lat, 0.0); wcs2pix(input.wcs, lon, lat, &oxpix, &oypix, &offscl); if(!offscl) { if(i+0.5 < oxpixMin) oxpixMin = i+0.5; if(i+0.5 > oxpixMax) oxpixMax = i+0.5; if(output.wcs->nypix+0.5 < oypixMin) oypixMin = output.wcs->nypix+0.5; if(output.wcs->nypix+0.5 > oypixMax) oypixMax = output.wcs->nypix+0.5; } } if(oxpixMax < oxpixMin) continue; if(oypixMax < oypixMin) continue; /* Remove any possible compression extension */ strcpy(ofile, input.fname); if(strlen(ofile) > 3 && strcmp(ofile+strlen(ofile)-3, ".gz") == 0) ofile[strlen(ofile)-3] = '\0'; else if(strlen(ofile) > 2 && strcmp(ofile+strlen(ofile)-2, ".Z") == 0) ofile[strlen(ofile)-2] = '\0'; else if(strlen(ofile) > 2 && strcmp(ofile+strlen(ofile)-2, ".z") == 0) ofile[strlen(ofile)-2] = '\0'; else if(strlen(ofile) > 4 && strcmp(ofile+strlen(ofile)-4, ".zip") == 0) ofile[strlen(ofile)-4] = '\0'; else if(strlen(ofile) > 2 && strcmp(ofile+strlen(ofile)-2, "-z") == 0) ofile[strlen(ofile)-2] = '\0'; else if(strlen(ofile) > 3 && strcmp(ofile+strlen(ofile)-3, "-gz") == 0) ofile[strlen(ofile)-3] = '\0'; /* Make sure the extension is ".fits" */ if(strlen(ofile) > 5 && strcmp(ofile+strlen(ofile)-5, ".fits") == 0) ofile[strlen(ofile)-5] = '\0'; else if(strlen(ofile) > 5 && strcmp(ofile+strlen(ofile)-5, ".FITS") == 0) ofile[strlen(ofile)-5] = '\0'; else if(strlen(ofile) > 4 && strcmp(ofile+strlen(ofile)-4, ".fit") == 0) ofile[strlen(ofile)-4] = '\0'; else if(strlen(ofile) > 4 && strcmp(ofile+strlen(ofile)-4, ".FIT") == 0) ofile[strlen(ofile)-4] = '\0'; else if(strlen(ofile) > 4 && strcmp(ofile+strlen(ofile)-4, ".fts") == 0) ofile[strlen(ofile)-4] = '\0'; else if(strlen(ofile) > 4 && strcmp(ofile+strlen(ofile)-4, ".FTS") == 0) ofile[strlen(ofile)-4] = '\0'; strcat(ofile, ".fits"); if(iscale >= 0) { fprintf(fraw, rfmt, nimages+1, output.wcs->ctype[0], output.wcs->ctype[1], oxpixMax - oxpixMin + 1, oypixMax - oypixMin + 1, output.wcs->crval[0], output.wcs->crval[1], output.wcs->crpix[0] - oxpixMin, output.wcs->crpix[1] - oypixMin, output.wcs->cdelt[0], output.wcs->cdelt[1], output.wcs->rot, output.epoch, scale, ofile); } else { fprintf(fraw, rfmt, nimages+1, output.wcs->ctype[0], output.wcs->ctype[1], oxpixMax - oxpixMin + 1, oypixMax - oypixMin + 1, output.wcs->crval[0], output.wcs->crval[1], output.wcs->crpix[0] - oxpixMin, output.wcs->crpix[1] - oypixMin, output.wcs->cdelt[0], output.wcs->cdelt[1], output.wcs->rot, output.epoch, ofile); } fprintf(fproj, pfmt, nimages+1, output.wcs->ctype[0], output.wcs->ctype[1], oxpixMax - oxpixMin + 1, oypixMax - oypixMin + 1, output.wcs->crval[0], output.wcs->crval[1], output.wcs->crpix[0] - oxpixMin, output.wcs->crpix[1] - oypixMin, output.wcs->cdelt[0], output.wcs->cdelt[1], output.wcs->rot, output.epoch, ofile); fprintf(fcorr, cfmt, nimages+1, output.wcs->ctype[0], output.wcs->ctype[1], oxpixMax - oxpixMin + 1, oypixMax - oypixMin + 1, output.wcs->crval[0], output.wcs->crval[1], output.wcs->crpix[0] - oxpixMin, output.wcs->crpix[1] - oypixMin, output.wcs->cdelt[0], output.wcs->cdelt[1], output.wcs->rot, output.epoch, ofile); ++nimages; } fclose(fraw); fclose(fproj); fclose(fcorr); fprintf(fstatus, "[struct stat=\"OK\", count=\"%d\", total=\"%d\"]\n", nimages, ntotal); fflush(stdout); exit(0); }
int main(int argc, char **argv) { int c; int args; int offscl; int dim1, dim2; int ix, iy; int offset; int debug=0; int dowcs=1; int idx=0; char tbuf[SZ_LINE]; double dval1, dval2; double dx, dy; double *dbuf; struct WorldCoor *wcs; Fun fun; /* process switch arguments */ while( (c = getopt(argc, argv, "d")) != -1){ switch(c){ case 'd': debug = 1; break; case 'i': dowcs = 0; break; } } /* check for required arguments */ args = argc - optind; if( args < 1 ){ fprintf(stderr, "usage: %s iname -d -i\n", argv[0]); fprintf(stderr, "\n"); fprintf(stderr, "where:\n"); fprintf(stderr, " -d\tprint out input and output position values\n"); fprintf(stderr, " -i\tinput values are image x,y (not ra,dec in deg)\n"); fprintf(stderr, "\n"); exit(1); } /* exit on gio errors */ setgerror(2); /* open the input FITS file */ if( !(fun = FunOpen(argv[optind], "r", NULL)) ) gerror(stderr, "could not FunOpen input file: %s\n", argv[optind]); /* extract and bin the data section into a double float image buffer */ if( !(dbuf = FunImageGet(fun, NULL, "bitpix=-64")) ) gerror(stderr, "could not FunImageGet: %s\n", argv[1]); /* get required information from funtools structure */ FunInfoGet(fun, FUN_SECT_DIM1, &dim1, FUN_SECT_DIM2, &dim2, FUN_WCS, &wcs, 0); /* for each line in the contour file ... */ while( fgets(tbuf, SZ_LINE, stdin) ){ /* ignore comments */ if( *tbuf == '#' ) continue; /* blank lines means reset counter */ if( *tbuf == '\n' ){ fprintf(stdout, "\n"); idx = 0; continue; } /* input contour values: ra, dec in degrees (or image coords if -i) */ if(sscanf(tbuf, "%lf %lf", &dval1, &dval2) != 2){ gerror(stderr, "invalid line in contour file: %s\n", tbuf); } /* convert input ra, dec to image x, y */ if( dowcs ){ wcs2pix(wcs, dval1, dval2, &dx, &dy, &offscl); /* make sure we are not off scale */ if( offscl ){ fprintf(stderr, "warning: wcs position is offscale: %s\n", tbuf); continue; } } else{ dx = dval1; dy = dval2; } /* convert image values to integers */ ix = (int)(dx+0.5); iy = (int)(dy+0.5); /* sanity checks -- must be inside the image */ if( (ix < 1) || (ix > dim1) || (iy < 1) || (iy > dim2)){ fprintf(stderr, "warning: image position off image: %s\n", tbuf); continue; } /* calculate offset into dbuf */ offset = (iy-1)*dim1 + ix-1; /* write out the index and the pixel value at the image position */ fprintf(stdout, "%d\t%f", idx, dbuf[offset]); /* debugging info, if necessary */ if( debug ){ fprintf(stdout, "\t %f %f\t%d %d", dval1, dval2, ix, iy); } /* finish off line */ fprintf(stdout, "\n"); /* bump to next index */ idx++; } /* close output first so that flush happens automatically */ FunClose(fun); if( dbuf ) free(dbuf); return(0); }
int read_file(char *file, struct FitsFile *input, struct CatalogRow *cat) { char s[MAXGETS]; FILE *fp; int i, count, len, line, nobj, status; int apos[1000]; int alen[1000]; char buf[100]; int offscl; double x, y; struct CatalogRow c; if ((fp = fopen(file, "r")) == NULL) { fprintf(stderr,"file open error : %s\n",file); exit(EXIT_FAILURE); } line = nobj = 0; while (fgets(s, MAXGETS, fp) != NULL) { status = SPACE; count = 0; //puts(s); for (i=0; i<MAXGETS; i++) { //putc(s[i],stdout); //printf("i=%d s[i]='%c' count=%d\n",i,s[i],count); if (s[i]=='\0' || s[i]=='#') { if (status==WORD) { alen[count] = len; count++; } break; } switch (s[i]) { case ' ': case '\t': if (status==WORD) { alen[count] = len; //strncpy(buf,&s[apos[count]],alen[count]); //buf[alen[count]] = '\0'; //printf("count=%i buf=%s\n",count,buf); count++; len = 0; status = SPACE; } break; default: if (status==SPACE) { apos[count] = i; status = WORD; } len++; } } if (count >= NCOLS) { c.line = line; strncpy(buf,&s[apos[COL_LON]],alen[COL_LON]); c.lon = atof(buf); strncpy(buf,&s[apos[COL_LAT]],alen[COL_LAT]); c.lat = atof(buf); strncpy(buf,&s[apos[COL_ERR]],alen[COL_ERR]); c.err = atof(buf); wcs2pix(input->wcs, c.lon, c.lat, &x, &y, &offscl); //printf("lon=%f lat=%f x=%f y=%f offscl=%d\n",c.lon,c.lat,x,y,offscl); /* 0 if within bounds, else off scale */ if (offscl==0) { //printf("line=%d lon=%f lat=%f x=%f y=%f offscl=%d\n",c.line,c.lon,c.lat,x,y,offscl); cat[nobj++] = c; } } //if (line>2) exit(0); line++; } fclose(fp); return nobj; }