calc.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. *openPilot Log - A FOSS Pilot Logbook Application
  3. *Copyright (C) 2020 Felix Turowsky
  4. *
  5. *This program is free software: you can redistribute it and/or modify
  6. *it under the terms of the GNU General Public License as published by
  7. *the Free Software Foundation, either version 3 of the License, or
  8. *(at your option) any later version.
  9. *
  10. *This program is distributed in the hope that it will be useful,
  11. *but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. *GNU General Public License for more details.
  14. *
  15. *You should have received a copy of the GNU General Public License
  16. *along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. #include "calc.h"
  19. #include "dbman.cpp"
  20. /*!
  21. * \brief calc::blocktime Calculates Block Time for a given departure and arrival time
  22. * \param tofb QTime Time Off Blocks
  23. * \param tonb QTime Time On Blocks
  24. * \return Block Time in minutes
  25. */
  26. QTime calc::blocktime(QTime tofb, QTime tonb)
  27. /* This function calculates block time for a given Departure Time and Arrival time.
  28. */
  29. {
  30. if(tonb > tofb)// landing same day
  31. {
  32. QTime blocktimeout(0,0); // initialise return value at midnight
  33. int blockseconds = tofb.secsTo(tonb); // returns seconds between 2 time objects
  34. blocktimeout = blocktimeout.addSecs(blockseconds);
  35. return blocktimeout;
  36. } else // landing next day
  37. {
  38. QTime midnight(0,0);
  39. QTime blocktimeout(0,0); // initialise return value at midnight
  40. int blockseconds = tofb.secsTo(midnight); // returns seconds passed until midnight
  41. blocktimeout = blocktimeout.addSecs(blockseconds);
  42. blockseconds = midnight.secsTo(tonb); // returns seconds passed after midnight
  43. blocktimeout = blocktimeout.addSecs(blockseconds);
  44. return blocktimeout;
  45. }
  46. }
  47. /*!
  48. * \brief calc::minutes_to_string Converts database time to String Time
  49. * \param blockminutes int from database
  50. * \return String hh:mm
  51. */
  52. QString calc::minutes_to_string(QString blockminutes)
  53. /* Converts time from database (integer of minutes since midnight) to a QString "hh:mm"
  54. */
  55. {
  56. int minutes = blockminutes.toInt();
  57. QString hour = QString::number(minutes/60);
  58. if (hour.size() < 2) {hour.prepend("0");}
  59. QString minute = QString::number(minutes % 60);
  60. if (minute.size() < 2) {minute.prepend("0");}
  61. QString blocktime = hour + ":" + minute;
  62. return blocktime;
  63. };
  64. /*!
  65. * \brief calc::time_to_minutes converts QTime to int minutes
  66. * \param time QTime
  67. * \return int time as number of minutes
  68. */
  69. int calc::time_to_minutes(QTime time)
  70. {
  71. QString timestring = time.toString("hh:mm");
  72. int minutes = (timestring.left(2).toInt()) * 60;
  73. minutes += timestring.right(2).toInt();
  74. return minutes;
  75. }
  76. /*!
  77. * \brief calc::string_to_minutes Converts String Time to String Number of Minutes
  78. * \param timestring "hh:mm"
  79. * \return String number of minutes
  80. */
  81. int calc::string_to_minutes(QString timestring)
  82. {
  83. int minutes = (timestring.left(2).toInt()) * 60;
  84. minutes += timestring.right(2).toInt();
  85. timestring = QString::number(minutes);
  86. return minutes;
  87. }
  88. /*!
  89. * The purpose of these functions is to provide functionality enabling the calculation of
  90. * night flying time. EASA defines night as follows:
  91. *
  92. * ‘Night’ means the period between the end of evening civil twilight and the beginning of
  93. * morning civil twilight or such other period between sunset and sunrise as may be prescribed
  94. * by the appropriate authority, as defined by the Member State.
  95. *
  96. *
  97. *
  98. * This is the proccess of calculating night time in this program:
  99. *
  100. * 1) A flight from A to B follows the Great Circle Track along these two points
  101. * at an average cruising height of 11km. (~FL 360)
  102. *
  103. * 2) Any time the Elevation of the Sun at the current position is less
  104. * than -6 degrees, night conditions are present.
  105. * 3) The calculation is performed for every minute of flight time.
  106. *
  107. * In general, input and output for most functions is decimal degrees, like coordinates
  108. * are stowed in the airports table. Calculations are normally done using
  109. * Radians.
  110. */
  111. /*!
  112. * \brief radToDeg Converts radians to degrees
  113. * \param rad
  114. * \return degrees
  115. */
  116. double calc::radToDeg(double rad)
  117. {
  118. double deg = rad * (180 / M_PI);
  119. return deg;
  120. }
  121. /*!
  122. * \brief degToRad Converts degrees to radians
  123. * \param deg
  124. * \return radians
  125. */
  126. double calc::degToRad(double deg)
  127. {
  128. double rad = deg * (M_PI / 180);
  129. return rad;
  130. }
  131. /*!
  132. * \brief radToNauticalMiles Convert Radians to nautical miles
  133. * \param rad
  134. * \return nautical miles
  135. */
  136. double calc::radToNauticalMiles(double rad)
  137. {
  138. double nm = rad * 3440.06479482;
  139. return nm;
  140. }
  141. /*!
  142. * \brief greatCircleDistance Calculates Great Circle distance between two coordinates, return in Radians.
  143. * \param lat1 Location Latitude in degrees -90:90 ;S(-) N(+)
  144. * \param lon1 Location Longitude in degrees -180:180 W(-) E(+)
  145. * \param lat2 Location Latitude in degrees -90:90 ;S(-) N(+)
  146. * \param lon2 Location Longitude in degrees -180:180 W(-) E(+)
  147. * \return
  148. */
  149. double calc::greatCircleDistance(double lat1, double lon1, double lat2, double lon2)
  150. {
  151. // Converting Latitude and Longitude to Radians
  152. lat1 = degToRad(lat1);
  153. lon1 = degToRad(lon1);
  154. lat2 = degToRad(lat2);
  155. lon2 = degToRad(lon2);
  156. // Haversine Formula
  157. double deltalon = lon2 - lon1;
  158. double deltalat = lat2 - lat1;
  159. double result = pow(sin(deltalat / 2), 2) +
  160. cos(lat1) * cos(lat2) * pow(sin(deltalon / 2), 2);
  161. result = 2 * asin(sqrt(result));
  162. return result;
  163. }
  164. /*!
  165. * \brief Calculates a list of points (lat,lon) along the Great Circle between two points.
  166. * The points are spaced equally, one minute of block time apart.
  167. * \param lat1 Location Latitude in degrees -90:90 ;S(-) N(+)
  168. * \param lon1 Location Longitude in degrees -180:180 W(-) E(+)
  169. * \param lat2 Location Latitude in degrees -90:90 ;S(-) N(+)
  170. * \param lon2 Location Longitude in degrees -180:180 W(-) E(+)
  171. * \param tblk Total Blocktime in minutes
  172. * \return coordinates {lat,lon} along the Great Circle Track
  173. */
  174. QVector<QVector<double>> calc::intermediatePointsOnGreatCircle(double lat1, double lon1, double lat2, double lon2, int tblk)
  175. {
  176. double d = greatCircleDistance(lat1, lon1, lat2, lon2); //calculate distance (radians)
  177. // Converting Latitude and Longitude to Radians
  178. lat1 = degToRad(lat1);
  179. lon1 = degToRad(lon1);
  180. lat2 = degToRad(lat2);
  181. lon2 = degToRad(lon2);
  182. //loop for creating one minute steps along the Great Circle
  183. // 0 is departure point, 1 is end point
  184. QVector<QVector<double>> coordinates;
  185. double fraction = 1.0/tblk;
  186. for(int i = 0; i <= tblk; i++) {
  187. // Calculating intermediate point for fraction of distance
  188. double A=sin((1-fraction * i) * d)/sin(d);
  189. double B=sin(fraction * i * d)/sin(d);
  190. double x = A*cos(lat1) * cos(lon1) + B * cos(lat2) * cos(lon2);
  191. double y = A*cos(lat1) * sin(lon1) + B * cos(lat2) * sin(lon2);
  192. double z = A*sin(lat1) + B * sin(lat2);
  193. double lat = atan2(z, sqrt( pow(x, 2) + pow(y, 2) ));
  194. double lon = atan2(y, x);
  195. QVector<double> coordinate = {lat,lon};
  196. coordinates.append(coordinate);
  197. }
  198. return coordinates;
  199. }
  200. /*!
  201. * \brief Calculates solar elevation angle for a given point in time and latitude/longitude coordinates
  202. *
  203. * It is based on the formulas found here: http://stjarnhimlen.se/comp/tutorial.html#5
  204. *
  205. * Credit also goes to Darin C. Koblick for his matlab implementation of various of these
  206. * formulas and to Kevin Godden for porting it to C++.
  207. *
  208. * Darin C. Koblock: https://www.mathworks.com/matlabcentral/profile/authors/1284781
  209. * Kevin Godden: https://www.ridgesolutions.ie/index.php/about-us/
  210. *
  211. * \param utc_time_point - QDateTime (UTC) for which the elevation is calculated
  212. * \param lat - Location Latitude in degrees -90:90 ;S(-) N(+)
  213. * \param lon - Location Longitude in degrees -180:180 W(-) E(+)
  214. * \return elevation - double of solar elevation in degrees.
  215. */
  216. double calc::solarElevation(QDateTime utc_time_point, double lat, double lon)
  217. {
  218. double Alt = 11; // I am taking 11 kilometers as an average cruising height for a commercial passenger airplane.
  219. // convert current DateTime Object to a J2000 value used in the calculation
  220. double d = utc_time_point.date().toJulianDay() - 2451544 + utc_time_point.time().hour()/24.0 + utc_time_point.time().minute()/1440.0;
  221. // Orbital Elements (in degress)
  222. double w = 282.9404 + 4.70935e-5 * d; // (longitude of perihelion)
  223. double e = 0.016709 - 1.151e-9 * d; // (eccentricity)
  224. double M = fmod(356.0470 + 0.9856002585 * d, 360.0); // (mean anomaly, needs to be between 0 and 360 degrees)
  225. double oblecl = 23.4393 - 3.563e-7*d; // (Sun's obliquity of the ecliptic)
  226. double L = w + M; // (Sun's mean longitude)
  227. // auxiliary angle
  228. double E = M + (180 / M_PI)*e*sin(M*(M_PI / 180))*(1 + e*cos(M*(M_PI / 180)));
  229. // The Sun's rectangular coordinates in the plane of the ecliptic
  230. double x = cos(E*(M_PI / 180)) - e;
  231. double y = sin(E*(M_PI / 180))*sqrt(1 - pow(e, 2));
  232. // find the distance and true anomaly
  233. double r = sqrt(pow(x,2) + pow(y,2));
  234. double v = atan2(y, x)*(180 / M_PI);
  235. // find the longitude of the sun
  236. double solarlongitude = v + w;
  237. // compute the ecliptic rectangular coordinates
  238. double xeclip = r*cos(solarlongitude*(M_PI / 180));
  239. double yeclip = r*sin(solarlongitude*(M_PI / 180));
  240. double zeclip = 0.0;
  241. //rotate these coordinates to equitorial rectangular coordinates
  242. double xequat = xeclip;
  243. double yequat = yeclip*cos(oblecl*(M_PI / 180)) + zeclip * sin(oblecl*(M_PI / 180));
  244. double zequat = yeclip*sin(23.4406*(M_PI / 180)) + zeclip * cos(oblecl*(M_PI / 180));
  245. // convert equatorial rectangular coordinates to RA and Decl:
  246. r = sqrt(pow(xequat, 2) + pow(yequat, 2) + pow(zequat, 2)) - (Alt / 149598000); //roll up the altitude correction
  247. double RA = atan2(yequat, xequat)*(180 / M_PI);
  248. double delta = asin(zequat / r)*(180 / M_PI);
  249. // GET UTH time
  250. double UTH = utc_time_point.time().hour() + utc_time_point.time().minute()/60.0 + utc_time_point.time().second()/3600.0;
  251. // Calculate local siderial time
  252. double GMST0 = fmod(L + 180, 360.0) / 15;
  253. double SIDTIME = GMST0 + UTH + lon / 15;
  254. // Replace RA with hour angle HA
  255. double HA = (SIDTIME*15 - RA);
  256. // convert to rectangular coordinate system
  257. x = cos(HA*(M_PI / 180))*cos(delta*(M_PI / 180));
  258. y = sin(HA*(M_PI / 180))*cos(delta*(M_PI / 180));
  259. double z = sin(delta*(M_PI / 180));
  260. // rotate this along an axis going east - west.
  261. double zhor = x*sin((90 - lat)*(M_PI / 180)) + z*cos((90 - lat)*(M_PI / 180));
  262. // Find the Elevation
  263. double elevation = asin(zhor)*(180 / M_PI);
  264. return elevation;
  265. }
  266. /*!
  267. * \brief Calculates which portion of a flight was conducted in night conditions.
  268. * \param dept - ICAO 4-letter code of Departure Airport
  269. * \param dest - ICAO 4-letter Code of Destination Airport
  270. * \param departureTime - QDateTime of Departure (UTC)
  271. * \param tblk - Total block time in minutes
  272. * \return Total number of minutes under night flying conditions
  273. */
  274. int calc::calculateNightTime(QString dept, QString dest, QDateTime departureTime, int tblk)
  275. {
  276. double deptLat = db::retreiveIcaoCoordinates(dept)[0];
  277. qDebug() << "calc::calculateNightTime deptLat = " << deptLat;
  278. double deptLon = db::retreiveIcaoCoordinates(dept)[1];
  279. qDebug() << "calc::calculateNightTime deptLon = " << deptLon;
  280. double destLat = db::retreiveIcaoCoordinates(dest)[0];
  281. qDebug() << "calc::calculateNightTime destLat = " << destLat;
  282. double destLon = db::retreiveIcaoCoordinates(dest)[1];
  283. qDebug() << "calc::calculateNightTime destLon = " << destLon;
  284. QVector<QVector<double>> route = intermediatePointsOnGreatCircle(deptLat, deptLon, destLat, destLon, tblk);
  285. int nightTime = 0;
  286. for(int i = 0; i < tblk ; i++) {
  287. if(solarElevation(departureTime.addSecs(60*i),radToDeg(route[i][0]),radToDeg(route[i][1])) < -0.6) {
  288. nightTime ++;
  289. }
  290. }
  291. qDebug() << "calc::calculateNightTime result: " << nightTime << " minutes night flying time.";
  292. return nightTime;
  293. }