{"id":6841,"date":"2019-12-06T13:54:15","date_gmt":"2019-12-06T18:54:15","guid":{"rendered":"https:\/\/blogs.mathworks.com\/community\/?p=6841"},"modified":"2020-02-05T11:10:51","modified_gmt":"2020-02-05T16:10:51","slug":"crepuscular-calculations","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/community\/2019\/12\/06\/crepuscular-calculations\/","title":{"rendered":"Crepuscular Calculations"},"content":{"rendered":"\n<p>Happy Crepusculus! Never heard of Crepusculus? I&#8217;ll come back to that later. Instead, let me begin with a fun fact: the shortest day of the year is December 21st. <\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"600\" height=\"358\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/crepusculus_01.png\" alt=\"\" class=\"alignnone size-full wp-image-6849\" \/><\/p>\n<p>What? You already knew that fun fact? Okay smarty-pants, here&#8217;s a trick follow-up question: when is the earliest sunset? It turns out it comes well before the shortest day. <\/p>\n<p>Sounds crazy doesn&#8217;t it? But I&#8217;ll prove it to you, courtesy of a lovely File Exchange submission called <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/64692-sunrise-sunrise-and-sunset-times\">SUNRISE<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/1195687-fran%C3%A7ois-beauducel\">Fran&ccedil;ois Beauducel<\/a>. As Fran&ccedil;ois says, it &#8220;computes sunrise and sunset times from any geographical location on Earth.&#8221; He works at the <a href=\"http:\/\/www.ipgp.fr\/en\">Institut de Physique du Globe de Paris<\/a>, so I&#8217;m pretty sure he knows what he&#8217;s doing here. <\/p>\n<p>Watch this. Here I am in Natick, Massachusetts.<\/p>\n<pre class=\"matlab-code\" id=\"matlabcode\" style=\"background-color: #F7F7F7;font-family: monospace;font-weight:normal;border-style: solid; border-width: 1px ;border-color:#E9E9E9;padding-top:5px;padding-bottom:5px;line-height:150%;\">lat = 42.3;\r\nlon = -71.3;\r\nalt = 0;\r\ntzone = -5;\r\n<\/pre>\n<p>I&#8217;m going to use our lovely <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/datetime.html\">DATETIME<\/a> to create a year&#8217;s worth of dates. I&#8217;m going to start my calendar in July because I want December to be in the middle. <\/p>\n<pre class=\"matlab-code\" id=\"matlabcode\" style=\"background-color: #F7F7F7;font-family: monospace;font-weight:normal;border-style: solid; border-width: 1px ;border-color:#E9E9E9;padding-top:5px;padding-bottom:5px;line-height:150%;\">d = datetime(2019,7,1:365);\r\n<\/pre>\n<p>See what I did there? I created a vector of dates from July 1, 2019 to July 365, 2019! DATETIME handles the part that says &#8220;Silly! There is no July 200, 2019, so I&#8217;ll convert it to January 16, 2020 for you.&#8221; It&#8217;s a very convenient way to make date vectors. <\/p>\n<p>The SUNRISE function expects an old-school <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/datenum.html\">DATENUM<\/a>, but that&#8217;s easily managed. <\/p>\n<pre class=\"matlab-code\" id=\"matlabcode\" style=\"background-color: #F7F7F7;font-family: monospace;font-weight:normal;border-style: solid; border-width: 1px ;border-color:#E9E9E9;padding-top:5px;padding-bottom:5px;line-height:150%;\">[srise,sset] = sunrise(lat,lon,alt,tzone,datenum(d));\r\n<\/pre>\n<p>Merci Fran&ccedil;ois! Now we just calculate the hour and plot. Voil&agrave;!<\/p>\n<pre class=\"matlab-code\" id=\"matlabcode\" style=\"background-color: #F7F7F7;font-family: monospace;font-weight:normal;border-style: solid; border-width: 1px ;border-color:#E9E9E9;padding-top:5px;padding-bottom:5px;line-height:150%;\">sriseHour = 24*(srise - floor(srise));\r\nssetHour  = 24*(sset  - floor(sset));\r\nplot(d, sriseHour, d, ssetHour, <span style=\"color:rgb(160, 32, 240);\">'LineWidth'<\/span>, 3)\r\nset(gca, <span style=\"color:rgb(0, 0, 255);\">...<\/span>\r\n    <span style=\"color:rgb(160, 32, 240);\">'YLim'<\/span>,[0 24], <span style=\"color:rgb(0, 0, 255);\">...<\/span>\r\n    <span style=\"color:rgb(160, 32, 240);\">'YTick'<\/span>,[0 6 12 18 24], <span style=\"color:rgb(0, 0, 255);\">...<\/span>\r\n    <span style=\"color:rgb(160, 32, 240);\">'YTickLabel'<\/span>,{<span style=\"color:rgb(160, 32, 240);\">'Midnight'<\/span>,<span style=\"color:rgb(160, 32, 240);\">'6:00 AM'<\/span>,<span style=\"color:rgb(160, 32, 240);\">'Noon'<\/span>,<span style=\"color:rgb(160, 32, 240);\">'6:00 PM'<\/span>,<span style=\"color:rgb(160, 32, 240);\">'Midnight'<\/span>})\r\ntitle(<span style=\"color:rgb(160, 32, 240);\">'Sunrise and Sunset'<\/span>)\r\nxlabel(<span style=\"color:rgb(160, 32, 240);\">'Date'<\/span>)\r\nylabel(<span style=\"color:rgb(160, 32, 240);\">'Time of Day'<\/span>)\r\nlegend({<span style=\"color:rgb(160, 32, 240);\">'Sunrise'<\/span>,<span style=\"color:rgb(160, 32, 240);\">'Sunset'<\/span>},<span style=\"color:rgb(160, 32, 240);\">'Location'<\/span>,<span style=\"color:rgb(160, 32, 240);\">'best'<\/span>)\r\nset(gca,<span style=\"color:rgb(160, 32, 240);\">'YDir'<\/span>,<span style=\"color:rgb(160, 32, 240);\">'reverse'<\/span>)\r\ngrid <span style=\"color:rgb(160, 32, 240);\">on<\/span>\r\nbox <span style=\"color:rgb(160, 32, 240);\">on<\/span>\r\n<\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"560\" height=\"420\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/crepusculus_02.png\" alt=\"\" class=\"alignnone size-full wp-image-6851\" \/><\/p>\n<p>You can see the two curves are somewhat offset. The earliest sunset advances, but the latest sunrise retreats, leaving the shortest day safely in place between them. <\/p>\n<p>Where I live, the earliest sunset is on December 10th this year, fully eleven days before the solstice.<\/p>\n<pre class=\"matlab-code\" id=\"matlabcode\" style=\"background-color: #F7F7F7;font-family: monospace;font-weight:normal;border-style: solid; border-width: 1px ;border-color:#E9E9E9;padding-top:5px;padding-bottom:5px;line-height:150%;\">[~,ix] = min(ssetHour);\r\nd.Format = <span style=\"color:rgb(160, 32, 240);\">'dd MMM yyyy HH:mm:ss'<\/span>;\r\nearliestSunset = datetime(sset(ix),<span style=\"color:rgb(160, 32, 240);\">'ConvertFrom'<\/span>,<span style=\"color:rgb(160, 32, 240);\">'datenum'<\/span>);\r\nlegend(<span style=\"color:rgb(160, 32, 240);\">'off'<\/span>)\r\nline(d(ix),ssetHour(ix), <span style=\"color:rgb(0, 0, 255);\">...<\/span>\r\n    <span style=\"color:rgb(160, 32, 240);\">'Marker'<\/span>,<span style=\"color:rgb(160, 32, 240);\">'o'<\/span>,<span style=\"color:rgb(160, 32, 240);\">'Color'<\/span>,[0.85 0.325 0.098], <span style=\"color:rgb(0, 0, 255);\">...<\/span>\r\n    <span style=\"color:rgb(160, 32, 240);\">'LineWidth'<\/span>,3,<span style=\"color:rgb(160, 32, 240);\">'MarkerSize'<\/span>,18)\r\nxlim([datetime(2019,7,1) datetime(2020,7,1)])\r\nylim([12.0 24.0])\r\ntext(d(ix),ssetHour(ix), <span style=\"color:rgb(0, 0, 255);\">...<\/span>\r\n    sprintf(<span style=\"color:rgb(160, 32, 240);\">'Crepusculus!\\n%s\\n\\n\\n\\n'<\/span>,string(earliestSunset)), <span style=\"color:rgb(0, 0, 255);\">...<\/span>\r\n    <span style=\"color:rgb(160, 32, 240);\">'HorizontalAlignment'<\/span>,<span style=\"color:rgb(160, 32, 240);\">'center'<\/span>)\r\n<\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"560\" height=\"420\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/crepusculus_03.png\" alt=\"\" class=\"alignnone size-full wp-image-6853\" \/><\/p>\n<p>I like to call this date Crepusculus in honor of the word &#8220;crepuscular&#8221;, which means &#8220;of or relating to twilight&#8221;. I give it a fancy name because 1. it sounds cool and 2. it&#8217;s worth celebrating! After this date, the sun will set later every day until June. That&#8217;s almost as good as the solstice itself, especially for those of us that don&#8217;t often see the sun rise. And since it happens before the solstice, it gives me a sorely needed head start on solstitial merriment. <\/p>\n<p>But it&#8217;s not the same for everybody! The earliest sunset changes by latitude. How much? Let&#8217;s look! We&#8217;ll loop across a whole bunch of latitudes and make a plot. <\/p>\n<pre class=\"matlab-code\" id=\"matlabcode\" style=\"background-color: #F7F7F7;font-family: monospace;font-weight:normal;border-style: solid; border-width: 1px ;border-color:#E9E9E9;padding-top:5px;padding-bottom:5px;line-height:150%;\">latitudeList = 1:0.2:65;\r\nearliestSunset = NaT(size(latitudeList));\r\n<\/pre>\n<p>The <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/nat.html\">NaT<\/a> function (NaT stands for &#8220;Not a Time&#8221;) is a nice shorthand for allocating a vector of DATETIMEs. It functions like ONES or ZEROS, only for dates. <\/p>\n<pre class=\"matlab-code\" id=\"matlabcode\" style=\"background-color: #F7F7F7;font-family: monospace;font-weight:normal;border-style: solid; border-width: 1px ;border-color:#E9E9E9;padding-top:5px;padding-bottom:5px;line-height:150%;\"><span style=\"color:rgb(0, 0, 255);\">for<\/span> i = 1:length(latitudeList)\r\n    lat = latitudeList(i);\r\n    \r\n    [srise,sset] = sunrise(lat,lon,alt,tzone,datenum(d));\r\n    ssetHour = 24*(sset - floor(sset));\r\n    \r\n    [~,ix] = min(ssetHour);\r\n    earliestSunset(i) = d(ix);\r\n<span style=\"color:rgb(0, 0, 255);\">end<\/span>\r\nplot(latitudeList,earliestSunset,<span style=\"color:rgb(160, 32, 240);\">'.'<\/span>)\r\ngrid\r\nxlabel(<span style=\"color:rgb(160, 32, 240);\">'Latitude (deg)'<\/span>)\r\nylabel(<span style=\"color:rgb(160, 32, 240);\">'Date of the Earliest Sunset'<\/span>)\r\n<\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"560\" height=\"420\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/crepusculus_04.png\" alt=\"\" class=\"alignnone size-full wp-image-6855\" \/><\/p>\n<p>Use the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/diff.html\">DIFF<\/a> function to find the leading edge of each step change. <\/p>\n<pre class=\"matlab-code\" id=\"matlabcode\" style=\"background-color: #F7F7F7;font-family: monospace;font-weight:normal;border-style: solid; border-width: 1px ;border-color:#E9E9E9;padding-top:5px;padding-bottom:5px;line-height:150%;\">xlim([25 50])\r\nix = find(diff(datenum(earliestSunset)))+1;\r\nhold <span style=\"color:rgb(160, 32, 240);\">on<\/span>\r\nplot(latitudeList(ix),earliestSunset(ix),<span style=\"color:rgb(160, 32, 240);\">'ro'<\/span>)\r\nhold <span style=\"color:rgb(160, 32, 240);\">off<\/span>\r\n<\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"560\" height=\"420\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/crepusculus_05.png\" alt=\"\" class=\"alignnone size-full wp-image-6857\" \/><\/p>\n<p>Since tables are nice, let&#8217;s make a <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/table.html\">TABLE<\/a> of the mapping from latitude to date. <\/p>\n<pre class=\"matlab-code\" id=\"matlabcode\" style=\"background-color: #F7F7F7;font-family: monospace;font-weight:normal;border-style: solid; border-width: 1px ;border-color:#E9E9E9;padding-top:5px;padding-bottom:5px;line-height:150%;\">latTable = table(latitudeList(ix)',earliestSunset(ix)', <span style=\"color:rgb(0, 0, 255);\">...<\/span>\r\n    <span style=\"color:rgb(160, 32, 240);\">'VariableNames'<\/span>,{<span style=\"color:rgb(160, 32, 240);\">'Latitude'<\/span>,<span style=\"color:rgb(160, 32, 240);\">'EarliestSunset'<\/span>})\r\n<\/pre>\n<pre class=\"output\" style=\"font-family:monospace;border:none;background-color:white;color:rgba(64, 64, 64, 1);\">latTable=<em>45&times;2 table<\/em>\r\n    Latitude    EarliestSunset\r\n    ________    ______________\r\n\r\n       1.6       05-Nov-2019  \r\n       2.2       06-Nov-2019  \r\n       2.8       07-Nov-2019  \r\n       3.4       08-Nov-2019  \r\n       4.2       09-Nov-2019  \r\n       4.8       10-Nov-2019  \r\n       5.6       11-Nov-2019  \r\n       6.2       12-Nov-2019  \r\n         7       13-Nov-2019  \r\n       7.8       14-Nov-2019  \r\n       8.6       15-Nov-2019  \r\n       9.4       16-Nov-2019  \r\n      10.2       17-Nov-2019  \r\n      11.2       18-Nov-2019  \r\n        12       19-Nov-2019  \r\n        13       20-Nov-2019  \r\n      &#8942;\r\n\r\n<\/pre>\n<p>Now for the big finish. Let&#8217;s use <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/geoplot.html\">GEOPLOT<\/a> to paint the crepuscular minima across the lower 48 states of the US. <\/p>\n<pre class=\"matlab-code\" id=\"matlabcode\" style=\"background-color: #F7F7F7;font-family: monospace;font-weight:normal;border-style: solid; border-width: 1px ;border-color:#E9E9E9;padding-top:5px;padding-bottom:5px;line-height:150%;\">ixLow = find(latTable.Latitude &gt; 25,1);\r\nixHigh = find(latTable.Latitude &gt; 48,1);\r\nlons = -130:5:-60;\r\n<span style=\"color:rgb(0, 0, 255);\">for<\/span> ix = ixLow:ixHigh\r\n    \r\n    lat = latTable.Latitude(ix);\r\n    dat = latTable.EarliestSunset(ix);\r\n    geoplot(lat*ones(size(lons)),lons,<span style=\"color:rgb(160, 32, 240);\">'Color'<\/span>,<span style=\"color:rgb(160, 32, 240);\">'red'<\/span>);\r\n    hold <span style=\"color:rgb(160, 32, 240);\">on<\/span>\r\n    dat.Format = <span style=\"color:rgb(160, 32, 240);\">'MMM d'<\/span>;\r\n    text(lat,max(lons),<span >\"<\/span> <span >\"<\/span> + string(dat),<span style=\"color:rgb(160, 32, 240);\">'FontSize'<\/span>,9)\r\n    \r\n<span style=\"color:rgb(0, 0, 255);\">end<\/span>\r\nhold <span style=\"color:rgb(160, 32, 240);\">off<\/span>\r\ngeolimits([25 50],[-130 -60])\r\n<\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"560\" height=\"420\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/crepusculus_06.png\" alt=\"\" class=\"alignnone size-full wp-image-6845\" \/><\/p>\n<p>And now let&#8217;s do the same for Europe.<\/p>\n<pre class=\"matlab-code\" id=\"matlabcode\" style=\"background-color: #F7F7F7;font-family: monospace;font-weight:normal;border-style: solid; border-width: 1px ;border-color:#E9E9E9;padding-top:5px;padding-bottom:5px;line-height:150%;\">ixLow = find(latTable.Latitude &gt; 35,1);\r\nixHigh = find(latTable.Latitude &gt; 60,1);\r\nlons = -20:5:40;\r\n<span style=\"color:rgb(0, 0, 255);\">for<\/span> ix = ixLow:ixHigh\r\n    \r\n    lat = latTable.Latitude(ix);\r\n    dat = latTable.EarliestSunset(ix);\r\n    geoplot(lat*ones(size(lons)),lons,<span style=\"color:rgb(160, 32, 240);\">'Color'<\/span>,<span style=\"color:rgb(160, 32, 240);\">'red'<\/span>);\r\n    hold <span style=\"color:rgb(160, 32, 240);\">on<\/span>\r\n    dat.Format = <span style=\"color:rgb(160, 32, 240);\">'MMM d'<\/span>;\r\n    text(lat,max(lons),<span >\"<\/span> <span >\"<\/span> + string(dat),<span style=\"color:rgb(160, 32, 240);\">'FontSize'<\/span>,9)\r\n    \r\n<span style=\"color:rgb(0, 0, 255);\">end<\/span>\r\nhold <span style=\"color:rgb(160, 32, 240);\">off<\/span>\r\ngeolimits([30 65],[0 30])\r\n<\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"560\" height=\"420\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/crepusculus_07.png\" alt=\"\" class=\"alignnone size-full wp-image-6847\" \/><\/p>\n<p>If you live in the regions contained by these maps, you now know when to celebrate Crepusculus. If not, take my code and do some quick crepuscular calculations. <\/p>\n<p>However you celebrate the solstice, I hope you have a good one!<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/crepusculus_01.png\" onError=\"this.style.display ='none';\" \/><\/div>\n<p>\nHappy Crepusculus! Never heard of Crepusculus? I&#8217;ll come back to that later. Instead, let me begin with a fun fact: the shortest day of the year is December 21st. <\/p>\n<p>What? You already knew that&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/community\/2019\/12\/06\/crepuscular-calculations\/\">read more >><\/a><\/p>\n","protected":false},"author":69,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[251],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/6841"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/users\/69"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/comments?post=6841"}],"version-history":[{"count":8,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/6841\/revisions"}],"predecessor-version":[{"id":6871,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/6841\/revisions\/6871"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/media?parent=6841"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/categories?post=6841"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/tags?post=6841"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}