<?xml version="1.0" encoding="UTF-8"?>
<posts to="2009-11-28T23:23:08+00:00" from="2009-11-28T23:23:08+00:00"><post><id>41</id><rid>43</rid><title>PHP Function to return Beaufort Scale</title><section>Code Tutorial</section><author><username>thebluerhino.myopenid.com</username><name>Andrew Milsted</name></author><content><![CDATA[I was working on a project that needed to list the Beaufort wind force. The first attempt was to do a lookup based function using the data here: http://en.wikipedia.org/wiki/Beaufort_scale

[b]Attempt 1[/b][code=php]function beaufort_scale($speed, $unit="m/s"){

    $units['m/s'] = 1;
    $units['kph'] = 1/3.6;
    $units['mph'] = 0.44704;
    $units['kts'] = 0.514444444;

    if(!in_array($unit,array_keys($units))){
        return "Error: couldn't recognise unit";
    }

    $speed *= $units[$unit];
    
    $scale = array(0.3,1.5,3.4,5.4,7.9,10.7,13.8,17.1,20.7,24.4,28.4,32.6,1000);
    
    foreach($scale as $key=>$val){
        if($speed <= $val) return $key;
    }
    return 'Error';
}[/code]
I then noticed on the wikipedia page the scale's empirical formula
so i thought i could improve the function ;)
[quote]v = 0.866 * B<sup>3/2</sup> (units m/s)[/quote]


[b]Attempt 2[/b][code=php]function beaufort_scale($speed, $unit="m/s", $rnd=0){

   $units['m/s'] = 1;
   $units['kph'] = 1/3.6;
   $units['mph'] = 0.44704;
   $units['kts'] = 0.514444444;

   if(!in_array($unit,array_keys($units))){
      return "Error: couldn't recognise unit";
   }

   $speed *= $units[$unit];

   $scale = pow(($speed/0.836),(2/3));

   return round($scale,$rnd);
}[/code] This one has the added ability of returning a decimal answer]]></content><html><![CDATA[<b>Code Tags:</b> PHP<br />
I was working on a project that needed to list the Beaufort wind force. The first attempt was to do a lookup based function using the data here: <a href="http://en.wikipedia.org/wiki/Beaufort_scale">http://en.wikipedia.org/wiki/Beaufort_scale</a><br style="clear:left;"/><br style="clear:left;"/><b>Attempt 1</b><pre class="php" style="color: #000066; border: 1px solid #d0d0d0; background-color: #f0f0f0; padding:5px;"><div style="font-family: Verdana, Arial, sans-serif; color: #808080; font-size: 70%; font-weight: bold; border-bottom: 1px solid #d0d0d0; padding: 0px;">php code:</div><span style="color: #000000; font-weight: bold;">function</span> beaufort_scale<span style="color: #009900;">&#40;</span><span style="color: #000033;">$speed</span><span style="color: #339933;">,</span> <span style="color: #000033;">$unit</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;m/s&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br style="clear:left;"/>&nbsp;<br style="clear:left;"/>    <span style="color: #000033;">$units</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'m/s'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><br style="clear:left;"/>    <span style="color: #000033;">$units</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'kph'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">3.6</span><span style="color: #339933;">;</span><br style="clear:left;"/>    <span style="color: #000033;">$units</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'mph'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0.44704</span><span style="color: #339933;">;</span><br style="clear:left;"/>    <span style="color: #000033;">$units</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'kts'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0.514444444</span><span style="color: #339933;">;</span><br style="clear:left;"/>&nbsp;<br style="clear:left;"/>    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/in_array"><span style="color: #990000;">in_array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000033;">$unit</span><span style="color: #339933;">,</span><a href="http://www.php.net/array_keys"><span style="color: #990000;">array_keys</span></a><span style="color: #009900;">&#40;</span><span style="color: #000033;">$units</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br style="clear:left;"/>        <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;Error: couldn't recognise unit&quot;</span><span style="color: #339933;">;</span><br style="clear:left;"/>    <span style="color: #009900;">&#125;</span><br style="clear:left;"/>&nbsp;<br style="clear:left;"/>    <span style="color: #000033;">$speed</span> <span style="color: #339933;">*=</span> <span style="color: #000033;">$units</span><span style="color: #009900;">&#91;</span><span style="color: #000033;">$unit</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br style="clear:left;"/>&nbsp;<br style="clear:left;"/>    <span style="color: #000033;">$scale</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0.3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1.5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3.4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5.4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">7.9</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">10.7</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">13.8</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">17.1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">20.7</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">24.4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">28.4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">32.6</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br style="clear:left;"/>&nbsp;<br style="clear:left;"/>    <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$scale</span> <span style="color: #b1b100;">as</span> <span style="color: #000033;">$key</span><span style="color: #339933;">=&gt;</span><span style="color: #000033;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br style="clear:left;"/>        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$speed</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000033;">$val</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #000033;">$key</span><span style="color: #339933;">;</span><br style="clear:left;"/>    <span style="color: #009900;">&#125;</span><br style="clear:left;"/>    <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'Error'</span><span style="color: #339933;">;</span><br style="clear:left;"/><span style="color: #009900;">&#125;</span></pre><br style="clear:left;"/>I then noticed on the wikipedia page the scale's empirical formula<br style="clear:left;"/>so i thought i could improve the function ;)<br style="clear:left;"/><blockquote>v = 0.866 * B<sup>3/2</sup> (units m/s)</blockquote><br style="clear:left;"/><br style="clear:left;"/><br style="clear:left;"/><b>Attempt 2</b><pre class="php" style="color: #000066; border: 1px solid #d0d0d0; background-color: #f0f0f0; padding:5px;"><div style="font-family: Verdana, Arial, sans-serif; color: #808080; font-size: 70%; font-weight: bold; border-bottom: 1px solid #d0d0d0; padding: 0px;">php code:</div><span style="color: #000000; font-weight: bold;">function</span> beaufort_scale<span style="color: #009900;">&#40;</span><span style="color: #000033;">$speed</span><span style="color: #339933;">,</span> <span style="color: #000033;">$unit</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;m/s&quot;</span><span style="color: #339933;">,</span> <span style="color: #000033;">$rnd</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br style="clear:left;"/>&nbsp;<br style="clear:left;"/>   <span style="color: #000033;">$units</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'m/s'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><br style="clear:left;"/>   <span style="color: #000033;">$units</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'kph'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">3.6</span><span style="color: #339933;">;</span><br style="clear:left;"/>   <span style="color: #000033;">$units</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'mph'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0.44704</span><span style="color: #339933;">;</span><br style="clear:left;"/>   <span style="color: #000033;">$units</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'kts'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0.514444444</span><span style="color: #339933;">;</span><br style="clear:left;"/>&nbsp;<br style="clear:left;"/>   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/in_array"><span style="color: #990000;">in_array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000033;">$unit</span><span style="color: #339933;">,</span><a href="http://www.php.net/array_keys"><span style="color: #990000;">array_keys</span></a><span style="color: #009900;">&#40;</span><span style="color: #000033;">$units</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br style="clear:left;"/>      <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;Error: couldn't recognise unit&quot;</span><span style="color: #339933;">;</span><br style="clear:left;"/>   <span style="color: #009900;">&#125;</span><br style="clear:left;"/>&nbsp;<br style="clear:left;"/>   <span style="color: #000033;">$speed</span> <span style="color: #339933;">*=</span> <span style="color: #000033;">$units</span><span style="color: #009900;">&#91;</span><span style="color: #000033;">$unit</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br style="clear:left;"/>&nbsp;<br style="clear:left;"/>   <span style="color: #000033;">$scale</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/pow"><span style="color: #990000;">pow</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$speed</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">0.836</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br style="clear:left;"/>&nbsp;<br style="clear:left;"/>   <span style="color: #b1b100;">return</span> <a href="http://www.php.net/round"><span style="color: #990000;">round</span></a><span style="color: #009900;">&#40;</span><span style="color: #000033;">$scale</span><span style="color: #339933;">,</span><span style="color: #000033;">$rnd</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br style="clear:left;"/><span style="color: #009900;">&#125;</span></pre> This one has the added ability of returning a decimal answer<div class="postTools"></div>

]]></html><datestamp>2009-11-28T23:23:08+00:00</datestamp><timestamp>2009-11-30T21:39:08+00:00</timestamp><blog>8</blog><key>7dcc524a8659251fdb06a4a9dd23f51a</key><metadata><code_tags>PHP</code_tags></metadata><links><uri>http://blogs.bluerhinos.co.uk/uri/c</uri><permalink>http://blogs.bluerhinos.co.uk/thinkblue/41/PHP_Function_to_return_Beaufort_Scale.html</permalink></links><formats><format type="text/html">http://blogs.bluerhinos.co.uk/thinkblue/41/PHP_Function_to_return_Beaufort_Scale.html</format><format type="text/xml">http://blogs.bluerhinos.co.uk/thinkblue/41/PHP_Function_to_return_Beaufort_Scale.xml</format><format type="image/png">http://blogs.bluerhinos.co.uk/thinkblue/41/PHP_Function_to_return_Beaufort_Scale.png</format></formats><revisions><revision>http://blogs.bluerhinos.co.uk/thinkblue/41/PHP_Function_to_return_Beaufort_Scale.xml?revision=41</revision><revision>http://blogs.bluerhinos.co.uk/thinkblue/41/PHP_Function_to_return_Beaufort_Scale.xml?revision=42</revision><revision current="true">http://blogs.bluerhinos.co.uk/thinkblue/41/PHP_Function_to_return_Beaufort_Scale.xml?revision=43</revision></revisions><comments/></post></posts>

