How do we get Night Time Icons at Night (instead of wrong day time icons)?

  • 4
  • Question
  • Updated 2 years ago
Using this to get icon: http://icons.wxug.com/i/c/a/ICON.gif

ICON replaced with condition. So for hazy it would be: http://icons.wxug.com/i/c/a/hazy.gif

However, it always returns daytime icon. Even in the middle of the night. Should we be using some other url to get night time weather icons.
Photo of visualweather

visualweather

  • 4 Posts
  • 0 Reply Likes

Posted 6 years ago

  • 4
Photo of visualweather

visualweather

  • 4 Posts
  • 0 Reply Likes
Update: The solution is:

The response has two fields 'icon_url' and 'icon'. 'icon_url' is full url to icon from default icon set. 'icon' is just icon name for use if you want to use a different icon set. I used 'icon' field, but found that 'icon_url' can contains daytime and night time icons, but 'icon' contains only daytime icons. So switching from using icon field to icon_url field fixed the issue.
Photo of Brendan Hayes

Brendan Hayes, Official Rep

  • 962 Posts
  • 122 Reply Likes
You're correct. We're going to update the "icon" field to also show nt_ for night time automatically during our next code update.
Photo of Marcel Villerius

Marcel Villerius

  • 2 Posts
  • 0 Reply Likes
Brendan, can you tell in what way daytime icon names distinguish from night time names?
Photo of sconners .

sconners .

  • 3 Posts
  • 0 Reply Likes
I might be misunderstanding this but the 'icon' field doesn't show the nt_ but the 'icon_url' field does.  Is this going to be changed?

This is based on a query run 2 days ago.
Photo of Marcel Villerius

Marcel Villerius

  • 2 Posts
  • 0 Reply Likes
Hi Brendan.

I discovered that the nt_ icons are available in the conditions feature, for 'icon_url'. The next update it will be available for the 'icon'.
Does the appearance of the 'nt_icon' depend on the location query?
And will the nt_icons become also available in the forecast feature?
Photo of Will

Will

  • 7 Posts
  • 1 Reply Like
Anyone have any updates on this subject? I just implemented the API for my own site, and I too have noticed that nighttime icons are not showing up, just daytime icons. I see that the last activity on this topic was 10 months ago... Thanks.
Photo of Will

Will

  • 7 Posts
  • 1 Reply Like
So is this not going to happen then? Updating "icon" to include night-time image file names? Because it hasn't happened so far, despite the update above as of 12 months ago...
Photo of Brendan Hayes

Brendan Hayes, Official Rep

  • 962 Posts
  • 122 Reply Likes
I know, right? You're "I'm blah" emoticon is accurate. It is on the list of to-do's, and we are working towards it.
Photo of Will

Will

  • 7 Posts
  • 1 Reply Like
Thanks Brendan, I appreciate the reply.

For now I've got a simple script that checks the current time and picks the appropriate local image according to whether it's between 6am & 6pm or not, but obviously I'd rather have the icons based on actual sunup/sundown times on any given day. Anyway...
Photo of Paul Reed

Paul Reed

  • 23 Posts
  • 7 Reply Likes
I am presently using "icon_url" to add a weather icon to my website, but would like to use a different icon set (maybe even my own images).
Has anyone got a script that will substitute the text returned via "icon", to a corresponding image file/url.

Thanks

Paul
Photo of Will

Will

  • 7 Posts
  • 1 Reply Like
Hey Paul, I ended up manually renaming the files of the different image set I wanted to use (so they would correspond to the names being returned by the API) and just adjusting the "URL" path variable to point to them instead. I can paste in the relevant script if you like... I'm doing it in C#, fyi.
Photo of Paul Reed

Paul Reed

  • 23 Posts
  • 7 Reply Likes
I'm not good at coding!! but I have a php script parsing the data on my Raspberry Pi, and passing the url to the website page, like this;


