Esempio n. 1
0
static int wcslib_write_to(const anwcslib_t* anwcslib, FILE* fid) {
	int res;
	int Ncards;
	char* hdrstr;
	res = wcshdo(-1, anwcslib->wcs, &Ncards, &hdrstr);
	if (res) {
		ERROR("wcshdo() failed: %s", wcshdr_errmsg[res]);
		return -1;
	}

	// FIXME -- incomplete!  Add other required headers and write to file.
	int i;
	printf("wcslib header:\n");
	for (i=0; i<Ncards; i++)
		printf("%.80s\n", hdrstr + i*80);
	printf("\n\n");

	ERROR("wcslib_write_to() is unfinished.\n");
	return -1;
}
Esempio n. 2
0
void
ui_preparations(struct cropparams *p)
{
  fitsfile *tmpfits;
  struct inputimgs *img;
  int status, firsttype=0;
  size_t input_counter, firstndim=0;


  /* For polygon and section, there should be no center checking. */
  if(p->polygon || p->section)
    p->checkcenter=0;


  /* Allocate space for all the input images. This is done here because
     WCSLIB is unfortunately not thread-safe when reading the WCS
     information from the FITS files. In cases where the number of cropped
     images are more than the input images, this can also be a preformance
     boost because each image information is only read once.

     The images are filled in opposite order because we used a linked list
     to read them in, which is a first in first out structure.*/
  errno=0;
  p->imgs=malloc(p->numin*sizeof *p->imgs);
  if(p->imgs==NULL)
    error(EXIT_FAILURE, errno, "%s: %zu bytes for p->imgs",
          __func__, p->numin*sizeof *p->imgs);


  /* Fill in the WCS information of each image. */
  input_counter=p->numin;
  while(p->inputs)
    {
      /* Pop from the list of input images and get the info. */
      status=0;
      img=&p->imgs[--input_counter];
      img->name=gal_list_str_pop(&p->inputs);
      tmpfits=gal_fits_hdu_open_format(img->name, p->cp.hdu, 0);
      gal_fits_img_info(tmpfits, &p->type, &img->ndim, &img->dsize,
                        NULL, NULL);
      img->wcs=gal_wcs_read_fitsptr(tmpfits, p->hstartwcs, p->hendwcs,
                                    &img->nwcs);
      if(img->wcs)
        {
          gal_wcs_decompose_pc_cdelt(img->wcs);
          status=wcshdo(0, img->wcs, &img->nwcskeys, &img->wcstxt);
          if(status)
            error(EXIT_FAILURE, 0, "wcshdo ERROR %d: %s", status,
                  wcs_errmsg[status]);
        }
      else
        if(p->mode==IMGCROP_MODE_WCS)
          error(EXIT_FAILURE, 0, "the WCS structure of %s (hdu: %s) "
                "image is not recognized. So WCS mode cannot be used "
                "as input coordinates. You can try with pixel coordinates "
                "with `--mode=img'", img->name, p->cp.hdu);
      fits_close_file(tmpfits, &status);
      gal_fits_io_error(status, NULL);

      /* Make sure all the images have the same type and dimensions. */
      if(firsttype==0)
        {
          /* Set the basic information. */
          firsttype = p->type;
          firstndim = img->ndim;
          p->bitnul = gal_fits_key_img_blank(p->type);

          /* Make sure the number of dimensions is supported. */
          if(firstndim>MAXDIM)
            error(EXIT_FAILURE, 0, "%s: is as %zu dimensional dataset, Crop "
                  "currently only supports a maximum of %d dimensions",
                  img->name, firstndim, MAXDIM);

          /* Make sure the number of coordinates given for center
             correspond to the dimensionality of the data. */
          if(p->center && p->center->size!=firstndim)
            error(EXIT_FAILURE, 0, "%s (hdu %s) has %zu dimensions, but "
                  "%zu coordinates were given to `--center'", img->name,
                  p->cp.hdu, firstndim, p->center->size);
        }
      else
        {
          if(firsttype!=p->type)
            error(EXIT_FAILURE, 0, "%s: type is `%s' while previous input(s) "
                  "were `%s' type. All inputs must have the same pixel data "
                  "type.\n\nYou can use Gnuastro's Arithmetic program to "
                  "convert `%s' to `%s', please run this command for more "
                  "information (press `SPACE' for going down and `q' to "
                  "return to the command-line):\n\n"
                  "    $ info Arithmetic\n",
                  img->name, gal_type_name(p->type, 1),
                  gal_type_name(firsttype, 1), img->name,
                  gal_type_name(p->type, 1));
          if(firstndim!=img->ndim)
            error(EXIT_FAILURE, 0, "%s: type has %zu dimensions, while "
                  "previous input(s) had %zu dimensions. All inputs must "
                  "have the same number of dimensions", img->name, img->ndim,
                  firstndim);
        }

      /* In WCS mode, we need some additional preparations. */
      if(p->mode==IMGCROP_MODE_WCS) wcsmode_check_prepare(p, img);
    }


  /* Unify central crop methods into `p->centercoords'. */
  if(p->catname || p->center)
    ui_prepare_center(p);


  /* `ui_read_cols' set the number of output crops when a catalog was
     given, in all other cases, we only have one output. */
  if(p->catname==NULL) p->numout=1;


  /* Prepare the log file if the user has asked for it. */
  ui_make_log(p);
}
Esempio n. 3
0
int main(int argc, char *argv[])

