summaryrefslogtreecommitdiff
path: root/libgweather/gweather-private.h
blob: f5ea83becb08aed9cd0b46c49daa58bfd5886877 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* weather-priv.h - Private header for weather server functions.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see
 * <https://www.gnu.org/licenses/>.
 */

#ifndef __WEATHER_PRIV_H_
#define __WEATHER_PRIV_H_

#include "config.h"

#include <time.h>
#include <libintl.h>
#include <math.h>
#include <gio/gio.h>
#include <libsoup/soup.h>
#include <glib/gi18n-lib.h>

#include "gweather-weather.h"
#include "gweather-location.h"
#include "gweather-db.h"

#define INVALID_IDX G_MAXUINT16
#define IDX_VALID(idx) ((idx) >= 0 && (idx) < 0xffff)
#define EMPTY_TO_NULL(s) ((s)[0] == '\0' ? NULL : (s))

void        _gweather_gettext_init (void);


typedef struct {
    GMappedFile *map;
    DbWorldRef world;
    DbArrayofLocationRef locations_ref;
    DbWorldTimezonesRef timezones_ref;

    GPtrArray *locations;
    GPtrArray *timezones;

    time_t year_start;
    time_t year_end;
} GWeatherDb;

struct _GWeatherLocation {
    GWeatherDb *db;
    guint       db_idx;
    DbLocationRef ref;

    /* Attributes with _ may be fetched/filled from the database on the fly */
    char *_english_name, *_local_name, *_local_sort_name, *_english_sort_name;
    guint16 parent_idx; /* From the DB, except for nearest clones */
    GWeatherLocation *_parent, **_children;
    GWeatherTimezone *_timezone;
    GWeatherLocationLevel level;
    char *_country_code;
    guint16 tz_hint_idx;
    char *_station_code;
    double latitude, longitude;
    gboolean latlon_valid;

    int ref_count;
};

#define WEATHER_LOCATION_CODE_LEN 4

typedef struct {
    gchar *name;
    gchar *code;
    gchar *zone;
    gchar *radar;
    gboolean latlon_valid;
    gdouble  latitude;
    gdouble  longitude;
    gchar *country_code;
    gchar *tz_hint;
} WeatherLocation;

GWeatherLocation *_gweather_location_new_detached (GWeatherLocation *nearest_station,
						   const char       *name,
						   gboolean          latlon_valid,
						   gdouble           latitude,
						   gdouble           longitude);

void              _gweather_location_update_weather_location (GWeatherLocation *gloc,
							      WeatherLocation  *loc);

GWeatherTimezone * _gweather_timezone_ref_for_idx (GWeatherDb       *db,
						    guint             idx);


/*
 * Weather information.
 */

typedef gdouble GWeatherTemperature;
typedef gdouble GWeatherHumidity;
typedef gdouble GWeatherWindSpeed;
typedef gdouble GWeatherPressure;
typedef gdouble GWeatherVisibility;
typedef time_t GWeatherUpdate;

struct _GWeatherInfo {
    GObject parent_instance;

    GWeatherProvider providers;
    GSettings *settings;

    gboolean valid;
    gboolean network_error;
    gboolean sunriseValid;
    gboolean sunsetValid;
    gboolean midnightSun;
    gboolean polarNight;
    gboolean moonValid;
    gboolean tempMinMaxValid;

    /* TRUE if we don't need to calc humidity from
       temperature and dew point (and conversely, we
       need to calculate the dew point from temperature
       and humidity)
    */
    gboolean hasHumidity;

    WeatherLocation location;
    GWeatherLocation *glocation;
    GWeatherUpdate update;
    GWeatherUpdate current_time;
    GWeatherSky sky;
    GWeatherConditions cond;
    GWeatherTemperature temp;
    GWeatherTemperature temp_min;
    GWeatherTemperature temp_max;
    GWeatherTemperature dew;
    GWeatherHumidity humidity;
    GWeatherWindDirection wind;
    GWeatherWindSpeed windspeed;
    GWeatherPressure pressure;
    GWeatherVisibility visibility;
    GWeatherUpdate sunrise;
    GWeatherUpdate sunset;
    GWeatherMoonPhase moonphase;
    GWeatherMoonLatitude moonlatitude;
    GSList *forecast_list; /* list of GWeatherInfo* for the forecast, NULL if not available */
    gchar *forecast_attribution;
    gchar *radar_buffer;
    gchar *radar_url;
    GdkPixbufLoader *radar_loader;
    GdkPixbufAnimation *radar;
    SoupSession *session;
    GSList *requests_pending;
};

