Skip to content

Commit

Permalink
working example code
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-zippenfenig committed Mar 14, 2024
1 parent d6b42ee commit 737d76d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
36 changes: 34 additions & 2 deletions OpenMeteo.Tests/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,41 @@ public class UnitTest1
public async void Test1()
{
var client = new OpenMeteoClient();
var uri = new Uri("https://api.open-meteo.com/v1/forecast?latitude=52.52&longitude=13.41&hourly=temperature_2m&format=flatbuffers");
var uri = new Uri("https://api.open-meteo.com/v1/forecast?latitude=52.52&longitude=13.41&hourly=temperature_2m,windspeed_10m&daily=temperature_2m_max,temperature_2m_min&format=flatbuffers&timezone=auto");
var results = await client.GetWeather(uri);

Console.WriteLine(results[0].Latitude);
var result = results[0];
Console.WriteLine($"Latitude: {result.Latitude}");
Console.WriteLine($"Longitude: {result.Longitude}");
Console.WriteLine($"Elevation: {result.Elevation}");
Console.WriteLine($"Timezone: {result.Timezone} (UTC offset {result.UtcOffsetSeconds} seconds)");

var hourly = result.Hourly ?? throw new ArgumentNullException(nameof(result.Hourly));
var temperature_2m = hourly.Variables(0)?.GetValuesArray() ?? throw new ArgumentNullException("temperature_2m");
var windspeed_10m = hourly.Variables(1)?.GetValuesArray() ?? throw new ArgumentNullException("windspeed_10m");
Console.WriteLine();
Console.WriteLine("hour | temperature_2m | windspeed_10m");
for (var i = 0; i < (hourly.TimeEnd - hourly.Time) / hourly.Interval; i += 1) {
// By adding `UtcOffsetSeconds` the print function below will print the local date
var time = hourly.Time + i * hourly.Interval + result.UtcOffsetSeconds;
Console.Write(DateTimeOffset.FromUnixTimeSeconds(time).ToString("yyyy-MM-dd HH:mm:ss"));
Console.Write($" | {temperature_2m[i]}");
Console.Write($" | {windspeed_10m[i]}");
Console.Write("\n");
}

var daily = result.Daily ?? throw new ArgumentNullException(nameof(result.Daily));
var temperature_2m_max = daily.Variables(0)?.GetValuesArray() ?? throw new ArgumentNullException("temperature_2m_max");
var temperature_2m_min = daily.Variables(1)?.GetValuesArray() ?? throw new ArgumentNullException("temperature_2m_min");
Console.WriteLine();
Console.WriteLine("day | temperature_2m_max | temperature_2m_min");
for (var i = 0; i < (daily.TimeEnd - daily.Time) / daily.Interval; i += 1) {
// By adding `UtcOffsetSeconds` the print function below will print the local date
var time = daily.Time + i * daily.Interval + result.UtcOffsetSeconds;
Console.Write(DateTimeOffset.FromUnixTimeSeconds(time).ToString("yyyy-MM-dd HH:mm:ss"));
Console.Write($" | {temperature_2m_max[i]}");
Console.Write($" | {temperature_2m_min[i]}");
Console.Write("\n");
}
}
}
13 changes: 13 additions & 0 deletions OpenMeteo/OpenMeteo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,18 @@ protected override async Task<HttpResponseMessage> SendAsync(
return response;

Check warning on line 104 in OpenMeteo/OpenMeteo.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference return.

Check warning on line 104 in OpenMeteo/OpenMeteo.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference return.

Check warning on line 104 in OpenMeteo/OpenMeteo.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference return.

Check warning on line 104 in OpenMeteo/OpenMeteo.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference return.

Check warning on line 104 in OpenMeteo/OpenMeteo.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference return.
}
}

/*public static class VariablesWithTimeExtension {
/// <summary>
/// Iterate over timestamps
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static IEnumerable<DateTimeOffset> DateTimeEnumerator(this VariablesWithTime value)
{
for (var time = value.Time; time < value.TimeEnd; time += value.Interval)
yield return DateTimeOffset.FromUnixTimeSeconds(time);
}
}*/
}

0 comments on commit 737d76d

Please # to comment.