Calculate average, azimut and distance

Gents,

Please i will to get the distance and azimut from 2 coordinates:

Usig excel formula i get the correct values, but i will like to do it using awk.

Example

A         35089.0 50345.016 9 75   1   2101774 77 70 79 483911.6 2380106.9 137.4     1 1 6 1
A         35089.0 50345.01620 75   1   2111874 80 71 78 483911.7 2380118.1 137.4     1 1 6 1

formula to apply

average x = average(483911.6, 483911.7 ) = 483911.7
average y = average(2380106.9, 2380118.1) = 2380112.5
average z = average(137.4, 137.4) = 137.4
Azimut =IF(483911.6=483911.7),90,(ABS(DEGREES(ATAN((2380106.9-2380118.1)/(483911.6-483911.7))))))
Distance =SQRT((483911.6-483911.7)^2+(2380106.9-2380118.1)^2)
A         35089.0 50345.016 9 75   1   2101774 77 70 79 483911.7 2380112.5 137.4     1 1 6 1 89.5  11.2

input

A         35089.0 50345.016 9 75   1   2101774 77 70 79 483911.6 2380106.9 137.4     1 1 6 1
A         35089.0 50345.01620 75   1   2111874 80 71 78 483911.7 2380118.1 137.4     1 1 6 1
A         35089.0 50357.016 9 75   1   2111674 77 70 79 484062.3 2380118.1 136.5     2 1 6 1
A         35089.0 50357.01620 75   1   3111974 78 72 79 484061.7 2380106.8 136.4     2 1 6 1
A         35089.0 50369.016 9 75   1   2 91574 77 69 81 484212.8 2380105.6 136.7     3 1 6 1
A         35089.0 50369.01620 75   1   2101474 79 70 79 484212.9 2380116.8 136.2     3 1 6 1
A         35089.0 50381.016 9 75   1   2 91574 77 71 78 484361.9 2380117.9 134.9     4 1 6 1
A         35089.0 50381.01620 75   1   2101374 79 73 78 484361.7 2380106.6 135.4     4 1 6 1

output desired

A         35089.0 50345.016 9 75   1   2101774 77 70 79 483911.7 2380112.5 137.4     1 1 6 1 89.5  11.2
A         35089.0 50357.016 9 75   1   2111674 77 70 79 484062.8 2380112.5 136.5     2 1 6 1 87.0  11.3
A         35089.0 50369.016 9 75   1   2 91574 77 69 81 484212.9 2380111.2 136.5     3 1 6 1 89.5  11.2
A         35089.0 50381.016 9 75   1   2 91574 77 71 78 484361.8 2380112.3 135.2     4 1 6 1 89.0  11.3

Hope you can help me

What awk code have you written to try to solve this problem?

1 Like

Hi Don,
Nothing yet..
I have it in Excel, but as the text file is to Big i would like to start to do something with awk.

With more than 250 posts we would like to think that you are learning something from all of the help we have provided you in the past. Please try to do this on your own, show us the code you have tried, and tell us what you can't get to work. We'll be happy to help you help you when you get stuck.

But, we are not here to act as your unpaid programming staff.

1 Like

Hi Don

I manage to do this script.. I got the distance but I cant get the azimut

#!/bin/bash

cat input.txt | awk '$0 !~ /^ *$/' |
      awk '{
  	      pv[NR]  = substr($3,1,5)
              line[NR]  = substr($2,1,5)
              x[NR]   = substr($0,57,8)
              y[NR]   = substr($0,66,9)
           } END {
                   first = 1
                   count = 0
                   for ( i=1 ; i <= NR ; i++ ) {
                       if (pv != pv[i+1] || line != line[i+1]) {
                          last = i
                          count++
                          dx = x[first]-x[last]
                          dy = y[first]-y[last]
                          len[count] = sqrt(dx^2+dy^2)
                             if ( len[count] <= 1000 && len[count] > 0 ) {
                                          printf (" %4s %4s : %-8.2f\n", line,pv,len[count])
                          }\
                          first = i+1
                      }
                   }
                  }' > output.txt

Input

A         35591.0 52025.01714 75   1  -2101674 79 69 81 504913.2 2386393.5 122.9  1747 1 7 1
A         35591.0 52025.01726 75   1   2102374 78 72 81 504913.0 2386381.2 122.8  1747 1 7 1
A         35623.0 50561.01222 75   1   2121874 78 73 80 486612.3 2386795.1 134.2  1333 112 1
A         35623.0 50561.012 6 75   1   3122374 80 70 79 486612.0 2386782.6 134.3  1333 112 1
A         35179.0 51449.01328 75   1   2113274 79 71 80 497712.4 2381242.9 129.9   531 1 3 1
A         35179.0 51449.01330 75   1   3172374 80 72 79 497712.7 2381231.0 130.3   531 1 3 1

Output

35591 52025 : 12.30   
35623 50561 : 12.50   
35179 51449 : 11.90 

Please, I will like to get the results as mentioned previously #1... I try my best but i cant get even the azimut..Please try to help me to get it.

Are those always pairs of lines in the input?
And, which is the desired output - the sample in post#1 or in post#5

Hi RudiC,

Yes always there is pairs of lines in the input
key

substr($2,1,5)
substr($3,1,5)

Output desired like the sample in the post #1

Thanks for your help.

How about

awk '
BEGIN           {LC[1] = LC[2] = LC[3] = 0
                 AC[1] = AC[2] = AC[3] = 0
                 DGARC  = 180 / (atan2(1,1) * 4)
                }
function GETCOORD(C)
                {C[1] = substr($0, 57, 8)
                 C[2] = substr($0, 66, 9)
                 C[3] = substr($0, 76, 5)
                }

NR%2            {GETCOORD (LC)
                 RT = substr($0, 1, 56)
                 next
                }
                {GETCOORD (AC)
                 AVX  = (AC[1] + LC[1]) / 2
                 AVY  = (AC[2] + LC[2]) / 2
                 AVZ  = (AC[3] + LC[3]) / 2
                 DX   = AC[1] - LC[1]
                 DY   = AC[2] - LC[2]
                 DZ   = AC[3] - LC[3]
                 DIST = sqrt(DX ^ 2 + DY ^ 2 + DZ ^ 2)
                 AZI  = (atan2(DY, DX) * DGARC + 180 ) % 180
                 print RT, AVX, AVY, AVZ, $(NF-3), $(NF-2), $(NF-1), $NF, DIST, AZI
                }
' OFMT="%9.1f"  file
A         35089.0 50345.016 9 75   1   2101774 77 70 79   483911.7 2380112.5     137.4 1 1 6 1      11.2      89.5
A         35089.0 50357.016 9 75   1   2111674 77 70 79  484062 2380112.5     136.4 2 1 6 1      11.3      87.0
A         35089.0 50369.016 9 75   1   2 91574 77 69 81   484212.8 2380111.2     136.4 3 1 6 1      11.2      89.5
A         35089.0 50381.016 9 75   1   2 91574 77 71 78   484361.8 2380112.2     135.2 4 1 6 1      11.3      89.0
1 Like

RudiC,

Great,, Thanks a lot it is wonderful.. works perfect.

Thanks again