File: FileLoaderMBLUE.cpp

package info (click to toggle)
zygrib 8.0.1%2Bdfsg.1-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 189,396 kB
  • ctags: 6,957
  • sloc: cpp: 59,202; makefile: 56
file content (293 lines) | stat: -rw-r--r-- 9,497 bytes parent folder | download
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/**********************************************************************
zyGrib: meteorological GRIB file viewer
Copyright (C) 2008-2012 - Jacques Zaninetti - http://www.zygrib.org

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 3 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 <http://www.gnu.org/licenses/>.
***********************************************************************/

#include <QApplication>
#include <QTextStream>
#include <QDomDocument>
#include <QMessageBox>

#include <cassert>

#include "FileLoaderMBLUE.h"
#include "Util.h"
#include "Version.h"

//-------------------------------------------------------------------------------
FileLoaderMBLUE::FileLoaderMBLUE (QNetworkAccessManager *manager, QWidget *parent)
			: FileLoader (manager)
{
	this->parent = parent;
    step = 0;
	downloadError = false;
	reply_step1 = NULL;
	reply_step2 = NULL;
	scriptpath = "/noaa/";
	scriptstock = "313O562/";
    zygriblog = "a07622b82b18524d2088c9b272bb3feeb0eb1737";
    zygribpwd = "61c9b2b17db77a27841bbeeabff923448b0f6388";
}
//-------------------------------------------------------------------------------
FileLoaderMBLUE::~FileLoaderMBLUE () 
{
	if (reply_step1) {
		reply_step1->close ();
		downloadError = true;
	}
	if (reply_step2) {
		reply_step2->close ();
		downloadError = true;
	}
}

//-------------------------------------------------------------------------------
void FileLoaderMBLUE::stop () 
{
	if (reply_step1) {
		reply_step1->close ();
		downloadError = true;
	}
	if (reply_step2) {
		reply_step2->close ();
		downloadError = true;
	}
}

//-------------------------------------------------------------------------------
void FileLoaderMBLUE::getMblueFile(
        float x0, float y0, float x1, float y1,
        float /*resolution*/, int interval, int days,
        bool wind, bool pressure, bool rain,
        bool cloud, bool temp, bool humid, bool isotherm0,
		bool tempMin, bool tempMax, bool snowDepth,
		bool snowCateg, bool frzRainCateg,
		bool CAPEsfc, 
		bool altitudeData200,
		bool altitudeData300,
		bool altitudeData500,
		bool altitudeData700,
		bool altitudeData850,
		bool GUSTsfc
	)
{
    QString page;
	step = 0;
    //----------------------------------------------------------------
    // Etape 1 : Demande la création du fichier Grib (nom en retour)
    //----------------------------------------------------------------
    QString parameters = "";
    if (wind) {
        parameters += "W";
    }
    if (GUSTsfc) {
        parameters += "G";
    }
    if (pressure) {
        parameters += "P";
    }
    if (rain) {
        parameters += "R";
    }
    if (cloud) {
        parameters += "C";
    }
    if (temp) {
        parameters += "T";
    }
    if (humid) {
        parameters += "H";
    }
	if (isotherm0) {
		parameters += "I";
    }    
    if (tempMin) {
        parameters += "m";
    }
    if (tempMax) {
        parameters += "M";
    }
    if (snowDepth) {
        parameters += "S";
    }
    if (snowCateg) {
        parameters += "s";
    }
    if (frzRainCateg) {
        parameters += "Z";
    }
    if (CAPEsfc) {
        parameters += "c";
    }
    if (altitudeData200) parameters += "2";
    if (altitudeData300) parameters += "3";
    if (altitudeData500) parameters += "5";
    if (altitudeData700) parameters += "7";
    if (altitudeData850) parameters += "8";
    if (parameters != "")
    {
        step = 1;
        emit signalGribSendMessage(
        		tr("Make file on server... Please wait..."));
        emit signalGribReadProgress(step, 0, 0);
		QString phpfilename;
		phpfilename = scriptpath+"getmbluefile.php?";
        QTextStream(&page) << phpfilename
                           << "but=prepfile"
                           << "&ver=1"
                           << "&hrs=" << interval
                           << "&htot=" << days*24
                           << "&la1=" <<  (y0)
                           << "&la2=" <<  (y1)
                           << "&lo1=" <<  (x0)
                           << "&lo2=" <<  (x1)
                           << "&par=" << parameters
                           << "&l=" << zygriblog
                           << "&m=" << zygribpwd
                           << "&client=" << Version::getCompleteName()
                           ;
		QNetworkRequest request = Util::makeNetworkRequest ("http://"+Util::getServerName()+page,x0,y0,x1,y1);
		reply_step1 = networkManager->get (request);
		connect (reply_step1, SIGNAL(downloadProgress (qint64,qint64)), 
				 this, SLOT(downloadProgress (qint64,qint64)));
		connect (reply_step1, SIGNAL(error(QNetworkReply::NetworkError)),
				 this, SLOT(slotNetworkError (QNetworkReply::NetworkError)));
		connect (reply_step1, SIGNAL(finished()),
				 this, SLOT(slotFinished_step1 ()));
    }
}

