<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Intellovations</title>
	<atom:link href="http://www.intellovations.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.intellovations.com</link>
	<description></description>
	<lastBuildDate>Wed, 25 Apr 2012 15:51:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Of Window Managers and Desktop Environments</title>
		<link>http://www.intellovations.com/2012/04/25/of-window-managers-and-desktop-environments/</link>
		<comments>http://www.intellovations.com/2012/04/25/of-window-managers-and-desktop-environments/#comments</comments>
		<pubDate>Wed, 25 Apr 2012 15:39:33 +0000</pubDate>
		<dc:creator>efloehr</dc:creator>
				<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://www.intellovations.com/?p=189</guid>
		<description><![CDATA[Re-evaluating My Desktop I&#8217;ve been using Linux as a desktop for the past three or four years. For much of that time I used Ubuntu and GNOME 2. This past fall, a number of things aligned that caused me to spend a lot of time rethinking my desktop. The first was Ubuntu&#8217;s decision to move [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Re-evaluating My Desktop</strong></p>
<p>I&#8217;ve been using Linux as a desktop for the past three or four years.  For much of that time I used <a href="http://www.ubuntu.com/">Ubuntu</a> and GNOME 2.  This past fall, a number of things aligned that caused me to spend a lot of time rethinking my desktop.</p>
<p>The first was Ubuntu&#8217;s decision to move to Unity.  That opened up a window for me, as I wasn&#8217;t entirely pleased with my ability to navigate Unity, and I really wasn&#8217;t happy with its initial lack of multi-monitor support. The second was that for the first time in my 20+ year programming career, I started having some symptoms of carpal tunnel.  The third was that after three years of Linux in a familiar-to-Windows-refugees desktop (GNOME 2) I was ready to take the next step and look for more productive desktops.  Because of all this, I decided to step back and learn more about all the options available to me, from distros to desktops.</p>
<p>The first thing that I discovered was that there is a big distinction between <a href="http://en.wikipedia.org/wiki/Window_manager">Window Managers</a> and <a href="http://en.wikipedia.org/wiki/Desktop_environment">Desktop Environments</a>.  For someone coming from Microsoft Windows, GNOME, or Unity, this may be an unfamiliar distinction.  A Window Manager is the part of the desktop that is concerned with the presentation, placement, management, and manipulation of windows.  That&#8217;s it.  Until I started researching new Linux desktops, I never knew that GNOME 2&#8242;s window manager was <a href="http://en.wikipedia.org/wiki/Metacity">Metacity</a>, GNOME 3 uses a window manager called <a href="http://en.wikipedia.org/wiki/Mutter_(window_manager)">Mutter</a>, and Unity uses <a href="http://www.compiz.org/">Compiz</a> for it&#8217;s window manager.</p>
<p><strong>It&#8217;s More Than Just Windows</strong></p>
<p>But a Linux desktop (or any desktop) is more than just windows.  There are lots of parts that combine to make a desktop experience convenient, productive, and enjoyable.  Stuff you generally don&#8217;t think about until it&#8217;s not there.  Things like: a login screen, a graphical file manager, a status bar or dock, having a USB thumbdrive auto-mount on insertion, having a program ask for root password or sudo if it needs escalated privileges, printer management, program launchers (so you don&#8217;t have to always launch from a terminal), wireless network management, notifications, keyrings (so you don&#8217;t constantly have to enter root or your password every time escalated privileges are required), an ability for unprivileged processes to talk to privileged processes so you don&#8217;t even have to sudo or run as root (i.e. PolicyKit), screenshot capturing, etc.  There are a lot of niceties that improve interaction and productivity or are just conveniences on the desktop that have nothing to do with window management.  These are things you rarely think about, until they aren&#8217;t there.</p>
<p>On Linux, there are five major &#8220;all-in-one&#8221; desktops available on the Linux deskop.  They are <a href="http://www.gnome.org/">GNOME</a>, <a href="http://www.kde.org/">KDE</a>, <a href="http://unity.ubuntu.com/">Unity</a> (which uses a lot of GNOME parts for the Desktop Environment), <a href="http://lxde.org/">LXDE</a>, and <a href="http://www.xfce.org/">XFCE</a>.  All five provide a complete desktop environment and window management ala Microsoft Windows.  Each chooses their own solution for many of the desktop &#8220;bits&#8221;.  For example, for graphical file management (and for some, USB automount management), GNOME and Unity use <a href="http://live.gnome.org/Nautilus">Nautilus</a>, KDE uses <a href="http://dolphin.kde.org/">Dolphin</a>, XFCE uses <a href="http://thunar.xfce.org/">Thunar</a>, and LXDE uses <a href="http://pcmanfm.sourceforge.net/">PCManFM</a>.  When you install a Linux distro with one of these &#8220;all-in-ones&#8221; you end up with a complete, working desktop that you don&#8217;t have to think too much about.  Most major distros have package groups or spins that allow you to run any of them in a nice, well packaged, well configured setup.  GNOME, KDE, and Unity are fully-featured and fully loaded, while LXDE and XFCE are a little less so (by design).</p>
<p>I evaluated all five of the &#8220;all-in-ones&#8221;.  These are my opinions, and quick thoughts based on several weeks of using each one as my primary desktop.  For me, it seems GNOME 3 and Unity are trying to move to a &#8220;post PC&#8221; desktop, where touch interaction is the primary interface to the computer.  If I were playing casual games or surfing the web all day clicking on reddit links, it would be a great interface.  With GNOME 3, it was a hassle even to get a weather icon in the status bar, like I could effortlessly in GNOME 2. More importantly, for me, who has a lot of open windows and spends a lot of time typing text, it&#8217;s not the direction I am heading.  My laptop and desktop and my profession is definitely not &#8220;post PC&#8221; nor will it be for the foreseeable future.  I did really like GNOME 3&#8242;s Window-key search, and Unity seemed to have a number of useful keyboard shortcuts out of the box.</p>
<p>LXDE and XFCE were fast and responsive, but are focused on providing a usable environment on even the most constrained devices (like netbooks).  Coming from GNOME 2, they were very familiar, and aren&#8217;t trying to break new interface ground.  KDE was the most intriguing and the KDE team seems able to provide a usable interface for netbooks and tablets but still innovate on the desktop.  No other all-in-one is working as much on trying to improve desktop productivity, and KDE&#8217;s semantic desktop and workspace concepts are areas to watch.</p>
<p><strong>Keyboard Control</strong></p>
<p>After trying out the &#8220;big 5&#8243; I still wasn&#8217;t satisfied. All the all-in-ones could be configured to make me reasonably productive. But throughout this process I became more aware of how I use the desktop. Generally, I have a lot of workspaces, either with some number of applications on them tiled nicely, or I have one big window. For example, one of my workspaces is just a web browser.  Another workspace is three terminal windows&#8230; one the command line, one an editor session, and one at a database prompt. I found it was easier to group related windows that I tended to work on together, well, together. I also wanted to create keyboard shortcuts for all the window management I wanted to do, as I believed some of my wrist pain was caused by moving from the keyboard to the mouse frequently, as I focused different windows or moved them as needed. After some investigation I discovered tiling window managers. Most of them gave me the ability to completely control my windows, window layout, workspace views, and focus with the keyboard. Tiling window managers also made easy what I always did with windows: either tiled or full-screen. I never had partially overlapping screens, except for the occasional floater (like calculator).</p>
<p><strong>Terrified of the Blank Screen</strong></p>
<p>I tried three tiling window managers: <a href="http://awesome.naquadah.org/">awesome</a>, <a href="http://xmonad.org/">xmonad</a>, and <a href="http://qtile.org/">qtile</a>.  After trying them all, I saw the potential, but they all initially scared me away.  Qtile isn&#8217;t a very popular window manager, but it&#8217;s how I learned about tiling window managers in the first place.  One Qtile user/developer presented an <strong><em>awesome</em></strong> lightning talk on it.  Watch it <a href="http://www.youtube.com/watch?v=r_8om4dsEmw">here on YouTube</a>.  Even if you have no interest in Qtile or tiling window managers, it&#8217;s a great, funny 5 minute talk.</p>
<p>I wanted to love Qtile, as it is written in Python, but I had trouble getting it running.  None of the major distros had pre-built packages for it.  Awesome and xmonad were available as pre-built packages on all the major distros (save awesome on Fedora for some odd reason, but it is available in a third-party repo).  Awesome was very easy to get running, and came pre-configured with a task bar.  When xmonad started I was terrified I had done something wrong&#8230; all there was was a blank screen (that&#8217;s normal).</p>
<p>After the initial shock, I started using each one for a time. Due simply to the available documentation and user base, I gravitated to awesome and xmonad. This is when I discovered the difference between a window manager and a desktop environment.  I started to try to do &#8220;desktopy&#8221; things: how do I configure wireless on my laptop?  How do I get a status bar in xmonad?  How to I launch programs outside of a terminal window? How do I set up my printers?  All the things that had come &#8220;standard&#8221; with my GNOME desktop now needed to be researched, installed, and configured.  I learned so much about how a modern Linux desktop is put together, and that was invaluable: PolicyKit, DBUS, session management&#8230; all fascinating.  I was able to get a productive desktop in both awesome and xmonad.  I leaned heavily on the great documentation that users of <a href="http://www.archlinux.org/">ArchLinux</a> have built.  If you ever want to learn how a Linux system is put together out of its parts, they do a wonderful job. Their wiki is comprehensive, well laid out, and informative even if you don&#8217;t choose Arch as your distro of choice (I didn&#8217;t).</p>
<p>But at the end of the day, even after all that, I wasn&#8217;t happy.  I loved the keyboard controllable interface and tiling.  But there were still little cracks here and there&#8230; functionality that I had gotten used to in GNOME that I couldn&#8217;t get configured exactly right.  And while I enjoyed the learning experience, for me it wasn&#8217;t about the ultimate configurability so much as having a keyboard-controllable tiling window manager with a full-featured desktop environment.</p>
<p><strong>Best of Both Worlds</strong></p>
<p>Then I discovered it was possible to get the productivity and keyboard control of a tiling window manager, with the desktop glue of a desktop environment. What I ended up doing was installing the &#8220;xmonad-gnome&#8221; package in <a href="http://fedoraproject.org/">Fedora</a>. Just &#8220;yum install xmonad-gnome&#8221;.  It uses the &#8220;fallback&#8221; interface within GNOME 3 (essentially a GNOME 2 status bar) which means I can even get my weather status icon! I am sure other distros have similar packages that make getting up and running fairly painless.  If not, both xmonad and awesome have easy-to-follow documentation on getting a tiling window manager working seemlessly in GNOME.  Xmonad&#8217;s documentation is <a href="http://www.haskell.org/haskellwiki/Xmonad/Using_xmonad_in_Gnome">here</a>, while ArchLinux provides great awesome window manager documentation <a href="http://awesome.naquadah.org/wiki/Quickly_Setting_up_Awesome_with_Gnome">here</a>.</p>
<p>I&#8217;ve found great productivity benefits using a tiling window manager, and having the GNOME desktop environment in the background makes my Linux desktop easy to use.  I&#8217;ve spent some time configuring Xmonad to my liking (a topic for another post), but I don&#8217;t have to worry about manually setting up the entire desktop environment.  Overall, I use the mouse less, and my productivity has increased. If you are interested in trying a tiling window manager, but were afraid of the lack of a built-in desktop environment, try xmonad-gnome in Fedora, or similar.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.intellovations.com/2012/04/25/of-window-managers-and-desktop-environments/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Wind Observation Calculations in FORTRAN and Python</title>
		<link>http://www.intellovations.com/2011/01/16/wind-observation-calculations-in-fortran-and-python/</link>
		<comments>http://www.intellovations.com/2011/01/16/wind-observation-calculations-in-fortran-and-python/#comments</comments>
		<pubDate>Sun, 16 Jan 2011 17:25:09 +0000</pubDate>
		<dc:creator>efloehr</dc:creator>
				<category><![CDATA[FORTRAN]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Weather]]></category>

		<guid isPermaLink="false">http://www.intellovations.com/?p=114</guid>
		<description><![CDATA[The National Climatic Data Center (NCDC) is the government organization in charge of preserving and archiving weather observations in the United States. One of the most popular datasets they provide is the Local Climatic Data (LCD) product. It contains daily summaries and raw hourly reports from about 1,600 stations. The daily summary includes high temperature, low temperature, amount of [...]]]></description>
			<content:encoded><![CDATA[<p>The <a title="National Climatic Data Center" href="http://www.ncdc.noaa.gov/oa/ncdc.html" target="_blank">National Climatic Data Center</a> (NCDC) is the government organization in charge of preserving and archiving weather observations in the United States. One of the most popular datasets they provide is the Local Climatic Data (LCD) product. It contains daily summaries and raw hourly reports from about 1,600 stations. The daily summary includes high temperature, low temperature, amount of precipitation, atmospheric pressure, and wind summaries. An example can be found <a title="LCD Daily Summary Example" href="http://cdo.ncdc.noaa.gov/qclcd/qclcddlysummary.htm" target="_blank">here</a>.</p>
<p>The daily summary has several wind observations. There are maximum five-second and two-minute speeds, and the average wind direction during those gusts. And then there are three aggregate observations: &#8220;Resultant wind speed&#8221;, &#8220;Resultant wind direction&#8221;, and &#8220;Average wind speed&#8221;. What do these observations mean, and what is the difference between &#8220;resultant&#8221; and &#8220;average&#8221;?</p>
<p>The average wind speed is a simple average of wind velocity, regardless of direction, over the day. If the wind blew from the north at 10 knots for half the day, and from the south at 20 knots for the other half, the average wind speed that day would be 15 knots.</p>
<p>Resultant wind speed and resultant wind direction are the vector average (the resultant vector) wind speed and direction. Imagine the wind observation as a vector starting from the origin (the observing station) pointing in the direction the wind is originating from and with a length equal to the wind speed. If you take all those vectors and add them up, the resulting direction of the vector would be the resultant wind direction, and the magnitude of the resulting vector divided by the number of vectors is the resultant wind speed.</p>
<p>Adding all of the hourly wind vectors and averaging them results in the values expressed in the daily resultant wind speed and resultant wind direction observations. That is how the NCDC calculates those values, and they do it in FORTRAN. I am going to walk through the specific code that generates those daily averages from hourly data. I&#8217;ll also show the same code in Python. Finally, using the complex math library in the Python standard library, I&#8217;ll show how the vector average can be calculated with far less code. The full FORTRAN example can be downloaded <a href="http://www.intellovations.com/programs/wind.for">here</a>, while the Python source can be found <a href="http://www.intellovations.com/programs/wind.py">here</a>.</p>
<p>The following FORTRAN code was supplied by the NCDC, and is the actual code they use to compute daily resultant wind direction and speed.  The Python code was created by me to exactly match the FORTRAN code, to compare and contrast the differences between FORTRAN and Python.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="fortran" style="font-family:monospace;">      <span style="color: #b1b100;">SUBROUTINE</span> R_WIND <span style="color: #009900;">&#40;</span>N_RWD, RRDD, RWND, RWDIR, RWSPD<span style="color: #009900;">&#41;</span>
&nbsp;
      <span style="color: #000066;">IMPLICIT</span> <span style="color: #000066;">NONE</span>
&nbsp;
      <span style="color: #000066;">REAL</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">8</span> RRDD<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">31</span><span style="color: #009900;">&#41;</span>, RWND<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">31</span><span style="color: #009900;">&#41;</span>, R_DIR, R_SPD, TOTRX, TOTRY
&nbsp;
      <span style="color: #000066;">INTEGER</span> X, WI1, WI2, N_RWD
&nbsp;
      <span style="color: #000066;">CHARACTER</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">2</span> RWDIR
      <span style="color: #000066;">CHARACTER</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">5</span> RWSPD, WC1
&nbsp;
      TOTRX <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0.0</span>
      TOTRY <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0.0</span></pre></td></tr></table></div>

<p>The subroutine <code>R_WIND</code> takes three parameters, and returns two. <code>N_RWD</code> is the number of hourly wind observations. <code>RRDD</code> is the array of wind directions, while <code>RWND</code> is the array of wind speeds. The wind direction angles passed in are degrees plus 180 converted into radians.</p>
<p>The subroutine returns two results, <code>RWDIR</code>, which is the resultant wind direction, and <code>RWSPD</code>, which is the resultant wind speed. These values are returned as character strings.</p>
<p>The Python equivalent is much shorter in code, because in Python you don&#8217;t need to explicitly declare variable types.  You do have to import the math library, since the sine and cosine functions that will be used are not implicitly available, as with FORTRAN.  There are also no explicitly declared output variables.  Because of this dynamic nature, documentation is even more important.  The text between triple-quotes immediately following the function definition is called a &#8220;docstring.&#8221;  A docstring is used in Python to document the code, and is used by documentation generators like <a href="http://sphinx.pocoo.org/">Sphinx</a> to build web-based documentation for your code.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">math</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> r_wind<span style="color: black;">&#40;</span>n_rwd, rrdd, rwnd<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;
    Takes an array of houry wind directions and speeds and returns the
    resultant average wind speed and direction, or None for speed and direction
    if there are fewer than 16 hourly observations.
&nbsp;
    n_rwd -- number of hourly wind observations
    rrdd  -- array of wind directions plus 180 degrees, in radians
    rwnd  -- array of wind speeds
&nbsp;
    Returns a tuple of resultant wind direction and wind speed.  Both are
    returned as strings.  Wind direction is in tens of degrees.
    &quot;&quot;&quot;</span><span style="color: #483d8b;">&quot;
    rwdir = rwspd = None
    totrx = 0.0
    totry = 0.0</span></pre></td></tr></table></div>

<p>Once the variables have been declared, the subroutine then checks to see if there are at least 16 hourly observations.  If there are not at least 16 hourly observations out of 24 (or two-thirds), it won&#8217;t try to calculate a daily resultant wind vector.  Then, it loops through the direction and speed arrays, converting into Cartesian coordinates.  Converting polar (angle and magnitude, in this case direction and speed) coordinates into Cartesian coordinates to sum them up, then average, is the normal way to add vectors.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="fortran" style="font-family:monospace;">      <span style="color: #b1b100;">IF</span> <span style="color: #009900;">&#40;</span>N_RWD <span style="color: #000000; font-weight: bold;">.<span style="color: #202020;">GT</span>.</span> <span style="color: #cc66cc;">15</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">THEN</span>
         X <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span>
         <span style="color: #b1b100;">DO</span> <span style="color: #b1b100;">WHILE</span> <span style="color: #009900;">&#40;</span>X <span style="color: #000000; font-weight: bold;">.<span style="color: #202020;">LE</span>.</span> N_RWD<span style="color: #009900;">&#41;</span>
            TOTRX <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>RWND<span style="color: #009900;">&#40;</span>X<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>DSIN<span style="color: #009900;">&#40;</span>RRDD<span style="color: #009900;">&#40;</span>X<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> TOTRX  <span style="color: #666666; font-style: italic;">! TOT X &amp; Y COOR.</span>
            TOTRY <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>RWND<span style="color: #009900;">&#40;</span>X<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>DCOS<span style="color: #009900;">&#40;</span>RRDD<span style="color: #009900;">&#40;</span>X<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> TOTRY
            X <span style="color: #339933;">=</span> X <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span>
         <span style="color: #b1b100;">END</span> <span style="color: #b1b100;">DO</span></pre></td></tr></table></div>

<p>There are a couple of unique things happening in this code.  First, adding 180 degrees to the wind directions prior to passing in the array.  Second, using sine for the X coordinate conversion and cosine for the Y coordinate conversion is the opposite of the general way to convert from polar to Cartesian coordinates (see <a href="http://en.wikipedia.org/wiki/Polar_coordinate_system#Converting_between_polar_and_Cartesian_coordinates">here</a>).  The reason this is done, even though it isn&#8217;t required in this subroutine, is to create a Cartesian plane that matches a map.  When wind is measured at 0 degrees, it means it is coming out of the north.  The visualized vector would be an arrow pointing down (the negative y-axis in a standard Cartesian plane).  Additionally, the angle would be measured clockwise (a 90 degree wind would be coming from the east, and thus a vector pointing left along the negative x-axis).  Using the standard Cartesian coordinate conversion, the zero angle vector would point right along the positive x-axis, with angles measured counter-clockwise.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>20
21
22
23
24
25
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">    <span style="color: #ff7700;font-weight:bold;">if</span> n_rwd <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">15</span>:
        x = <span style="color: #ff4500;">0</span>
        <span style="color: #ff7700;font-weight:bold;">while</span> x <span style="color: #66cc66;">&lt;</span> n_rwd:
            totrx = <span style="color: black;">&#40;</span>rwnd<span style="color: black;">&#91;</span>x<span style="color: black;">&#93;</span><span style="color: #66cc66;">*</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">math</span>.<span style="color: black;">sin</span><span style="color: black;">&#40;</span>rrdd<span style="color: black;">&#91;</span>x<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> + totrx <span style="color: #808080; font-style: italic;"># tot x &amp; y coor.</span>
            totry = <span style="color: black;">&#40;</span>rwnd<span style="color: black;">&#91;</span>x<span style="color: black;">&#93;</span><span style="color: #66cc66;">*</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">math</span>.<span style="color: black;">cos</span><span style="color: black;">&#40;</span>rrdd<span style="color: black;">&#91;</span>x<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> + totry
            x = x + <span style="color: #ff4500;">1</span></pre></td></tr></table></div>

<p>The Python version is quite similar.  The biggest difference is that Python arrays are zero indexed, while FORTRAN has arrays indexed starting at one.  Another difference is that Python arrays use brackets to indicate the index, with parentheses reserved for function calls.  Also, no &#8220;end while&#8221; statement is required, because in Python, indentation has syntactical meaning. Specficially, anything indented past the while statement are considered part of the while block.  Once code returns to the same indentation as the while, it is not longer part of the while block.  Finally, other differences are that in Python, parentheses are not required around conditional expressions, and comments in Python are indicated with a hash character rather than a bang.</p>
<p>The next section of the FORTRAN code finds the angle of the resultant x,y coordinates and averages the resultant length (which is speed).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
</pre></td><td class="code"><pre class="fortran" style="font-family:monospace;">         R_DIR <span style="color: #339933;">=</span> DATAN<span style="color: #009900;">&#40;</span>TOTRX<span style="color: #339933;">/</span>TOTRY<span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">! AVG R_WIND DIR</span>
         R_DIR <span style="color: #339933;">=</span> R_DIR <span style="color: #339933;">/</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3.14156</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">180</span><span style="color: #009900;">&#41;</span>   <span style="color: #666666; font-style: italic;">! convert radians back to degrees</span>
&nbsp;
         <span style="color: #b1b100;">IF</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>TOTRX<span style="color: #339933;">*</span>TOTRY<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">.<span style="color: #202020;">LT</span>.</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">THEN</span>
            <span style="color: #b1b100;">IF</span> <span style="color: #009900;">&#40;</span>TOTRX <span style="color: #000000; font-weight: bold;">.<span style="color: #202020;">LT</span>.</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">THEN</span>
               R_DIR <span style="color: #339933;">=</span> <span style="color: #cc66cc;">180</span> <span style="color: #339933;">+</span> R_DIR
            <span style="color: #b1b100;">ELSE</span>
               R_DIR <span style="color: #339933;">=</span> <span style="color: #cc66cc;">360</span> <span style="color: #339933;">+</span> R_DIR
            <span style="color: #b1b100;">END</span> <span style="color: #b1b100;">IF</span>
         <span style="color: #b1b100;">ELSE</span>
            <span style="color: #b1b100;">IF</span> <span style="color: #009900;">&#40;</span>TOTRX <span style="color: #000000; font-weight: bold;">.<span style="color: #202020;">GT</span>.</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> R_DIR <span style="color: #339933;">=</span> <span style="color: #cc66cc;">180</span> <span style="color: #339933;">+</span> R_DIR
         <span style="color: #b1b100;">END</span> <span style="color: #b1b100;">IF</span>
&nbsp;
         R_SPD <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>TOTRX<span style="color: #339933;">**</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>TOTRY<span style="color: #339933;">**</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> N_RWD<span style="color: #339933;">**</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">**</span>.5
&nbsp;
         <span style="color: #b1b100;">IF</span> <span style="color: #009900;">&#40;</span>R_DIR <span style="color: #000000; font-weight: bold;">.<span style="color: #202020;">EQ</span>.</span> <span style="color: #cc66cc;">0</span> <span style="color: #000000; font-weight: bold;">.<span style="color: #202020;">AND</span>.</span> R_SPD <span style="color: #000000; font-weight: bold;">.<span style="color: #202020;">NE</span>.</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> R_DIR <span style="color: #339933;">=</span> <span style="color: #cc66cc;">360</span>
         <span style="color: #b1b100;">IF</span> <span style="color: #009900;">&#40;</span>R_SPD <span style="color: #000000; font-weight: bold;">.<span style="color: #202020;">EQ</span>.</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> R_DIR <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span></pre></td></tr></table></div>

<p>Lines 23 and 24 take the arctangent of the x,y coordinate to compute the angle to convert back into polar coordinates.  Line 27-35 convert the angle to the original solution space, based on the quadrant the resultant x,y coordinate resides in.  Line 37 is simply the distance between the origin and the averaged x,y coordinate, which is the vector length of the averaged resultant vector, which is resultant wind speed.  This is <a href="http://en.wikipedia.org/wiki/Hypotenuse#Calculating_the_hypotenuse">calculated</a> by realizing that x and y are just legs of a right triangle, with hypotenuse being the distance between the origin and the point. Line 39 and 40 are just clean up.  The subroutine will return an angle of 360 for a north wind, and zero only if there is resultant wind.  Line 40 is required because the <code>DATAN</code> function will return <code>NaN</code> (<a href="http://en.wikipedia.org/wiki/NaN">Not A Number</a>) if x and y both sum to zero, which is the only time the speed would be zero.</p>
<p>The Python version is quite similar.  The only major difference is that there needs to be a check that <code>totry</code> isn&#8217;t zero, as that would result in a <code>ZeroDivisionError</code> Python exception.  When a division by zero is encountered in Python, it will interrupt code flow and throw an exception.  FORTRAN on the other hand returns the discrete value <code>NaN</code> which is then passed to the <code>DATAN</code> function, which knows how to handle <code>NaN</code>.  There actually is a <code>NaN</code> in Python, but Python chooses to throw an exception rather than return <code>NaN</code> on a division by zero.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">        <span style="color: #ff7700;font-weight:bold;">if</span> totry == <span style="color: #ff4500;">0.0</span>:
            r_dir = <span style="color: #ff4500;">0</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            r_dir = <span style="color: #dc143c;">math</span>.<span style="color: black;">atan</span><span style="color: black;">&#40;</span>totrx/totry<span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># avg r_wind dir</span>
        r_dir = r_dir / <span style="color: black;">&#40;</span><span style="color: #ff4500;">3.14156</span>/<span style="color: #ff4500;">180</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># convert radians back to degrees</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>totrx<span style="color: #66cc66;">*</span>totry<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">0</span>:
            <span style="color: #ff7700;font-weight:bold;">if</span> totrx <span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">0</span>:
                r_dir = <span style="color: #ff4500;">180</span> + r_dir
            <span style="color: #ff7700;font-weight:bold;">else</span>:
                r_dir = <span style="color: #ff4500;">360</span> + r_dir
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            <span style="color: #ff7700;font-weight:bold;">if</span> totrx <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">0</span>:
                r_dir = <span style="color: #ff4500;">180</span> + r_dir
&nbsp;
        r_spd = <span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>totrx<span style="color: #66cc66;">**</span><span style="color: #ff4500;">2</span> + totry<span style="color: #66cc66;">**</span><span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span> / n_rwd<span style="color: #66cc66;">**</span><span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">**</span>.5
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">if</span> r_dir == <span style="color: #ff4500;">0</span> <span style="color: #ff7700;font-weight:bold;">and</span> r_spd <span style="color: #66cc66;">!</span>= <span style="color: #ff4500;">0</span>:
            r_dir = <span style="color: #ff4500;">360</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">if</span> r_spd == <span style="color: #ff4500;">0</span>:
            r_dir = <span style="color: #ff4500;">0</span></pre></td></tr></table></div>

<p>The final section of the subroutine just rounds and formats.  It rounds the degrees to tens of degrees, and returns in tens of degrees.  Thus, 260 degrees would be returned as 26.  Speed is returned as tenths of a knot.  The values are returned as strings, as they will be ending up in a CSV file, not used in further processing.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
</pre></td><td class="code"><pre class="fortran" style="font-family:monospace;">         WRITE <span style="color: #009900;">&#40;</span>WC1, <span style="color: #b1b100;">FMT</span><span style="color: #339933;">=</span><span style="color: #ff0000;">'(F4.0)'</span><span style="color: #009900;">&#41;</span> R_DIR
&nbsp;
         READ <span style="color: #009900;">&#40;</span>WC1, <span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span> WI1, WI2
 <span style="color: #cc66cc;">10</span>      FORMAT <span style="color: #009900;">&#40;</span>I2, I1<span style="color: #009900;">&#41;</span>
&nbsp;
         <span style="color: #b1b100;">IF</span> <span style="color: #009900;">&#40;</span>WI2 <span style="color: #000000; font-weight: bold;">.<span style="color: #202020;">GT</span>.</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">THEN</span>
            WI1 <span style="color: #339933;">=</span> WI1 <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span>
         <span style="color: #b1b100;">END</span> <span style="color: #b1b100;">IF</span>
&nbsp;
         WRITE <span style="color: #009900;">&#40;</span>RWDIR, <span style="color: #b1b100;">FMT</span><span style="color: #339933;">=</span><span style="color: #ff0000;">'(I2)'</span><span style="color: #009900;">&#41;</span> WI1
         WRITE <span style="color: #009900;">&#40;</span>RWSPD, <span style="color: #b1b100;">FMT</span><span style="color: #339933;">=</span><span style="color: #ff0000;">'(F5.1)'</span><span style="color: #009900;">&#41;</span> R_SPD
      <span style="color: #b1b100;">END</span> <span style="color: #b1b100;">IF</span>
&nbsp;
      <span style="color: #b1b100;">RETURN</span>
      <span style="color: #b1b100;">END</span></pre></td></tr></table></div>

<p>The Python version is noticeably different as formatting and substring extraction is quite different between FORTRAN and Python.  Python uses C-style format strings.  The FORTRAN code is also using <code>CHARACTER</code> arrays rather than <code>STRING </code> arrays (perhaps when this was written the FORTRAN dialect used did not support <code>STRING</code>), which doesn&#8217;t support substring notation.  Python strings are character arrays and provide all the same functionality as other arrays, including sub-array indexing, which is used here.  Finally, in Python don&#8217;t declare the return variables in the function declaration, you return them explicitly, and anonymously, which gives the programmer a lot of power, but also adds to the responsibility of the programmer to document what is returned in docstrings.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>58
59
60
61
62
63
64
65
66
67
68
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">        wc1 = <span style="color: #483d8b;">'%#4.0f'</span> <span style="color: #66cc66;">%</span> r_dir
        wi1 = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>wc1<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span>:<span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
        wi2 = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>wc1<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span>:<span style="color: #ff4500;">3</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">if</span> wi2 <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">4</span>:
            wi1 = wi1 + <span style="color: #ff4500;">1</span>
&nbsp;
        rwdir = <span style="color: #483d8b;">'%2i'</span> <span style="color: #66cc66;">%</span> wi1
        rwspd = <span style="color: #483d8b;">'%#5.1f'</span> <span style="color: #66cc66;">%</span> r_spd
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> rwdir, rwspd</pre></td></tr></table></div>

<p>That&#8217;s it. Of course, if we were to rewrite this function today in Python, we wouldn&#8217;t need to do all of this calculation in our code, but we can instead rely on higher level functions in the standard library to help us out.  This is probably also true of FORTRAN today, but I&#8217;ve not actively written FORTRAN in many years, so don&#8217;t know.</p>
<p>Python has a complex math library, called <code>cmath</code> that we can import in order to give us the functionality we need.  We can convert polar coordinates into a Cartesian complex number, add up all the complex numbers and average them, then convert the complex number back to polar coordinates. This will accomplish the task of the original function, which is to return the resultant wind vector angle and speed.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> r_wind_new<span style="color: black;">&#40;</span>n_rwd, rrdd, rwnd<span style="color: black;">&#41;</span>:
    wind_vector_sum = <span style="color: #008000;">None</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> n_rwd <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">15</span>:
        <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, n_rwd<span style="color: black;">&#41;</span>:
            wind_polar = <span style="color: #dc143c;">cmath</span>.<span style="color: black;">rect</span><span style="color: black;">&#40;</span>rwnd<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span>, rrdd<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> - <span style="color: #dc143c;">math</span>.<span style="color: black;">pi</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">if</span> wind_vector_sum <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">None</span>:
                wind_vector_sum = wind_polar
            <span style="color: #ff7700;font-weight:bold;">else</span>:
                wind_vector_sum += wind_polar
&nbsp;
        r, phi = <span style="color: #dc143c;">cmath</span>.<span style="color: black;">polar</span><span style="color: black;">&#40;</span>wind_vector_sum / n_rwd<span style="color: black;">&#41;</span>
&nbsp;
        rwdir = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #008000;">round</span><span style="color: black;">&#40;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #008000;">round</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">math</span>.<span style="color: black;">degrees</span><span style="color: black;">&#40;</span>phi<span style="color: black;">&#41;</span> <span style="color: #66cc66;">%</span> <span style="color: #ff4500;">360</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> / <span style="color: #ff4500;">10.0</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        rwspd = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #008000;">round</span><span style="color: black;">&#40;</span>r<span style="color: #66cc66;">*</span><span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>/<span style="color: #ff4500;">10.0</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">None</span>, <span style="color: #008000;">None</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">'%2i'</span> <span style="color: #66cc66;">%</span> rwdir, <span style="color: #483d8b;">'%#5.1f'</span> <span style="color: #66cc66;">%</span> rwspd</pre></td></tr></table></div>

<p>This new Python function, <code>r_wind_new</code> is a drop-in replacement for the original <code>r_wind</code> function.  It uses <code>cmath.rect()</code> to convert a vector angle and length to a Cartesian complex number. These complex numbers are added together and then averaged.  Then, <code>cmath.polar()</code> is used to convert the complex number into an angle and length, which is the resultant wind vector.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.intellovations.com/2011/01/16/wind-observation-calculations-in-fortran-and-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hello world!</title>
		<link>http://www.intellovations.com/2010/10/27/hello-world/</link>
		<comments>http://www.intellovations.com/2010/10/27/hello-world/#comments</comments>
		<pubDate>Wed, 27 Oct 2010 12:57:55 +0000</pubDate>
		<dc:creator>efloehr</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[communication]]></category>
		<category><![CDATA[tags]]></category>
		<category><![CDATA[website]]></category>

		<guid isPermaLink="false">http://www.intellovations.com/?p=1</guid>
		<description><![CDATA[I am improving the Intellovations website so that I can better communicate with my customers and interested folks.]]></description>
			<content:encoded><![CDATA[<p>I am improving the Intellovations website so that I can better communicate with my customers and interested folks.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.intellovations.com/2010/10/27/hello-world/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

