shashwatak / satellite-js

Modular set of functions for SGP4 and SDP4 propagation of TLEs.
MIT License
902 stars 145 forks source link

Getting lat/lon for a whole day. #83

Closed JaniniRami closed 3 years ago

JaniniRami commented 3 years ago

Hey, I was trying to generate a list containing the lat/lon of the ISS in every second (preferably millisecond) of the day, I tried to do this and ended up crashing my browser, is there any efficient way to do so without crashing my browser?

function predPoints(tle1, tle2){
  var dateTime = new Date();
  var i;
  for (i = 0; i < 86401; i++){
    var satrec = satellite.twoline2satrec(tle1, tle2);

    var positionAndVelocity = satellite.propagate(satrec, dateTime.setSeconds(dateTime.getSeconds() + i ));
    var positionEci = positionAndVelocity.position;

    var gmst = satellite.gstime( dateTime.setSeconds(dateTime.getSeconds() + i ));

    var positionGd = satellite.eciToGeodetic(positionEci, gmst);

    var longitude = positionGd.longitude,
        latitude  = positionGd.latitude;

    var longitudeStr = satellite.degreesLong(longitude),
        latitudeStr  = satellite.degreesLat(latitude);

    position = [longitudeStr, latitude]
    console.log(position);
  }
davidcalhoun commented 3 years ago

It sounds like there's a problem in your code that is causing an infinite loop. Double check the stop condition and make sure it is indeed exiting. I'm using a similar looping strategy with own library and it works fine and is very fast.

One second intervals will get you a pretty nice ground track, except at extreme polar latitudes on Mercator projection maps, which have more map distortion and need <1s intervals for a slightly smoother-looking line at those latitudes.

thkruz commented 3 years ago

I regularly do the whole catalog on a one second loop, so I agree with @davidcalhoun that your issue is that it isn't exiting. If you post your current code then I'd be happy to take a look.

I'd recommend interpolating between 250 or 500 millisecond measurements if you really want that level of detail - but as was said, you probably don't need that except in really specific cases. What are you trying to accomplish?

JaniniRami commented 3 years ago

I felt that my function for predicting the position for every second in the day is wrong so I didn't include it in the issue, but ill include it now as you asked to see it, I hope I can get some feedback of what is the proper way to do it.

JaniniRami commented 3 years ago

@thkruz I am trying to get a list of lat/lon for the ISS throughout the day to plot them on a map creating a ground track look alike map.

thkruz commented 3 years ago

Two things that jump out (I can do some more testing when I get home):

Comment out the console.log and try again. You should instead do something like this:

Put this outside the loop.

var positionArray = [];

And this at the bottom

positionArray.push(position);
}

console.log(positionArray)

Second, lesser thing, is that having the var statements INSIDE the loop is going to cause new objects to be created every time. I would declare all your variables at the top ie:

var positionAndVelocity, positionEci, ... etc

and the just change the values inside the loop so that it reuses those objects over and over.

thkruz commented 3 years ago

@thkruz I am trying to get a list of lat/lon for the ISS throughout the day to plot them on a map creating a ground track look alike map.

Then @davidcalhoun is definitely correct that you do not need millisecond intervals. Try 500 milliseconds and see if you like the results. A more complete solution would change i to increment by 1000 unless the current latitude > 80 || < -80 and then increment by 250.

JaniniRami commented 3 years ago

Thank you so much, removing the vars and declaring them outside the function made it work, Also thanks for the advice I will try to use milliseconds instead of seconds and see how they compare in accuracy!