105 lines
4.6 KiB
HTML
105 lines
4.6 KiB
HTML
|
<html>
|
||
|
<title>GFESuite Documentation - Python Programming Guidelines</title>
|
||
|
<body>
|
||
|
<h1 align=center>
|
||
|
<a NAME="Guidelines"></a>Programming Guidelines</h1>
|
||
|
<div CLASS="2Heading"> <a href="#StyleSuggestions">Style
|
||
|
Suggestions</a></div>
|
||
|
|
||
|
<div CLASS="2Heading"> <a href="#Trouble-shootingIdeas">Trouble-shooting
|
||
|
Ideas</a></div>
|
||
|
<hr width="100%">
|
||
|
|
||
|
<h2 align=center>
|
||
|
<a NAME="StyleSuggestions"></a>Style Suggestions</h2>
|
||
|
|
||
|
<ul>
|
||
|
<li>
|
||
|
Comment your code -- it really makes a difference, not only to others trying
|
||
|
to understand your code, but to you when you're trying to debug it.</li>
|
||
|
|
||
|
<li>
|
||
|
Name your variables meaningfully. A variable named "threshold"
|
||
|
versus "x" makes code much easier to read.</li>
|
||
|
|
||
|
<li>
|
||
|
Simple or Compound Statements</li>
|
||
|
|
||
|
<br>There are many ways to write software. You can separate each
|
||
|
step in a calculation on a separate line and assign it to variables, or
|
||
|
you can combine everything on one line. It all amounts to readability.
|
||
|
<p>Take this example of a compound statement, which computes a convective
|
||
|
/ total precipitation ratio, then creates a boolean grid which is based
|
||
|
on total precipitation from 0 to 0.02", and then creates a grid with ProbPrecip
|
||
|
values if there is precipitation (precipMask) and the ratio (precipRatio)
|
||
|
is less than 10%:
|
||
|
<p><tt> precipRatio = cp_SFC
|
||
|
/ (tp_SFC + 0.0001)</tt>
|
||
|
<br><tt> precipMask = logical_and(greater(tp_SFC,
|
||
|
0.0), less(tp_SFC, 0.02*25.4))</tt>
|
||
|
<br><tt> grid = where(logical_and(precipMask,
|
||
|
less(precipRatio, 0.1)), ProbPrecip, 0.0)</tt>
|
||
|
<p>This same code fragment may be written in the following way. The precipRatio
|
||
|
is the calculated cp/tp ratio; precipPositive is a boolean grid where tp
|
||
|
> 0; precipLess2 is a boolean grid where tp < 0.02" (which includes
|
||
|
zero); precipBetweenZeroAnd2 is a boolean grid where tp > 0 and tp <
|
||
|
0.02"; precipRatioLess10 is a boolean grid where the precipRatio is less
|
||
|
than 10%, precipRatioAndLowPrecip is a boolean grid where tp > 0 and less
|
||
|
than 0.02", and where the cp/tp ratio is less than 0.1:
|
||
|
<p><tt> precipRatio = cp_SFC
|
||
|
/ (tp_SFC + 0.0001)</tt>
|
||
|
<br><tt> precipPositive = greater(tp_SFC,
|
||
|
0.0)</tt>
|
||
|
<br><tt> precipLess2 = less(tp_SFC,
|
||
|
0.02*25.4)</tt>
|
||
|
<br><tt> precipBetweenZeroAnd2
|
||
|
= logical_and(precipPositive, precipLess2)</tt>
|
||
|
<br><tt> precipRatioLess10 =
|
||
|
less(precipRatio, 0.1)</tt>
|
||
|
<br><tt> precipRatioAndLowPrecip
|
||
|
= logical_and(precipRatioLess10, precipBetweenZeroAnd2)</tt>
|
||
|
<br><tt> grid = where(precipRatioAndLowPrecip,
|
||
|
ProbPrecip, 0.0)</tt>
|
||
|
<p>The second fragment, though longer, is composed of simpler statements
|
||
|
and is easier to follow and read. Note also, that we have named the
|
||
|
variables to reflect the concepts and improve readability.</ul>
|
||
|
|
||
|
<h2 align=center>
|
||
|
<a NAME="Trouble-shootingIdeas"></a>Trouble-shooting Ideas</h2>
|
||
|
|
||
|
<ul>
|
||
|
<li>
|
||
|
Error Messages: Make sure you are running the GFE from
|
||
|
a terminal window so that you will see any Python error messages that occur
|
||
|
when you run your Smart Tool or Procedure. These messages will
|
||
|
give line numbers and descriptions that help you trace the origin of the
|
||
|
problem.</li>
|
||
|
|
||
|
<li>
|
||
|
Print Statements: Using the Python "print" statement within
|
||
|
your Smart Tool or Procedure is one of the quickest ways to determine where
|
||
|
a problem is occurring and what might be causing it. Simply interspersing
|
||
|
"progress" statements such as the following throughout your tool can help:</li>
|
||
|
</ul>
|
||
|
|
||
|
print "Made it to this point"
|
||
|
<p> Here are some other
|
||
|
example "print" statements:
|
||
|
<p> print "myVariable
|
||
|
=", myVariable
|
||
|
<p> # To print
|
||
|
Python Numeric array information:
|
||
|
<br> print
|
||
|
"Dimensions of array T:", T.shape, "Value of T at point
|
||
|
20, 20: ", T[20][20]
|
||
|
<ul>
|
||
|
<li>
|
||
|
Simplify: If the problem is within a complicated method, simplify
|
||
|
it by commenting out the code and gradually introducing pieces of it.
|
||
|
Go back to something you know works and then start adding functionality
|
||
|
to it.</li>
|
||
|
</ul>
|
||
|
</div>
|
||
|
|
||
|
</body>
|
||
|
</html>
|