source: nappy/trunk/nappy/listManipulator.py @ 3314

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/nappy/trunk/nappy/listManipulator.py@3314
Revision 3314, 2.7 KB checked in by astephen, 12 years ago (diff)

Re-factored under a nappy directory for a start.

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
21
22def recursiveListWriter(inlist, dimlist, spacer="    ", floatFormat="%8.3f"):
23    """
24    Function wrapper around class method RecursiveListManipulator().writeLines().
25    """
26    return RecursiveListManipulator().writeLines(inlist, dimlist, spacer=spacer, floatFormat=floatFormat)
27
28
29class RecursiveListManipulator:
30
31    """
32    Methods to convert a 1-D array into a multi-dimensional list.
33    """
34
35    def populator(self, inlist, array, dimlist):   
36        """
37        Populates the list object 'inlist' (e.g. []) with sublists of
38        dimensionality defined in the 'dimlist' list of dimensions (e.g [181, 360]).
39        At the deepest level it then inserts values from the long list
40        'array'.
41        """
42
43        if not hasattr(self, "_counter"):
44            self._counter=0
45        if len(dimlist[1:])>0:
46            for i in range(dimlist[0]):
47                inlist.append([])
48                self.populator(inlist[i], array, dimlist[1:])
49        else:
50            count=self._counter
51            self._counter=self._counter+dimlist[0]
52            endcount=self._counter
53            for i in range(count, endcount):
54                inlist.append(array[i])
55        return inlist
56
57    def writeLines(self, inlist, dimlist, spacer="    ", floatFormat="%8.3f"):
58        """
59        Method to walk through all the levels of the multi-level list object
60        'inlist' and writes out appropriate values to a list of lines called
61        'self.rtlines'. 'dimlist' is a list of the dimensions within 'inlist'.
62        """
63        if not hasattr(self, "rtlines"):
64            self.rtlines=[]
65        if len(dimlist[1:])>0:
66            for i in range(dimlist[0]):
67                self.writeLines(inlist[i], dimlist[1:], spacer=spacer, floatFormat=floatFormat)
68        else:
69            var_string=""
70            for i in range(dimlist[0]):
71                var_string=var_string+((floatFormat+spacer) % inlist[i])
72            self.rtlines.append("%s\n" % var_string.rstrip())
73        return self.rtlines
74           
Note: See TracBrowser for help on using the repository browser.