calc.cpp 12 KB

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