python将OpenDap加载到NetCdfile
我正在使用URL从opendap服务器(数据的子集)打开netcdf数据。当我打开它时,在请求变量之前,数据(据我所知)并没有实际加载。我想将数据保存到磁盘上的文件中,我将如何执行此操作 我目前有:python将OpenDap加载到NetCdfile,python,netcdf,opendap,Python,Netcdf,Opendap,我正在使用URL从opendap服务器(数据的子集)打开netcdf数据。当我打开它时,在请求变量之前,数据(据我所知)并没有实际加载。我想将数据保存到磁盘上的文件中,我将如何执行此操作 我目前有: import numpy as np import netCDF4 as NC url = u'http://etc/etc/hourly?varname[0:1:10][0:1:30]' set = NC.Dataset(url) # I think data is not yet loaded
import numpy as np
import netCDF4 as NC
url = u'http://etc/etc/hourly?varname[0:1:10][0:1:30]'
set = NC.Dataset(url) # I think data is not yet loaded here, only the "layout"
varData = set.variables['varname'][:,:] # I think data is loaded here
# now i want to save this data to a file (for example test.nc), set.close() obviously wont work
希望有人能帮忙,谢谢 这很简单;创建一个新的NetCDF文件,并复制您想要的任何内容:)幸运的是,在复制正确的尺寸、NetCDF属性。。。从输入文件。我很快编写了这个示例,输入文件也是一个本地文件,但是如果OPenDAP的读取已经工作了,它应该以类似的方式工作
import netCDF4 as nc4
# Open input file in read (r), and output file in write (w) mode:
nc_in = nc4.Dataset('drycblles.default.0000000.nc', 'r')
nc_out = nc4.Dataset('local_copy.nc', 'w')
# For simplicity; copy all dimensions (with correct size) to output file
for dim in nc_in.dimensions:
nc_out.createDimension(dim, nc_in.dimensions[dim].size)
# List of variables to copy (they have to be in nc_in...):
# If you want all vaiables, this could be replaced with nc_in.variables
vars_out = ['z', 'zh', 't', 'th', 'thgrad']
for var in vars_out:
# Create variable in new file:
var_in = nc_in.variables[var]
var_out = nc_out.createVariable(var, datatype=var_in.dtype, dimensions=var_in.dimensions)
# Copy NetCDF attributes:
for attr in var_in.ncattrs():
var_out.setncattr(attr, var_in.getncattr(attr))
# Copy data:
var_out[:] = var_in[:]
nc_out.close()
如果不让我知道,希望它能有所帮助。如果您可以使用xarray,它的作用如下:
import xarray as xr
url = u'http://etc/etc/hourly?varname[0:1:10][0:1:30]'
ds = xr.open_dataset(url, engine='netcdf4') # or engine='pydap'
ds.to_netcdf('test.nc')
还有一个例子说明了如何做到这一点