summaryrefslogtreecommitdiffstats
path: root/kweather/metar_parser.h
blob: 3b0b18075f7246bf475ebdf09c4c14336bf207e4 (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
/***************************************************************************
			metar_parser.h  -  Metar Parser
			-------------------
begin                : Wed June 7 2004
copyright            : (C) 2004 by John Ratke
email                : jratke@comcast.net
***************************************************************************/

/***************************************************************************
*                                                                         *
*   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.                                   *
*                                                                         *
***************************************************************************/

#ifndef METAR_PARSER_H
#define METAR_PARSER_H

#include <kglobal.h>
#include <klocale.h>
#include <krfcdate.h>

#include <tqdatetime.h>
#include <tqregexp.h>
#include <tqstringlist.h>

class StationDatabase;

struct WeatherInfo
{
	/** The current weather state outside */
	TQString theWeather;
	int clouds;
	float windMPH;
	float tempC;
	float dewC;
	bool heavy;
	TQStringList qsCoverList;
	TQStringList qsCurrentList;
	TQDate qsDate;
	TQString qsPressure;
	TQString qsTemperature;
	TQString qsDewPoint;
	TQString qsRelHumidity;
	TQTime qsTime;
	TQString qsVisibility;
	TQString qsWindSpeed;
	TQString qsWindChill;
	TQString qsHeatIndex;
	TQString qsWindDirection;
	TQString reportLocation;
	bool stationNeedsMaintenance;
};


class MetarParser
{
	public:
		MetarParser(StationDatabase *stationDB,
			    TDELocale::MeasureSystem units = TDELocale::Imperial,
			    TQDate date = TQDate::currentDate(),
			    TQTime time = TQTime::currentTime(), 
			    unsigned int localUTCOffset = KRFCDate::localUTCOffset());

		/* 
		 * Process a METAR string (the second parameter) and return a WeatherInfo struct 
		 *
		 * The first parameter is the station ICAO code, which is needed to match
		 * the code present in the METAR string in order to parse the rest of the 
		 * data correctly.  But the station code is also used to lookup the station 
		 * latitude and longitude to calculate the sunrise and sunset time to see if 
		 * the day or night icon should be used.
		 */
		struct WeatherInfo processData(const TQString &stationID, const TQString &metar);

	private:
		bool parseCover(const TQString &s);
		bool parseCurrent(const TQString &s);
		bool parseTemperature(const TQString &s);
		bool parseTemperatureTenths(const TQString &s);
		void calcTemperatureVariables();
		void removeTrailingDotZero(TQString &string);
		bool parseDate(const TQString &s);
		bool parseTime(const TQString &s);
		bool parseVisibility(TQStringList::ConstIterator it);
		bool parsePressure( const TQString &s );
		TQString parseWindDirection(const unsigned int direction);
		bool parseWindSpeed(const TQString &s);
		bool parseStationNeedsMaintenance(const TQString &s);
		void calcCurrentIcon();
		void calcWindChill();
		bool isNight(const TQString &stationID) const;
		TQString iconName( const TQString &icon ) const;

		/*
		 * Reset the internal WeatherInfo struct of the class so that
		 * processing can be run again.  (processData can be run again)
		 */
		void reset();

		StationDatabase* const m_stationDb;
		const TDELocale::MeasureSystem m_units;
		const TQDate m_date;
		const TQTime m_time;
		const unsigned int m_localUTCOffset;
		
		struct WeatherInfo weatherInfo;
		
		TQRegExp CoverRegExp;
		TQRegExp CurrentRegExp;
		TQRegExp WindRegExp;
		TQRegExp VisRegExp;
		TQRegExp VisFracRegExp;
		TQRegExp TempRegExp;
		TQRegExp TimeRegExp;
		TQRegExp DateRegExp;
		TQRegExp PressRegExp;
		TQRegExp TempTenRegExp;
};

#endif