//-------------------------------------------------------------------------------
void FileLoaderMBLUE::slotNetworkError (QNetworkReply::NetworkError /*err*/)
{
	if (! downloadError) {
		if (sender() == reply_step1) {
			emit signalGribLoadError (reply_step1->errorString());
			downloadError = true;
		}
		else if (sender() == reply_step2) {
			emit signalGribLoadError (reply_step2->errorString());
			downloadError = true;
		}
	}
}
//-------------------------------------------------------------------------------
void FileLoaderMBLUE::downloadProgress (qint64 done, qint64 total)
{
	if (downloadError) {
		step = 1000;
		return;
	}
	if (sender() == reply_step2) {
		emit signalGribReadProgress (step, done, total);
	}
}
//-------------------------------------------------------------------------------
void FileLoaderMBLUE::slotFinished_step1 ()
{
	if (!downloadError) {
		QString page;
		//int unzipFileSize=0;
		//-------------------------------------------
		// Retour de l'étape 1 : préparation du fichier
		//-------------------------------------------
		QByteArray data = reply_step1->readAll ();
		
		QString strbuf (data);
		QStringList lsbuf = strbuf.split("\n");

		QString status;
        for (int i=0; i < lsbuf.size(); i++)
        {
            QStringList lsval = lsbuf.at(i).split(":");
            if (lsval.size() >= 2) {
                if (lsval.at(0) == "status")
                    status = lsval.at(1);
                else if (lsval.at(0) == "file") 
                    fileName = QString(lsval.at(1)).replace(".mbz","%20");
                else if (lsval.at(0) == "size")
                    fileSize = lsval.at(1).toInt();
                else if (lsval.at(0) == "checksum")
                    checkSumSHA1 = lsval.at(1);
                //else if (lsval.at(0) == "unzipsize")
                //    unzipFileSize = lsval.at(1).toInt();
				else if (lsval.at(0) == "message") {
					QString m = QUrl::fromPercentEncoding (lsval.at(1).toUtf8());
					QMessageBox::warning (parent, tr("Information"), m);
				}
            }
        }
        //--------------------------------------------------
        // Etape 2 : Demande le contenu du fichier
        //--------------------------------------------------
        if (status == "ok") {
            step = 2;
            QString s;
            s = tr("Total size : ") + QString("%1 ko").arg(fileSize/1024, 0);
            emit signalGribSendMessage(s);
            QTextStream(&page) << scriptpath
			<<"313O562/"<<fileName;
            emit signalGribStartLoadData();            
			QNetworkRequest request;
			request.setUrl (QUrl("http://"+Util::getServerName()+page) );
			reply_step2 = networkManager->get (request);
			connect (reply_step2, SIGNAL(downloadProgress (qint64,qint64)), 
					 this, SLOT(downloadProgress (qint64,qint64)));
			connect (reply_step2, SIGNAL(error(QNetworkReply::NetworkError)),
					 this, SLOT(slotNetworkError (QNetworkReply::NetworkError)));
			connect (reply_step2, SIGNAL(finished()),
					this, SLOT(slotFinished_step2 ()));
        }
		else {
			emit signalGribLoadError (status);
			downloadError = true;
		}
    }
}

//-------------------------------------------------------------------------------
void FileLoaderMBLUE::slotFinished_step2 ()
{
	if (!downloadError) {
		//--------------------------------------------------
		// Reçu le contenu du fichier
		//--------------------------------------------------
		step = 1000;
		arrayContent = reply_step2->readAll ();
		if (arrayContent.size() == 0) {
			emit signalGribLoadError (tr("Empty file."));
			return;
		}
		//--------------------------------------------------
		// Vérifie le checksum
		//--------------------------------------------------
		emit signalGribSendMessage(tr("CheckSum control"));
		if (Util::sha1 (arrayContent) == checkSumSHA1)
		{
			//--------------------------------------------------
			// Signale la fin du téléchargement
			//--------------------------------------------------
			emit signalGribSendMessage(tr("Finish")
							+ QString(" %1 ko").arg(arrayContent.size()/1024));
			emit signalGribDataReceived (&arrayContent, fileName.replace("%20",".mbz"));
		}
		else {
			emit signalGribLoadError(tr("Bad checksum."));
		}
	}
}