简体   繁体   中英

How to find the minimum value of netCDF array excluding zero

I am using Python 2, and dealing with a netcdf data.

This array is a variable called cloud water mixing ratio, which is an output from WRF climate model that has 4 dimensions:

QC(time (25), vertical level (69), latitude (119), longitude (199))

I'm trying to get the minimum value of the values in this array. From initial analysis using NCVIEW visualisation, I found that the minimum value is approximately 1x10-5 and the maximum is 1x10-3.

I've used

var = fh.variables['QC']
var[:].max()
var[:].min()

The max works fine, but the min gives me 0.0.

Then I tried a solution from here , which is

var[var>0].min()

but I also get zero. Then I realised that the above code works for arrays with negatives, while mine doesn't have negatives.

I've tried looking for solutions here and there but found nothing that works for my situation. Please, if anyone could point me to the right directions, I'd appreciate it a lot. Thanks.

var[var>0].min is a function, you need to call it using ()

var[var>0].min() should work much better

sorry for not being able to post the data as I don't have the privilege to share the data. I have tried creating a random 4d array that is similar to the data, and used all the solution you all provided, especially by @Joao Abrantes, they all seemed to work fine. So I thought maybe there is some problem with the data.

Fortunately, there is nothing wrong with the data. I have discussed this with my friend and we have finally found the solution.

The solution is

qc[:][qc[:]>0].min()

I have to specify the [:] after the variable instead of just doing

qc[qc>0].min()

There is also another way, which is to specify the array into numpy array because, qc = fh.variables['QC'] returns a netCDF4.Variable. By adding the second line qc2 = qc[:] , it has become numpy.ndarray.

qc = fh.variables['QC']
qc2 = qc[:]      # create numpy array
qc2[qc2>0].min()

I'm sorry if my question was not clear when I posted it yesterday. As I have only learned about this today.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM