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); };
IDL_VPTR hds2idl( int argc, IDL_VPTR argv[] ) { /* ** Declare variables */ IDL_VPTR hds_name; /* IDL_VPTR to name of the HDS object to be read */ IDL_VPTR var; /* Variable pointer to IDL object */ IDL_VARIABLE temp; /* Temporary storage for primitive scalar variable */ IDL_StructDefPtr sdef; /* Structure definition of sub-structure */ IDL_STRING *objname; /* Pointer to object name as IDL string */ HDSLoc *objloc = NULL; /* Locator of file */ int status; /* Starlink status */ char type[DAT__SZTYP+1];/* HDS type of component */ UCHAR idltype; /* IDl type of component */ int ndims; /* Number of dimensions of object */ int dims[DAT__MXDIM]; /* Dimensions of object HDS style */ IDL_LONG idldims[DAT__MXDIM];/* Dimensions of object IDL style */ int i; /* loop index */ int fstat; /* Final status (before emsEload) */ int isstruct; /* Whether object is structure */ void *tdata; /* Pointer to data area of IDL variable or array */ char param[EMS__SZPAR+1]; /* Error message parameter name */ int parlen; /* Length of error message parameter name */ char opstr[EMS__SZMSG+1]; /* Error message */ int oplen; /* Length of error message */ IDL_LONG one[IDL_MAX_ARRAY_DIM]={1}; /* Start Error context */ status = SAI__OK; emsMark(); /* Check that the correct number of arguments were passed in */ if(argc != 1) { /* Print an error message and return */ status = SAI__ERROR; emsRep( " ", "hds2idl: Incorrect number of arguments", &status ); } else { /* Extract the arguments to comprehensible names */ hds_name = argv[0]; objname = &hds_name->value.str; /* Open the HDS object */ getcomp( IDL_STRING_STR(objname), "READ", &objloc, &status ); /* Check for structure or primitive */ datStruc( objloc, &isstruct, &status ); if (status == SAI__OK) { if ( isstruct ) { /* Create a structure */ sdef = idlstructdef( objloc, &status ); /* Create a temporary variable */ if ( status == SAI__OK ) { (void *)IDL_MakeTempStruct( sdef, 1, one, &var, TRUE ); idlstructfill( objloc, var->value.s, &status ); } } else { /* Object is primitive */ datType( objloc, type, &status ); idltype = getidltype( type ); datShape( objloc, DAT__MXDIM, dims, &ndims, &status ); if ( status == SAI__OK ) { if (ndims) { /* Get dimensions IDL style */ for (i=0;i<ndims;i++) idldims[i] = (IDL_LONG)dims[i]; /* Object is primitive array */ tdata = IDL_MakeTempArray( (int)idltype, ndims, idldims, IDL_BARR_INI_ZERO , &var ); } else { /* Object is primitive scalar */ var = &temp; var->type = idltype; var->flags = 0; tdata = &var->value; } idlprimfill( objloc, var, tdata, &status ); } } /* Annul the object (and close the file) */ datAnnul( &objloc, &status ); } } if ( status != SAI__OK ) { /* Report any error messages */ /* Adding Starlink-style !! and ! prefix */ fstat = status; while ( status != SAI__OK ) { emsEload( param, &parlen, opstr, &oplen, &status ); if ( status != SAI__OK ) IDL_Message( IDL_M_NAMED_GENERIC, IDL_MSG_INFO, opstr ); } /* Set to return undefined variable */ var = IDL_Gettmp(); /* and close error context */ emsRlse(); } /* That's it, return to the calling routine */ return var; }
static PyObject* pydat_get(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; if(state) { PyErr_SetString(PyExc_IOError, "dat_get error: cannot use on structures"); return NULL; } // get type char typ_str[DAT__SZTYP+1]; datType(loc, typ_str, &status); // get shape const int NDIMX=7; int ndim; hdsdim tdim[NDIMX]; datShape(loc, NDIMX, tdim, &ndim, &status); if (raiseHDSException(&status)) return NULL; PyArrayObject* arr = NULL; // Either return values as a single scalar or a numpy array // Reverse order of dimensions npy_intp rdim[NDIMX]; int i; for(i=0; i<ndim; i++) rdim[i] = tdim[ndim-i-1]; if(strcmp(typ_str, "_INTEGER") == 0 || strcmp(typ_str, "_LOGICAL") == 0) { arr = (PyArrayObject*) PyArray_SimpleNew(ndim, rdim, NPY_INT); } else if(strcmp(typ_str, "_REAL") == 0) { arr = (PyArrayObject*) PyArray_SimpleNew(ndim, rdim, NPY_FLOAT); } else if(strcmp(typ_str, "_DOUBLE") == 0) { arr = (PyArrayObject*) PyArray_SimpleNew(ndim, rdim, NPY_DOUBLE); } else if(strncmp(typ_str, "_CHAR", 5) == 0) { // work out the number of bytes size_t nbytes; datLen(loc, &nbytes, &status); if (status != SAI__OK) goto fail; int ncdim = 1+ndim; int cdim[ncdim]; cdim[0] = nbytes+1; for(i=0; i<ndim; i++) cdim[i+1] = rdim[i]; PyArray_Descr *descr = PyArray_DescrNewFromType(NPY_STRING); descr->elsize = nbytes; arr = (PyArrayObject*) PyArray_NewFromDescr(&PyArray_Type, descr, ndim, rdim, NULL, NULL, 0, NULL); } else if(strcmp(typ_str, "_WORD") == 0) { arr = (PyArrayObject*) PyArray_SimpleNew(ndim, rdim, NPY_SHORT); } else if(strcmp(typ_str, "_UWORD") == 0) { arr = (PyArrayObject*) PyArray_SimpleNew(ndim, rdim, NPY_USHORT); } else if(strcmp(typ_str, "_BYTE") == 0) { arr = (PyArrayObject*) PyArray_SimpleNew(ndim, rdim, NPY_BYTE); } else if(strcmp(typ_str, "_UBYTE") == 0) { arr = (PyArrayObject*) PyArray_SimpleNew(ndim, rdim, NPY_UBYTE); } else { PyErr_SetString(PyExc_IOError, "dat_get: encountered an unimplemented type"); return NULL; } if(arr == NULL) goto fail; datGet(loc, typ_str, ndim, tdim, arr->data, &status); if(status != SAI__OK) goto fail; return PyArray_Return(arr); fail: raiseHDSException(&status); Py_XDECREF(arr); return NULL; };