skyfielders / python-skyfield

Elegant astronomy for Python
MIT License
1.43k stars 213 forks source link

distance array #263

Closed bluefandango closed 5 years ago

bluefandango commented 5 years ago

Hello, I do not know this to be an issue, for sure. Please bear with me as I am quite a newbie in Python. I tried to build a date array to obtain corresponding distances, as suggested in here: https://rhodesmill.org/skyfield/time.html#date-arrays I obtained a result that i can not export to a csv file. The reason I think, is that it is not an array of distances. It does not behave like an array or a list.

Question:

  1. what is it? if it's not an array, it might be a good idea to make it so.
  2. how to circumvent this?

from skyfield.api import utc
from datetime import datetime, timedelta
import datetime
planets = load('de421.bsp')
earth, sun = planets['earth'], planets['sun']
ts = load.timescale()
n = datetime.datetime(2018, 12, 12, 19, 00, tzinfo=utc)
date_list = [n + datetime.timedelta(days=x) for x in range(0, 10)]
t = ts.utc(date_list)
astrometric = earth.at(t).observe(sun)
ra, dec, d = astrometric.radec()```
brandon-rhodes commented 5 years ago

“I obtained a result that i can not export to a csv file.” — I would need to see your CSV routine to know exactly what kind of input it needs, but, try accessing a Distance attribute like km if you want an array of actual numbers:

https://rhodesmill.org/skyfield/api.html#units

Let me know if that fixes the problem.

bluefandango commented 5 years ago

hello again, i wish i could have answered earlier. but here are the different approaches i tried:```

import csv
d = [1, 2, 3]
with open('/home/user/Documents/"lunar distance.csv', 'w') as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
wr.writerow(d)
got: expected an iterable**

#2nd way to export
import numpy
final_d = numpy.asarray(d)
numpy.savetxt("lunar distance.csv", a, delimiter=",")

got: ValueError: Expected 1D or 2D array, got 0D array instead

as for your piece of advice, i had trouble implementing it. indeed, as i told you before i am quite new to python and my understanding of the works of the language is sketchy. but i guessed that you directed me to do something like : d.km this gave me a list of distances with method #2. i have to check them out, but the results seem acceptable. is it the way to proceed, you think?

anyway, i realize i am taking much time and thank you for any help you can provide. i would like to tell you a story, though: this all started a year ago. i needed astronomical distances for a pet project of mine. so, i contacted: some random astronomy website, a french astronomical society and NASA. asking them for the data. they directed me to books, gave me an approximate formula to make up the distance. but none ever, told me about skyfield. it made me wonder for a while, if it had the right amount of advertising.

thanks again for your great job.

brandon-rhodes commented 5 years ago

Maybe I will have to think about whether Skyfield could get better advertising! But at NASA many groups would be using other languages and might not be able to use it.

When pasting in Python code, try formatting it as code — there should be a little button above the text area that, after you highlight text, lets you mark it with three back-ticks as code, so the indents show up and a reader can understand which code goes inside the "with" and which code is outside of it.

Your first code example works for me:

import csv
d = [1, 2, 3]
with open('lunar-distance.csv', 'w') as myfile:
    wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
    wr.writerow(d)

I get no error under Python 2 or Python 3.

If instead of using a NumPy-specific CSV export routine you are trying to use Python's built-in CSV routine, try something like:

    wr.writerow(list(d.km))

if d is a Distance object. If that doesn't work, share the output of print(d), print(d.km), and print(list(d.km)) — fixing code usually involves seeing what the data looks like, rather than staring at the code and trying to guess, which can be difficult.

bluefandango commented 5 years ago

hello again,

it's been a while and now i that i returned to the program, i finished it. i corrected my previous posts so that it show codes the proper way. but, something is wrong the distance that my program gives is different from the ones that can be found on the net (two different websites) so, i wanted to share my anxiety as to the value of the code i managed painstakingly to write: is there a mistake?? coordinates and time zone: paris (i double checked the coordinates) in plain words: i would like to know the distance from that city to the moon.

from skyfield.api import load
from skyfield.api import utc
from skyfield.api import Topos
from datetime import datetime, timedelta
import datetime
from pytz import timezone
import pytz

#setting timezone and coordinates
paris = timezone('Europe/Paris')
planets = load('de421.bsp')
earth, moon = planets['earth'], planets['moon']
ts = load.timescale()
n = datetime.datetime(2019, 1, 1, 19, 0, tzinfo=paris)
date_list = [n + datetime.timedelta(days=x) for x in range(0, 100)]
t = ts.utc(date_list)
paris_coordinates = earth + Topos('48.85341 N', '2.34880 E')
astrometric = paris_coordinates.at(t).observe(moon)
ra, dec, d = astrometric.radec()

d.km

#i came up with several possibilities, i chose the 2nd way to export
import numpy
q = numpy.asarray(d.km)
numpy.savetxt("lunar distance.csv", q, delimiter=",")

it might sound silly but i derived much pleasure doing this. indeed, i don't tackle code issues very often. it's proven a valuable intellectual experience.

thank you again for any help you can provide.

brandon-rhodes commented 5 years ago

To answer, I need to know what value you are expecting for the km. What value did the other two websites give for that distance?

bluefandango commented 5 years ago

of, course. silly me.

for 2019-01-01, from paris 19:00 local time @https://www.mooncalc.org/#/48.8792,2.3538,9/2019.01.01/19:00/1/3 ==> 389989 km for 2019-01-01, no particular place, 19:00 CET @https://www.wolframalpha.com/input/?i=lunar+distance+2019-01-01+19:00+cet ==> 389986 km and the code i came up with (thank you for your kind help, i am always puzzled to see people give their time to others) gives: 394955.786346329 2019-01-01 19:00 398299.642955366 01-02 401075.570939656 01-03

have a great summer!

brandon-rhodes commented 5 years ago

Those look like they're giving the distance from the Earth's center to the Moon, not from Paris to the Moon. If I hover over the number on the mooncalc site, the tooltip says "Distance between the center of Earth and Moon."

bluefandango commented 5 years ago

hello again, looks like i misinterpreted the results the sites give. i stand corrected and thank you. my questions have been clearly answered, and if you agree, i'd say the matter is settled and this thread can be closed. thanks again and have a great week end.

brandon-rhodes commented 5 years ago

I'm glad the results make sense now! Good luck with your computation.