summaryrefslogtreecommitdiffstats
path: root/kweather/metar_parser.h
blob: 946c34d3f9d4d4784bb248a6bb1dd7392671ef0a (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 <qdatetime.h>
#include <qregexp.h>
#include <qstringlist.h>

class StationDatabase;

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


class MetarParser
{
	public:
		MetarParser(StationDatabase *stationDB,
			    KLocale::MeasureSystem units = KLocale::Imperial,
			    QDate date = QDate::currentDate(),
			    QTime time = QTime::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 QString &stationID, const QString &metar);

	private:
		bool parseCover(const QString &s);
		bool parseCurrent(const QString &s);
		bool parseTemperature(const QString &s);
		bool parseTemperatureTenths(const QString &s);
		void calcTemperatureVariables();
		void removeTrailingDotZero(QString &string);
		bool parseDate(const QString &s);
		bool parseTime(const QString &s);
		bool parseVisibility(QStringList::ConstIterator it);
		bool parsePressure( const QString &s );
		QString parseWindDirection(const unsigned int direction);
		bool parseWindSpeed(const QString &s);
		bool parseStationNeedsMaintenance(const QString &s);
		void calcCurrentIcon();
		void calcWindChill();
		bool isNight(const QString &stationID) const;
		QString iconName( const QString &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 KLocale::MeasureSystem m_units;
		const QDate m_date;
		const QTime m_time;
		const unsigned int m_localUTCOffset;
		
		struct WeatherInfo weatherInfo;
		
		QRegExp CoverRegExp;
		QRegExp CurrentRegExp;
		QRegExp WindRegExp;
		QRegExp VisRegExp;
		QRegExp VisFracRegExp;
		QRegExp TempRegExp;
		QRegExp TimeRegExp;
		QRegExp DateRegExp;
		QRegExp PressRegExp;
		QRegExp TempTenRegExp;
};

#endif