Skip to content

Commit e610ae0

Browse files
authored
Merge pull request #10 from UV-CDAT/doc
Doc
2 parents f2edadc + f30ecb2 commit e610ae0

File tree

14 files changed

+1730
-1037
lines changed

14 files changed

+1730
-1037
lines changed

Lib/ASCII.py

Lines changed: 79 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,40 @@ def make_var(lap,id=None,shape=None):
2121
lap.id=id
2222
return lap
2323

24+
25+
# TODO: need an actual 'vars.txt' file for the doctests
2426
def readAscii( text_file ,header=0, ids=None, shape=None, next='------',separators=[';',',',':']):
25-
"""Reads data from an ascii file
26-
Usage :::
27-
vars = genutil.ASCII.readAscii( text_file ,header=0, ids=None, shape=None, next='------',separators=[';',',',':'])
28-
:::
29-
30-
Options :::
31-
text_file :: ASCII File to read from.
32-
header :: (0) Number of header lines, these lines will be skipped.
33-
ids :: (None) use the values in this list as variable ids (1 per variable returned)
34-
shape :: (None) use the tuple/list in this list as the final shape of the variable read.
35-
next :: ('------') character string marking separation between variables
36-
separators :: ([';',',', ':']) List of character recognized as column separator
37-
Output :::
38-
vars :: List containing transient(s) variable(s) possibly named after ids and reshaped from the 'shape' option.
39-
27+
"""
28+
Reads data from an ascii file to generate a list of transient(s)/varable(s)
29+
30+
:Example:
31+
32+
.. doctest:: genutil_ASCII_readascii
33+
34+
>>> vars=genutil.ASCII.readAscii("vars.txt") # use default params
35+
36+
:param text_file: A string, containing the path to an ASCII File to read from.
37+
:type text_file: str
38+
39+
:param header: Number of header lines, these lines will be skipped.
40+
:type header: int
41+
42+
:param ids: List of values to use as variable ids (1 per variable returned)
43+
:type ids: list
44+
45+
:param shape: use the tuple/list in this list as the final shape of the variable read.
46+
:type shape: tuple or list
47+
48+
:param next: character string marking separation between variables (i.e. '------')
49+
:type next: str
50+
51+
:param separators: ([';',',', ':']) List of characters recognized as column
52+
separators. Can be represented as a list or a string.
53+
If it is a string, separator characters must be space-delimited.
54+
:type separators: list or str
55+
56+
:returns: List containing transient(s) variable(s) possibly named after ids and reshaped from the 'shape' option.
57+
:rtype: list
4058
"""
4159
sep=[]
4260
if isinstance(separators,str):
@@ -82,24 +100,49 @@ def readAscii( text_file ,header=0, ids=None, shape=None, next='------',separato
82100
return vars[0]
83101

84102

85-
def read_col( text_file ,header=0, cskip=0, cskip_type='columns', axis=False, ids=None, idrow=0, separators=[';',',', ':']):
86-
""" Reads column-stored data from ASCII files
87-
Usage:::
88-
vars = genutil.ASCII.read_col( text_file ,header=0, cskip=0, cskip_type='columns', axis=False, ids=None, idrow=False, separators=[';',',', ':'])
89-
90-
Options :::
91-
text_file :: ASCII File to read from.
92-
header :: (0) Number of header lines, these lines will be skipped.
93-
cskip :: (0) Number of 'column'/'character' to skip (dummy column)
94-
cskip_type :: ('column') is 'cskip' a number of 'column' or 'character' to skip?
95-
axis :: (False) Use as the values for the first column as variable axis (x values in y(x))
96-
idrow :: (False) Is the first row representing the ids of var generated ?
97-
ids :: (None) use the values in this list as variable ids (1 per column returned)
98-
separators :: ([';',',', ':']) List of character recognized as column separator
99-
Output :::
100-
vars :: List containing 1 transient varialbe per column in the files.
101-
Variable ids are optionaly determined by first row.
102-
Variable axis may be the first column
103+
def read_col( text_file ,header=0, cskip=0, cskip_type='columns', axis=False, ids=None, idrow=0,
104+
separators=[';',',', ':']):
105+
"""
106+
Reads column-stored data from ASCII files
107+
108+
:Example:
109+
110+
.. doctest:: genutil_ASCII_read_col
111+
112+
>>> vars = genutil.ASCII.read_col("vars.txt") # use default params
113+
114+
115+
:param text_file: ASCII File to read from.
116+
:type text_file:
117+
118+
:param header: Number of header lines, these lines will be skipped.
119+
:type header: int
120+
121+
:param cskip: Number of 'column'/'character' to skip (dummy column)
122+
:type cskip: int
123+
124+
:param cskip_type: One of 'columns' or 'characters'. Specifies which should be skipped.
125+
:type cskip_type: str
126+
127+
:param axis: Boolean flag indicating whether to use as the values for the first column as
128+
variable axis (x values in y(x)).
129+
:type axis: bool
130+
131+
:param idrow: Is the first row representing the ids of var generated.
132+
:type idrow:
133+
134+
:param ids: (None) use the values in this list as variable ids (1 per column returned)
135+
:type ids:
136+
137+
:param separators: ([';',',', ':']) List of characters recognized as column
138+
separator. Can be represented as a list or a string.
139+
If it is a string, separator characters must be space-delimited.
140+
:type separators: list or str
141+
142+
:returns: List containing 1 transient variable per column in the files.
143+
Variable ids are optionally determined by first row.
144+
Variable axis may be the first column.
145+
:rtype: list
103146
"""
104147

