Skip to content

Instantly share code, notes, and snippets.

@jiffyclub
Created October 25, 2011 00:32
Show Gist options
  • Save jiffyclub/1310942 to your computer and use it in GitHub Desktop.
Save jiffyclub/1310942 to your computer and use it in GitHub Desktop.
Collapse 2-D array into one dimension.
import numpy
# Uses MEANCLIP from above
from meanclip import meanclip
def mytotal(inarray, axis, type='meanclip'):
"""
Collapse 2-D array in one dimension.
.. note:: MYTOTAL routine from ACS library.
:History:
* Obtained from M. Sirianni.
* Modified and converted to Python by P. L. Lim in 2009.
Examples
--------
>>> collapsed_array = mytotal(inarray, 1, type='median')
Parameters
----------
inarray: array_like
Input 2-D array.
axis: {1, 2}
Axis to collapse.
* 1: Return values along Y.
* 2: Return values along X.
type: {'median', 'meanclip', 'stdev'}
Algorithm to use.
Returns
-------
out_arr: array_like
1-D array collapsed along desired axis with desired
algorithm.
"""
func_name = 'MYTOTAL'
out_arr = 0.0
# Check inarray
if inarray.ndim != 2:
print '%s: Input array must be 2D' % func_name
return out_arr
# Check axis
if axis == 1:
n_out = inarray.shape[0]
elif axis == 2:
n_out = inarray.shape[1]
else:
print func_name, ': Axis not supported -', axis
return out_arr
# Initialize output array
out_arr = numpy.zeros(n_out)
out_rng = range(0, n_out)
# Check type
if type == 'meanclip':
for i in out_rng:
if axis == 1:
im_i = inarray[i,:]
else:
im_i = inarray[:,i]
mmean, msigma = meanclip(im_i, maxiter=10, converge_num=0.001)
out_arr[i] = mmean
elif type == 'stdev':
for i in out_rng:
if axis == 1:
im_i = inarray[i,:]
else:
im_i = inarray[:,i]
mmean, msigma = meanclip(im_i, maxiter=10, converge_num=0.001)
out_arr[i] = msigma
elif type == 'median':
for i in out_rng:
if axis == 1:
im_i = inarray[i,:]
else:
im_i = inarray[:,i]
out_arr[i] = numpy.median(im_i)
else:
print func_name, ': Type not supported -', type
out_arr = 0.0
return out_arr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment