### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: Problems with time and Distance Help Please  (Read 4619 times)

#### JLWest

• Hero Member
• Posts: 877
##### Problems with time and Distance Help Please
« on: July 02, 2020, 08:42:47 am »
I have been working on this for 4 days. I couldn't get it to work in the program so I broke out the problem and put it this demo. Compile and go no file loading all self contained  3 or 5 short procedures.

I need to know the local time at EDAM and RiBB.

I think if the figures are right the GMT time at the
aircraft is 23:53:52, RJBB is 16:48:33 and there is 2 hrs 14 min
and 51 sec left to EHAM the destination.

I also need to know if the figures are reasonably correct. Because of the Haversine calcs and other factors it can never be really correct.

I have included a form screen shot, the program and the code.

The data comes from a provider who has ADS-B access. I make an API call and get an XML doc of a 1,000 or so flights. Some flight are in the air, landed, started or unkown,

I also get a departure and arrival airports Lat/Lon of of the aircraft in route and a Unix EPOCH GMT time at the Lat and Lon position.
plus the call sign carrier, aircraft.

Thanks
FPC 3.2.0, Lazarus IDE v2.0.4
Windows 10 Pro 32-GB
Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

#### winni

• Hero Member
• Posts: 1894
##### Re: Problems with time and Distance Help Please
« Reply #1 on: July 02, 2020, 12:44:32 pm »
Hi!

After a short reading your code I detected one error.

This line is wrong :

Code: Pascal  [Select][+][-]
1. D := arcsin(sqrt(dx**2 + dy**2 + dz**2) / 2) * diameter;
2.
The division  by 2 should NOT  be inside the arcsin  but

Code: Pascal  [Select][+][-]
1. D := arcsin(sqrt(dx**2 + dy**2 + dz**2) ) * diameter/2;

And please: Don't compute miles. Since 1975 the rule for scientific work is to use metrics.
The last time the NASA did  not respect that rule the "Mars Climate Orbiter" missed  the Mars. And 200 Million \$ vanished in the universe.

Winni

• Hero Member
• Posts: 10516
##### Re: Problems with time and Distance Help Please
« Reply #2 on: July 02, 2020, 12:55:01 pm »
Further more "mile" is not well defined.
https://en.wikipedia.org/wiki/Mile
whereas metrics are....
« Last Edit: July 02, 2020, 01:00:12 pm by Thaddy »

#### winni

• Hero Member
• Posts: 1894
##### Re: Problems with time and Distance Help Please
« Reply #3 on: July 02, 2020, 01:08:31 pm »
Hi!

I will take the mile of my home : the landmile of Schleswig-Holstein (northern germany). It was about 10.5 kilometer. Nobody knew that exact. But valid until 1871!

Winni

#### JLWest

• Hero Member
• Posts: 877
##### Re: Problems with time and Distance Help Please
« Reply #4 on: July 02, 2020, 08:51:25 pm »

I changed the line as you suggested and get a SIGPFE error on the call to the  HaversineDist function.

As for going to the metrics system it would mean a rewrite. Not in the Demo but in the program. The Haversine formula gives miles. I'd have to find one that gives kilometers

Because I have GMT time for RJBB and EHAM at this point I need to convert those times to Local for each location.

So I would have to convert the Unix EPOCH Int64 to TDateTime using UnixToDateTime. And then use UniversalTimeToLocal to get the local times.

function UnixToDateTime(const AValue: Int64;aReturnUTC: Boolean = True ):TDateTime;

{function UniversalTimeToLocal(UT: TDateTime; TZOffset: Integer):TDateTime;}

Hi!

After a short reading your code I detected one error.

This line is wrong :

Code: Pascal  [Select][+][-]
1. D := arcsin(sqrt(dx**2 + dy**2 + dz**2) / 2) * diameter;
2.
The division  by 2 should NOT  be inside the arcsin  but

Code: Pascal  [Select][+][-]
1. D := arcsin(sqrt(dx**2 + dy**2 + dz**2) ) * diameter/2;

