// create a new NDF extension static PyObject* pyndf_xnew(NDF *self, PyObject *args) { int ndim = 0; const char *xname, *type; PyObject *dim; if(!PyArg_ParseTuple(args, "ss|iO:pyndf_xnew", &xname, &type, &ndim, &dim)) return NULL; int status = SAI__OK; HDSLoc *loc = NULL; // perform checks if we're not making an extension header if(ndim != 0) { // check for HDS types if (!checkHDStype(type)) return NULL; // need dims if it's not an ext if(ndim < 1 || dim == NULL) return NULL; PyArrayObject *npydim = (PyArrayObject*) PyArray_FROM_OTF(dim,NPY_INT,NPY_IN_ARRAY|NPY_FORCECAST); if (PyArray_SIZE(npydim) != ndim) return NULL; errBegin(&status); ndfXnew(self->_ndfid,xname,type,ndim,(int*)PyArray_DATA(npydim),&loc,&status); Py_DECREF(npydim); } else { // making an ext/struct errBegin(&status); ndfXnew(self->_ndfid,xname,type,0,0,&loc,&status); } if (raiseNDFException(&status)) return NULL; PyObject* pobj = NpyCapsule_FromVoidPtr(loc, PyDelLoc); return Py_BuildValue("O",pobj); }
// create a new NDF (simple) structure static PyObject* pyndf_new(NDF *self, PyObject *args) { // use ultracam defaults const char *ftype = "_REAL"; int ndim; PyObject* lb; PyObject* ub; if(!PyArg_ParseTuple(args, "siOO:pyndf_new", &ftype, &ndim, &lb, &ub)) return NULL; if(ndim < 0 || ndim > 7) return NULL; // TODO: check for ftype here int status = SAI__OK; PyArrayObject* lower = (PyArrayObject*) PyArray_FROM_OTF(lb, NPY_UINT, NPY_IN_ARRAY | NPY_FORCECAST); PyArrayObject* upper = (PyArrayObject*) PyArray_FROM_OTF(ub, NPY_UINT, NPY_IN_ARRAY | NPY_FORCECAST); if (!lower || !upper) return NULL; if(PyArray_SIZE(lower) != ndim || PyArray_SIZE(upper) != ndim) return NULL; errBegin(&status); ndfNew(ftype,ndim,(int*)PyArray_DATA(lower),(int*)PyArray_DATA(upper),&self->_place,&self->_ndfid,&status); // placeholder annulled by this routine Py_DECREF(lower); Py_DECREF(upper); if (raiseNDFException(&status)) return NULL; Py_RETURN_NONE; }
// open an existing or new NDF file static PyObject* pyndf_open(NDF *self, PyObject *args) { const char *name; const char *mode = "READ"; const char *stat = "OLD"; if(!PyArg_ParseTuple(args, "s|ss:pyndf_open", &name, &mode, &stat)) return NULL; // check for allowed values of mode and stat if(strcmp(mode,"READ") != 0 && strcmp(mode,"WRITE") != 0 && strcmp(mode,"UPDATE") != 0) { PyErr_SetString( PyExc_ValueError, "Incorrect mode for ndf_open"); return NULL; } if(strcmp(stat,"OLD") != 0 && strcmp(stat,"NEW") != 0 && strcmp(stat,"UNKNOWN") != 0) { PyErr_SetString( PyExc_ValueError, "Unknown status string for ndf_open" ); return NULL; } int indf, place; int status = SAI__OK; errBegin(&status); indf = NDF__NOID; place = NDF__NOPL; ndfOpen( NULL, name, mode, stat, &indf, &place, &status); if (raiseNDFException(&status)) return NULL; return NDF_create_object( indf, place ); };
static PyObject* pyndf_dim(NDF *self) { int i; PyArrayObject* dim = NULL; int ndim; int idim[NDF__MXDIM]; int status = SAI__OK; errBegin(&status); ndfDim(self->_ndfid, NDF__MXDIM, idim, &ndim, &status ); if (raiseNDFException(&status)) return NULL; npy_intp odim[1]; odim[0] = ndim; dim = (PyArrayObject*) PyArray_SimpleNew(1, odim, PyArray_INT); if(dim == NULL) goto fail; for(i=0; i<ndim; i++) ((int *)dim->data)[i] = idim[ndim-i-1]; return Py_BuildValue("N", PyArray_Return(dim)); fail: Py_XDECREF(dim); return NULL; };
static PyObject* pyndf_gtwcs(NDF *self) { AstFrameSet *wcs = NULL; char *string; PyObject *pywcs; PyObject *result = NULL; int status = SAI__OK; #if HAVE_AST errBegin(&status); astBegin; ndfGtwcs(self->_ndfid, &wcs, &status ); if( wcs ) { string = astToString( wcs ); wcs = astAnnul( wcs ); pywcs = PyAst_FromString( string ); string = astFree( string ); if( pywcs ) result = Py_BuildValue("O",pywcs); Py_XDECREF(pywcs); } astEnd; if (raiseNDFException(&status)) return NULL; #else PyErr_SetString( PyExc_NotImplementedError, "starlink.Ast must be available for WCS to be retrieved from an NDF"); #endif return result; };
static PyObject* pyndf_bound(NDF *self) { int i; PyArrayObject* bound = NULL; int ndim; int lbnd[NDF__MXDIM]; int ubnd[NDF__MXDIM]; npy_intp odim[2]; int status = SAI__OK; errBegin(&status); ndfBound(self->_ndfid, NDF__MXDIM, lbnd, ubnd, &ndim, &status ); if (raiseNDFException(&status)) return NULL; odim[0] = 2; odim[1] = ndim; bound = (PyArrayObject*) PyArray_SimpleNew(2, odim, PyArray_INT); if(bound == NULL) goto fail; int *bptr = (int *)bound->data; for(i=0; i<ndim; i++){ bptr[i] = lbnd[ndim-i-1]; bptr[i+ndim] = ubnd[ndim-i-1]; } return Py_BuildValue("N", PyArray_Return(bound)); fail: Py_XDECREF(bound); return NULL; };
// make a new primitive static PyObject* pydat_new(HDSObject *self, PyObject *args) { PyObject *dimobj; const char *type, *name; int ndim; if(!PyArg_ParseTuple(args, "ssiO:pydat_new", &name, &type, &ndim, &dimobj)) return NULL; HDSLoc* loc = HDS_retrieve_locator(self); if(!checkHDStype(type)) return NULL; int status = SAI__OK; errBegin(&status); if (ndim > 0) { PyArrayObject *npydim = (PyArrayObject*) PyArray_FROM_OTF(dimobj,NPY_INT,NPY_IN_ARRAY|NPY_FORCECAST); hdsdim *dims = (hdsdim*)PyArray_DATA(npydim); datNew(loc,name,type,ndim,dims,&status); Py_DECREF(npydim); } else { datNew(loc,name,type,0,0,&status); } if (raiseHDSException(&status)) return NULL; Py_RETURN_NONE; }
static PyObject* pydat_shape(HDSObject *self) { // Recover C-pointer passed via Python HDSLoc* loc = HDS_retrieve_locator(self); const int NDIMX=7; int ndim; hdsdim tdim[NDIMX]; int status = SAI__OK; errBegin(&status); datShape(loc, NDIMX, tdim, &ndim, &status); if (raiseHDSException(&status)) return NULL; // Return None in this case if(ndim == 0) Py_RETURN_NONE; // Create array of correct dimensions to save data to PyArrayObject* dim = NULL; npy_intp sdim[1]; int i; sdim[0] = ndim; dim = (PyArrayObject*) PyArray_SimpleNew(1, sdim, PyArray_INT); if(dim == NULL) goto fail; // Reverse order Fortran --> C convention int* sdata = (int*)dim->data; for(i=0; i<ndim; i++) sdata[i] = tdim[ndim-i-1]; return Py_BuildValue("N", PyArray_Return(dim)); fail: Py_XDECREF(dim); return NULL; };
// map access to array component static PyObject* pyndf_amap(NDF *self, PyObject* args) { int el; void* ptr; const char* comp; const char* type; const char* mmod; int iaxis; if(!PyArg_ParseTuple(args,"siss:pyndf_amap",&comp,&iaxis,&type,&mmod)) return NULL; int status = SAI__OK; if(strcmp(comp,"CENTRE") != 0 && strcmp(comp,"WIDTH") != 0 && strcmp(comp,"VARIANCE") != 0 && strcmp(comp,"ERROR") != 0) { PyErr_SetString( PyExc_ValueError, "Unsupported NDF data component" ); return NULL; } if(strcmp(mmod,"READ") != 0 && strcmp(mmod,"UPDATE") != 0 && strcmp(mmod,"WRITE") != 0) { PyErr_SetString( PyExc_ValueError, "Unsupported NDF update mode" ); return NULL; } if(!checkHDStype(type)) { PyErr_SetString( PyExc_ValueError, "Unsupported HDS data type" ); return NULL; } int naxis = tr_iaxis(self->_ndfid, iaxis, &status); errBegin(&status); ndfAmap(self->_ndfid,comp,naxis,type,mmod,&ptr,&el,&status); if (raiseNDFException(&status)) return NULL; return NDFMapped_create_object( self, comp, type, mmod, ptr, el, naxis ); }
static PyObject* pyndf_end(NDF *self) { int status = SAI__OK; errBegin(&status); ndfEnd(&status); if (raiseNDFException(&status)) return NULL; Py_RETURN_NONE; };
// THINK - THIS IS A DESTRUCTOR static PyObject* pyndf_annul(NDF *self) { int status = SAI__OK; errBegin(&status); ndfAnnul(&self->_ndfid, &status); if (raiseNDFException(&status)) return NULL; Py_RETURN_NONE; };
static PyObject* pyndf_init(NDF *self, PyObject *args) { int argc = 0, status = SAI__OK; char **argv = NULL; errBegin(&status); ndfInit(argc, argv, &status); if (raiseNDFException(&status)) return NULL; Py_RETURN_NONE; };
static void NDF_dealloc(NDF* self) { int status = SAI__OK; errBegin(&status); if (self->_ndfid != NDF__NOID) ndfAnnul( &self->_ndfid, &status); if (status != SAI__OK) errAnnul(&status); errEnd(&status); PyObject_Del( self ); }
static PyObject* pyndf_xnumb(NDF *self) { int status = SAI__OK, nextn; errBegin(&status); ndfXnumb(self->_ndfid, &nextn, &status); if (raiseNDFException(&status)) return NULL; return Py_BuildValue("i", nextn); };
static PyObject* pydat_annul(HDSObject *self) { // Recover C-pointer passed via Python HDSLoc* loc = HDS_retrieve_locator(self); int status = SAI__OK; errBegin(&status); datAnnul(&loc, &status); if(raiseHDSException(&status)) return NULL; Py_RETURN_NONE; };
static PyObject* pydat_state(HDSObject *self, PyObject *args) { // Recover C-pointer passed via Python HDSLoc* loc = HDS_retrieve_locator(self); int status = SAI__OK, state; errBegin(&status); datState(loc, &state, &status); if (raiseHDSException(&status)) return NULL; return Py_BuildValue("i", state); };
static PyObject* pyndf_state(NDF *self, PyObject *args) { const char *comp; if(!PyArg_ParseTuple(args, "s:pyndf_state", &comp)) return NULL; int state, status = SAI__OK; errBegin(&status); ndfState(self->_ndfid, comp, &state, &status); if (raiseNDFException(&status)) return NULL; return PyBool_FromLong( state ); };
static PyObject* pydat_struc(HDSObject *self) { // Recover C-pointer passed via Python HDSLoc* loc = HDS_retrieve_locator(self); // guard against structures int state, status = SAI__OK; errBegin(&status); datStruc(loc, &state, &status); if (raiseHDSException(&status)) return NULL; return Py_BuildValue("i", state); };
static PyObject* pyndf_anorm(NDF *self, PyObject *args) { int iaxis; if(!PyArg_ParseTuple(args, "i:pyndf_anorm", &iaxis)) return NULL; int state, status = SAI__OK; errBegin(&status); int naxis = tr_iaxis(self->_ndfid, iaxis, &status); ndfAnorm(self->_ndfid, naxis, &state, &status); if (raiseNDFException(&status)) return NULL; return PyBool_FromLong( state ); };
static PyObject* pydat_ncomp(HDSObject *self) { // Recover C-pointer passed via Python HDSLoc* loc = HDS_retrieve_locator(self); int status = SAI__OK, ncomp; errBegin(&status); datNcomp(loc, &ncomp, &status); if (raiseHDSException(&status)) return NULL; return Py_BuildValue("i", ncomp); };
static int pyndf_cput_helper ( int ndfid, const char * comp, const char * value ) { int status = SAI__OK; errBegin(&status); if (value) { ndfCput(value, ndfid, comp, &status); } else { ndfReset( ndfid, comp, &status ); } if (raiseNDFException(&status)) return -1; return 0; }
static PyObject* pyndf_xstat(NDF *self, PyObject *args) { const char *xname; if(!PyArg_ParseTuple(args, "si:pyndf_xstat", &xname)) return NULL; int state, status = SAI__OK; errBegin(&status); ndfXstat(self->_ndfid, xname, &state, &status); if (raiseNDFException(&status)) return NULL; return PyBool_FromLong( state ); };
/* Routine shared by all code retrieving an NDF character component. */ static PyObject * pyndf_cget_helper( int ndfid, const char *comp ) { // Return None if component does not exist int state, status = SAI__OK; errBegin(&status); ndfState(ndfid, comp, &state, &status); if (raiseNDFException(&status)) return NULL; if(!state) Py_RETURN_NONE; int clen; errBegin(&status); ndfClen(ndfid, comp, &clen, &status); if (raiseNDFException(&status)) return NULL; char value[clen+1]; errBegin(&status); value[0] = 0; ndfCget(ndfid, comp, value, clen+1, &status); if (raiseNDFException(&status)) return NULL; return Py_BuildValue("s", value); }
static PyObject* pydat_type(HDSObject *self) { // Recover C-pointer passed via Python HDSLoc* loc = HDS_retrieve_locator(self); char typ_str[DAT__SZTYP+1]; int status = SAI__OK; errBegin(&status); datType(loc, typ_str, &status); if (raiseHDSException(&status)) return NULL; return Py_BuildValue("s", typ_str); };
static PyObject* pyndf_xname(NDF *self, PyObject *args) { int nex, nlen = 32; if(!PyArg_ParseTuple(args, "i|i:pyndf_xname", &nex, &nlen)) return NULL; char xname[nlen+1]; int status = SAI__OK; errBegin(&status); ndfXname(self->_ndfid, nex+1, xname, nlen+1, &status); if (raiseNDFException(&status)) return NULL; return Py_BuildValue("s", xname); };
static PyObject* pyndf_astat(NDF *self, PyObject *args) { const char *comp; int iaxis; if(!PyArg_ParseTuple(args, "si:pyndf_astat", &comp, &iaxis)) return NULL; int state, status = SAI__OK; errBegin(&status); int naxis = tr_iaxis(self->_ndfid, iaxis, &status); ndfAstat(self->_ndfid, comp, naxis, &state, &status); if (raiseNDFException(&status)) return NULL; return PyBool_FromLong( state ); };
static PyObject* pyndf_aform(NDF *self, PyObject *args) { const char *comp; int iaxis; if(!PyArg_ParseTuple(args, "si:pyndf_aform", &comp, &iaxis)) return NULL; int status = SAI__OK; int naxis = tr_iaxis(self->_ndfid, iaxis, &status); char value[30]; errBegin(&status); ndfAform(self->_ndfid, comp, naxis, value, 30, &status); if (raiseNDFException(&status)) return NULL; return Py_BuildValue("s", value); };
static PyObject* pyndf_xloc(NDF *self, PyObject *args) { const char *xname, *mode; if(!PyArg_ParseTuple(args, "ss:pyndf_xloc", &xname, &mode)) return NULL; HDSLoc* loc = NULL; int status = SAI__OK; errBegin(&status); ndfXloc(self->_ndfid, xname, mode, &loc, &status); if (raiseNDFException(&status)) return NULL; // PyCObject to pass pointer along to other wrappers PyObject *pobj = NpyCapsule_FromVoidPtr(loc, PyDelLoc); return Py_BuildValue("O", pobj); };
// write a primitive static PyObject* pydat_put(HDSObject *self, PyObject *args) { PyObject *value, *dimobj; PyArrayObject *npyval; const char* type; int ndim; if(!PyArg_ParseTuple(args,"siOO:pydat_put",&type,&ndim,&dimobj,&value)) return NULL; if(!checkHDStype(type)) return NULL; HDSLoc* loc = HDS_retrieve_locator(self); // create a pointer to an array of the appropriate data type if(strcmp(type,"_INTEGER") == 0) { npyval = (PyArrayObject*) PyArray_FROM_OTF(value, NPY_INT, NPY_IN_ARRAY | NPY_FORCECAST); } else if(strcmp(type,"_REAL") == 0) { npyval = (PyArrayObject*) PyArray_FROM_OTF(value, NPY_FLOAT, NPY_IN_ARRAY | NPY_FORCECAST); } else if(strcmp(type,"_DOUBLE") == 0) { npyval = (PyArrayObject*) PyArray_FROM_OTF(value, NPY_DOUBLE, NPY_IN_ARRAY | NPY_FORCECAST); } else if(strcmp(type,"_BYTE") == 0) { npyval = (PyArrayObject*) PyArray_FROM_OTF(value, NPY_BYTE, NPY_IN_ARRAY | NPY_FORCECAST); } else if(strcmp(type,"_UBYTE") == 0) { npyval = (PyArrayObject*) PyArray_FROM_OTF(value, NPY_UBYTE, NPY_IN_ARRAY | NPY_FORCECAST); } else if(strncmp(type,"_CHAR*",6) == 0) { npyval = (PyArrayObject*) PyArray_FROM_OT(value, NPY_STRING); } else { return NULL; } void *valptr = PyArray_DATA(npyval); int status = SAI__OK; errBegin(&status); if (ndim > 0) { // npydim is 1-D array stating the size of each dimension ie. npydim = numpy.array([1072 1072]) // these are stored in an hdsdim type (note these are declared as signed) PyArrayObject *npydim = (PyArrayObject*) PyArray_FROM_OTF(dimobj,NPY_INT,NPY_IN_ARRAY|NPY_FORCECAST); hdsdim *dims = (hdsdim*)PyArray_DATA(npydim); datPut(loc,type,ndim,dims,valptr,&status); Py_DECREF(npydim); } else { datPut(loc,type,0,0,valptr,&status); } if (raiseHDSException(&status)) return NULL; Py_DECREF(npyval); Py_RETURN_NONE; }
static PyObject* pydat_putc(HDSObject *self, PyObject *args) { PyObject *strobj,*locobj; int strlen; if(!PyArg_ParseTuple(args,"Oi:pydat_putc",&strobj,&strlen)) return NULL; HDSLoc *loc = HDS_retrieve_locator(self); PyArrayObject *npystr = (PyArrayObject*) PyArray_FROM_OTF(strobj,NPY_STRING,NPY_FORCECAST); char *strptr = PyArray_DATA(npystr); int status = SAI__OK; errBegin(&status); datPutC(loc,0,0,strptr,(size_t)strlen,&status); if (raiseHDSException(&status)) return NULL; Py_DECREF(npystr); Py_RETURN_NONE; }