int fits_print_keyvalue(fitsfile* fptr, char* keyname, int* status) { /* Standard string lengths defined in fitsio.h */ static char keyval[FLEN_VALUE]; int oldstatus = *status; /* Read and print requested keyword value */ fits_read_key_str(fptr, keyname, keyval, 0, status); if (*status != KEY_NO_EXIST) { printf("%s", keyval); } else { *status = oldstatus; printf("absent"); } return *status; }
int create_input() { const double TWOPI = 2.0 * 3.14159265358979323846; /* These must match wcstab.keyrec. */ const char infile[] = "test/wcstab.keyrec"; const long NAXIS1 = 256; const long NAXIS2 = 128; const long NAXIS3 = 4; const char *ttype1[3] = {"CelCoords", "RAIndex", "DecIndex"}; const char *tform1[3] = {"5600E", "70E", "40E"}; const char *tunit1[3] = {"deg", "", ""}; const char *ttype2[4] = {"WaveIndex", "WaveCoord", "TimeIndex", "TimeCoord"}; const char *tform2[4] = {"8E", "8D", "8E", "8D"}; const char *tunit2[4] = {"", "m", "", "a"}; /* The remaining parameters may be chosen at will. */ float refval[] = {150.0f, -30.0f}; float span[] = {5.0f, (5.0f*K2)/K1}; float sigma[] = {0.10f, 0.05f}; double wcoord[] = {0.21106114, 0.21076437, 2.0e-6, 2.2e-6, 500.0e-9, 650.0e-9, 1.24e-9, 2.48e-9}; double windex[] = {0.5, 1.5, 1.5, 2.5, 2.5, 3.5, 3.5, 4.5}; double tcoord[] = {1997.84512, 1997.84631, 1993.28451, 1993.28456, 2001.59234, 2001.59239, 2002.18265, 2002.18301}; double tindex[] = {0.0, 1.0, 1.0, 2.0, 2.0, 3.0, 3.0, 4.0}; char keyrec[84]; int i, status; long dummy, firstelem, k1, k2, p1, p2, p3; float array[2*K1*K2], *fp, image[256]; double s, t, x1, x2, z, z1, z2; FILE *stream; fitsfile *fptr; /* Look for the input header keyrecords. */ if ((stream = fopen(infile+5, "r")) == 0x0) { if ((stream = fopen(infile, "r")) == 0x0) { printf("ERROR opening %s\n", infile); return 1; } } /* Create the FITS output file, deleting any pre-existing file. */ status = 0; fits_create_file(&fptr, "!wcstab.fits", &status); /* Convert header keyrecords to FITS. */ while (fgets(keyrec, 82, stream) != NULL) { /* Ignore meta-comments (copyright information, etc.). */ if (keyrec[0] == '#') continue; /* Strip off the newline. */ i = strlen(keyrec) - 1; if (keyrec[i] == '\n') keyrec[i] = '\0'; fits_write_record(fptr, keyrec, &status); } fclose(stream); /* Create and write some phoney image data. */ firstelem = 1; for (p3 = 0; p3 < NAXIS3; p3++) { for (p2 = 0; p2 < NAXIS2; p2++) { fp = image; s = (p3 + 1) * cos(0.2 * p2); t = cos(0.8*p2); for (p1 = 0; p1 < NAXIS1; p1++) { /* Do not adjust your set! */ *(fp++) = sin(0.1*(p1+p2) + s) * cos(0.4*p1) * t; } fits_write_img_flt(fptr, 0L, firstelem, NAXIS1, image, &status); firstelem += NAXIS1; } } /* Add the first binary table extension. */ fits_create_tbl(fptr, BINARY_TBL, 1L, 3L, (char **)ttype1, (char **)tform1, (char **)tunit1, NULL, &status); /* Write EXTNAME and EXTVER near the top, after TFIELDS. */ fits_read_key_lng(fptr, "TFIELDS", &dummy, NULL, &status); fits_insert_key_str(fptr, "EXTNAME", "WCS-TABLE", "WCS Coordinate lookup table", &status); fits_insert_key_lng(fptr, "EXTVER", 1L, "Table number 1", &status); /* Write the TDIM1 keyrecord after TFORM1. */ fits_read_key_str(fptr, "TFORM1", keyrec, NULL, &status); sprintf(keyrec, "(2,%ld,%ld)", K1, K2); fits_insert_key_str(fptr, "TDIM1", keyrec, "Dimensions of 3-D array", &status); /* Plate carrée projection with a bit of noise for the sake of realism. */ fp = array; for (k2 = 0; k2 < K2; k2++) { for (k1 = 0; k1 < K1; k1++) { /* Box-Muller transformation: uniform -> normal distribution. */ x1 = lcprng(); x2 = lcprng(); if (x1 == 0.0) x1 = 1.0; z = sqrt(-2.0 * log(x1)); x2 *= TWOPI; z1 = z * cos(x2); z2 = z * sin(x2); *(fp++) = refval[0] + span[0] * (k1/(K1-1.0) - 0.5) + z1 * sigma[0]; *(fp++) = refval[1] + span[1] * (k2/(K2-1.0) - 0.5) + z2 * sigma[1]; } } fits_write_col_flt(fptr, 1, 1L, 1L, 2*K1*K2, array, &status); fp = array; for (k1 = 0; k1 < K1; k1++) { *(fp++) = 4.0f * k1; } fits_write_col_flt(fptr, 2, 1L, 1L, K1, array, &status); fp = array; for (k2 = 0; k2 < K2; k2++) { *(fp++) = 4.0f * k2; } fits_write_col_flt(fptr, 3, 1L, 1L, K2, array, &status); /* Add the second binary table extension. */ if (fits_create_tbl(fptr, BINARY_TBL, 1L, 4L, (char **)ttype2, (char **)tform2, (char **)tunit2, NULL, &status)) { fits_report_error(stderr, status); return 1; } /* Write EXTNAME and EXTVER near the top, after TFIELDS. */ fits_read_key_lng(fptr, "TFIELDS", &dummy, NULL, &status); fits_insert_key_str(fptr, "EXTNAME", "WCS-TABLE", "WCS Coordinate lookup table", &status); fits_insert_key_lng(fptr, "EXTVER", 2L, "Table number 2", &status); /* Write the TDIM2 keyrecord after TFORM2. */ fits_read_key_str(fptr, "TFORM2", keyrec, NULL, &status); fits_insert_key_str(fptr, "TDIM2", "(1,8)", "Dimensions of 2-D array", &status); /* Write the TDIM4 keyrecord after TFORM4. */ fits_read_key_str(fptr, "TFORM4", keyrec, NULL, &status); fits_insert_key_str(fptr, "TDIM4", "(1,8)", "Dimensions of 2-D array", &status); fits_write_col_dbl(fptr, 1, 1L, 1L, 8L, windex, &status); fits_write_col_dbl(fptr, 2, 1L, 1L, 8L, wcoord, &status); fits_write_col_dbl(fptr, 3, 1L, 1L, 8L, tindex, &status); fits_write_col_dbl(fptr, 4, 1L, 1L, 8L, tcoord, &status); fits_close_file(fptr, &status); if (status) { fits_report_error(stderr, status); return 1; } return 0; }
int HEALPixIn(struct healpix *hpxdat) { char crdsys[32], ordering[32]; int anynul, hdutype, iaxis, nfound, status; long firstpix, ipix, lastpix, naxis, *naxes = 0x0, nside = 0, repeat; float *datap, nulval; LONGLONG firstelem, irow, nelem, npix = 0, nrow = 0; fitsfile *fptr; status = 0; hpxdat->data = 0x0; /* Open the FITS file and move to the first HDU with NAXIS != 0. */ if (fits_open_data(&fptr, hpxdat->infile, READONLY, &status)) goto fitserr; /* Is this the primary HDU or an extension? */ if (fits_get_hdu_type(fptr, &hdutype, &status)) goto fitserr; if (!(hdutype == IMAGE_HDU || hdutype == BINARY_TBL)) { fprintf(stderr, "ERROR: %s does not contain HEALPix data.\n", hpxdat->infile); return 1; } /* Get the image size. */ if (fits_read_key_lng(fptr, "NAXIS", &naxis, 0x0, &status)) goto fitserr; naxes = malloc(naxis*sizeof(long)); if (fits_read_keys_lng(fptr, "NAXIS", 1, (int)naxis, naxes, &nfound, &status)) goto fitserr; if (hdutype == IMAGE_HDU) { /* Look for the first non-degenerate image axis. */ for (iaxis = 0; iaxis < nfound; iaxis++) { if (naxes[iaxis] > 1) { /* Assume for now that it is the total number of pixels. */ npix = naxes[iaxis]; break; } } } else if (hdutype == BINARY_TBL) { /* Binary tables are simpler. */ if (nfound > 1) nrow = naxes[1]; /* (Note that fits_get_coltypell() is not available in cfitsio 2.x.) */ if (fits_get_coltype(fptr, hpxdat->col, 0x0, &repeat, 0x0, &status)) { goto fitserr; } nelem = (LONGLONG)repeat; } if (!npix && !nrow) { fprintf(stderr, "ERROR: Could not determine image size.\n"); goto cleanup; } /* Number of pixels per side of each base-resolution pixel. */ if (fits_read_key_lng(fptr, "NSIDE", &nside, 0x0, &status)) { /* Some HEALPix files, e.g. SFD dust maps, don't record NSIDE. */ if (status != KEY_NO_EXIST) goto fitserr; status = 0; } /* FIRSTPIX and LASTPIX, if present, record the 0-relative pixel numbers of * the first and last pixels stored in the data. */ firstpix = -1; if (fits_read_key_lng(fptr, "FIRSTPIX", &firstpix, 0x0, &status)) { if (status != KEY_NO_EXIST) goto fitserr; status = 0; } lastpix = -1; if (fits_read_key_lng(fptr, "LASTPIX", &lastpix, 0x0, &status)) { if (status != KEY_NO_EXIST) goto fitserr; status = 0; } if (!nside) { /* Deduce NSIDE. */ if (lastpix >= 0) { /* If LASTPIX is present without NSIDE we can only assume it's npix. */ nside = (int)(sqrt((double)((lastpix+1) / 12)) + 0.5); } else if (npix) { nside = (int)(sqrt((double)(npix / 12)) + 0.5); } else if (nrow) { nside = (int)(sqrt((double)((nrow * nelem) / 12)) + 0.5); } } hpxdat->nside = (int)nside; hpxdat->npix = 12*nside*nside; /* Ensure that FIRSTPIX and LASTPIX are set. */ if (firstpix < 0) firstpix = 0; if (lastpix < 0) lastpix = hpxdat->npix - 1; /* Any sign of a coordinate system identifier? */ if (fits_read_key_str(fptr, "COORDSYS", crdsys, 0x0, &status)) { if (status != KEY_NO_EXIST) goto fitserr; status = 0; } else if (crdsys[0] == 'G') { hpxdat->crdsys = 'G'; } else if (crdsys[0] == 'E') { hpxdat->crdsys = 'E'; } else if (crdsys[0] == 'C') { /* ("celestial") */ hpxdat->crdsys = 'Q'; } /* Nested or ring ordering? */ if (fits_read_key_str(fptr, "ORDERING", ordering, 0x0, &status)) { /* Some HEALPix files, e.g. SFD dust maps, don't record ORDERING. */ if (status != KEY_NO_EXIST) goto fitserr; status = 0; } else if (strcmp(ordering, "NESTED") == 0) { hpxdat->ordering = 'N'; } else if (strcmp(ordering, "RING") == 0) { hpxdat->ordering = 'R'; } else { fprintf(stderr, "WARNING: Invalid ORDERING keyword: %s.\n", ordering); } /* Allocate memory and read the data. */ if ((hpxdat->data = malloc((hpxdat->npix)*sizeof(float))) == NULL) { perror("HPXcvt"); goto cleanup; } nulval = HEALPIX_NULLVAL; datap = hpxdat->data; for (ipix = 0; ipix < firstpix; ipix++) { *(datap++) = nulval; } firstelem = (LONGLONG)1; if (hdutype == IMAGE_HDU) { if (fits_read_img_flt(fptr, 0l, firstelem, npix, nulval, datap, &anynul, &status)) goto fitserr; } else if (hdutype == BINARY_TBL) { for (irow = 0; irow < nrow; irow++) { if (fits_read_col_flt(fptr, hpxdat->col, irow+1, firstelem, nelem, nulval, datap, &anynul, &status)) goto fitserr; datap += nelem; } } datap = hpxdat->data + (lastpix + 1); for (ipix = (lastpix+1); ipix < hpxdat->npix; ipix++) { *(datap++) = nulval; } /* Clean up. */ fits_close_file(fptr, &status); status = 0; return 0; fitserr: fits_report_error(stderr, status); cleanup: if (naxes) free(naxes); if (hpxdat->data) free(hpxdat->data); hpxdat->data = 0x0; return 1; }
void process_file (char *filename) { fitsfile *fptr; int status, nfound, anynull; double median; long naxes[4], fpixel, nbuffer, npixels, npix, ii; char camera_name[80]; #define BUFFSIZE 1000 float nullval, buffer[BUFFSIZE]; #define printerror(sta) fits_report_error (stderr, status) status = 0; if (fits_open_file (&fptr, filename, READONLY, &status)) { printerror (status); return; } if (fits_read_keys_lng (fptr, (char *) "NAXIS", 1, 2, naxes, &nfound, &status)) goto err; if (nfound < 1) { if (verbose) printf ("No image found in %s\n", filename); fits_close_file (fptr, &status); check_unlink (filename); return; } nfound--; npixels = naxes[nfound]; for (; nfound > 0; nfound--) npixels *= naxes[nfound]; npix = npixels; if (fits_read_key_str (fptr, (char *) "CAM_NAME", camera_name, NULL, &status)) { if (verbose) printf ("No CAM_NAME in %s, default will be used\n", filename); if (isnan (min)) { if (config->getDouble ("flatprocess", "min", min)) min = -INFINITY; } if (isnan (max)) { if (config->getDouble ("flatprocess", "max", max)) max = INFINITY; } status = 0; } else { if (isnan (min)) { if (config->getDouble (camera_name, "flatmin", min)) min = -INFINITY; } if (isnan (max)) { if (config->getDouble (camera_name, "flatmax", max)) max = INFINITY; } } if (verbose > 1) printf ("%s: min %f, max %f, npix: %li\n", filename, min, max, npixels); fpixel = 1; median = 0; while (npixels > 0) { if (npixels > BUFFSIZE) nbuffer = BUFFSIZE; else nbuffer = npixels; if (fits_read_img (fptr, TFLOAT, fpixel, nbuffer, &nullval, buffer, &anynull, &status)) goto err; for (ii = 0; ii < nbuffer; ii++) median += buffer[ii]; npixels -= nbuffer; fpixel += nbuffer; } median = median / npix; if (verbose) printf ("%s %f\n", filename, median); if (fits_close_file (fptr, &status)) printerror (status); if (median < min || median > max) check_unlink (filename); return; err: printerror (status); if (fits_close_file (fptr, &status)) printerror (status); return; }