And please: Don't compute miles. Since 1975 the rule for scientific work is to use metrics.
The last time the NASA did  not respect that rule the "Mars Climate Orbiter" missed  the Mars. And 200 Million \$ vanished in the universe.

Winni
FPC 3.2.0, Lazarus IDE v2.0.4
Windows 10 Pro 32-GB
Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

#### winni

• Hero Member
• Posts: 1894
##### Re: Problems with time and Distance Help Please
« Reply #5 on: July 02, 2020, 10:18:19 pm »
Hi!

The division by two inside the arcsin is definitly wrong.

So for better results use:

Code: Pascal  [Select][+][-]
1. const Deg2Rad= pi/180;
2.
3. Function EarthDistance (const long1, lat1, long2 , lat2 : single): single; inline;   // km
4.  Const R = 6371.000; // km
5. var Fi1, Fi2, DeltaFi, DeltaLambda, a, SinDeltaFi2, SinDeltaLambda2  : single;
6.
7. begin
8. Fi1 := Lat1 * Deg2Rad;
9. Fi2 := Lat2 * Deg2Rad;
10. DeltaFi := Fi2 - Fi1;
11. DeltaLambda := (Long2 - long1) * Deg2Rad;
12.
13. SinDeltaFi2 := sin (DeltaFi*0.5);
14. SinDeltaLambda2 := Sin (DeltaLambda*0.5);
15. a :=  sqr (SinDeltaFi2) +
16.       cos (Fi1)* cos(Fi2) *
17.       sqr (SinDeltaLambda2);
18. result := 2 * arctan2(sqrt(a), sqrt (1-a));
19. result := R * result;
20. end;
21.

And concerning the time:

Compute everything in UTC (GMT is dead).
And if your results are fine, you can start to try to convert it in local time.

But that is a lousy thing. Pilots don't do that.

When does the daylight saving time end in Arizona?
Has Turkey now abolished day light saving - as they anounced?
And how many Gulf wars it has taken to abolish the half -hour-time-zone in Irak?

The circumstances are permanently changing. You are heavily involved in political unlogical stuff. Not an easy job .

Winni

« Last Edit: July 02, 2020, 10:20:16 pm by winni »

#### JLWest

• Hero Member
• Posts: 877
##### Re: Problems with time and Distance Help Please
« Reply #6 on: July 02, 2020, 11:54:00 pm »
Thanks I'll try it.

And concerning the time:

"Compute everything in UTC (GMT is dead)."

Yea, I know. GMT is a timezone, The website were I do the API call list the time that comes back as GMT. That's why I show the Heathrow info bottom left.

"And if your results are fine, you can start to try to convert it in local time."
"But that is a lousy thing. Pilots don't do that."

It's not really for pilots per say. The final record the program will produce looks like:

OPER  Dep      Arr      AC      DTime   Callsign    AC-ID      Day  ATime
AAL   , KLAX  , KJFK , B778 , 0810  ,  AA2134 , N7548AA   4    1206

This record is data for a program which generates flights in a Flight simulator. Some of the planes will be parked at a gate, some taxing and some en-route depending on the values in the record.

The above record looks like the info displayed at the airport showing flight information, except they don't have the day value.

Thanks
FPC 3.2.0, Lazarus IDE v2.0.4
Windows 10 Pro 32-GB
Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

#### JLWest

• Hero Member
• Posts: 877
##### Re: Problems with time and Distance Help Please
« Reply #7 on: July 03, 2020, 12:03:30 am »
@winni

Function EarthDistance (const long1, lat1, long2 , lat2 : single): single; inline;   // km

I don't know how to call the function. I understand the lat1 and lat2 but not the long1, and long2.

FPC 3.2.0, Lazarus IDE v2.0.4
Windows 10 Pro 32-GB
Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

#### winni

• Hero Member
• Posts: 1894
##### Re: Problems with time and Distance Help Please
« Reply #8 on: July 03, 2020, 12:09:31 am »
Hi!

Again (and again and ...) : GMT is dead.
It was replaced in 1972 by UTC .

The only country that talks about GMT is GB.
They also think that the brexit will enrich them .....

About your flight data from Los Angeles to New York:

