-3

hello i want to calcul the distance between me and the ISS so this my code

i want to print this sentence :

The ISS is currently located at -30.1461, -50.7975: 10010km from us!

my code :

#!/bin/bash
pi = 3.14
earthRadiumKm=6371
lat2=48.813875
on2=2.392521
com=$( curl -s 'aletum.jails.simplerezo.com/etna-iss.json' | sed s/\"//g | awk\ -v RS =',' -F: '{print $1 $2 $3}' )
lat1=$( echo $com | cut c-60-66)
lon1=$( echo $com | cut c-42-50)
dLat=$( echo "($lat2 - $lat1) * $pi / 180" | bc -l)
dLat=$( echo "($lon2 - $lon1) * $pi / 180" | bc -l)
l1=$( echo "($lat1) * $pi / 180" | bcc -l)
l2=$( echo "($lat2) * $pi / 180" | bcc -l)
a=$( echo "sinus($dLat / 2) * sinus(dLat / 2) + sinus(dLon / 2) * sinus(dLon / 2) * cosine($l1) * consine($l2) | bc -l)
result=$( echo "2 * atan2(sqrt($a), sqrt(1-$a)) * $earthRaduisKm" | bc -1)
echo "The ISS is currently located at $dLat, $dLon, : ${result}KM from us!"

my Problem is my calcul are false because the result echo 0.6 km and that is impossible i thinks it's because i don't know how to use atan2

TS2IR 2018
  • 27
  • 3

1 Answers1

1

Fun little project. I have no idea if your math is right, but your code is quite terribly wrong:

  • no spaces around the = in an assignment: pi = 3.14 => pi=3.14
  • spelling error for variable names: earthRadiumKm=... => $earthRaduisKm
  • incorrect bc function names
  • incorrect system command names: awk\ -v, bcc -l
  • unclosed quoted string

Refactored:

#!/bin/bash

read lon1 lat1 time < <(
    curl -s 'aletum.jails.simplerezo.com/etna-iss.json' | 
      jq -r '"\(.iss_position.longitude) \(.iss_position.latitude) \(.timestamp)"' 
)

lon2=2.392521
lat2=48.813875

{ read dLat; read dLon; read result; } < <(
    bc -l <<END
        pi = (4*a(1/5) - a(1/239))*4 
        earthrad = 6371
        dlat = ($lat2 - $lat1) * pi/180
        dlon = ($lon2 - $lon1) * pi/180
        a = s(dlat/2) * s(dlat/2) + s(dlon/2) * s(dlon/2) * c($lat1 * pi/180) * c($lat2 * pi/180)
        result = 2 * a( sqrt(a) / sqrt(1-a) ) * earthrad
        dlat
        dlon
        result
END
)

printf "At %s,\n  the ISS is currently located at %.4f,%.4f : %.2f KM from us\n" \
    "$(date -d "@$time" "+%F %T %Z")" \
    "$dLat" "$dLon" "$result"

result

At 2018-09-12 08:53:32 EDT,
  the ISS is currently located at 0.5916,-2.2398 : 11296.11 KM from us

Notes:

  • repeat, I have no idea if your math is right
  • Process Substitutions to execute some code and read the results into variables.
  • use to parse JSON
  • I pushed all the math into a single bc call.
  • bc -l uses s for sine, c for cosine and a for arctan: read the bc man page
  • bc has no arctan2 function
  • I use a formula to get pi a little more precise
  • bc variable names must be all lowercase.
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • nice! But I'm surprised you didn't go with `awk`. Any thing besides (old)awk seem to have `atan2`. Good luck to all. – shellter Oct 17 '18 at 23:31
  • 1
    Yeah well, I got stuck in be land. Only thing to worry about, aside from all the math I apparently no longer remember, is if the `a` variable would equal 1. In that case though the ISS would be infinitely far away so we'd have other more pressing matters at hand. – glenn jackman Oct 18 '18 at 01:56