MathWorks-Teaching-Resources / Programming-A-Starter-Project-Using-MATLAB-and-Python

A scaffolded project involving pulling and installing a Live Task and using MATLAB and Python in combination to access and analyze data from the OpenWeather web API.
BSD 3-Clause "New" or "Revised" License
24 stars 5 forks source link

Weather examples don't work for newly-created OpenWeatherMap accounts #2

Open blakeNaccarato opened 1 year ago

blakeNaccarato commented 1 year ago

See https://github.com/domoticz/domoticz/issues/5336#issuecomment-1245132310 for this problem affecting another project as well. Basically, only the following URL works for newly-created accounts:

https://api.openweathermap.org/data/2.5/weather?lat=40&lon=-115&appid=$appid

this URL differs from the URL in the tutorial in that onecall is replaced with weather. I think the URL above should work for old and new accounts alike.

But this may break again some day as well. If OpenWeatherMap fully shuts down their 2.5 API, as the onecall API requires users to follow this process (scroll to bottom of page, reply by Uwe Jacobs), which requires a credit card and a billing subscription even though you're signing up for the "free" plan. If this tutorial eventually has to go down that path (well, an alternative should be found in that case), then users should be instructed to set their billing limit to 1000 calls so that their credit card won't get charged for excess calls.

For now, swapping out onecall for weather in the URL should work, but you may want to try this on an old OpenWeatherMap account (over a year old) and a newly-created account just in case! The result of the API call is the following, so if the tutorial uses other keys not present here, the actual tutorial contents may need modification as well:

{
  "coord": {
    "lon": -115,
    "lat": 40
  },
  "weather": [
    {
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "01d"
    }
  ],
  "base": "stations",
  "main": {
    "temp": 296.68,
    "feels_like": 295.48,
    "temp_min": 296.68,
    "temp_max": 296.68,
    "pressure": 1014,
    "humidity": 15,
    "sea_level": 1014,
    "grnd_level": 817
  },
  "visibility": 10000,
  "wind": {
    "speed": 4.37,
    "deg": 239,
    "gust": 5.02
  },
  "clouds": {
    "all": 6
  },
  "dt": 1697491946,
  "sys": {
    "country": "US",
    "sunrise": 1697464268,
    "sunset": 1697504381
  },
  "timezone": -25200,
  "id": 5707899,
  "name": "Rock House",
  "cod": 200
}
blakeNaccarato commented 9 months ago

I know I opened this awhile ago, I had occasion to revisit this and figured I'd drop another breadcrumb. In my above comment I talk about modifying a URL in the repo, specifically I'm referring to this line in checkweather.py:

https://github.com/MathWorks-Teaching-Resources/Programming-A-Starter-Project-Using-MATLAB-and-Python/blob/e2f07ee4e6c8c87a5017a132cbb5a8747193dbfa/checkweather.py#L23

where something like the following should work:

BASEURL = '
https://api.openweathermap.org/data/2.5/weather?lat={}&lon={}&exclude=minutely,daily,alerts&units={}&appid={}'

anyways, some other changes will probably be needed in the tutorial flow, but that sums it up. You won't be able to reproduce this locally if you're using an existing OpenWeatherMap account, you'll have to try to follow the tutorial flow with a brand new account to witness the breakage.

eszmw commented 8 months ago

Just to follow up, I am working on this but it is a bigger fix than I hoped because the data available in the "weather" API is much less than the data that used to be available in the "onecall 2.5" API. I'm playing with various options. For anyone who wants to keep the current flow and is willing to give OpenWeather billing information, there are still 1000 free calls/day to the Onecall 3.0 API: https://api.openweathermap.org/data/3.0/onecall?lat={lat}&lon={lon}&exclude={part}&appid=[{API key}](https://home.openweathermap.org/api_keys)

The change is that to register for access requires payment information which was not the case in 2.5 unless you went over 1000 calls/day.

blakeNaccarato commented 8 months ago

Ah, yeah that's understandably a bigger fix than just swapping the calls! I imagine that probably means a complete rewrite of some of the tutorial contents. It's a bummer that an API changing out from under you like that can create so much more work. Depending on the severity of the rewrite that is necessary, I wonder whether there are more stable weather data sources to base the tutorial on? It could probably be stabilized on a historical weather data source, but I understand the tutorial might end up less "awesome" if the user can't actually plot actual weather in their zip code that way.

Thanks for the heads up about the 1000 free calls per day for those brave enough to add a credit card 😅. I'll have to try that and see how many calls the tutorial consumes per run-through. Other options might be to introduce a "run once" flag into the notebooks/project, which guarantees the API will only be hit once, caching the pulled data for local use. Or to bundle an example weather dataset which ensures the example can always be run against the sample data. But of course I understand how those solutions sort of "miss the point", because the central thesis is to demonstrate how Matlab uses Python to talk to APIs.

In any case, thanks for the update!

eszmw commented 7 months ago

Okay, I finally pushed my updates. Blake, if you get a chance to try them out an make sure they work for you before we close out this issue? I'm pretty hopeful I hunted down the bugs this time, though. The new version walks through building a real app to display your results, too.

blakeNaccarato commented 7 months ago

Thanks for reaching out, I'm looking forward to trying it out. Wow, it's a big changeset! I appreciate the accessibility improvement of adding alt-text to images, I didn't realize you could test Matlab on GitHub Actions, that's pretty cool.

I'll try to run through the new material over the next few weeks and check in as I go along. I've been meaning to write up a quick-reference to version-controlled Matlab/Python projects for colleagues, but I'm on a new machine and I've been waiting till 2024a to reinstall Matlab and jump through the hoops.