Skip to content

Instantly share code, notes, and snippets.

View j-faria's full-sized avatar

João Faria j-faria

View GitHub Profile
@j-faria
j-faria / spaced.py
Created May 6, 2025 07:32
Linearly and maximally spaced values from an array
def linspaced_in_range(arr, n, return_indices=False, return_reverse_indices=False):
""" Return n values spaced linearly from within arr """
i = np.linspace(0, arr.size - 1, n, dtype=int)
spaced = np.sort(arr)[i]
spaced_i = np.argsort(arr)[i]
spaced_ri = np.array(list(set(np.arange(arr.size)) - set(spaced_i)))
ret = [spaced]
if return_indices:
ret.append(spaced_i)
if return_reverse_indices:
@j-faria
j-faria / howto.md
Last active October 7, 2020 13:20
Auto-reload modules in IPython
  1. Create a file ~/.pythonrc.py
  2. Add
try:
    from IPython import get_ipython
    get_ipython().run_line_magic('load_ext', 'autoreload')
    get_ipython().run_line_magic('autoreload', '2')
except AttributeError:
 pass
import contextlib
import os
@contextlib.contextmanager
def working_directory(path):
"""
A context manager which changes the working directory to the given
path, and then changes it back to its previous value on exit.
"""
prev_cwd = os.getcwd()
@j-faria
j-faria / ExoPlanetCurves
Last active December 5, 2019 14:52
The Keplerian and transit curves
Two small demonstration notebooks of how the Keplerian and transit curves respond to a change in the parameters.
@j-faria
j-faria / eso_access_phase3.sh
Created September 6, 2019 16:48
query and download reduced data from the ESO Archive
#!/bin/sh
#***********************************************************************
#* ESO Science Archive Facility
#* Programmatic Access
#*
#* Script: eso_access_phase3.sh
#* Shell: bash
#* Date: 15-Jul-2015
#* Contact: [email protected]
#* Description: Script to query and download reduced data (Phase 3)
@j-faria
j-faria / journal_abbreviations.py
Created July 2, 2019 19:00
Simplified abbreviations of frequently used journals (according to A&A)
# https://www.aanda.org/67-author-information/frequent-abbreviations
name_abbrv = {
'Astronomy and Astrophysics': 'A&A',
'Astronomy & Astrophysics': 'A&A',
'Monthly Notices of the Royal Astronomical Society': 'MNRAS',
'The Astrophysical Journal': 'ApJ',
'The Astronomical Journal': 'AJ',
'Publications of the Astronomical Society of the Pacific': 'PASP',
'Annual Review of Astronomy and Astrophysics': 'ARA&A' ,
@j-faria
j-faria / gist:fdc0a97a72023dca26b084ec28ac9939
Created April 15, 2019 12:42
ESO archive downloads in parallel
sh downloadRequestNNNNNNscript.sh -X "-P 8 -L 1"
@j-faria
j-faria / mass_function_solve_sympy.py
Created February 28, 2019 00:32
How to solve the binary mass function for m2, using sympy
from sympy import *
from astropy.constants import G
import astropy.units as u
G = G.to(u.m**3 / (u.solMass * u.s**2))
f_rhs = lambda P, K, e: (P * K**3 * (1 - e**2)**(3/2)) / (2 * pi * G.value)
f_lhs = lambda m1, m2, i: (m2**3 * sin(i)) / (m1 + m2)**2
# the earth
@j-faria
j-faria / kepler_solve_sympy.py
Last active February 27, 2019 00:36
How to solve Kepler's equation with sympy
from sympy import symbols, sin, nsolve
E = symbols('E')
def eccentric_anomaly(M, ecc, prec=15):
return nsolve(E - e*sin(E) - M, M, prec=prec)
import mpmath as mp