{
  char *header, *hptr;
  int  dohdr = 0, dopixel = 0, doworld = 0;
  int  i, nkeyrec, nreject, nwcs, stat[NWCSFIX], status = 0;
  double imgcrd[2], phi, pixcrd[2], theta, world[2];
  fitsfile *fptr;
  struct wcsprm *wcs;


  /* Parse options. */
  for (i = 1; i < argc && argv[i][0] == '-'; i++) {
    if (!argv[i][1]) break;

    switch (argv[i][1]) {
    case 'h':
      dohdr = 1;
      break;
    case 'p':
      dopixel = 1;
      break;
    case 'w':
      doworld = 1;
      break;
    default:
      fprintf(stderr, "Usage: twcshdr [-h | -p | -w] <file>\n");
      return 1;
    }
  }

  if (i != (argc-1)) {
    fprintf(stderr, "Usage: twcshdr [-h | -p | -w] <file>\n");
    return 1;
  }

  /* Open the FITS test file and read the primary header. */
  fits_open_file(&fptr, argv[i], READONLY, &status);
  if ((status = fits_hdr2str(fptr, 1, NULL, 0, &header, &nkeyrec, &status))) {
    fits_report_error(stderr, status);
    return 1;
  }


  /*-----------------------------------------------------------------------*/
  /* Basic steps required to interpret a FITS WCS header, including -TAB.  */
  /*-----------------------------------------------------------------------*/

  /* Parse the primary header of the FITS file. */
  if ((status = wcspih(header, nkeyrec, WCSHDR_all, 2, &nreject, &nwcs,
                       &wcs))) {
    fprintf(stderr, "wcspih ERROR %d: %s.\n", status,wcshdr_errmsg[status]);
  }

  /* Read coordinate arrays from the binary table extension. */
  if ((status = fits_read_wcstab(fptr, wcs->nwtb, (wtbarr *)wcs->wtb,
                                 &status))) {
    fits_report_error(stderr, status);
    return 1;
  }

  /* Translate non-standard WCS keyvalues. */
  if ((status = wcsfix(7, 0, wcs, stat))) {
    for (i = 0; i < NWCSFIX; i++) {
      if (stat[i] > 0) {
        fprintf(stderr, "wcsfix ERROR %d: %s.\n", status,
                wcsfix_errmsg[stat[i]]);
      }
    }

    return 1;
  }

  /*-----------------------------------------------------------------------*/
  /* The wcsprm struct is now ready for use.                               */
  /*-----------------------------------------------------------------------*/

  /* Finished with the FITS file. */
  fits_close_file(fptr, &status);
  free(header);

  /* Initialize the wcsprm struct, also taking control of memory allocated by
   * fits_read_wcstab(). */
  if ((status = wcsset(wcs))) {
    fprintf(stderr, "wcsset ERROR %d: %s.\n", status, wcs_errmsg[status]);
    return 1;
  }

  if (dohdr) {
    if ((status = wcshdo(WCSHDO_all, wcs, &nkeyrec, &header))) {
      return 1;
    }

    hptr = header;
    printf("\n\n");
    for (i = 0; i < nkeyrec; i++, hptr += 80) {
      printf("%.80s\n", hptr);
    }

    free(header);

  } else if (dopixel) {
    while (1) {
      printf("Enter pixel coordinates: ");
      if (scanf("%lf%*[ ,]%lf", pixcrd, pixcrd+1) != wcs->naxis) break;
      status = wcsp2s(wcs, 1, 2, pixcrd, imgcrd, &phi, &theta, world, stat);
      printf("  (%20.15f, %20.15f) ->\n  (%20.15f, %20.15f)\n\n",
        pixcrd[0], pixcrd[1], world[0], world[1]);
    }

  } else if (doworld) {
    while (1) {
      printf("Enter world coordinates: ");
      if (scanf("%lf%*[ ,]%lf", world, world+1) != wcs->naxis) break;
      status = wcss2p(wcs, 1, 2, world, &phi, &theta, imgcrd, pixcrd, stat);
      printf("  (%20.15f, %20.15f) ->\n  (%20.15f, %20.15f)\n\n",
        world[0], world[1], pixcrd[0], pixcrd[1]);
    }

  } else {
    /* Print the struct. */
    if ((status = wcsprt(wcs))) {
      return 1;
    }
  }

  /* Clean up. */
  status = wcsvfree(&nwcs, &wcs);

  return 0;
}