123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388 |
- #include "calc.h"
- QTime calc::blocktime(QTime tofb, QTime tonb)
- {
- if(tonb > tofb)
- {
- QTime blocktimeout(0,0);
- int blockseconds = tofb.secsTo(tonb);
- blocktimeout = blocktimeout.addSecs(blockseconds);
- return blocktimeout;
- } else
- {
- QTime midnight(0,0);
- QTime blocktimeout(0,0);
- int blockseconds = tofb.secsTo(midnight);
- blocktimeout = blocktimeout.addSecs(blockseconds);
- blockseconds = midnight.secsTo(tonb);
- blocktimeout = blocktimeout.addSecs(blockseconds);
- return blocktimeout;
- }
- }
- QString calc::minutes_to_string(QString blockminutes)
- {
- int minutes = blockminutes.toInt();
- QString hour = QString::number(minutes/60);
- if (hour.size() < 2) {hour.prepend("0");}
- QString minute = QString::number(minutes % 60);
- if (minute.size() < 2) {minute.prepend("0");}
- QString blocktime = hour + ":" + minute;
- return blocktime;
- };
- int calc::time_to_minutes(QTime time)
- {
- QString timestring = time.toString("hh:mm");
- int minutes = (timestring.left(2).toInt()) * 60;
- minutes += timestring.right(2).toInt();
- return minutes;
- }
- int calc::string_to_minutes(QString timestring)
- {
- int minutes = (timestring.left(2).toInt()) * 60;
- minutes += timestring.right(2).toInt();
- timestring = QString::number(minutes);
- return minutes;
- }
- double calc::radToDeg(double rad)
- {
- double deg = rad * (180 / M_PI);
- return deg;
- }
- double calc::degToRad(double deg)
- {
- double rad = deg * (M_PI / 180);
- return rad;
- }
- double calc::radToNauticalMiles(double rad)
- {
- double nm = rad * 3440.06479482;
- return nm;
- }
- double calc::greatCircleDistance(double lat1, double lon1, double lat2, double lon2)
- {
-
- lat1 = degToRad(lat1);
- lon1 = degToRad(lon1);
- lat2 = degToRad(lat2);
- lon2 = degToRad(lon2);
-
- double deltalon = lon2 - lon1;
- double deltalat = lat2 - lat1;
- double result = pow(sin(deltalat / 2), 2) +
- cos(lat1) * cos(lat2) * pow(sin(deltalon / 2), 2);
- result = 2 * asin(sqrt(result));
- return result;
- }
- double calc::greatCircleDistanceBetweenAirports(QString dept, QString dest)
- {
- if(dbAirport::retreiveIcaoCoordinates(dept).isEmpty() || dbAirport::retreiveIcaoCoordinates(dest).isEmpty()){
- qWarning() << "greatCircleDistance - invalid input. aborting.";
- return 0;
- }
- double lat1 = degToRad(dbAirport::retreiveIcaoCoordinates(dept)[0]);
- double lon1 = degToRad(dbAirport::retreiveIcaoCoordinates(dept)[1]);
- double lat2 = degToRad(dbAirport::retreiveIcaoCoordinates(dest)[0]);
- double lon2 = degToRad(dbAirport::retreiveIcaoCoordinates(dest)[1]);
-
- double deltalon = lon2 - lon1;
- double deltalat = lat2 - lat1;
- double result = pow(sin(deltalat / 2), 2) +
- cos(lat1) * cos(lat2) * pow(sin(deltalon / 2), 2);
- result = 2 * asin(sqrt(result));
- return radToNauticalMiles(result);
- }
- QVector<QVector<double>> calc::intermediatePointsOnGreatCircle(double lat1, double lon1, double lat2, double lon2, int tblk)
- {
- double d = greatCircleDistance(lat1, lon1, lat2, lon2);
-
- lat1 = degToRad(lat1);
- lon1 = degToRad(lon1);
- lat2 = degToRad(lat2);
- lon2 = degToRad(lon2);
-
-
- QVector<QVector<double>> coordinates;
- double fraction = 1.0/tblk;
- for(int i = 0; i <= tblk; i++) {
-
- double A=sin((1-fraction * i) * d)/sin(d);
- double B=sin(fraction * i * d)/sin(d);
- double x = A*cos(lat1) * cos(lon1) + B * cos(lat2) * cos(lon2);
- double y = A*cos(lat1) * sin(lon1) + B * cos(lat2) * sin(lon2);
- double z = A*sin(lat1) + B * sin(lat2);
- double lat = atan2(z, sqrt( pow(x, 2) + pow(y, 2) ));
- double lon = atan2(y, x);
- QVector<double> coordinate = {lat,lon};
- coordinates.append(coordinate);
- }
- return coordinates;
- }
- double calc::solarElevation(QDateTime utc_time_point, double lat, double lon)
- {
- double Alt = 11;
-
- double d = utc_time_point.date().toJulianDay() - 2451544 + utc_time_point.time().hour()/24.0 + utc_time_point.time().minute()/1440.0;
-
- double w = 282.9404 + 4.70935e-5 * d;
- double e = 0.016709 - 1.151e-9 * d;
- double M = fmod(356.0470 + 0.9856002585 * d, 360.0);
- double oblecl = 23.4393 - 3.563e-7*d;
- double L = w + M;
-
- double E = M + (180 / M_PI)*e*sin(M*(M_PI / 180))*(1 + e*cos(M*(M_PI / 180)));
-
- double x = cos(E*(M_PI / 180)) - e;
- double y = sin(E*(M_PI / 180))*sqrt(1 - pow(e, 2));
-
- double r = sqrt(pow(x,2) + pow(y,2));
- double v = atan2(y, x)*(180 / M_PI);
-
- double solarlongitude = v + w;
-
- double xeclip = r*cos(solarlongitude*(M_PI / 180));
- double yeclip = r*sin(solarlongitude*(M_PI / 180));
- double zeclip = 0.0;
-
- double xequat = xeclip;
- double yequat = yeclip*cos(oblecl*(M_PI / 180)) + zeclip * sin(oblecl*(M_PI / 180));
- double zequat = yeclip*sin(23.4406*(M_PI / 180)) + zeclip * cos(oblecl*(M_PI / 180));
-
- r = sqrt(pow(xequat, 2) + pow(yequat, 2) + pow(zequat, 2)) - (Alt / 149598000);
- double RA = atan2(yequat, xequat)*(180 / M_PI);
- double delta = asin(zequat / r)*(180 / M_PI);
-
- double UTH = utc_time_point.time().hour() + utc_time_point.time().minute()/60.0 + utc_time_point.time().second()/3600.0;
-
- double GMST0 = fmod(L + 180, 360.0) / 15;
- double SIDTIME = GMST0 + UTH + lon / 15;
-
- double HA = (SIDTIME*15 - RA);
-
- x = cos(HA*(M_PI / 180))*cos(delta*(M_PI / 180));
- y = sin(HA*(M_PI / 180))*cos(delta*(M_PI / 180));
- double z = sin(delta*(M_PI / 180));
-
- double zhor = x*sin((90 - lat)*(M_PI / 180)) + z*cos((90 - lat)*(M_PI / 180));
-
- double elevation = asin(zhor)*(180 / M_PI);
- return elevation;
- }
- int calc::calculateNightTime(QString dept, QString dest, QDateTime departureTime, int tblk)
- {
- if(dbAirport::retreiveIcaoCoordinates(dept).isEmpty() || dbAirport::retreiveIcaoCoordinates(dest).isEmpty()){
- qWarning() << "calculateNightTime - invalid input. aborting.";
- return 0;
- }
- double deptLat = dbAirport::retreiveIcaoCoordinates(dept)[0];
- qDebug() << "calc::calculateNightTime deptLat = " << deptLat;
- double deptLon = dbAirport::retreiveIcaoCoordinates(dept)[1];
- qDebug() << "calc::calculateNightTime deptLon = " << deptLon;
- double destLat = dbAirport::retreiveIcaoCoordinates(dest)[0];
- qDebug() << "calc::calculateNightTime destLat = " << destLat;
- double destLon = dbAirport::retreiveIcaoCoordinates(dest)[1];
- qDebug() << "calc::calculateNightTime destLon = " << destLon;
- QVector<QVector<double>> route = intermediatePointsOnGreatCircle(deptLat, deptLon, destLat, destLon, tblk);
- int nightTime = 0;
- for(int i = 0; i < tblk ; i++) {
- if(solarElevation(departureTime.addSecs(60*i),radToDeg(route[i][0]),radToDeg(route[i][1])) < -0.6) {
- nightTime ++;
- }
- }
- qDebug() << "calc::calculateNightTime result: " << nightTime << " minutes night flying time.";
- return nightTime;
- }
- QString calc::formatTimeInput(QString userinput)
- {
- QString output;
- QTime temptime;
- bool containsSeperator = userinput.contains(":");
- if(userinput.length() == 4 && !containsSeperator)
- {
- temptime = QTime::fromString(userinput,"hhmm");
- }else if(userinput.length() == 3 && !containsSeperator)
- {
- if(userinput.toInt() < 240)
- {
- QString tempstring = userinput.prepend("0");
- temptime = QTime::fromString(tempstring,"hhmm");
- }else
- {
- temptime = QTime::fromString(userinput,"Hmm");
- }
- }else if(userinput.length() == 4 && containsSeperator)
- {
- temptime = QTime::fromString(userinput,"h:mm");
- }else if(userinput.length() == 5 && containsSeperator)
- {
- temptime = QTime::fromString(userinput,"hh:mm");
- }
- output = temptime.toString("hh:mm");
- if(output.isEmpty())
- {
-
- qDebug() << "Time input is invalid.";
- }
- return output;
- }
|