source: nappy/tags/NDG0-1/listManipulator.py @ 349

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/nappy/tags/NDG0-1/listManipulator.py@3005
Revision 349, 2.6 KB checked in by selatham, 15 years ago (diff)

Inserted license information.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#   Copyright (C) 2004 CCLRC & NERC( Natural Environment Research Council ).
2#   This software may be distributed under the terms of the
3#   Q Public License, version 1.0 or later. http://ndg.nerc.ac.uk/public_docs/QPublic_license.txt
4
5"""
6listManipulator.py
7===================
8
9A container module for RecursiveListManipulator classes that transform
10arrays into multi-dimensional list objects. Wrapper functions are
11provided to call these directly as functions.
12
13"""
14
15def recursiveListPopulator(inlist, array, dimlist):
16    """
17    Function wrapper around class method RecursiveListManipulator().populator().
18    """
19    return RecursiveListManipulator().populator(inlist, array, dimlist)
20
21def recursiveListWriter(inlist, dimlist, floatFormat="%s"):
22    """
23    Function wrapper around class method RecursiveListManipulator().writeLines().
24    """
25    return RecursiveListManipulator().writeLines(inlist, dimlist, floatFormat)
26
27
28class RecursiveListManipulator:
29
30    """
31    Methods to convert a 1-D array into a multi-dimensional list.
32    """
33
34    def populator(self, inlist, array, dimlist):   
35        """
36        Populates the list object 'inlist' (e.g. []) with sublists of
37        dimensionality defined in the 'dimlist' list of dimensions (e.g [181, 360]).
38        At the deepest level it then inserts values from the long list
39        'array'.
40        """
41
42        if not hasattr(self, "_counter"):
43            self._counter=0
44        if len(dimlist[1:])>0:
45            for i in range(dimlist[0]):
46                inlist.append([])
47                self.populator(inlist[i], array, dimlist[1:])
48        else:
49            count=self._counter
50            self._counter=self._counter+dimlist[0]
51            endcount=self._counter
52            for i in range(count, endcount):
53                inlist.append(array[i])
54        return inlist
55
56    def writeLines(self, inlist, dimlist, floatFormat="%s"):
57        """
58        Method to walk through all the levels of the multi-level list object
59        'inlist' and writes out appropriate values to a list of lines called
60        'self.rtlines'. 'dimlist' is a list of the dimensions within 'inlist'.
61        """
62        if not hasattr(self, "rtlines"):
63            self.rtlines=[]
64        if len(dimlist[1:])>0:
65            for i in range(dimlist[0]):
66                self.writeLines(inlist[i], dimlist[1:], floatFormat)
67        else:
68            var_string=""
69            for i in range(dimlist[0]):
70                var_string=var_string+((floatFormat+"    ") % inlist[i])
71            self.rtlines.append("%s\n" % var_string.rstrip())
72        return self.rtlines
73           
Note: See TracBrowser for help on using the repository browser.