source: nappy/trunk/nappy/utils/list_manipulator.py @ 3336

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/nappy/trunk/nappy/utils/list_manipulator.py@3336
Revision 3336, 3.4 KB checked in by astephen, 13 years ago (diff)
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
9Contains a set of list-related functions and the RecursiveListManipulator
10class that is wrapped in nice functions. This class transforms
11arrays into multi-dimensional list objects. Wrapper functions are
12provided to call these directly as functions.
13
14"""
15
16def arrayToList(array, inlist):
17    """
18    Takes an n-dimensional Numeric array and converts it to an
19    n-dimensional list object.
20    """
21    dimlist = array.shape
22    if len(dimlist[1:])>0:
23        for i in range(dimlist[0]):
24            arrayToList(inlist[i], array[i])
25    else:
26        for i in range(dimlist[0]):
27            inlist.append(array[i])
28    return inlist
29
30
31def listOfListsCreator(inlist, dimlist):
32    """
33    Creates a list of lists with dimensions defined in dimlist (a list of integers).
34    """
35    if len(dimlist[1:]) > 0:
36        for i in range(dimlist[0]):
37            inlist.append([])
38            listOfListsCreator(inlist[i], array, dimlist[1:])
39    return inlist
40
41
42def recursiveListPopulator(inlist, array, dimlist):
43    "Function wrapper around class method RecursiveListManipulator().populator()."
44    return RecursiveListManipulator().populator(inlist, array, dimlist)
45
46
47def recursiveListWriter(inlist, dimlist, spacer="    ", floatFormat="%8.3f"):
48    "Function wrapper around class method RecursiveListManipulator().writeLines()."
49    return RecursiveListManipulator().writeLines(inlist, dimlist, spacer=spacer, floatFormat=floatFormat)
50
51
52class RecursiveListManipulator:
53    """
54    Container class with methods to convert a 1-D array into a multi-dimensional list.
55    """
56
57    def populator(self, inlist, array, dimlist):   
58        """
59        Populates the list object 'inlist' (e.g. []) with sublists of
60        dimensionality defined in the 'dimlist' list of dimensions (e.g [181, 360]).
61        At the deepest level it then inserts values from the long list
62        'array'.
63        """
64
65        if not hasattr(self, "_counter"):
66            self._counter=0
67        if len(dimlist[1:])>0:
68            for i in range(dimlist[0]):
69                inlist.append([])
70                self.populator(inlist[i], array, dimlist[1:])
71        else:
72            count=self._counter
73            self._counter=self._counter+dimlist[0]
74            endcount=self._counter
75            for i in range(count, endcount):
76                inlist.append(array[i])
77        return inlist
78
79    def writeLines(self, inlist, dimlist, spacer="    ", floatFormat="%8.3f"):
80        """
81        Method to walk through all the levels of the multi-level list object
82        'inlist' and writes out appropriate values to a list of lines called
83        'self.rtlines'. 'dimlist' is a list of the dimensions within 'inlist'.
84        """
85        if not hasattr(self, "rtlines"):
86            self.rtlines=[]
87        if len(dimlist[1:])>0:
88            for i in range(dimlist[0]):
89                self.writeLines(inlist[i], dimlist[1:], spacer=spacer, floatFormat=floatFormat)
90        else:
91            var_string=""
92            for i in range(dimlist[0]):
93                var_string=var_string+((floatFormat+spacer) % inlist[i])
94            self.rtlines.append("%s\n" % var_string.rstrip())
95        return self.rtlines
96           
Note: See TracBrowser for help on using the repository browser.