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 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
|
/**********************************************************************
XyGrib: 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 <QNetworkProxy>
#include <QMessageBox>
#include <cassert>
#include <cmath>
#include "FileLoaderGRIB.h"
#include "Util.h"
#include "Version.h"
//========================================================================
FileLoaderGRIB::FileLoaderGRIB (QNetworkAccessManager *manager, QWidget *parent)
: FileLoader (manager)
{
this->parent = parent;
step = 0;
downloadError = false;
reply_step1 = nullptr;
reply_step2 = nullptr;
scriptpath = "/";
}
//-------------------------------------------------------------------------------
FileLoaderGRIB::~FileLoaderGRIB ()
{
if (reply_step1) {
reply_step1->deleteLater ();
reply_step1 = nullptr;
}
if (reply_step2) {
reply_step2->deleteLater ();
reply_step2 = nullptr;
}
}
//-------------------------------------------------------------------------------
void FileLoaderGRIB::stop ()
{
if (reply_step1) {
reply_step1->close ();
downloadError = true;
}
if (reply_step2) {
reply_step2->close ();
downloadError = true;
}
}
//-------------------------------------------------------------------------------
void FileLoaderGRIB::getGribFile(
const QString& atmModel,
float x0, float x1, float y0, float y1,
float resolution, int interval, int days,
const QString& cycle,
bool wind, bool pressure, bool rain,
bool cloud, bool temp, bool humid, bool isotherm0,
bool snowDepth,
bool snowCateg, bool frzRainCateg,
bool CAPEsfc, bool CINsfc, bool reflectivity,
bool altitudeData200,
bool altitudeData300,
bool altitudeData400,
bool altitudeData500,
bool altitudeData600,
bool altitudeData700,
bool altitudeData850,
bool altitudeData925,
bool skewTData,
bool GUSTsfc,
const QString& wvModel,
bool sgwh,
bool swell,
bool wwav
)
{
QString page;
step = 0;
//----------------------------------------------------------------
// Step 1: Request the creation of the Grib file (status, name, size & checksum in return)
//----------------------------------------------------------------
// build the parameter string
QString parameters = "";
if (wind) {
parameters += "W;";
}
if (pressure && (atmModel != "ECMWF" && atmModel != "Arpege-EU" && atmModel != "Arome 0.025°")) {
parameters += "P;";
}
if (pressure && (atmModel == "ECMWF" || atmModel == "Arpege-EU" || atmModel == "Arome 0.025°")){
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 (CINsfc) {
parameters += "i;";
}
if (reflectivity) {
parameters += "r;";
}
if (GUSTsfc) {
parameters += "G;";
}
// if (SUNSDsfc) { // not currently used
// parameters += "D;";
// }
if (altitudeData200) parameters += "2;";
if (altitudeData300) parameters += "3;";
if (altitudeData400) parameters += "4;";
if (altitudeData500) parameters += "5;";
if (altitudeData600) parameters += "6;";
if (altitudeData700) parameters += "7;";
if (altitudeData850) parameters += "8;";
if (altitudeData925) parameters += "9;";
// if (cloudLayers) parameters += "L;";
if (skewTData) parameters += "skewt;";
// wave parameters have their own string
QString waveParams = "";
if (sgwh) {
waveParams += "s;";
}
if (swell) {
waveParams += "H;D;P;";
}
if (wwav) {
waveParams += "h;d;p;";
}
// model identifier (combines resolution)
QString amod = "";
if (atmModel == "GFS" && resolution == 0.25 ){
amod = "gfs_p25_";
} else if (atmModel == "GFS" && resolution == 0.50 ){
amod = "gfs_p50_";
} else if (atmModel == "GFS" && resolution == 1.0 ){
amod = "gfs_1p0_";
} else if (atmModel == "ICON"){
amod = "icon_p25_";
} else if (atmModel == "Arpege"){
amod = "arpege_p50_";
} else if (atmModel == "ECMWF"){
amod = "ecmwf_p50_";
} else if (atmModel == "NAM CONUS"){
amod = "nam_conus_12km_";
} else if (atmModel == "NAM CACBN"){
amod = "nam_cacbn_12km_";
} else if (atmModel == "NAM PACIFIC"){
amod = "nam_pacific_12km_";
} else if (atmModel == "ICON-EU"){
amod = "icon_eu_p06_";
} else if (atmModel == "Arpege-EU"){
amod = "arpege_eu_p10_";
} else if (atmModel == "Arome 0.025°"){
amod = "arome_p025_";
} else if (atmModel == "None"){
amod = "none";
}
// wave model identifier
QString wmod = "";
if (wvModel == "WW3"){
wmod = "ww3_p50_";
} else if (wvModel == "GWAM" ){
wmod = "gwam_p25_";
} else if (wvModel == "EWAM"){
wmod = "ewam_p05_";
} else if (wvModel == "None"){
wmod = "none";
}
// QString runCycle = Util::getSetting("downloadRunCycle", "last").toString().toLower();
//strbuf.clear();
if (!(parameters == "" && waveParams == ""))
{
step = 1;
emit signalGribSendMessage(
tr("Preparing file on server... Please wait..."));
emit signalGribReadProgress(step, 0, 0);
QString phpfilename = scriptpath+ "getmygribs2.php?";
QString appVer = Version::getVersion();
QString ptype = "Unknown";
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
ptype = QSysInfo::productType();
#endif
QTextStream(&page) << phpfilename
<< "osys=" << ptype
<< "&ver=" << appVer
<< "&model=" << amod
<< "&la1=" << y0
<< "&la2=" << y1
<< "&lo1=" << x0
<< "&lo2=" << x1
<< "&intv=" << interval
<< "&days=" << days
<< "&cyc=" << cycle
<< "&par=" << parameters
<< "&wmdl=" << wmod
<< "&wpar=" << waveParams
;
// << "&client=" << Version::getCompleteName()
QNetworkRequest request = Util::makeNetworkRequest ("http://"+Util::getServerName()+page);
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 ()));
}
else
{
QMessageBox::warning(parent, tr("Bad Request"), tr("No atmospheric or wave parameters were selected"));
}
}
//-------------------------------------------------------------------------------
void FileLoaderGRIB::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 FileLoaderGRIB::downloadProgress (qint64 done, qint64 total)
{
if (downloadError) {
step = 1000;
return;
}
if (sender() == reply_step2) {
emit signalGribReadProgress (step, done, total);
}
}
//-------------------------------------------------------------------------------
void FileLoaderGRIB::slotFinished_step1 ()
{
DBG("slotFinished_step1");
if (!downloadError) {
//-------------------------------------------
// Back from step 1: preparing the file
//-------------------------------------------
//xserv=reply_step1->rawHeader("XServer");
//strbuf.append(xserv);
QByteArray data = reply_step1->readAll ();
QJsonDocument jsondoc = QJsonDocument::fromJson(data);
QJsonObject jsondata = jsondoc.object();
bool status = jsondata["status"].toBool();
// if the result is valid the status is true and the message contains object
// with name, size and checksum
if (status) {
QJsonObject msg = jsondata["message"].toObject();
fileName = msg["url"].toString();
fileSize = msg["size"].toInt();
checkSumSHA1 = msg["sha1"].toString();
}else{ // message contains only the error message
QString m = jsondata["message"].toString();
// QMessageBox::warning(parent, tr("Information"), m);
emit signalGribLoadError(m);
downloadError = true;
reply_step1->close();
}
//-------------------------------------------------------------
// Start Step 2: Request the contents of the Grib file
//-------------------------------------------------------------
if (status) {
step = 2;
QString s;
s = tr("Total size : ") + QString("%1 kb").arg(fileSize/1024, 0);
emit signalGribSendMessage(s);
emit signalGribStartLoadData();
QNetworkRequest request;
request.setUrl (QUrl(fileName));
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 {
downloadError = true;
}
}
}
//-------------------------------------------------------------------------------
void FileLoaderGRIB::slotFinished_step2 ()
{
// DBG("slotFinished_step2");
if (!downloadError) {
step = 1000;
arrayContent = reply_step2->readAll ();
if (arrayContent.size() < 80) {
emit signalGribLoadError (tr("Empty file."));
return;
}
emit signalGribSendMessage(tr("CheckSum control"));
if (Util::sha1 (arrayContent) == checkSumSHA1)
{
emit signalGribSendMessage(tr("Finish")
+ QString(" %1 kb").arg(arrayContent.size()/1024.0,0,'f',1));
// TODO not sure about the .grb thing here
emit signalGribDataReceived(&arrayContent, fileName.replace("%20",".grb"));
}
else {
emit signalGribLoadError (tr("Bad checksum."));
}
}
}
|