CREATE OR REPLACE FUNCTION daylength(real, int)<br>  RETURNS double precision AS<br>'select 24*(ACOS(1-(1-TAN(radians($1))*TAN(.409088 * COS(.0172024 * int))))/PI());'<br>  LANGUAGE 'sql' VOLATILE;<br><br>usage:
<br>select daylength(latitude,dayofyear)<br><br>so if you have a geometry column and a day of year column...<br>select daylength(Y(geom),dayofyear) from your table;<br><br>If you want it to just take a date instead of dayofyear, you could use this:
<br><br>CREATE OR REPLACE FUNCTION daylength(real, date)<br>  RETURNS double precision AS<br>'select 24*(ACOS(1-(1-TAN(radians($1))*TAN(.409088 * COS(.0172024 * extract(doy from $2)))))/PI());'<br>  LANGUAGE 'sql' VOLATILE;
<br><br>I pulled these equations from here:<br><a href="http://dc.metblogs.com/archives/2006/12/the_longest_nig.phtml">http://dc.metblogs.com/archives/2006/12/the_longest_nig.phtml</a><br><br><br>David<br><br><div><span class="gmail_quote">
On 1/26/07, <b class="gmail_sendername">Kirk Wythers</b> <<a href="mailto:kwythers@umn.edu">kwythers@umn.edu</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I am looking for ideas to add a daylength calculation to a postgis<br>database. The database in question has a table named "sites", which<br>contains lat, lon, and geometry info for a number of locations. A<br>second table contains climate data including a day_of_the_year column.
<br><br>I would like to add the capability to calculate the length of each<br>day (could be done as a view or another table). The equation for day<br>length calculation takes as input day_of_the_year and latitude, does<br>
some trig (uses acos, tan, cos, radians, and pi) and spits out an<br>estimate of the day length in hours.<br><br>How would folks recommend I approach this problem?<br><br>Thanks,<br><br>Kirk<br>_______________________________________________
<br>postgis-users mailing list<br><a href="mailto:postgis-users@postgis.refractions.net">postgis-users@postgis.refractions.net</a><br><a href="http://postgis.refractions.net/mailman/listinfo/postgis-users">http://postgis.refractions.net/mailman/listinfo/postgis-users
</a><br></blockquote></div><br><br clear="all"><br>-- <br>************************************<br>David William Bitner