Between DTime and ATime is a difference of ~ 4 hours.
Is that the real flight time or must we add 3 hours time zone difference?

Europe and China are easier with such things:
Everything in one zone.

Winni

#### winni

• Hero Member
• Posts: 1894
##### Re: Problems with time and Distance Help Please
« Reply #9 on: July 03, 2020, 12:20:24 am »
@winni

Function EarthDistance (const long1, lat1, long2 , lat2 : single): single; inline;   // km

I don't know how to call the function. I understand the lat1 and lat2 but not the long1, and long2.

Latitude and Longitude define every point on this earth.
Do you need further information?

And again and again and ... : GMT is dead.
It was replaced 1972  by UTC.

Winni

#### JLWest

• Hero Member
• Posts: 877
##### Re: Problems with time and Distance Help Please
« Reply #10 on: July 03, 2020, 12:38:15 am »
"Between DTime and ATime is a difference of ~ 4 hours.
Is that the real flight time or must we add 3 hours time zone difference?"

OPER  Dep      Arr      AC      DTime   Callsign    AC-ID      Day  ATime
AAL   , KLAX  , KJFK , B778 , 0810  ,  AA2134 , N7548AA   4    1506

Yea, it's local time so it would be NY local time.

So those are longitudes, yea makes sense now.  I just thought whenever you expressed latitude and longitude latitude always comes first. Now that is an English Convention.

Thanks

FPC 3.2.0, Lazarus IDE v2.0.4
Windows 10 Pro 32-GB
Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

#### winni

• Hero Member
• Posts: 1894
##### Re: Problems with time and Distance Help Please
« Reply #11 on: July 03, 2020, 01:06:17 am »
Hi!

So if lift off is 8:10 in Los Angeleses and touchdown is 12:06 in New York we got ~ 4 hours plus 3 hours timezone Shift. Are 7 hours realistic for a flight west to east in US? I don't know - I am European .

If you have to deal with local times, then these are horrible data. Look if you get some with UTC.

If not: There is a TimeZone package for Lazarus in the Online Package Manager.

I don't no nothing about it, but you can give a try.

But it would be better to get flight data with UTC.

Winni

PS.: Lat/Long or Long/Lat - everything a point of view.
In school we learned that zero  for Y is at bottom.  Now it is a the top ....

#### JLWest

• Hero Member
• Posts: 877
##### Re: Problems with time and Distance Help Please
« Reply #12 on: July 03, 2020, 02:57:14 am »
The only time I have to work with is the Unix EPOCH 1592610832 at 63.62 Lat / 38.28 Lon.

The provider says the time is an Uniz/EPOCH time and is GMT, however it's really UTC.
FPC 3.2.0, Lazarus IDE v2.0.4
Windows 10 Pro 32-GB
Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

#### winni

• Hero Member
• Posts: 1894
##### Re: Problems with time and Distance Help Please
« Reply #13 on: July 03, 2020, 11:57:35 pm »
Hi!

So I assume that  EPOCH 1592610832 at 63.62 Lat / 38.28 Lon is the temorary time and position of the Airplane. Because this is Russia -  100 km west of Archangelsk.

So why is an Airplane way from LA to NY in Russia?

I don't understand your data.

Winni

#### JLWest

• Hero Member
• Posts: 877
##### Re: Problems with time and Distance Help Please
« Reply #14 on: July 04, 2020, 12:19:18 am »
@Winni

Sorry for the late reply.

RJBB is Sennan District, Osaka Prefecture, Japan flying to EHAM Badhoevedorp, Noord-Holland, Netherlands. The route would be over Russia.

But there is something wrong with my  math, logic or method I think.

if I calculate the local time for RJBB and EHAM I come up with:

RJBB Departure Local time:  6/19/2020    16:48  +9(TZ)
EHAM Arrival Local time:       6/20/2020    2207   + 2(TZ)   Net of +7

I don't think those times add up to 9hr 22min. More like 20 some hours.

The flight should take about 9 hr and 22 min.

Trying to find where but can't come up with error.

FPC 3.2.0, Lazarus IDE v2.0.4
Windows 10 Pro 32-GB
Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB