2016-10-20 4 views
0

Übersicht: Ich habe Schwierigkeiten zu verstehen, wo ich falsch mit dieser verschachtelten Maske mit np.where. Was ich hoffe, ist - wenn Schnee wahr ist, 1 zuweisen, wenn falsch den zweiten np bewerten. Wo, was testet, wenn no_snow wahr ist, weise 0 zu, wenn falsch (was bedeutet, dass Schnee falsch ist und no_snow wenn falsch), dann ordne 2 zu .geschachtelt np.where und boolean Array Indizierung Ausgabe

# open IMS & pull necessary keys. 
hf = h5py.File(ims_dir + 'ims_daily_snow_cover.h5', 'r') 
ims = hf['snow_cover'][...] 


# create an empty parameter to be later written to new hdf file as gap_fill_flag. 
dataset_fill = np.zeros(ims.shape) 

# loop through fill - branch based on temporal fill or merra fill. 
for day in range(len(fill)): 
    # print len(day) 
    print day 
    fill[day] == 2 
    year = days[day][:4] 
    # merra fill - more than one consecutive day missing. 
    if (fill[day-1] == 2) | (fill[day+1] == 2): 
     # run merra_fill function 
     # fill with a 2 to signify data are filled from merra. 
     ims[day, :] = merra_fill(days[day], ims[day, :]) 
     dataset_fill[day, :] = 2 
    else: 
     # temporal_fill - less than one consecutive day missing. 
     snow = ((ims[day - 1:day+2, :] == 1).sum(axis=0)) == 2 
     no_snow = ((ims[day - 1:day+2, :] == 0).sum(axis=0)) == 2 
     # nested np.where. 
     ims[day, :] = np.where(snow == True, 1, np.where(no_snow == True, 0, 2)) 

     dataset_fill[day, :][ims[day, :] < 2] = 1 
     dataset_fill[day, :][ims[day, :] == 2] = 2 

     ims[day, :][ims[day, :] == 2] = merra_fill(days[day], ims[day, :]) 

Fehler:

ims[day, :] = np.where(snow == True, 1, np.where(no_snow == True, 0, 2)) 
ValueError: NumPy boolean array indexing assignment cannot assign 2005409 input values to the 0 output values where the mask is true 

Helfen Sie mir, Stackoverflow. Du bist meine einzige Hoffnung.

+0

Was ist ims? ... –

+0

@ Jérôme ims ist ein Attribut aus einer hdf-Datei mit der Form (6574, 2005409) und repräsentiert die Schneedecke. Es wird als globale Variable bezeichnet. – Nikolai

+0

PyCharm schlägt vor, ich verwende 'if cond ist True:' oder 'if cond:' Wie würde das in dieser Codezeile gelten und wäre das Ergebnis dasselbe? – Nikolai

Antwort

2

Von Hilfe (np.where):

where(condition, [x, y]) 

Return elements, either from `x` or `y`, depending on `condition`. 

If only `condition` is given, return ``condition.nonzero()``. 

Parameters 
---------- 
condition : array_like, bool 
    When True, yield `x`, otherwise yield `y`. 
x, y : array_like, optional 
    Values from which to choose. `x` and `y` need to have the same 
    shape as `condition`. 

Ich bezweifle, dass np.where(no_snow...) die gleiche Form wie snow == True hat.

Verwandte Themen