weather.c

← Back to explorer
src/weather.c
#include <string.h>
#include "weather.h"

void reset_weather(Weather *pWeather) {
  memset(pWeather, 0, sizeof(*pWeather));
  pWeather->temperature = pWeather->humidity = pWeather->windspeed = pWeather->winddirection = pWeather->rainintensity = pWeather->rainaccumulation = pWeather->pressure = pWeather->visibility = NAN;
  pWeather->http_status = 0;
  pWeather->OK = 0;
  pWeather->data_source = 0;
  pWeather->lightning = 0;
  pWeather->api_message[0] = '\0';
}

WeatherCondition infer_weather_condition(const Weather *pWeather) {
  if (!pWeather || !pWeather->OK){
    return ZW_UNKNOWN;
  }

  int rain_severity_rain = (!isnan(pWeather->rainintensity) && pWeather->rainintensity > 0.02);
  int rain_severity_drizzle = (!isnan(pWeather->rainintensity) && pWeather->rainintensity > 0.0 && pWeather->rainintensity <= 0.02);
  int low_visibility = (!isnan(pWeather->visibility) && pWeather->visibility < 3.0); // in kilometers.

  if (pWeather->lightning && (rain_severity_rain || rain_severity_drizzle)) {
    return ZW_THUNDERSTORM;
  }

  if (rain_severity_rain) {
    return ZW_RAIN;
  }

  if (rain_severity_drizzle) {
    return ZW_DRIZZLE;
  }

  if (low_visibility) {
    return ZW_HAZE;
  }

  if (!isnan(pWeather->humidity) && pWeather->humidity > 85.0) {
    return ZW_CLOUDY;
  }

  if (!isnan(pWeather->humidity) && pWeather->humidity > 65.0) {
    return ZW_PARTLY_CLOUDY;
  }

  return ZW_SUNNY;
}