Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Initial bearing in metereological convention

    Hello,

    I am trying to calculate to calculate intial bearing between point 1 (source) and point 2 (destination) in metereological convention i.e. 0 degrees being North, 90 dgerees being East, 180 degrees being South and 270 degrees being West.

    I have tried the following code in stata but it isn't giving the correct output, for example the correct bearing between a point 1 (latitude = -45, longitude = 90) and point 2 (latitude = 0, longitude = 0) should be 135 in metereological convention. However stata program (followed from this post: https://www.statalist.org/forums/for...on-as-an-input) gives the output as 270.

    STATA CODE with 7 sample points:

    *****============================================= ===================********
    clear all
    program initial_bearing

    args lat1 lon1 lat2 lon2 newvar

    tempname d2r r2d
    scalar `d2r' = _pi / 180
    scalar `r2d' = 180 / _pi

    gen `newvar' = atan2(sin((`lon2'-`lon1') * `d2r') * cos(`lat2' * `d2r') , ///
    cos(`lat1' * `d2r') * sin(`lat2' * `d2r') - ///
    sin(`lat1' * `d2r') * cos(`lat2' * `d2r') * ///
    cos((`lon2'-`lon1') * `d2r'))

    // normalize atan2 results (-pi to pi) to range from 0 to 360 degrees
    replace `newvar' = mod((`newvar' * `r2d') + 360,360)
    end


    ************************************************** ***********
    set obs 7
    gen latfire = 0 if _n == 1
    gen lonfire = 90 if _n == 1
    gen latnum = 0 if _n == 1
    gen longnum = 0 if _n == 1

    replace latfire = -45 if _n == 2
    replace lonfire = 90 if _n == 2
    replace latnum = 0 if _n == 2
    replace longnum = 0 if _n == 2

    replace latfire = -45 if _n == 3
    replace lonfire = -90 if _n == 3
    replace latnum = 0 if _n == 3
    replace longnum = 0 if _n == 3

    replace latfire = 0 if _n == 4
    replace lonfire = -90 if _n == 4
    replace latnum = 0 if _n == 4
    replace longnum = 0 if _n == 4

    replace latfire = 45 if _n == 5
    replace lonfire = -90 if _n == 5
    replace latnum = 0 if _n == 5
    replace longnum = 0 if _n == 5

    replace latfire = 45 if _n == 6
    replace lonfire = 0 if _n == 6
    replace latnum = 0 if _n == 6
    replace longnum = 0 if _n == 6

    replace latfire = 45 if _n == 7
    replace lonfire = 90 if _n == 7
    replace latnum = 0 if _n == 7
    replace longnum = 0 if _n == 7

    initial_bearing latfire lonfire latnum longnum ib

    order lonfire latfire
    browse

    ************************************************** **************************

    Thanks alot for your help!

  • #2
    This seems to work correctly. If the distance between the 2 points is very large then it can be confusing as bearing changes as one moves along earth's curvature. If the points however are close enough (as the examples show below) then everything works as it should.

    *****============================================= ===================********
    clear all
    program initial_bearing

    args lat1 lon1 lat2 lon2 newvar

    tempname d2r r2d
    scalar `d2r' = _pi / 180
    scalar `r2d' = 180 / _pi

    gen `newvar' = atan2(sin((`lon2'-`lon1') * `d2r') * cos(`lat2' * `d2r') , ///
    cos(`lat1' * `d2r') * sin(`lat2' * `d2r') - ///
    sin(`lat1' * `d2r') * cos(`lat2' * `d2r') * ///
    cos((`lon2'-`lon1') * `d2r'))

    // normalize atan2 results (-pi to pi) to range from 0 to 360 degrees
    replace `newvar' = mod((`newvar' * `r2d') + 360,360)
    end


    *****============================================= ===================********


    set obs 7
    gen latfire = 0 if _n == 1
    gen lonfire = 1 if _n == 1
    gen latnum = 0 if _n == 1
    gen longnum = 0 if _n == 1

    replace latfire = -1 if _n == 2
    replace lonfire = 1 if _n == 2
    replace latnum = 0 if _n == 2
    replace longnum = 0 if _n == 2

    replace latfire = -1 if _n == 3
    replace lonfire = -1 if _n == 3
    replace latnum = 0 if _n == 3
    replace longnum = 0 if _n == 3

    replace latfire = 0 if _n == 4
    replace lonfire = -1 if _n == 4
    replace latnum = 0 if _n == 4
    replace longnum = 0 if _n == 4

    replace latfire = 1 if _n == 5
    replace lonfire = -1 if _n == 5
    replace latnum = 0 if _n == 5
    replace longnum = 0 if _n == 5

    replace latfire = 1 if _n == 6
    replace lonfire = 0 if _n == 6
    replace latnum = 0 if _n == 6
    replace longnum = 0 if _n == 6

    replace latfire = 1 if _n == 7
    replace lonfire = 1 if _n == 7
    replace latnum = 0 if _n == 7
    replace longnum = 0 if _n == 7

    initial_bearing latfire lonfire latnum longnum ib

    order lonfire latfire
    browse

    Comment

    Working...
    X