我有一个netCDF文件,其时间维度包含2小时的数据.我希望平均每小时获得每小时的平均时数.我试过这个:
import xarray as xr
ds = xr.open_mfdataset('ecmwf_usa_2015.nc')
ds.groupby(['time.month', 'time.hour']).mean('time')
Run Code Online (Sandbox Code Playgroud)
但我得到这个错误:
*** TypeError: `group` must be an xarray.DataArray or the name of an xarray variable or dimension
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?如果我这样做:
ds.groupby('time.month', 'time.hour').mean('time')
Run Code Online (Sandbox Code Playgroud)
我没有得到错误,但结果的时间维度为12(每个月一个值),而我想要每个月的小时平均值,即每12个月24个值.数据可在此处获取:https://www.dropbox.com/s/yqgg80wn8bjdksy/ecmwf_usa_2015.nc?dl = 0
我正在尝试使用R包ncdf创建一个多维NetCDF文件.我正在进行一组1500点的气候日常观测,每个点的观测数量为~18250.问题是NetCDF文件(create.ncdf)的结构占用4Gb,每个点使文件的大小增加超过3 Gb(put.var.ncdf)
这是我正在使用的代码:
# Make a few dimensions we can use
dimX <- dim.def.ncdf( "Long", "degrees", Longvector )
dimY <- dim.def.ncdf( "LAT", "degrees", Latvector )
dimT <- dim.def.ncdf( "Time", "days", 1:18250, unlim=FALSE )
# Make varables of various dimensionality, for illustration purposes
mv <- -9999 # missing value to use
var1d <- var.def.ncdf( "var1d", "units", dimX, mv,prec="double" )
var2d <- var.def.ncdf( "var2d", "units", list(dimX,dimY), mv,prec="double" )
var3d <- var.def.ncdf( "var3d", "units", list(dimX,dimY,dimT), mv,prec="double" ) …Run Code Online (Sandbox Code Playgroud) 我不知道为什么这些软件包总是很难安装.我已经使用NetCDF/HDF5很长一段时间了,无论是在Linux还是OSX上,无论是C,C++还是现在的python,无论是在Linux还是OSX上,它总是让他们无法安装或正常运行.netcdf4和hdf5之间的简单依赖关系是许多人痛苦的源头,我真的希望这些软件包的开发人员最终能够做些什么.
所以,我面临的最新具体问题是:我正在尝试为python安装netCDF4.我收到以下错误:
Package hdf5 was not found in the pkg-config search path
Perhaps you should add the directory containing `hdf5.pc'
Run Code Online (Sandbox Code Playgroud)
我尝试使用apt-get安装hdf5软件包,包括:
使用pip,我试过:
pip install h5py
Run Code Online (Sandbox Code Playgroud)
这很难以解决对Cython的依赖,然后我手动安装.之后安装(显然)但我无法在任何地方找到文件hdf5.pc.
我在这里拔毛.有谁知道如何解决这个问题?
我试过安装ncdf4包.我有R版3.1.1.
问题似乎是找到了nc-config.如果我谷歌针对这个特定的问题,那么我发现有相同问题的人,在安装libnetcdf-dev之前通过安装Debian软件包来解决它ncdf4.
我已经安装了该软件包,但我一直遇到同样的问题,
错误如下:
checking for nc-config... no
-----------------------------------------------------------------------------------
Error, nc-config not found or not executable. This is a script that comes with the netcdf library, version 4.1-beta2 or later, and must be present for configuration to succeed.
Run Code Online (Sandbox Code Playgroud) 我对使用 xarrays 还很陌生。我想就地修改 NetCDF 文件的属性。但是,内置函数提供了另一个新的数据集。
ds = xr.open_dataset(file_)
# ds has "time" as one of the coordinates whose attributes I want to modify
#here is ds for more clarity
ds
>><xarray.Dataset>
Dimensions: (lat: 361, lev: 1, lon: 720, time: 1)
Coordinates:
* lon (lon) float32 0.0 0.5 1.0 1.5 2.0 ... 357.5 358.0 358.5 359.0 359.5
* lat (lat) float32 -90.0 -89.5 -89.0 -88.5 -88.0 ... 88.5 89.0 89.5 90.0
* lev (lev) float32 1.0
* time (time) timedelta64[ns] 00:00:00 …Run Code Online (Sandbox Code Playgroud) 我有一些气象数据导出到netCDF格式,我想在谷歌地图中显示它们.
我尝试了Panoply软件,主要遵循这些http://marinedataliteracy.org/ops/pano_gridsvecs.htm说明,并成功将数据导出到.kml文件中.
kml文件在Google地球中显示正常,但在Google地图中加载时,没有显示任何内容.
加载kml的javascript代码肯定是正确的,因为正确显示了其他类型的kml.以防万一,这是javascript代码
var kmzLayer = new google.maps.KmlLayer("LINK_TO_KML");
kmzLayer.setMap(map);
Run Code Online (Sandbox Code Playgroud)
这里还有一个名为netcdf2gmaps的项目http://code.google.com/p/netcdf2gmaps/,但它似乎已被放弃,并且没有任何反馈意见.
有没有办法将netCDF数据显示到Google地图?
这是我第一次使用netCDF,我正试着用它来解决问题.
我有多个版本3 netcdf文件(NOAA NARR air.2m每日平均一年).每个文件跨越1979年至2012年的一年.它们是349 x 277网格,分辨率约为32千米.数据是从这里下载的.
维度是时间(自1800年1月1日以来的小时数),我感兴趣的变量是空气.我需要计算温度<0的累计天数.例如
Day 1 = +4 degrees, accumulated days = 0
Day 2 = -1 degrees, accumulated days = 1
Day 3 = -2 degrees, accumulated days = 2
Day 4 = -4 degrees, accumulated days = 3
Day 5 = +2 degrees, accumulated days = 0
Day 6 = -3 degrees, accumulated days = 1
Run Code Online (Sandbox Code Playgroud)
我需要将这些数据存储在一个新的netcdf文件中.我熟悉Python并且熟悉R.什么是循环每天的最佳方法,检查前一天的值,并在此基础上,将值输出到具有完全相同的维度和变量的新netcdf文件...或者可能只是使用我正在寻找的输出将另一个变量添加到原始netcdf文件中.
最好将所有文件分开或合并吗?我将它们与ncrcat结合使用它工作正常,但文件是2.3gb.
感谢您的投入.
我目前在python中取得的进展:
import numpy
import netCDF4
#Change my working DIR
f = netCDF4.Dataset('air7912.nc', 'r') …Run Code Online (Sandbox Code Playgroud) 我每天下载600MB netcdf-4文件,它们具有以下结构:
netcdf myfile {
dimensions:
time_counter = 18 ;
depth = 50 ;
latitude = 361 ;
longitude = 601 ;
variables:
salinity
temp, etc
我正在寻找一种更好的方法将time_counter维度从固定大小(18)转换为无限维度.
我找到了一种使用netcdf命令和sed的方法.像这样:
ncdump myfile.nc | sed -e "s#^.time_counter = 18 ;#time_counter = UNLIMITED ; // (currently 18)#" | ncgen -o myfileunlimited.nc
这对我来说对于小文件很有用,但是当转储600 MB的netcdf文件时,需要花费大量的内存和时间.
有人知道另一种方法来完成这个吗?
我得到了foll.尝试使用joblib缓存结果时的用户警告:
from tempfile import mkdtemp
cachedir = mkdtemp()
from joblib import Memory
memory = Memory(cachedir=cachedir, verbose=0)
@memory.cache
def get_nc_var3d(path_nc, var, year):
"""
Get value from netcdf for variable var for year
:param path_nc:
:param var:
:param year:
:return:
"""
try:
hndl_nc = open_or_die(path_nc)
val = hndl_nc.variables[var][int(year), :, :]
except:
val = numpy.nan
logger.info('Error in getting var ' + var + ' for year ' + str(year) + ' from netcdf ')
hndl_nc.close()
return val
Run Code Online (Sandbox Code Playgroud)
我得到了foll.使用foll调用此函数时发出警告.参数:
UserWarning: Persisting input arguments took …Run Code Online (Sandbox Code Playgroud) 我想将我的 xarray 对象重新采样到较低的空间分辨率(LESS PIXELS)。
import pandas as pd
import numpy as np
import xarray as xr
time = pd.date_range(np.datetime64('1998-01-02T00:00:00.000000000'), np.datetime64('2005-12-28T00:00:00.000000000'), freq='8D')
x = np.arange(1200)
y = np.arange(1200)
latitude = np.linspace(40,50,1200)
longitude = np.linspace(0,15.5572382,1200)
latitude, longitude = np.meshgrid(latitude, longitude)
BHR_SW = np.ones((365, 1200, 1200))
output_da = xr.DataArray(BHR_SW, coords=[time, y, x])
latitude_da = xr.DataArray(latitude, coords=[y, x])
longitude_da = xr.DataArray(longitude, coords=[y, x])
output_da = output_da.rename({'dim_0':'time','dim_1':'y','dim_2':'x'})
latitude_da = latitude_da.rename({'dim_0':'y','dim_1':'x'})
longitude_da = longitude_da.rename({'dim_0':'y','dim_1':'x'})
output_ds = output_da.to_dataset(name='BHR_SW')
output_ds = output_ds.assign({'latitude':latitude_da, 'longitude':longitude_da})
print(output_ds)
<xarray.Dataset> …Run Code Online (Sandbox Code Playgroud) netcdf ×10
python ×6
r ×3
google-maps ×1
hdf5 ×1
joblib ×1
linux ×1
nco ×1
numpy ×1
pip ×1
python-2.7 ×1
resampling ×1
sed ×1
xarray ×1