Example #1
0
static int
default_wcsmap_direct(struct wcsmap_param_t* m,
                      const double xd, const double yd,
                      const integer_t n,
                      double* xin /*[n]*/, double* yin /*[n]*/,
                      /* Output parameters */
                      double* xout, double* yout,
                      struct driz_error_t* error) {

  integer_t  i;
  int        status;
  int        result = 1;
  double    *memory = NULL;
  double    *ptr    = NULL;
  double    *xyin   = NULL;
  double    *skyout = NULL;
  double    *xyout  = NULL;
  double    *imgcrd = NULL;
  double    *phi    = NULL;
  double    *theta  = NULL;
  int       *stat   = NULL;

  /* Allocate memory for new 2-D array */
  ptr = memory = (double *) malloc(n * 10 * sizeof(double));
  if (memory == NULL) goto exit;
  xyin = ptr;
  ptr += n * 2;
  xyout = ptr;
  ptr += n * 2;
  skyout = ptr;
  ptr += n * 2;
  imgcrd = ptr;
  ptr += n * 2;
  phi = ptr;
  ptr += n;
  theta = ptr;
  stat = (int *)malloc(n * sizeof(int));
  if (stat == NULL) goto exit;

  /* The input arrays need to be converted to 2-D arrays for input
     to the PyWCS (and related) functions. */

  /* Populate new 2-D array with values from x and y input arrays */
  for (i = 0; i < n; ++i) {
    xyin[2*i] = xin[i];
    xyin[2*i+1] = yin[i];
  }

  /*
    Apply pix2sky() transformation from PyWCS
  */

  wcsprm_python2c(m->input_wcs->wcs);
  status = pipeline_all_pixel2world(m->input_wcs, n, 2, xyin, skyout);
  wcsprm_c2python(m->input_wcs->wcs);
  if (status) {
    goto exit;
  }

  /*
    Finally, call wcs_sky2pix() for the output object.
  */
  wcsprm_python2c(m->output_wcs->wcs);
  status = wcss2p(m->output_wcs->wcs, n, 2,
                  skyout, phi, theta, imgcrd, xyout, stat);
  wcsprm_c2python(m->output_wcs->wcs);
  if (status) {
    goto exit;
  }

  /*
    Transform results back to 2 1-D arrays, like the input.
  */
  for (i = 0; i < n; ++i){
    xout[i] = xyout[2*i];
    yout[i] = xyout[2*i+1];
  }

  result = 0;

 exit:
  /*
  Free memory allocated to internal 2-D arrays
  */
  free(memory);
  free(stat);

  return result;
}
Example #2
0
int
default_wcsmap_init(struct wcsmap_param_t* m,
                    pipeline_t* input,
                    pipeline_t* output,
                    int nx, int ny,
                    double factor,
                    struct driz_error_t* error) {
  int     n;
  int     table_size;
  double *pixcrd = NULL;
  double *ptr    = NULL;
  double *tmp    = NULL;
  double *phi    = NULL;
  double *theta  = NULL;
  double *imgcrd = NULL;
  int    *stat   = NULL;
  int     snx;
  int     sny;
  int     i;
  int     j;
  int     status = 1;
  int     istat;

  assert(m);
  assert(input);
  assert(output);
  assert(m->input_wcs == NULL);
  assert(m->output_wcs == NULL);
  assert(m->table == NULL);

  if (factor > 0) {
    snx = (int)((double)nx / factor) + 2;
    sny = (int)((double)ny / factor) + 2;

    n = (snx) * (sny);
    table_size = n << 1;

    pixcrd = malloc(table_size * sizeof(double));
    if (pixcrd == NULL) {
      driz_error_set_message(error, "Out of memory");
      goto exit;
    }

    m->table = malloc(table_size * sizeof(double));
    if (m->table == NULL) {
      driz_error_set_message(error, "Out of memory");
      goto exit;
    }

    tmp = malloc(table_size * sizeof(double));
    if (tmp == NULL) {
      driz_error_set_message(error, "Out of memory");
      goto exit;
    }

    phi = malloc(n * sizeof(double));
    if (phi == NULL) {
      driz_error_set_message(error, "Out of memory");
      goto exit;
    }

    theta = malloc(n * sizeof(double));
    if (theta == NULL) {
      driz_error_set_message(error, "Out of memory");
      goto exit;
    }

    imgcrd = malloc(table_size * sizeof(double));
    if (imgcrd == NULL) {
      driz_error_set_message(error, "Out of memory");
      goto exit;
    }

    stat = malloc(n * sizeof(int));
    if (stat == NULL) {
      driz_error_set_message(error, "Out of memory");
      goto exit;
    }

    ptr = pixcrd;
    for (j = 0; j < sny; ++j) {
      for (i = 0; i < snx; ++i) {
        *ptr++ = (double)i * factor;
        *ptr++ = (double)j * factor;
      }
    }

    wcsprm_python2c(input->wcs);
    istat = pipeline_all_pixel2world(input, n, 2, pixcrd, tmp);
    wcsprm_c2python(input->wcs);

    if (istat) {
      free(m->table);
      m->table = NULL;
      driz_error_set_message(error, wcslib_get_error_message(istat));
      goto exit;
    }

    wcsprm_python2c(output->wcs);
    istat = wcss2p(output->wcs, n, 2, tmp, phi, theta, imgcrd, m->table, stat);
    wcsprm_c2python(output->wcs);

    if (istat) {
      free(m->table);
      m->table = NULL;
      driz_error_set_message(error, wcslib_get_error_message(istat));
      goto exit;
    }
  } /* End if_then for factor > 0 */

  m->input_wcs = input;
  m->output_wcs = output;

  m->nx = nx;
  m->ny = ny;
  m->snx = snx;
  m->sny = sny;
  m->factor = factor;

  status = 0;

 exit:

  free(pixcrd);
  free(tmp);
  free(phi);
  free(theta);
  free(imgcrd);
  free(stat);

  return status;
}
Example #3
0
/*@null@*/ static PyObject*
Wcs_all_pix2world(
    Wcs* self,
    PyObject* args,
    PyObject* kwds) {

    int            naxis      = 2;
    PyObject*      pixcrd_obj = NULL;
    int            origin     = 1;
    PyArrayObject* pixcrd     = NULL;
    PyArrayObject* world      = NULL;
    int            status     = -1;
    const char*    keywords[] = {
        "pixcrd", "origin", NULL
    };

    if (!PyArg_ParseTupleAndKeywords(
                args, kwds, "Oi:all_pix2world", (char **)keywords,
                &pixcrd_obj, &origin)) {
        return NULL;
    }

    naxis = self->x.wcs->naxis;

    pixcrd = (PyArrayObject*)PyArray_ContiguousFromAny(pixcrd_obj, PyArray_DOUBLE, 2, 2);
    if (pixcrd == NULL) {
        return NULL;
    }

    if (PyArray_DIM(pixcrd, 1) < naxis) {
        PyErr_Format(
            PyExc_RuntimeError,
            "Input array must be 2-dimensional, where the second dimension >= %d",
            naxis);
        goto exit;
    }

    world = (PyArrayObject*)PyArray_SimpleNew(2, PyArray_DIMS(pixcrd), PyArray_DOUBLE);
    if (world == NULL) {
        goto exit;
    }

    /* Make the call */
    Py_BEGIN_ALLOW_THREADS
    preoffset_array(pixcrd, origin);
    wcsprm_python2c(self->x.wcs);
    status = pipeline_all_pixel2world(&self->x,
                                      (unsigned int)PyArray_DIM(pixcrd, 0),
                                      (unsigned int)PyArray_DIM(pixcrd, 1),
                                      (double*)PyArray_DATA(pixcrd),
                                      (double*)PyArray_DATA(world));
    wcsprm_c2python(self->x.wcs);
    unoffset_array(pixcrd, origin);
    Py_END_ALLOW_THREADS
    /* unoffset_array(world, origin); */

exit:
    Py_XDECREF(pixcrd);

    if (status == 0 || status == 8) {
        return (PyObject*)world;
    } else if (status == -1) {
        PyErr_SetString(
            PyExc_ValueError,
            "Wrong number of dimensions in input array.  Expected 2.");
        return NULL;
    } else {
        Py_DECREF(world);
        if (status == -1) {
            /* exception already set */
            return NULL;
        } else {
            wcserr_to_python_exc(self->x.err);
            return NULL;
        }
    }
}