예제 #1
0
파일: pp_packing.c 프로젝트: djkirkham/iris
/* rle_decode(byte_array, lbrow, lbnpt, mdi) */
static PyObject *rle_decode_py(PyObject *self, PyObject *args)
{
    char *bytes_in=NULL;
    PyArrayObject *npy_array_out=NULL;
    int bytes_in_len;
    npy_intp dims[2];
    int lbrow, lbnpt, npts;
    float mdi;

    if (!PyArg_ParseTuple(args, "s#iif", &bytes_in, &bytes_in_len, &lbrow, &lbnpt, &mdi)) return NULL;

    // Unpacking algorithm accepts an int - so assert that lbrow*lbnpt does not overflow
    if (lbrow > 0 && lbnpt >= INT_MAX / (lbrow+1)) {
        PyErr_SetString(PyExc_ValueError, "Resulting unpacked PP field is larger than PP supports.");
        return NULL;
    } else {
        npts = lbnpt*lbrow;
    }

    // We can't use the macros Py_BEGIN_ALLOW_THREADS / Py_END_ALLOW_THREADS
    // because they declare a new scope block, but we want multiple exits.
    PyThreadState *_save;
    _save = PyEval_SaveThread();

    float *dataout = (float*)calloc(npts, sizeof(float));

    if (dataout == NULL) {
        PyEval_RestoreThread(_save);
        PyErr_SetString(PyExc_ValueError, "Unable to allocate memory for wgdos_unpacking.");
        return NULL;
    }

    function func;  // function is defined by wgdosstuff.
    set_function_name(__func__, &func, 0);
    int status = unpack_ppfield(mdi, (bytes_in_len/BYTES_PER_INT_UNPACK_PPFIELD), bytes_in, LBPACK_RLE_PACKED, npts, dataout, &func);

    /* Raise an exception if there was a problem with the REL algorithm */
    if (status != 0) {
        free(dataout);
        PyEval_RestoreThread(_save);
        PyErr_SetString(PyExc_ValueError, "RLE decode encountered an error.");
        return NULL;
    }
    else {
        /* The data came back fine, so make a Numpy array and return it */
        dims[0]=lbrow;
        dims[1]=lbnpt;
        PyEval_RestoreThread(_save);
        npy_array_out=(PyArrayObject *) PyArray_SimpleNewFromData(2, dims, NPY_FLOAT, dataout);

        if (npy_array_out == NULL) {
            PyErr_SetString(PyExc_ValueError, "Failed to make the numpy array for the packed data.");
            return NULL;
        }

        // give ownership of dataout to the Numpy array - Numpy will then deal with memory cleanup.
        npy_array_out->flags = npy_array_out->flags | NPY_OWNDATA;
        return (PyObject *)npy_array_out;
    }
}
예제 #2
0
/* wgdos_unpack(byte_array, lbrow, lbnpt, mdi) */
static PyObject *wgdos_unpack_py(PyObject *self, PyObject *args)
{
    char *bytes_in=NULL;
    PyArrayObject *npy_array_out=NULL;
    int bytes_in_len;
    npy_intp dims[2];
    int lbrow, lbnpt, npts;
    float mdi;

    if (!PyArg_ParseTuple(args, "s#iif", &bytes_in, &bytes_in_len, &lbrow, &lbnpt, &mdi)) return NULL;

    // Unpacking algorithm accepts an int - so assert that lbrow*lbnpt does not overflow 
    if (lbrow > 0 && lbnpt >= INT_MAX / (lbrow+1)) {
        PyErr_SetString(PyExc_ValueError, "Resulting unpacked PP field is larger than PP supports.");
        return NULL;
    } else{
        npts = lbnpt*lbrow;
    }

    /* Do the unpack of the given byte array */
    float *dataout = (float*)calloc(npts, sizeof(float));

    if (dataout == NULL) {
        PyErr_SetString(PyExc_ValueError, "Unable to allocate memory for wgdos_unpacking.");
        return NULL;
    }

    function func; // function is defined by wgdosstuff.
    set_function_name(__func__, &func, 0);
    int status = unpack_ppfield(mdi, 0, bytes_in, LBPACK_WGDOS_PACKED, npts, dataout, &func);

    /* Raise an exception if there was a problem with the WGDOS algorithm */
    if (status != 0) {
      free(dataout);
      PyErr_SetString(PyExc_ValueError, "WGDOS unpack encountered an error."); 
      return NULL;
    }
    else {
        /* The data came back fine, so make a Numpy array and return it */
        dims[0]=lbrow;
        dims[1]=lbnpt;
        npy_array_out=(PyArrayObject *) PyArray_SimpleNewFromData(2, dims, NPY_FLOAT, dataout);

        if (npy_array_out == NULL) {
          PyErr_SetString(PyExc_ValueError, "Failed to make the numpy array for the packed data.");
          return NULL;
        }

        // give ownership of dataout to the Numpy array - Numpy will then deal with memory cleanup.
        npy_array_out->flags = npy_array_out->flags | NPY_OWNDATA;

        return (PyObject *)npy_array_out;
    }
}
예제 #3
0
int byteorder_data_unpack_ppfield(float mdi, int data_size, char* data, int network_order_in,
                                  int pack, int unpacked_size, float* to, int network_order_out,
                                  function* parent) {
  int retval=0;
  int* ip_in;
  int* ip_out;
  int count;
  char* newdata=NULL;
  int temp;
  function subroutine;
  set_function_name(__func__, &subroutine, parent);
#if __BYTE_ORDER == __LITTLE_ENDIAN
  if (network_order_in) {
    switch (pack) {
    case 1:
      newdata=malloc(data_size*sizeof(int));
      ip_in=(int*)data;
      ip_out=(int*)newdata;
      for (count=0; count<data_size; count++) {
        ip_out[count]=ntohl(ip_in[count]);
      }
      break;
    default:
      newdata=data;
    }
  }
#endif

  retval=unpack_ppfield(mdi, data_size, newdata, pack, unpacked_size, to, &subroutine);

#if __BYTE_ORDER == __LITTLE_ENDIAN
  if (network_order_out && (to!=NULL)) {
    ip_in=(int*)to;
    ip_out=(int*)to;
    for (count=0; count<unpacked_size; count++) {
      ip_out[count]=htonl(ip_in[count]);
    }
  }
#endif

  if (newdata!=data) {
    free (newdata);
  }
  return retval;
}
예제 #4
0
int unpack_ppfield32(uint32_t* lookup, char* data, float* to, function* parent) {
  int unpacked_size;
  int data_size;
  int pack;
  float mdi;
  int ret;
  function subroutine;
  set_function_name(__func__, &subroutine, parent);

  mdi=*(float*)(lookup+MDI);
  unpacked_size=lookup[NROWS]*lookup[NCOLS];
  data_size = lookup[FIELD_LENGTH] - lookup[EXT];
  pack=lookup[PACK] % 10;
  ret=unpack_ppfield(mdi, data_size, data, pack, unpacked_size, to, parent);
  lookup[FIELD_LENGTH]=unpacked_size + lookup[EXT];
  lookup[PACK]=0;
  return (ret);
}
예제 #5
0
int unpack_ppfield64(uint64_t* lookup, char* data, float* to, function* parent) {
  int unpacked_size;
  int data_size;
  int pack;
  float mdi;
  double dmdi;
  int ret;
  function subroutine;
  set_function_name(__func__, &subroutine, parent);

  if (sizeof(float) != 8) {
    dmdi=*(double*)(lookup+MDI);
    mdi=(float)dmdi;
  } else {
    mdi=*(float*)(lookup+MDI);
  }
  unpacked_size=lookup[NROWS]*lookup[NCOLS];
  data_size = (lookup[FIELD_LENGTH] - lookup[EXT]);
  pack=lookup[PACK] % 10;
  ret=unpack_ppfield(mdi, data_size, data, pack, unpacked_size, to, parent);
  lookup[FIELD_LENGTH]=unpacked_size + lookup[EXT];
  lookup[PACK]=0;
  return (ret);
}