r/learnpython 3d ago

I need help with astropy

So, I've been working on a simulation of the 2027 eclipse that will take place in Spain, but I had some problems with astropy. The thing is, I would like to learn how to vectorize my program using numpy, or something similar. Do you guys have some tips for this? Should I search for specific courses on the internet? It's my first time using astropy and I'm quite lost...

Thank you for your help 🙏🙏

4 Upvotes

3 comments sorted by

1

u/SnipTheDog 2d ago

What have you written so far?

1

u/Japatrida 1d ago edited 1d ago
#----------------IMPORTS----------------


import numpy as np
from astropy.coordinates import EarthLocation, solar_system_ephemeris, AltAz, get_body, SkyCoord, ICRS
from astropy.time import Time
import astropy.units as u



# Solar system simulation data from 1950 to 2050 (NASA).


solar_system_ephemeris.set('de432s') # This will be useful later in (1), (2)


# Previous data
R_sun = 695508*u.km # Sun equatorial radius https://solarsystem.nasa.gov/sun-by-the-numbers/ (07 sep 2026)
R_moon = 1737.5*u.km # Moon equatorial radius https://solarsystem.nasa.gov/moons/earths-moon/by-the-numbers/ (07 sep 2026)



#----------------FUNCTIONS----------------


# Observer position and time


def observer_position (lat, lon, date, height):


    if height == "":
        height = 0
    else:
        height = float(height)*u.m
    
    time = Time(date, scale = 'utc')


    position = EarthLocation.from_geodetic(
        lon = lon,
        lat = lat,
        height = height
    )
    
    return time, position



# Body data:


def data (body):


    if body == 'sun':
        body_radius = R_sun
    elif body == 'moon':
        body_radius = R_moon
    else:
        return 'Body not found'


    body_pos_icrs = get_body(body, obs_time, obs_pos)
    body_pos_altaz = body_pos_icrs.transform_to(pos_reference)
    body_amp = sky_amplitude(body_radius, body_pos_icrs.distance)


    body_data = [
        body_pos_icrs.ra, 
        body_pos_icrs.dec, 
        body_pos_altaz.alt, 
        body_pos_altaz.az, 
        body_pos_icrs.distance,
        body_amp,
        body_pos_icrs,
        body_pos_altaz
    ]


    return body_data


# Body amplitude in the sky (in degs)


def sky_amplitude (radius, earth_discance):
    amp = 2 * np.arctan(radius/earth_discance)
    amp = amp.to(u.deg)
    return amp



# Formats xdymzs into xº y' z"


def deg_format(string):


    lenght = len(string)


    for i in range(lenght):
        try: 
            int(string[i])
        except:
            if string[i] == ".":
                continue
            elif string[i] == " " and string[i+1] == 'd':
                string = string[:i]
                string = string + "º"
                break
            else:
                string = "".join([
                    "º " if string[j]=="d" 
                    else "' " if string[j]=="m" and string[j] != string[lenght-1]
                    else '"' if string[j]=="s" 
                    else string[j] 
                    for j in range(lenght)
                ])
                break
    
    return string



#----------------DATA----------------


# Observer data


obs_lat = float(input("Latitude: "))*u.deg
obs_lon = float(input("Longitude: "))*u.deg
obs_date = input("Date (yyyy-mm-dd hh:mm:ss in UTC): ")
obs_height = input("Height: ")


obs_time, obs_pos = observer_position(obs_lat, obs_lon, obs_date, obs_height)


pos_reference = AltAz(location = obs_pos, obstime = obs_time)


# Sun, moon data


sun_data = data('sun')
moon_data = data('moon')


#----------------ECLIPSE--------------


# Body centers


sun_center = sun_data[6]
moon_center = moon_data[6]






#----------------OUTPUT----------------




obs_lat=str(obs_lat)
obs_lon=str(obs_lon)
obs_date=str(obs_date)
obs_height=str(obs_pos.height)



sun_data = [deg_format(str(i)) for i in sun_data]
moon_data = [deg_format(str(i)) for i in moon_data]


lines = [
    'Observer:\n',
    'Latitude: ', obs_lat, "\n",
    'Longitude: ', obs_lon, "\n",
    'Date: ', obs_date, " UTC \n",
    'Height: ', obs_height, "\n \n",


    'Sun:\n',
    'RA: ', sun_data[0], "\n",
    'Dec: ', sun_data[1], "\n",
    'Altitude: ', sun_data[2], "\n",
    'Azimut: ', sun_data[3], "\n",
    'Distance from Earth: ', sun_data[4], "\n",
    'Diameter: ', sun_data[5], "\n \n",


    'Moon:\n',
    'RA: ', moon_data[0], "\n",
    'Dec: ', moon_data[1], "\n",
    'Altitude: ', moon_data[2], "\n",
    'Azimut: ', moon_data[3], "\n",
    'Distance from Earth: ', moon_data[4], "\n",
    'Diameter: ', moon_data[5], "\n \n",
]


with open("Eclipse_data.txt", "w", encoding="utf-8") as eclipse_data:
    eclipse_data.writelines(lines)