Skip to content

Instantly share code, notes, and snippets.

View zonca's full-sized avatar

Andrea Zonca zonca

View GitHub Profile
@zonca
zonca / radec_mollview.py
Created September 13, 2011 21:01
example of Mollview hitmap
import numpy as np
import healpy
RA = np.random.random(10000)*360
DEC= np.random.random(10000)*135-45
NSIDE = 64 #nside determines the size of the pixels
pixels = healpy.ang2pix(NSIDE, np.radians(90-DEC), np.radians(RA)) #NOTE healpy uses colatitude (north pole 0) instead of latitude
hitmap = np.ones(healpy.nside2npix(NSIDE)) * healpy.UNSEEN #by convention non hit pixels have a specific value
pixels_binned = np.bincount(pixels)
hitmap[:len(pixels_binned)] = pixels_binned
healpy.mollview(hitmap, xsize=2000)
@zonca
zonca / rebin.py
Created November 8, 2011 19:12
IDL rebin in python
import numpy as np
def rebin(a, new_shape):
"""
Resizes a 2d array by averaging or repeating elements,
new dimensions must be integral factors of original dimensions
Parameters
----------
a : array_like
@zonca
zonca / testopen.py
Created November 9, 2011 19:12
Python closing test
from contextlib import closing
class File(object):
def __init__(self, filename):
self.filename = filename
print('Created object for %s' % self.filename)
def open(self):
print('Opening %s' % self.filename)
@zonca
zonca / testazel.py
Created January 22, 2012 14:49
PyTPM performance issue
import datetime
import time
import numpy as np
from pytpm import convert, tpm
az = 3.30084818
el = 0.94610742
lat = 34.64
lon = -103.7
@zonca
zonca / ephemradec.py
Created January 24, 2012 21:46
PyEphem convert Azimuth/Elevation to RA/Dec
import datetime
import time
import numpy as np
import ephem
az = np.radians(6.8927)
el = np.radians(-60.7665)
lon = -1 * np.radians(109 + 24/60. + 53.1/60**2)
lat = np.radians(33 + 41/60. + 46.0/60.**2)
@zonca
zonca / buildit
Created January 31, 2012 22:42
Simple Trilinos build test
module swap PrgEnv-pgi PrgEnv-gnu
CC -g -o simpletest.o -I/opt/cray/trilinos/10.8.3.0/GNU/46/x86_64/include -c simpletest.cpp
CC -g simpletest.o -o simpletest -rdynamic -ltpetra_gnu -lkokkos_gnu -lkokkosnodeapi_gnu -ltpi_gnu -lteuchos_gnu -L/opt/cray/trilinos/10.8.3.0/GNU/46/x86_64/lib
@zonca
zonca / accumulate.pyx
Created May 24, 2012 22:53
Cython Numpy accumulate
cimport numpy as np
ctypedef np.long_t DTYPE_int_t
ctypedef np.double_t DTYPE_double_t
cimport cython
@cython.boundscheck(False) # turn of bounds-checking for entire function
@cython.wraparound(False)
def accumulate(
np.ndarray[DTYPE_double_t, ndim=1] a not None,
@zonca
zonca / checkband.py
Created June 1, 2012 18:42
check bands
import pyfits
from scipy.constants import c
from scipy.integrate import trapz
rimo=pyfits.open('rimo.fits')
for ext in rimo[3:]:
f = c/ext.data['WAVENUMBER']/1.e6 #GHz
t = ext.data['TRANSMISSION']
print "%s = Sum: %.2f, Max: %.2f, Integ: %.2f" % (ext.name, t.sum(), t.max(), trapz(t[1:], f[1:]))
@zonca
zonca / write_background.py
Created June 16, 2012 17:54
Spawn process to write in background
from multiprocessing import Pool
import numpy as np
from time import sleep
import logging as l
l.basicConfig(format = "%(asctime)-15s [Process: %(process)s] %(message)s", level=l.DEBUG)
def write_your_array(filename, a):
l.info("writing %s in background process" % filename)
# implement writing
@zonca
zonca / create_testfile.py
Created July 24, 2012 08:47
hdf5 read compound with boolean
import numpy as np
import h5py
data = np.ones(10, dtype=[("THETA",np.double),("PHI",np.double),("PSI",np.double),("FLAG",np.bool)])
with h5py.File("testout.h5", mode='w') as f:
f.create_dataset("data", data=data)