/* Values common to the parsing source files */

#define DATA_SIZE			5000

#define CONST_DIGITS			"0123456789"
#define CONST_ALPHABET			"ABCDEFGHIJKLMNOPQRSTUVWXYZ"

/* Units conversions and names */

#define TEMP_F_TO_C(f)			(((f) - 32.0) * 0.555556)
#define TEMP_F_TO_K(f)			(TEMP_F_TO_C (f) + 273.15)
#define TEMP_C_TO_F(c)			(((c) * 1.8) + 32.0)

#define WINDSPEED_KNOTS_TO_KPH(knots)	((knots) * 1.851965)
#define WINDSPEED_KNOTS_TO_MPH(knots)	((knots) * 1.150779)
#define WINDSPEED_KNOTS_TO_MS(knots)	((knots) * 0.514444)
#define WINDSPEED_MS_TO_KNOTS(ms)	((ms) / 0.514444)
/* 1 bft ~= (1 m/s / 0.836) ^ (2/3) */
#define WINDSPEED_KNOTS_TO_BFT(knots)	(pow ((knots) * 0.615363, 0.666666))

#define PRESSURE_INCH_TO_KPA(inch)	((inch) * 3.386)
#define PRESSURE_INCH_TO_HPA(inch)	((inch) * 33.86)
#define PRESSURE_INCH_TO_MM(inch)	((inch) * 25.40005)
#define PRESSURE_INCH_TO_MB(inch)	(PRESSURE_INCH_TO_HPA (inch))
#define PRESSURE_INCH_TO_ATM(inch)	((inch) * 0.033421052)
#define PRESSURE_MBAR_TO_INCH(mbar)	((mbar) * 0.029533373)

#define VISIBILITY_SM_TO_KM(sm)		((sm) * 1.609344)
#define VISIBILITY_SM_TO_M(sm)		(VISIBILITY_SM_TO_KM (sm) * 1000)

#define DEGREES_TO_RADIANS(deg)		((fmod ((deg),360.) / 180.) * M_PI)
#define RADIANS_TO_DEGREES(rad)		((rad) * 180. / M_PI)
#define RADIANS_TO_HOURS(rad)		((rad) * 12. / M_PI)

char           *_radians_to_degrees_str (gdouble radians);

/*
 * Planetary Mean Orbit and their progressions from J2000 are based on the
 * values in http://ssd.jpl.nasa.gov/txt/aprx_pos_planets.pdf
 * converting longitudes from heliocentric to geocentric coordinates (+180)
 */
#define EPOCH_TO_J2000(t)          ((gdouble)(t)-946727935.816)
#define MEAN_ECLIPTIC_LONGITUDE(d) (280.46457166 + (d)/36525.*35999.37244981)
#define SOL_PROGRESSION            (360./365.242191)
#define PERIGEE_LONGITUDE(d)       (282.93768193 + (d)/36525.*0.32327364)

void		metar_start_open	(GWeatherInfo *info);
gboolean	iwin_start_open		(GWeatherInfo *info);
gboolean        metno_start_open         (GWeatherInfo *info);
gboolean        owm_start_open          (GWeatherInfo *info);

gboolean	metar_parse		(gchar *metar,
					 GWeatherInfo *info);

void            _gweather_info_begin_request (GWeatherInfo *info,
					      SoupMessage  *message);
void		_gweather_info_request_done (GWeatherInfo *info,
					     SoupMessage  *message);

void		ecl2equ			(gdouble t,
					 gdouble eclipLon,
					 gdouble eclipLat,
					 gdouble *ra,
					 gdouble *decl);
gdouble		sunEclipLongitude	(time_t t);

void            _gweather_info_ensure_sun  (GWeatherInfo *info);
void            _gweather_info_ensure_moon (GWeatherInfo *info);

void		free_forecast_list	  (GWeatherInfo *info);

GWeatherInfo   *_gweather_info_new_clone  (GWeatherInfo *original);

#endif /* __WEATHER_PRIV_H_ */