int datCcopy(const HDSLoc *locator1, const HDSLoc *locator2, const char *name, HDSLoc **locator3, int *status ) { char type_str[DAT__SZTYP+1]; hdsdim hdims[DAT__MXDIM]; int ndims; if (*status != SAI__OK) return *status; /* Validate input locators. */ dat1ValidateLocator( "datCcopy", 1, locator1, 1, status ); dat1ValidateLocator( "datCcopy", 1, locator2, 0, status ); if (dat1IsStructure(locator1, status)) { /* need the type and dimensionality of the structure to create in new location */ datType( locator1, type_str, status ); datShape( locator1, DAT__MXDIM, hdims, &ndims, status ); *locator3 = dat1New( locator2, 0, name, type_str, ndims, hdims, status ); } else { hdsbool_t state = 0; /* We only copy if the primitive object is defined */ datState( locator1, &state, status ); if ( state ) { datCopy( locator1, locator2, name, status ); } else { /* Undefined so just make something of the right shape and type */ datType( locator1, type_str, status ); datShape( locator1, DAT__MXDIM, hdims, &ndims, status ); datNew( locator2, name, type_str, ndims, hdims, status ); } /* and get a locator to the copied entity */ datFind( locator2, name, locator3, status ); } return *status; }
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); };
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; }
void idlprimfill( HDSLoc *cloc, IDL_VPTR datav, void *datptr, int *status ) { int j; /* loop counters */ UCHAR idltype; /* The IDL type */ char type[DAT__SZTYP+1]; /* Type in which to to map HDS data */ int bpix; /* Number of bytes/value */ int defined; /* If HDS value defined */ void *cpntr; /* True C pointer to mapped data */ size_t nels; /* Number of mapped elements */ int nels_i; size_t nbytes; /* Number of bytes in array */ int flen; /* length of HDS strings */ int clen; /* length of corresponding C string */ char *chars; /* pointer to imported characters */ IDL_STRING *strings; /* pointer to array of string structures */ IDL_VPTR chptr; /* Scratch variable pointer */ if ( *status != SAI__OK ) return; /* check type compatibility */ /* Get the number of bytes per element */ /* and the HDS type in which to map the data */ idltype = datav->type; switch (idltype) { case IDL_TYP_FLOAT: strcpy( type, "_REAL" ); bpix = 4; break; case IDL_TYP_LONG: strcpy( type, "_INTEGER" ); bpix = 4; break; case IDL_TYP_INT: strcpy( type, "_WORD" ); bpix = 2; break; case IDL_TYP_DOUBLE: strcpy( type, "_DOUBLE" ); bpix = 8; break; case IDL_TYP_BYTE: strcpy( type, "_UBYTE" ); bpix = 1; break; case IDL_TYP_STRING: datType( cloc, type, status); bpix = 1; break; default: /* flag no data to copy */ bpix = 0; *status = SAI__ERROR; emsSeti( "TYPE", idltype ); emsRep( " ", "Illegal IDL type ^TYPE", status ); break; } /* end of case */ if ( (*status == SAI__OK ) && bpix ) { /* Map the data as if a vector - provided it is defined */ datState( cloc, &defined, status ); if ( defined ) { datMapV( cloc, type, "READ", &cpntr, &nels, status ); if ( *status != SAI__OK ) { emsRep(" ", "Failed to map HDS component", status ); } else { if ( idltype == IDL_TYP_STRING ) { flen = atoi( type + 6 ); clen = flen + 1; /* Import the Fortran strings to C */ nels_i = (int)nels; chars = IDL_GetScratch( &chptr, nels_i, clen ); cnfImprta( cpntr, flen, chars, clen, 1, &nels_i ); /* set strings to be a pointer to the IDL_STRING structure(s) */ strings = (IDL_STRING *)datptr; /* store the imported strings into the STRING structures */ for ( j=0; j<nels; j++ ) IDL_StrStore( strings+j, &chars[j*clen] ); IDL_Deltmp( chptr ); } else { /* Type other than string */ if ( datav->flags & IDL_V_ARR ) { /* Number Array */ /* copy the data to the array */ nbytes = bpix * nels; memcpy( datptr, cpntr, nbytes ); } else { /* Number Scalar */ switch (idltype) { case IDL_TYP_FLOAT: ((IDL_ALLTYPES *)datptr)->f = *(float *)cpntr; break; case IDL_TYP_LONG: ((IDL_ALLTYPES *)datptr)->l = *(int *)cpntr; break; case IDL_TYP_INT: ((IDL_ALLTYPES *)datptr)->i = *(short *)cpntr; break; case IDL_TYP_DOUBLE: ((IDL_ALLTYPES *)datptr)->d = *(double *)cpntr; break; case IDL_TYP_BYTE: ((IDL_ALLTYPES *)datptr)->c = *(UCHAR *)cpntr; break; } /* end of case */ } /* end of if array */ } /* end if string */ datUnmap( cloc, status ); } /* end of mapped data */ } /* end of if defined */ } /* end of bpix non-zero */ return; }
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; };