Esempio n. 1
0
    def test_simplenew(self):
        # okay we have all the data, time to open us up an ndf
        indf = ndf.open(self.testndf,'WRITE','NEW')
        newindf = indf.new('_REAL',2,
                           numpy.array([0,0]),numpy.array([4,4]))

        # map primary data to make sure NDF does not complain
        ptr,el = newindf.map('DATA','_REAL','WRITE')

        # make sure we got a file
        self.assertTrue( os.path.exists( self.testndf ), "Test existence of NDF file" )
Esempio n. 2
0
    def test_newwithwrite(self):
        # okay we have all the data, time to open us up an ndf
        indf = ndf.open(self.testndf,'WRITE','NEW')
        newindf = indf.new('_REAL',2,
                           numpy.array([0,0]),numpy.array([4,4]))

        # create PAMELA extension
        loc = newindf.xnew('PAMELA','STRUCT')

        hdsloc = hds._transfer(loc)
        name = hdsloc.name()
        self.assertEqual( name, "PAMELA" )

        ccd = numpy.zeros([5,5])

        # map primary data
        ptr,el = newindf.map('DATA','_REAL','WRITE')
        ndf.ndf_numpytoptr(ccd,ptr,el,'_REAL')

        # shut down ndf system
        newindf.annul()

        # make sure we got a file
        self.assertTrue( os.path.exists( self.testndf ), "Test existence of NDF file" )
Esempio n. 3
0
 def test_filnf(self):
     with self.assertRaises(IOError):
         indf = ndf.open('shouldnotbepresent','READ','OLD')
Esempio n. 4
0
 def test_badstate(self):
     with self.assertRaises(ValueError):
         indf = ndf.open('badmode', 'READ', 'VERYOLD')
Esempio n. 5
0
 def test_badmode(self):
     with self.assertRaises(ValueError):
         indf = ndf.open('badmode', 'UNKNOWN', 'OLD')
Esempio n. 6
0
    def __init__(self, fname):
        """
        Initialise an NDF from a file.

        This slurps the whole thing in, including all extensions, axes etc. This could
        cause memory problems on large files. You can use either standard format
        NDF sections in such case or Pythonic ones. e.g. Given an NDF 'image' listed by
        hdstrace to have a data array DATA(3,4,5), the entire image can be specified
        using any of 'image', 'image(1:3,1:4,1:5)' or 'image[0:5,0:4,0:3]' where as usual
        with the index ranges in Python, the last index is NOT included and indices start
        at 0. While those used to dealing with NDFs may find the first with () more
        familiar, the second may be simpler if you deal with the outout from a python script
        since it is identically ordered and more consistent with taking sub-sections using
        Python as in

        ndf = starlink.ndf.Ndf('image')
        subim = image.data[0:5,0:4,0:3]

        The following attributes are created:

        data    -- the data array, a numpy N-d array
        bound   -- pixel limits of data array. 2xndim array of lower and upper bounds
        var     -- variances, a numpy N-d array
        axes    -- Axis components
        label   -- label string
        title   -- title string
        units   -- data unit string
        head    -- header/extensions, a dictionary
        """
        object.__init__(self)

        # Next section changes from a pseudo-Pythonic version of an NDF section
        # to a Fortran-like one i.e. reverse the indices, add 1 to the first of a pair
        # or to the sole index
        reg = re.compile(r'([^\[\]]*)\[([^\[\]]*)\]')
        m = reg.match(fname)
        if m != None:
            tup = m.group(2).split(',')
            nname = m.group(1) + '('
            for st in tup[-1:0:-1]:
                subt = st.split(':')
                if len(subt) == 1:
                    add = str(int(subt[0])+1)
                elif len(subt) == 2:
                    add = str(int(subt[0])+1) + ':' + str(int(subt[1]))
                else:
                    raise Exception('Could not understand ' + fname)
                nname += add + ','
            subt = tup[0].split(':')
            if len(subt) == 1:
                add = str(int(subt[0])+1)
            elif len(subt) == 2:
                add = str(int(subt[0])+1) + ':' + str(int(subt[1]))
            else:
                raise Exception('Could not understand ' + nname)
            nname += add + ')'
            fname = nname

        # OK, get on with NDF stuff
        ndf.init()
        ndf.begin()
        try:
            indf = ndf.open(fname)
            self.data  = indf.read('Dat')
            self.bound = indf.bound()
            self.var   = indf.read('Var')
            self.label = indf.cget('Label')
            self.title = indf.cget('Title')
            self.units  = indf.cget('Units')

            # Read the axes
            self.axes = []
            for nax in range(self.data.ndim):
                self.axes.append(Axis(indf, nax))

            # Read the extensions
            self.head = {}
            nextn = indf.xnumb()
            for nex in range(nextn):
                xname = indf.xname(nex)
                loc1 = indf.xloc(xname, 'READ')
                hdsloc = hds._transfer(loc1)
                _read_hds(hdsloc, self.head)
                hdsloc.annul()

            ndf.end()
        except:
            ndf.end()
            raise