105148
sep=[]
@@ -108,9 +151,9 @@ def read_col( text_file ,header=0, cskip=0, cskip_type='columns', axis=False, id
108151
for s in separators:
109152
sep.append(s)
110153

111-
f=open( text_file )
112-
lst = f.readlines( )
113-
f.close( )
154+
f=open(text_file)
155+
lst = f.readlines()
156+
f.close()
114157
lst=lst[header:]
115158
if not isinstance(ids,(tuple,list)):
116159
ids=[ids]

Lib/Filler.py

Lines changed: 80 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,40 @@
11
import cdat_info
22
class StringConstructor:
3-
""" This Class aims at spotting keyword in string and replacing them
4-
Usage
5-
Filler=StringConstructor(template)
6-
or
7-
Filler=StringConstructor()
8-
Filler.template=template
9-
10-
template is a string of form
11-
template = 'my string here with %(keywords) in it'
12-
13-
You can have has many 'keyword' as you want, and use them as many times as you want
14-
keywords are delimited on the left by %( and ) on the right
15-
16-
In order to construct the string (i.e. replace keywrods with some values:
3+
"""
4+
This class aims at spotting keywords in a string and replacing them
175
18-
str=Filler(keyword='kwstr')
19-
or
20-
Filler.keyword='kwstr'
21-
str=Filler()
6+
:Usage:
227
23-
Example:
24-
structure='/pcmdi/amip/mo/%(variable)/%(model)/%(variable)_%(model).xml'
25-
Filler=StringConstructor(structure)
26-
Filler.variable='tas'
27-
myfilename=Filler.construct(structure,model='ugamp-98a')
8+
.. code-block:: python
289
29-
print myfilename # '/pcmdi/amip/mo/tas/ugamp-98a/tas_ugamp-98a.xml'
30-
10+
>>> template = "templates are strings containing any number of %(keywords) using %(this_format)"
11+
>>> Filler=StringConstructor(template)
12+
# or
13+
>>> Filler=StringConstructor()
14+
>>> Filler.template=template
15+
# In order to construct the string (i.e. replace keywords with some values):
16+
>>> str=Filler(keywords='keywdstr',this_format='')
17+
# or
18+
>>> Filler.keyword='kwstr'
19+
>>> Filler.this_format=''
20+
>>> str=Filler()
21+
22+
23+
24+
template is a string of form: 'my string here with %(keywords) in it'
25+
26+
You can have has many keywords as you want, and use them as many times as you want.
27+
Keywords are delimited on the left by %( and on the right by ).
3128
"""
3229
def __init__(self,template=None):
30+
"""
31+
Instantiates a StringConstructor object.
32+
:param template: A string used by StringConstructor for keyword
33+
replacement. template is a string of form:
34+
'my string here with %(keywords) in it'.
35+
There can be an unlimited number of keywords, delimited by %( on
36+
the left and ) on the right.
37+
"""
3338
cdat_info.pingPCMDIdb("cdat","genutil.StringConstructor")
3439
self.template=template
3540
## ok we need to generate the keys and set them to empty it seems like a better idea
@@ -59,16 +64,29 @@ def keys(self,template=None):
5964

6065
def construct(self,template=None,**kw):
6166
"""
62-
construct, accepts a string with a unlimited number of keyword to replace
63-
keyword to replace must be in the format %(keyword) within the string
64-
keyword value are either passed as keyword to the construct function or preset
65-
Example:
66-
structure='/pcmdi/amip/mo/%(variable)/%(model)/%(variable)_%(model).xml'
67-
Filler=StringConstructor()
68-
Filler.variable='tas'
69-
myfilename=Filler.construct(structure,model='ugamp-98a')
70-
71-
print myfilename
67+
Accepts a string with an unlimited number of keywords to replace.
68+
Keywords to replace must be in the format %(keyword) within the string.
69+
Keyword values are either passed as keyword to the construct function or preset.
70+
71+
:Example:
72+
73+
.. doctest:: Filler_construct
74+
75+
>>> structure='/pcmdi/amip/mo/%(variable)/%(model)/%(variable)_%(model).xml'
76+
>>> Filler=StringConstructor()
77+
>>> Filler.variable='tas'
78+
>>> myfilename=Filler.construct(structure,model='ugamp-98a')
79+
>>> print myfilename
80+
'/pcmdi/amip/mo/tas/ugamp-98a/tas_ugamp-98a.xml'
81+
82+
:param template: A string used by StringConstructor for keyword replacement.
83+
template is a string of form: 'my string here with %(keywords) in it'.
84+
There can be an unlimited number of keywords, delimited by %( on the left and ) on the right.
85+
:type template: str
86+
87+
:param kw: Comma-delimited list of keyword to string value mappings, i.e.:
88+
keyword1='kwd1 string',keyword2='kwd2 string', ...
89+
:type kw: list
7290
"""
7391
if template is None:
7492
template=self.template
@@ -82,6 +100,31 @@ def construct(self,template=None,**kw):
82100
return template
83101

84102
def reverse(self,name,debug=False):
103+
"""
104+
The reverse function attempts to take a template and derive its keyword values based on name parameter.
105+
106+
:Example:
107+
108+
.. doctest:: Filler_reverse
109+
110+
>>> Filler=StringConstructor(template="%(a).%(b)")
111+
>>> Filler.reverse("A.B")
112+
{a:"A", b:"B"}
113+
114+
:param name: String to test the template's keyword values.
115+
:type name: str
116+
117+
:param debug: Boolean flag to indicate whether or not to print debug output.
118+
:type debug: bool
119+
120+
:returns: A dictionary mapping the StringConstructor's template's keywords to the corresponding values,
121+
according to the format of the name parameter.
122+
:rtype: dict
123+
124+
.. warning::
125+
126+
reverse makes its best effort at deriving keyword values from a string, but it is not guaranteed to work.
127+
"""
85128
out={}
86129
template = self.template
87130
for k in self.keys():
@@ -121,11 +164,6 @@ def reverse(self,name,debug=False):
121164
def __call__(self,*args,**kw):
122165
"""default call is construct function"""
123166
return self.construct(*args,**kw)
124-
125-
Filler=StringConstructor()
126167

127-
if __name__=='__main__':
128-
Filler.variable='tas'
129-
structure='/pcmdi/amip/mo/%(variable)/%(model)/%(variable)_%(model).xml'
130-
myfilename=Filler.construct(structure,model='*')
131-
print myfilename
168+
169+
Filler=StringConstructor()

Lib/Statusbar_Pmw.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Pmw, Tkinter
22
class Statusbar(Pmw.MegaWidget):
3-
""" Megawidget containing a scale and an indicator.
3+
"""
4+
Megawidget containing a scale and an indicator.
45
"""
56

67
def show(self,value):

Lib/arrayindexing.py

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,29 @@
66
def get(Array,Indices,axis=0):
77
"""
88
Arrayrrayindexing returns Array[Indices], indices are taken along dimension given with axis
9-
10-
Usage:
11-
C=get(Array,Indices,axis=0) # i.e. C=Array[Indices]
12-
Indices accepts negative value ,e.g: -1 is last element
9+
10+
:Example:
11+
12+
.. doctest:: arrayindexing_get
13+
14+
>>> import numpy as np
15+
>>> Array=np.array([2,3,1,0,1,2,3])
16+
>>> Indices=[0,-1, len(Array)-2] # get the first, last, and second-to-last indices of the array
17+
>>> C=get(Array,Indices,axis=0) # i.e. C=Array[Indices]
18+
19+
:param Array: A cdms2 variable, or numpy array, to access the indices of
20+
:type Array: cdms.tvariable.TransientVariable or numpy.array
21+
22+
:param Indices: List of integers specifying the indices of Array to access and return
23+
24+
.. note::
25+
26+
Negative index value will access indices starting from the end of the array.
27+
i.e. -1 will be the last item.
28+
:type Indices: list
29+
30+
:param axis: Axis of a cdms variable
31+
:type axis: int or str
1332
"""
1433
## First some checks
1534

@@ -28,11 +47,11 @@ def get(Array,Indices,axis=0):
2847
if isinstance(Indices,int):
2948
return Array[Indices]
3049
if Indices.shape!=Array.shape[1:]:
31-
raise "Error uncompatible shapes: "+str(Array.shape)+" and "+str(Indices.shape)
50+
raise "Error incompatible shapes: "+str(Array.shape)+" and "+str(Indices.shape)
3251
else:
3352
Array,Indices,weights,axis,ax=statistics.__checker(Array,Indices,None,axis)
3453
if Indices.shape!=Array.shape:
35-
raise "Error uncompatible shapes: "+str(Array.shape)+" and "+str(Indices.shape)
54+
raise "Error incompatible shapes: "+str(Array.shape)+" and "+str(Indices.shape)
3655

3756
m=Array.mask
3857
if not isinstance(Indices,int): Indices=Indices.data.astype('i') # Sometihng happened with masking of y by x mask
@@ -52,11 +71,31 @@ def get(Array,Indices,axis=0):
5271
def set(Array,Indices,Values,axis=0):
5372
"""
5473
Arrayrrayindexing set Array[Indices] with Values, indices are taken along dimension given with axis
55-
56-
Usage:
57-
Array=set(Array,Indices,Values,axis=0) # i.e. Array[Indices]=Values
5874
59-
Indices accepts negative value ,e.g: -1 is last element
75+
:Example:
76+
77+
.. doctest:: arrayindexing_set
78+
79+
>>> import numpy as np
80+
>>> Array=np.array([2,3,1,0,1,2,3])
81+
>>> Indices=[0,-1, len(Array)-2] # get the first, last, and second-to-last indices of the array
82+
>>> Values = [5, 7, 9]
83+
>>> Array=set(Array,Indices,Values,axis=0) # i.e. Array[Indices]=Values
84+
85+
86+
:param Array: A cdms2 variable, or numpy array, to set the indices of
87+
:type Array: cdms.tvariable.TransientVariable or numpy.array
88+
89+
:param Indices: List of integers specifying the indices of Array to access and set.
90+
91+
.. note::
92+
93+
Negative index value will access indices starting from the end of the array.
94+
i.e. -1 will be the last item.
95+
:type Indices: list
96+
97+
:param axis: Axis of a cdms variable
98+
:type axis: int or str
6099
"""
61100
## if Indices.ndim==0:
62101
## Array[Indices]=Values

0 commit comments

Comments
 (0)