[英]Extract data from NetCDF4 file with regional mask file
I am working on multiple NetCDF4 file and want to extract the monthly_rain
values from it.我正在处理多个 NetCDF4 文件,并希望从中提取monthly_rain
值。 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.但是,我想从文件中提取所有monthly_rain
值,以便对数据集进行进一步检查。 Are there any method that can allow me do that?有什么方法可以让我这样做吗?
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.现在我可以回答了……只是不要在循环中计算平均值,而是直接对结果进行 append(屏蔽)“数据”,然后进行任何额外的后处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.