(had to post as an image because the site has banned url's & certain code!)

So, yes if you could post the code or point me in the right direction, that would be great.

Paul
Photo of Will

Will

  • 7 Posts
  • 1 Reply Like
Okay, here's the ParseWeather method in my widget where the URL ("weather.ImageUri") is constructed. You can see how it gets built:


private static Weather ParseWeather(string zipCode, string json)
{
WeatherInformation[] forecastItems = new WeatherInformation[4];
Weather weather = new Weather(zipCode, forecastItems);

JsonValue jsonObject = JsonValue.Parse(json);
JsonValue location = jsonObject["value"]["items"][0]["current_observation"]["display_location"];
JsonValue currentConditions = jsonObject["value"]["items"][0]["current_observation"];
JsonValue forecastConditions = jsonObject["value"]["items"][0]["forecast"]["simpleforecast"]["forecastday"];

weather.Location = location["full"] + " " + "(" + zipCode + ")";
weather.Description = currentConditions["weather"];
weather.Temperature = currentConditions["temp_f"];

if (DateTime.Now.Hour > 05 && DateTime.Now.Hour < 18)
{
weather.ImageUri = "/WUimages/" + currentConditions["icon"] + ".png";
}

else
{
weather.ImageUri = "/WUimages/nt_" + currentConditions["icon"] + ".png";
}

weather.Humidity = currentConditions["relative_humidity"];
weather.Feelslike = currentConditions["feelslike_f"];
weather.UV = currentConditions["UV"];

string UnconvertedDateTime = (currentConditions["local_epoch"]);
UnconvertedDateTime = UnconvertedDateTime.Substring(0, Math.Min(UnconvertedDateTime.Length, 10));

double DateTimeConverted = double.Parse(UnconvertedDateTime) - (double)14400;

DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);

origin = origin.AddSeconds(DateTimeConverted);

weather.TimeStamp = origin.ToShortDateString() + " " + origin.ToShortTimeString();

for (int i = 1; i < 5; i++)
{
WeatherInformation wi = new WeatherInformation();
JsonValue forecast = forecastConditions[i];
JsonValue dayInfo = forecast["conditions"];
JsonValue nightInfo = forecast["conditions"];
JsonValue IconDay = forecast["icon"];
JsonValue IconNight = forecast["icon"];

string UnconvertedDateTimeFC = (forecast["date"]["epoch"]);
UnconvertedDateTimeFC = UnconvertedDateTimeFC.Substring(0, Math.Min(UnconvertedDateTimeFC.Length, 10));

double DateTimeConvertedFC = double.Parse(UnconvertedDateTimeFC);

DateTime originFC = new DateTime(1970, 1, 1, 0, 0, 0, 0);

origin = originFC.AddSeconds(DateTimeConvertedFC);

wi.Day = forecast["date"]["weekday"].ToString().Substring(1, 3);
wi.Date = originFC.ToShortDateString() + " " + originFC.ToShortTimeString();
wi.Low = forecast["low"]["fahrenheit"];
wi.High = forecast["high"]["fahrenheit"];
wi.Precip = forecast["pop"] + "%";
wi.DayDescription = dayInfo;
wi.DayImageUrl = "/WUimages/" + IconDay + ".png";
wi.NightDescription = nightInfo;
wi.NightImageUrl = "/WUimages/" + IconNight + ".png";

forecastItems[i - 1] = wi;
}

return weather;
Photo of Paul Reed

Paul Reed

  • 23 Posts
  • 7 Reply Likes
Will, I'll spend some time working through that, but I think I can see how you have constructed the uri, particularly;

weather.ImageUri = "/WUimages/" + currentConditions["icon"] + ".png";

Thank you

Paul
Photo of Will

Will

  • 7 Posts
  • 1 Reply Like
Yep, that's it, and depending on time of day, I have it make a choice between daytime and nighttime versions of my images (nighttime gets the "nt_" prefix). And I just point to the local image directory on my server instead of WU's image URL.
Photo of Paul Reed

Paul Reed

  • 23 Posts
  • 7 Reply Likes
I've just updated my previous post (as it was partly blocked due to containing code!)

Paul
Photo of DaveL17

DaveL17

  • 9 Posts
  • 1 Reply Like
I've written a Python function which, while simplistic, is also inelegant...

### Code begins ###

from urlparse import urlparse

def iconName(val):
....val1= urlparse(val) # Parse the icon URL
....val2 = val1.path # Keep only the path component of the parsed URL
....val3 = val2.lstrip("/i/c/a/") # Strip the path info
....val4 = val3rstrip(".gif") # Strip the filename extension
....return val4 # return the proper icon name with nt_ where appropriate

x = "Weather Underground Icon URL Parsed From JSON"
iconStrip = iconName(x)

### Code ends ###

This of course will break if WU changes the path to the icon image.

[The leading periods should be changed to 4 spaces.]
[Replace the 'a' in the path to the corresponding image set.]
Photo of DaveL17

DaveL17

  • 9 Posts
  • 1 Reply Like
i actually found that there is a bug in the version of urlparse (or in lstrip, not sure) I'm using (Python 2.5) which sometimes broke the icon name when the url was parsed. So I decided to go a different route that relies more heavily on the proper icon name coming from WUND and prepending the indicator for night time (nt_). It's based on this logic:

### Code begins ###

if sunrise [equalto/lessthan] current time [equalto/lessthan] sunset:
....properIcon = parsed['current_observation']['icon']
else:
....properIcon = 'nt_' + parsed['current_observation']['icon']

### Code ends ###
Photo of Mike McRoberts

Mike McRoberts

  • 4 Posts
  • 0 Reply Likes
Can anyone recommend another weather API as it is obvious that no development occurs with Wunderground. There are things that have bene promised that are now 4 years out of date, such as this query, that still have not been resolved. Is there a good weather API where the developers care that anyone can recommend?
Photo of Paul Reed

Paul Reed

  • 23 Posts
  • 7 Reply Likes
Try this url for the api;
http://api.wunderground.com/api/your-api-key/conditions_v11/q/EGCN.json

The '_v11' in the api string will return the night time icons.

Paul
Photo of DaveL17

DaveL17

  • 5 Posts
  • 0 Reply Likes
Thanks for this information, I'll check it out.  How did you find out about the version flag? 
Photo of Paul Reed

Paul Reed

  • 23 Posts
  • 7 Reply Likes
google!
Photo of Mike McRoberts

Mike McRoberts

  • 4 Posts
  • 0 Reply Likes
I presume the EGCN part of that string I have to replace with my city?
Photo of DaveL17

DaveL17

  • 5 Posts
  • 0 Reply Likes
That's right, but the format may vary depending on whether you're using an ICAO site, a city name or a PWS (as examples).
Photo of Mike McRoberts

Mike McRoberts

  • 4 Posts
  • 0 Reply Likes
Thanks Paul, this works and I am now getting the correct night icons. Why this isn't part of the standard API is totally beyond me. The mind boggles. 
Photo of Paul Reed

Paul Reed

  • 23 Posts
  • 7 Reply Likes
The mind boggles...
Yep, mine too! it's hard to believe that a business as large as WU is failing to address the concerns of it's users, especially when they already have a solution....

Paul