Skip to content

Instantly share code, notes, and snippets.

View j-faria's full-sized avatar
🎯
Focusing

João Faria j-faria

🎯
Focusing
View GitHub Profile
@j-faria
j-faria / acknowledgements.py
Last active September 8, 2017 13:51
Print the damn acknowledgements!
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Print the acknowledgements sentence with all the project references!
See acknowledgements.py -h or run acknowledgements.py ? for help or simply use as
acknowledgements.py Author1 Author2
"""
name = 'acknowledgements'
@j-faria
j-faria / develop.py
Created September 6, 2017 13:39
re-import everything at any stage
def develop():
import reimport, os
mod = reimport.modified()
try:
mod.remove('__main__')
except ValueError:
pass
reimport.reimport(*mod)
print 'Done re-importing'
@j-faria
j-faria / planet.py
Created May 10, 2017 19:16
Calculate the mass and semi-major axis of a planet, from its orbital period, semi-amplitude and eccentricity
from astropy.constants import G
import astropy.units as u
# we assume m_planet << m_star
def get_planet_mass(P, K, e, star_mass=1.0):
"""
P in days, K in m/s, star_mass in solar masses
output is planet mass in Jupiter masses
"""
@j-faria
j-faria / binom_fraction.py
Created May 10, 2017 12:37
Probability distribution for fraction of X, given the total sample size N, and the number of Xs in the sample, n. Follows the Appendix of Burgasser et al. (ApJ, 586:512, 2003)
import numpy as np
from scipy.optimize import bisect
from scipy.special import binom as binom_coeff
from scipy.integrate import quad
from functools import partial
def binom_function(N, n, p):
c = binom_coeff(N, n)
return c * p**n * (1.-p)**(N-n)
@j-faria
j-faria / mount.sh
Created February 2, 2017 00:54
Mount a remote directory locally using `sshfs`
ssh -N -f -L port:server2:22 user@server1
# mount the directory locally
sshfs -C -p port user@localhost:remote_directory local_directory(must exist)
# unmount
sudo umount local_directory
@j-faria
j-faria / gist:3c642e1fa69b00653a608232d86dffe6
Created November 23, 2016 17:37
Special function for Vardan
def fun(teff=None, mass=None):
if teff and mass:
# gave two parameters
pass # goto 1
elif teff:
# don't have mass, have teff
mass = f(teff)
elif mass:
# don't have teff, have mass
# teff = f(mass)
@j-faria
j-faria / bw.py
Last active April 6, 2016 16:53
Convert a PDF file to black&white and display it
#!/usr/bin/env python
"""
Convert a PDF file to black&white and display it.
Run as: python bw.py input.pdf
"""
viewer = 'evince'
from contextlib import contextmanager
import tempfile
@j-faria
j-faria / HARPS_ESOarchive.py
Created March 25, 2016 15:27
Query the ESO archive for HARPS data. Works with astroquery.eso version in my fork https://github.com/j-faria/astroquery/commit/01aba0d488acec92c78b5cc792ceca628aa9ef22
import sys
from astroquery.eso import Eso
eso = Eso()
# should use the default username
# and the keyring stored password
authentication = eso.login()
if not authentication:
raise RuntimeError('Something went wrong with authentication!')
@j-faria
j-faria / configfile.py
Created January 9, 2014 20:17
INI file reader
# file configfile.py
# create a custom INI file reader and read a file called 'test.ini'
import ConfigParser
class iniReader(ConfigParser.ConfigParser):
def as_dict(self):
d = dict(self._sections)
for k in d:
d[k] = dict(self._defaults, **d[k])
@j-faria
j-faria / powernoise.py
Created December 14, 2013 16:35
Generate samples of power law noise.
from math import floor
from numpy import arange, exp, pi, flipud, append, conj, real
from numpy.fft import ifft
from numpy.random import randn
def powernoise(alpha, N, randpower=False, normalize=False):
"""
Generate samples of power law noise. The power spectrum
of the signal scales as f^(-alpha).