我从一个min/max文件中提取变量数据值的min/max的通常方法在切换到netCDF4 Python模块时比scipy.io.netcdf慢一个数量级。
我正在处理相对较大的海洋模型输出文件(来自ROMS),在给定的地图区域(夏威夷)具有多个深度级别。当它们出现在NetCDF-3中时,我使用了scipy.io.netcdf.
既然这些文件都在NetCDF-4 (“经典”)中,我就不能再使用scipy.io.netcdf,转而使用netCDF4 Python模块。然而,慢度是一个问题,我想知道是否有一种更有效的方法来提取变量的数据范围(最小和最大数据值)?
下面是我使用的NetCDF 3方法:
import scipy.io.netcdf
netcdf = scipy.io.netcdf.netcdf_file(file)
var = netcdf.variables['sea_water_potential_temperature']
min = var.data.min()
max = var.data.max()下面是我使用netCDF4的NetCDF-4方法:
import netCDF4
netcdf = netCDF4.Dataset(file)
var = netcdf.variables['sea_water_potential_temperature']
var_array = var.data.flatten()
min = var_array.data.min()
max = var_array.data.max()值得注意的区别是,I必须首先将netCDF4中的数据数组扁平化,而且这个操作显然会减慢速度。
有没有更好/更快的方法?
发布于 2017-11-28 14:34:57
根据hpaulj的建议,这里有一个使用subprocess调用nco命令ncwa的函数。当使用OPeNDAP地址时,它会挂起,而且我手头没有任何文件可以在本地测试它。
你可以看到它是否适合你,以及速度的差别是什么。
这假设您已经安装了nco库。
def ncwa(path, fnames, var, op_type, times=None, lons=None, lats=None):
'''Perform arithmetic operations on netCDF file or OPeNDAP data
Args
----
path: str
prefix
fnames: str or iterable
Names of file(s) to perform operation on
op_type: str
ncwa arithmetic operation to perform. Available operations are:
avg,mabs,mebs,mibs,min,max,ttl,sqravg,avgsqr,sqrt,rms,rmssdn
times: tuple
Minimum and maximum timestamps within which to perform the operation
lons: tuple
Minimum and maximum longitudes within which to perform the operation
lats: tuple
Minimum and maximum latitudes within which to perform the operation
Returns
-------
result: float
Result of the operation on the selected data
Note
----
Adapted from the OPeNDAP examples in the NCO documentation:
http://nco.sourceforge.net/nco.html#OPeNDAP
'''
import os
import netCDF4
import numpy
import subprocess
output = 'tmp_output.nc'
# Concatenate subprocess command
cmd = ['ncwa']
cmd.extend(['-y', '{}'.format(op_type)])
if times:
cmd.extend(['-d', 'time,{},{}'.format(times[0], times[1])])
if lons:
cmd.extend(['-d', 'lon,{},{}'.format(lons[0], lons[1])])
if lats:
cmd.extend(['-d', 'lat,{},{}'.format(lats[0], lats[1])])
cmd.extend(['-p', path])
cmd.extend(numpy.atleast_1d(fnames).tolist())
cmd.append(output)
# Run cmd and check for errors
subprocess.run(cmd, stdout=subprocess.PIPE, check=True)
# Load, read, close data and delete temp .nc file
data = netCDF4.Dataset(output)
result = float(data[var][:])
data.close()
os.remove(output)
return result
path = 'https://ecowatch.ncddc.noaa.gov/thredds/dodsC/hycom/hycom_reg6_agg/'
fname = 'HYCOM_Region_6_Aggregation_best.ncd'
times = (0.0, 48.0)
lons = (201.5, 205.5)
lats = (18.5, 22.5)
smax = ncwa(path, fname, 'salinity', 'max', times, lons, lats)发布于 2020-05-08 10:23:00
如果您只是获得一个变量数组的min/max值,则可以使用xarray。
%matplotlib inline
import xarray as xr
da = xr.open_dataset('infile/file.nc')
max = da.sea_water_potential_temperature.max()
min = da.sea_water_potential_temperature.min()这将分别给出一个最小/最大值的值。您还可以在选定的维度(如时间、经度、纬度等)获得变量的最小/最大值。Xarray非常适合处理多维数组,这就是为什么在python中不使用CDO和NCO等其他操作工具时很容易处理NetCDF的原因。最后,xarray还用于处理python ( http://xarray.pydata.org/en/stable/related-projects.html )中的天气和气候数据的其他相关库。
发布于 2020-06-12 14:27:02
Python解决方案(使用CDO作为后端)是我的软件包nctoolkit (https://pypi.org/project/nctoolkit/ https://nctoolkit.readthedocs.io/en/latest/installing.html)。
这有许多内置的方法来计算不同类型的min/max值。
我们首先需要将文件读入数据集:
将nctoolkit导入为nc data =nc.open_data(文件)
如果您想要跨空间的最大值,对于每个时间步骤,您将执行以下操作:
data.spatial_max()
每个网格单元和时间步骤的所有深度的最大值将按以下方式计算:
data.vertical_max()
如果您希望时间越长越好,您可以这样做:
data.max()
这些方法是可链接的,并且CDO后端非常高效,因此应该是处理ROMS数据的理想方法。
https://stackoverflow.com/questions/21740324
复制相似问题