Exemplo n.º 1
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
Exemplo n.º 2
0
 def tearDown(self):
     ndf.end()
     os.remove( self.testndf )