Exemple #1
0
int fits_update_keycard(fitsfile* fptr, char* keystring, int* status) {
  static char keyname[FLEN_KEYWORD];

  /* update directly the full card */
  int l;
  fits_get_keyname(keystring, keyname, &l, status);
  fits_update_card(fptr, keyname, keystring, status);
  fits_print_error(*status);
  
  return *status;
}
Exemple #2
0
int fits_update_keycomment(fitsfile* fptr, char* keystring, int* status) {
  static char keyname[FLEN_KEYWORD];
  static char keycomment[FLEN_COMMENT];

  /* decode current name and new name, then modify it */
  if (!stringsep2(keystring, keyname, keycomment, '=')) return *status;
  fits_modify_comment(fptr, keyname, keycomment, status);
  fits_print_error(*status);
  
  return *status;
}
Exemple #3
0
int fits_update_keyname(fitsfile* fptr, char* keystring, int* status) {
  static char oldname[FLEN_KEYWORD];
  static char newname[FLEN_KEYWORD];

  /* decode current name and new name, then modify it */
  if (!stringsep2(keystring, oldname, newname, '=')) return *status;
  fits_modify_name(fptr, oldname, newname, status);
  fits_print_error(*status);
  
  return *status;
}
Exemple #4
0
int fits_close(fitsfile *fptr, int *status) {

  int iomode;
  fits_file_mode(fptr, &iomode, status);
  if (iomode == READWRITE) {
    fits_write_date(fptr, status);
  }

  fits_close_file(fptr, status);
  fits_print_error(*status);

  return *status;
}
Exemple #5
0
int fits_update_keyvalue(fitsfile* fptr, char* keystring, int* status) {
  static char keyname[FLEN_KEYWORD];
  static char keyval[FLEN_VALUE];

  /* decode the string, recognize the type, and update the key */
  if (!stringsep2(keystring, keyname, keyval, '=')) return *status;

  int datatype = fits_detect_key_type(keyval);
  fits_update_key_from_str(fptr, datatype, keyname, keyval, 0, status);
  fits_print_error(*status);

  return *status;
}
Exemple #6
0
fitsfile* fits_init(char *filename, int iomode, int *nrelhdu, int *status) {
  *status = 0;
  fitsfile *fptr;
  if (fits_open_file(&fptr, filename, iomode, status)) {
    fits_print_error(*status);
    return 0;
  }

  /* Get the current HDU position and number of HDUs*/
  int hdupos, nhdu;
  fits_get_hdu_num (fptr, &hdupos);
  fits_get_num_hdus(fptr, &nhdu, status);

  /* will process only a single header if a specific extension was given, 
     otherwise all HDUs */ 
  if  (hdupos != 1 || strchr(filename, '[') || nhdu == 1) 
    *nrelhdu = 0;
  else *nrelhdu = nhdu-hdupos;

  return fptr;
}
Exemple #7
0
int fits_print_header(fitsfile* fptr, int* status) {
  /* get # of keywords */
  int nkeys = 0;
  fits_get_hdrspace(fptr, &nkeys, 0, status);
  if (*status && nkeys == 0) return *status;

  /* Standard string lengths defined in fitsio.h */
  static char card[FLEN_CARD];
  
  int i;
  /* Read and print each keywords */
  for (i=1; i<=nkeys; i++) {
    if (fits_read_record(fptr, i, card, status)) break;
    printf("%s\n", card);
  }
  
  /* terminate listing with END */
  printf("END\n");
  
  fits_print_error(*status);

  return *status;
}
Exemple #8
0
int main(int argc, char *argv[])
{
    char opt, *framename, *listname, *outname, *e_outname, buffer[BUFSIZ];
    int n, row, col, cX, cY, npatch, npxi, npxt, nprof;
    int flag_autoname, flag_verbose, status, bitmask[YAR][XAR];
    long xsize, ysize, lowleft[DIM], upright[DIM], increment[DIM] = {1L, 1L}, framesize[DIM];
    float mask_val, cutoff, framefragment[YAR][XAR];
    struct star* patchstars;
    FILE *listfile;
    fitsfile *frame, *maskedframe;
    extern char *optarg;
    extern int opterr;


    framename = NULL;
    listname = NULL;
    outname = NULL;
    mask_val = DEFAULT_MASK;
    cutoff = DEFAULT_CUTOFF;
    flag_autoname = 1;
    flag_verbose = 0;

    opterr = 0;
    while ((opt=getopt(argc, argv, "f:l:o:m:c:v")) != -1) {
        switch (opt) {
            case 'f':
                framename = optarg;
                break;
            case 'l':
                listname = optarg;
                break;
            case 'o':
                flag_autoname = 0;
                outname = optarg;
                break;
            case 'm':
                mask_val = atof(optarg);
                break;
            case 'c':
                cutoff = atof(optarg);
                break;
            case 'v':
                flag_verbose = 1;
                break;
            default:
                usage(argv);
        }
    }

    /* check mandatory parameters: */
    if (framename == NULL || listname == NULL)
        usage(argv);

    if (cutoff < 0.0) {
        fprintf(stderr, " WARNING: invalid cut-off value (%.1f ADU), using default (%.1f ADU)\n",
            cutoff, DEFAULT_CUTOFF);
        cutoff = DEFAULT_CUTOFF;
    }

    /* read ID and coordinates from any single-lined Daophot output, detect and skip the header if necessary: */
    if ((listfile=fopen(listname, "r")) == NULL) {
        file_read_error(listname);
    } else {
        npatch = line_count(listfile);

        if (has_daophot_header(listfile) == 1) {
            npatch -= DAO_HEADER_SIZE;
            line_skip(listfile, DAO_HEADER_SIZE);
        }

        if (npatch <= 0) {
            fclose(listfile);
            fprintf(stderr, " couldn't read contents of '%s', exiting.\n\n", listname);
            exit(EXIT_FAILURE);
        }

        patchstars = calloc(npatch, sizeof(struct star));
        if (patchstars == NULL) {
            fclose(listfile);
            memory_error();
        }

        for (n=0; n < npatch; n++) {
            fgets(buffer, BUFSIZ, listfile);
            sscanf(buffer, "%d %lf %lf", &patchstars[n].num, &patchstars[n].X, &patchstars[n].Y);
        }

        fclose(listfile);
    }

    status = 0;

    fits_open_file(&frame, framename, READONLY, &status);
    if (status != 0) {
        free(patchstars);
        file_read_error(framename);
    }

    fits_get_img_size(frame, DIM, framesize, &status); fits_print_error(status);
    xsize = framesize[0];
    ysize = framesize[1];
    printf(" %s: %ld x %ld pixels\n", framename, xsize, ysize);
    printf(" %s: %d stars in the list\n", listname, npatch);
    printf(" mask value: %.1f ADU\n", mask_val);
    printf(" cut-off level: %.1f ADU\n", cutoff);

    if (flag_autoname == 1) {
        outname = expand_filename(framename, "-msk", 0, 0);
        if (outname == NULL) {
            free(patchstars);
            fits_close_file(frame, &status);
            memory_error();
        }
    }
    printf(" output file: %s\n", outname);

    /* force cfitsio to overwrite already existing file (prepend ! to the filename) */
    e_outname = prepend_bang(outname);
    if (e_outname == NULL) {
        free(patchstars);
        fits_close_file(frame, &status);
        memory_error();
    }

    /* create output (masked) frame: */
    fits_create_file(&maskedframe, e_outname, &status); fits_print_error(status);

    free(e_outname);
    if (flag_autoname == 1)  /* only necessary if outname was created automatically */
        free(outname);

    fits_copy_file(frame, maskedframe, 1, 1, 1, &status); fits_print_error(status);

    npxt = 0;
    nprof = 0;

    /* process the stars in the list: */
    for (n=0; n < npatch; n++)
    {
        if (flag_verbose == 1)
            printf("\n star #%d:\n", patchstars[n].num);

        /* cfitsio doesn't understand fractional pixels, round the coordinates to nearest integer: */
        cX = rint(patchstars[n].X);
        cY = rint(patchstars[n].Y);

        /* make sure center is in the picture, skip this star if it is not: */
        if (cX < 1 || cY < 1 || cX > xsize || cY > ysize) {
            printf(" star #%d is outside the frame (%d,%d), skipping.\n", patchstars[n].num, cX, cY);
            continue;
        }
       /*
        *  in fitsio pixel index runs from 1 to N, not from 0 to N-1.
        *  consider XAR=21 (reading 10 pixels right and left from center), XSIZE=2044.
        *  example 1: star with x=10. fitsio will reach and try to read pixel at x=0, ILLEGAL.
        *  example 2: star with x=2034. fitsio will reach and try to read pixel at x=2044, LEGAL.
        *  we have to use LESS OR EQUAL when checking lower and left boundary,
        *  for upper and right boundary only using GREATER is fine
        */
        else if (cX <= (XAR-1)/2 || cY <= (YAR-1)/2 || cX > xsize-(XAR-1)/2 || cY > ysize-(YAR-1)/2) {
            printf(" star #%d is too close to the edge or partially outside the frame (%d,%d), skipping.\n",
                patchstars[n].num, cX, cY);
            continue;
        }

        if (flag_verbose == 1)
            printf(" coordinates of star #%d center in current frame: (%d,%d)\n", patchstars[n].num, cX, cY);

        /* compute the coordinates of lower left and upper right pixel: */
        lowleft[0] = cX - (XAR-1)/2;
        lowleft[1] = cY - (YAR-1)/2;
        upright[0] = cX + (XAR-1)/2;
        upright[1] = cY + (YAR-1)/2;

        /* load frame fragment with the star into table: */
        fits_read_subset(frame, TFLOAT, lowleft, upright, increment, 0, framefragment, 0, &status); fits_print_error(status);

        /* initialise the bitmask with zeros: */
        for (row=0; row < YAR; row++)
            for (col=0; col < XAR; col++)
                bitmask[row][col] = 0;

        /* detect holes and write to bitmask: */
        for (row=YAR-1; row >= 0; row--)
            examine_row(framefragment, bitmask, row, cutoff);

        for (col=0; col < XAR; col++)
            examine_col(framefragment, bitmask, col, cutoff);

        npxi = apply_bitmask(framefragment, bitmask, mask_val);
        npxt += npxi;
        if (npxi > 0)
            ++nprof;

        if (flag_verbose == 1) {
            printf("\n");
            print_bitmask(bitmask);
            printf("\n star %d: %d pixels masked\n", patchstars[n].num, npxi);
        }

        /* write the modified frame subsection to output frame: */
        fits_write_subset(maskedframe, TFLOAT, lowleft, upright, framefragment, &status); fits_print_error(status);
    }

    printf(" %d pixels were masked in %d stars.\n", npxt, nprof);

    fits_close_file(frame, &status); fits_print_error(status);
    fits_close_file(maskedframe, &status); fits_print_error(status);

    free(patchstars);

    return 0;
}