-1

I am working on multiple NetCDF4 file and want to extract the monthly_rain values from it. Here is my code:

import numpy
import netCDF4

with netCDF4.Dataset('abc.nc', 'r') as mask_dataset:
    mask_data = mask_dataset.variables['mask'][:]

results = []

for year in range(2010, 2019):
    with netCDF4.Dataset('{:d}.monthly_rain.nc'.format(year), 'r') as dataset:
        data = dataset.variables['monthly_rain'][:]
        data.mask = mask_data.mask

        average = numpy.mean(data)

    results.append(average)

print(results)

The outcome from above code is:

[92.82600598372804, 67.01124693612452, 54.30168356893234, 39.58771623758809, 45.30353874205824, 39.017626997972684, 50.94861235658874, 44.55133832249074, 41.7971056907917]

which is the result I want.

However, I want to extract all the monthly_rain value from the file so that I can do further inspection on the dataset. Are there any method that can allow me do that?

Bart
  • 9,825
  • 5
  • 47
  • 73

1 Answers1

-1

Now I can answer... Just do not calculate averages in the loop, but directly append (masked) "data" to the results, and then do any additional post-processing.

Andrew
  • 720
  • 1
  • 6
  • 9
  • But it did't return the value, i can only get 9 masked array list, and the result are like this: mask=[[[ True, True, True, ..., True, True, True], [ True, True, True, ..., True, True, True], [ True, True, True, ..., True, True, True], – lai hokhim Apr 26 '20 at 11:32
  • Did you use results.append(data)? – Andrew Apr 26 '20 at 16:57
  • yes, in the list i got 10 masked array with size( 12,681,841). But i want to get the attribute value from it. For example, the value of monthly_rain. How can i work on it? – lai hokhim Apr 27 '20 at 01:23
  • What is an "attribute value"? Something different than your "data"? Please, use ncinfo and show the structure of your "regular" netCDF4 files. And your corrected code also. – Andrew Apr 27 '20 at 07:34