AlienDwarf / open-meteo-dotnet

A .Net library for the Open-Meteo.com API.
MIT License
26 stars 9 forks source link

Daily and Hourly properties always null #24

Closed leofox2021 closed 1 year ago

leofox2021 commented 1 year ago

when I try to access the following properties by doing

OpenMeteoClient.Query(city).Daily

or

OpenMeteoClient.Query(city).Hourly

I get null no matter the city.

Version

0.2.8

Additional context

.Net 7.0, ASP.NET project

AlienDwarf commented 1 year ago

Hello @leofox2021 ! Thank you for your bug report.

You didn't include any code snippets, so I'll try to guess what the problem is.

public WeatherForecast? Query(string location) returns a WetherForecast? with the following options:

So you can only access the current weather if you use Query(string location).

Solution If you want to set different options you must assign them manually. public async Task<WeatherForecast?> QueryAsync(string location, WeatherForecastOptions options)

Example 1 (consumes around 50mb (!) of RAM)

OpenMeteoClient client = new();
WeatherForecastOptions options = new()
{
   Hourly = HourlyOptions.All,
   Daily = DailyOptions.All,
};
var res = client.Query("Tokyo", options);
// res.Hourly is now not null

Example 2 only specific options

OpenMeteoClient client = new();
// Constructor
// public WeatherForecastOptions(float latitude, float longitude, TemperatureUnitType temperature_Unit, WindspeedUnitType windspeed_Unit, PrecipitationUnitType precipitation_Unit, string timezone, HourlyOptions hourly, DailyOptions daily, bool current_Weather, TimeformatType timeformat, int past_Days, string start_date, string end_date, WeatherModelOptions models, CellSelectionType cell_selection)

WeatherForecastOptions options = new WeatherForecastOptions(0f, 1f, TemperatureUnitType.celsius, WindspeedUnitType.kmh, PrecipitationUnitType.mm, "GMT", new HourlyOptions(HourlyOptionsParameter.temperature_2m), new DailyOptions(DailyOptionsParameter.sunrise), true, TimeformatType.iso8601, 0, "2022-08-30", "2022-08-31", null, CellSelectionType.nearest);

var res = client.Query("Tokyo", options);

// res.Daily.Sunrise, res.Hourly.Temperature_2m not NULL
// res.Daily_units.Sunrise = ""
// res.Hourly_units.Temperature_2m = "°C"

HourlyOptions and DailyOptions also accept an Array in their constructor: public HourlyOptions(HourlyOptionsParameter[] parameter)

Example:

HourlyOptions options = new HourlyOptions(new HourlyOptionsParameter[] { HourlyOptionsParameter.cape, HourlyOptionsParameter.cloudcover, HourlyOptionsParameter.temperature_120m });
AlienDwarf commented 1 year ago

Hello @leofox2021

Please let me know if this solved your problem and if you have further questions :)

leofox2021 commented 1 year ago

Thanks, this has worked! Maybe it's worth putting into the documentation? I can help you in case you need it.

AlienDwarf commented 1 year ago

With pleasure! I am happy about everyone who wants to support this project.

Yea, the documentation is still far from being production ready... I'm working on some optimizations myself and trying to get backwards compatibility but it will take some time.

So feel free to commit to this project :)