Counting the number of times a threshold is exceeded in a multidimensional array in Python

I have a numpy array that I brought from a netCDF file with the form (930, 360, 720), where it is organized as (time, latitude, longitude).

For each lat / lon pair for each of the 930 timestamps, I need to count the number of times the value matches or exceeds the threshold value "x" (for example, 0.2 or 0.5, etc.) and ultimately calculates the percentage that the threshold was exceeded at each point, then output the results so that they can be built later.

I tried to use many methods, but here is the last one:

lat_length = len(lats) 

#where lats has been defined earlier when unpacked from the netCDF dataset

lon_length = len(lons) 

#just as lats; also these were defined before using np.meshgrid(lons, lats)

for i in range(0, lat_length):
     for j in range(0, lon_length):
          if ice[:,i,j] >= x:
               #code to count number of occurrences here
               #code to calculate percentage here
               percent_ice[i,j] += count / len(time) #calculation 

 #then go on to plot percent_ice

Hope this makes sense! I would really appreciate any help. I myself teach in Python, so I can skip something simple.

any()? , ?

+4
2

3D- x, ndarray.sum(axis=0), , , , , -

# Calculate count after thresholding with x and summing along first axis
count = (ice > x).sum(axis=0)

# Get percentages (ratios) by dividing with first axis length
percent_ice = np.true_divide(count,ice.shape[0])
+3

, , !

, , , numpy MaskedArray, , , , :

Numpy , MaskedArray, numpy. , . , ( np.ma.masked_greater() ):

ice = np.ma.masked_greater_equal(ice)

ice.count(), , lat/lon, , :

n_good = ice.count(axis=0)

2- , . , n_good ice.shape[0]:

n_bad = ice.shape[0] - n_good

, , :

perc_bad = n_bad/float(ice.shape[0])

, MaskedArray. , .

+2

Source: https://habr.com/ru/post/1620450/


All Articles