1

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.

11
  • qc[qc!=0].min() may be? Commented May 20, 2015 at 13:17
  • 3
    can't reproduce your error just tried it A=np.array((1e-10,0)) print A[A>0].min() >>1e-10 Commented May 20, 2015 at 13:19
  • Does ir actually return zero or only prints zero? Commented May 20, 2015 at 13:29
  • Hi @JohnGalt, I've also tried that. Doesn't work either. Commented May 20, 2015 at 13:31
  • Hi @JoãoAbrantes I've tried your code, but it gives me 1e-10 instead of the real minimum values in the data. Maybe my description isn't clear in the question. Using ncview to view the netcdf data, I found the minimum to be 0.00009306, but that's only part of the data. I want to be able to run the whole domain to get the lowest values, hence why I'm trying to use Python to get it. Commented May 20, 2015 at 13:33

2 Answers 2

2

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

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

Sign up to request clarification or add additional context in comments.

1 Comment

Hi @firelynx Thanks for that, I actually used the second one in my script (and it didn't work), but I forgot to type the brackets in here. I've edited my question.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.