Search code examples
pythonarraysmaskhealpy

applying healpy mask to array of maps


I have a series of maps with two different indices, i and j. Let this be indexed like map_series[i][j].

EDIT 1/21: A minimal working example would be something like map_series=np.array([np.array([np.arange(12) + 0.1*(i+1) + 0.01*(j+1) for j in range(3)]) for i in range(5)])

I'd like to apply the same mask to each; if map_series is one-dimensional, these each work.

I can imagine a few different ways of applying these maps:

(A) Applying the mask to the whole array:

map_series_ma = hp.ma(map_series)
map_series_ma.mask = predefined_mask

(B1) Applying the mask to each element of the array:

map_series_ma = np.zeros_like(map_series)
for i in range(len(map_series)):
    for j in range(len(map_series[0])):
        temp = hp.ma(map_series[i][j])
        temp.mask = predefined_mask
        map_series_ma[i][j] = temp

(B2) Applying the mask to each element of the array:

map_series_ma = np.zeros_like(map_series)
for i in range(len(map_series)):
    for j in range(len(map_series[0])):
        map_series_ma[i][j] = hp.ma(map_series[i][j])
        map_series_ma[i][j].mask = predefined_mask

(C) Pythonically enumerating the list:

map_series_ma = np.array([hp.ma(map_series[i][j]) for j in range(j_max) for i in range(i_max)])
map_series_ma.mask = predetermined_mask

All of these fail to give my desired output, however.

Upon trying (A) or (C) I get an error after the first step, telling me TypeError: bad number of pixels.

Upon trying (B1) I don't get an error, but I also none of the elements of the maps_series_ma have masks; in fact, they do not even appear to be hp.ma objects. Oddly enough, though: when I return temp it does have the appropriate mask.

Upon trying (B2) I get the error AttributeError: 'numpy.ndarray' object has no attribute 'mask' (which, after looking at my syntax, I totally understand!)

I'm a little confused how to go about this. Both (A) and (B1) seem acceptable to me...

Any help is much appreciated, Thanks, Sam


Solution

  • this works for me:

    import numpy as np
    import healpy as hp
    
    map_series=np.array([np.array([np.arange(12) + 0.1*(i+1) + 0.01*(j+1) for j in range(3)]) for i in range(5)])
    map_series_ma = map(lambda x: hp.ma(x), map_series)
    pm=[True, True,True,True,True,True,False,False,False,False,False,False]
    for m in map_series_ma:
        for mm in m:
            mm.mask=pm