<commit_before>using System;
using Newtonsoft.Json.Linq;

namespace IvionWebSoft
{
    public class OpenWeatherMap
    {
        public string ApiKey { get; private set; }
        readonly string owmQuery;


        public OpenWeatherMap(string apiKey)
        {
            if (apiKey == null)
                throw new ArgumentNullException(nameof(apiKey));

            ApiKey = apiKey;
            owmQuery = string.Concat(
                "https://api.openweathermap.org/data/2.5/weather?appid=", apiKey,
                "&units=metric&q="
            );
        }


        public WeatherConditions GetConditions(string location)
        {
            if (location == null)
                throw new ArgumentNullException(nameof(location));

            var query = string.Concat(owmQuery, Uri.EscapeDataString(location));

            var queryResult = WebString.Download(query);
            if (!queryResult.Success)
                return new WeatherConditions(queryResult);

            var json = JObject.Parse(queryResult.Document);
            var message = (string)json["message"];

            if (string.IsNullOrEmpty(message))
            {
                return new WeatherConditions(queryResult.Location, json);
            }
            else
            {
                var ex = new JsonErrorException(message);
                return new WeatherConditions(queryResult.Location, ex);
            }
        }
    }


    public class WeatherConditions : WebResource
    {
        public string WeatherLocation { get; private set; }
        public string Description { get; private set; }

        public double TemperatureInC { get; private set; }
        public double TemperatureInF { get; private set; }
        public double RelativeHumidity { get; private set; }

        //public double PrecipitationInInches { get; private set; }
        //public double PrecipitationInMillimeters { get; private set; }

        public string WindDirection { get; private set; }

        public double WindSpeedInKph { get; private set; }
        public double WindSpeedInMph { get; private set; }


        public WeatherConditions(WebResource resource) : base(resource) {}

        public WeatherConditions(Uri uri, Exception ex) : base(uri, ex) {}

        internal WeatherConditions(Uri uri, JToken observation) : base(uri)
        {
            WeatherLocation =
                (string)observation["name"] + ", " +
                (string)observation["sys"]["country"];

            Description =
                Capitalize((string)observation["weather"][0]["description"]);

            TemperatureInC = (double)observation["main"]["temp"];
            // Convert to Fahrenheit.
            TemperatureInF = (TemperatureInC * 1.8) + 32;
            RelativeHumidity = (double)observation["main"]["humidity"];

            WindDirection = DegreesToDirection(observation["wind"]["deg"]);

            var windspeed = (double)observation["wind"]["speed"];
            // Wind speed is in m/s, convert to km/h and mph.
            WindSpeedInKph = windspeed * 3.6;
            // Approximate mph, 4 significant digits should be enough.
            WindSpeedInMph = windspeed * 2.237;
        }


        static string Capitalize(string s)
        {
            if (!string.IsNullOrEmpty(s))
            {
                return char.ToUpper(s[0]) + s.Substring(1);
            }
            return s;
        }

        static double ToDouble(JToken el, double defaultValue)<commit_msg>IvionWebSoft: Add precipitation and wind gust<commit_after>using System;
using Newtonsoft.Json.Linq;

namespace IvionWebSoft
{
    public class OpenWeatherMap
    {
        public string ApiKey { get; private set; }
        readonly string owmQuery;


        public OpenWeatherMap(string apiKey)
        {
            if (apiKey == null)
                throw new ArgumentNullException(nameof(apiKey));

            ApiKey = apiKey;
            owmQuery = string.Concat(
                "https://api.openweathermap.org/data/2.5/weather?appid=", apiKey,
                "&units=metric&q="
            );
        }


        public WeatherConditions GetConditions(string location)
        {
            if (location == null)
                throw new ArgumentNullException(nameof(location));

            var query = string.Concat(owmQuery, Uri.EscapeDataString(location));

            var queryResult = WebString.Download(query);
            if (!queryResult.Success)
                return new WeatherConditions(queryResult);

            var json = JObject.Parse(queryResult.Document);
            var message = (string)json["message"];

            if (string.IsNullOrEmpty(message))
            {
                return new WeatherConditions(queryResult.Location, json);
            }
            else
            {
                var ex = new JsonErrorException(message);
                return new WeatherConditions(queryResult.Location, ex);
            }
        }
    }


    public class WeatherConditions : WebResource
    {
        public string WeatherLocation { get; private set; }
        public string Description { get; private set; }

        public double TemperatureInC { get; private set; }
        public double TemperatureInF { get; private set; }
        public double RelativeHumidity { get; private set; }

        public double PrecipitationInMillimeters { get; private set; }
        public double PrecipitationInInches { get; private set; }

        public string WindDirection { get; private set; }

        public double WindSpeedInKph { get; private set; }
        public double WindSpeedInMph { get; private set; }

        public double WindGustInKph { get; private set; }
        public double WindGustInMph { get; private set; }


        public WeatherConditions(WebResource resource) : base(resource) {}

        public WeatherConditions(Uri uri, Exception ex) : base(uri, ex) {}

        internal WeatherConditions(Uri uri, JToken observation) : base(uri)
        {
            WeatherLocation =
                (string)observation["name"] + ", " +
                (string)observation["sys"]["country"];

            Description =
                Capitalize((string)observation["weather"][0]["description"]);

            TemperatureInC = (double)observation["main"]["temp"];
            // Convert to Fahrenheit.
            TemperatureInF = (TemperatureInC * 1.8) + 32;
            RelativeHumidity = (double)observation["main"]["humidity"];

            PrecipitationInMillimeters = Precip(observation["rain"], observation["snow"]);
            // Convert to inches.
            PrecipitationInInches = PrecipitationInMillimeters / 25.4;

            WindDirection = DegreesToDirection(observation["wind"]["deg"]);

            var windspeed = (double)observation["wind"]["speed"];
            // Wind speed is in m/s, convert to km/h and mph.
            WindSpeedInKph = windspeed * 3.6;
            // Approximate mph, 4 significant digits should be enough.
            WindSpeedInMph = windspeed * 2.237;

            // Same as wind speed, but wind gust is usually absent, so
            // use `ToDouble`.
            var windgust = ToDouble(observation["wind"]["gust"], 0);
            WindGustInKph = windgust * 3.6;
            WindGustInMph = windgust * 2.237;
        }


        static string Capitalize(string s)
        {
            if (!string.IsNullOrEmpty(s))
            {
                return char.ToUpper(s[0]) + s.Substring(1);
            }
            return s;
        }

        static double Precip(JToken rainEl, JToken snowEl)
        {
            // Futz rain and snow together to get total percipitation.
            // We also need to check for null because these elements are
            // usually absent.
            double sum = 0;
            if (rainEl != null)
                sum += ToDouble(rainEl["1h"], 0);
            if (snowEl != null)
                sum += ToDouble(snowEl["1h"], 0);

            return sum;
        }

        static double ToDouble(JToken el, double defaultValue)
{'fraction_non_alphanumeric': 0.09593958900718, 'fraction_numerical': 0.00581827184946769, 'mean_word_length': 1.9431693989071037, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3077530', 'n_tokens_mistral': 2207, 'n_tokens_neox': 2062, 'n_words': 533}