更新:更改了这个问题以更好地反映我目前的理解。
我有一个 NetCDF 版本 4.5 Grib2Record 对象。给定一个 (x,y) 网格点和一个变量名称,我想按预测时间从对象中提取该变量的所有预测数据(如果记录包含该变量的预测)。因为写磁盘索引文件的默认行为,我不希望使用更高级别的NetCDFFile接口。
我曾尝试查看较低级别的代码(Grib2Rectilyser、Grib2Customizer 等),但代码过于密集,我正在寻求有关从哪里开始的帮助。
我将不胜感激任何关于如何获取 Grib2Record 和 1. 检查其中是否包含特定预测变量的指示,以及 2. 如果是,则通过给定 xy 网格点的预测有效时间提取预测数据和z 级。
我有一个带有网格的 netCDF 文件(每步 0.25°)。我想要的是变量的值,比如 tempMax,在某个网格点,在过去 50 年里。
我知道您像这样将数据读入 python
lon = numpy.array(file.variables['longitude'][:])
lat = numpy.array(file.variables['latitude'][:])
temp = numpy.array(file.variables['tempMax'][:])
time = numpy.array(file.variables['time'][:])
Run Code Online (Sandbox Code Playgroud)
这给我留下了一个数组,我不知道如何“解开”它。如何在整个时间(存储在时间中)获取某个坐标(存储在 temp 中)的值?S 显示是在某个坐标处随时间变化的值。
任何想法我怎么能做到这一点?
谢谢!
我正在尝试使用我自己的 .csv 文件中的数据来创建使用 R 包“ncdf4”的 netCDF 文件。我的数据集由 3 列组成:经度、纬度和温度,有 2592 行。我一直在按照包中的建议将维度和变量添加到 netCDF 文件中。一切都很好,直到我想在我的文件上写入温度数据。我收到此错误:
Error in ncvar_put(nc = ncnew, varid = var_temp, data, start = c(1, 1, :
ncvar_put: error: you asked to write 65160 values, but the passed data
array only has 2592 entries!
Run Code Online (Sandbox Code Playgroud)
怎么了?
library(ncdf)
library(ncdf4)
TimeTable<-read.csv("time.csv",header=T,sep=",")
filename="time.nc"
xvals<-1:360
yvals<--90:90
nx<-length(xvals)
ny<-length(yvals)
lon1<-ncdim_def("longitude","degrees_east",xvals)
lat2<-ncdim_def("latitude", "degrees_north",yvals )
time<-ncdim_def("Time","months", 1:12, unlim=T )
mv <- -999 # missing value to use
var_temp<- ncvar_def("temperature", "celsius", list(lon1, lat2, time),longname="CRU_Global_1961-1990_Mean_Monthly_Surface_Temperature_Climatology",mv)
ncnew<-nc_create(filename,list(var_temp))
print(paste("The file has",ncnew$nvars,"variables"))#
print(paste("The …Run Code Online (Sandbox Code Playgroud) 我是使用 NetCDF 文件的新手,我无法在其他地方找到我的问题的答案。
2015 年的每日降水数据(来自 Gridmet):https ://www.northwestknowledge.net/metdata/data/pr_2015.nc
我的问题:地图在 x 轴上显示为纬度,在 y 轴上显示为长。如何翻转这些轴?此外,纬度值似乎也被反转了。(见下面的链接地图)
library(raster)
library(ncdf4)
nc15 <- nc_open("C:\\Users\\vsteen\\Desktop\\BorealToad\\Climate\\pr_2015.nc")
b <- brick("C:\\Users\\vsteen\\Desktop\\BorealToad\\Climate\\pr_2015.nc",varname="precipitation_amount")
plot(b[[3]])
print(nc15)
1 variables (excluding dimension variables):
float precipitation_amount[lat,lon,day]
units: mm
description: Daily Accumulated Precipitation
_FillValue: -32767
esri_pe_string: GEOGCS[\"GCS_WGS_1984\",DATUM[\"D_WGS_1984\",SPHEROID[\"WGS_1984\",6378137.0,298.257223563]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]]
coordinates: lon lat
cell_methods: time: sum(interval: 24 hours)
missing_value: -32767
3 dimensions:
lon Size:1386
units: degrees_east
description: longitude
lat Size:585
units: degrees_north
description: latitude
day Size:365
units: days since 1900-01-01 00:00:00
calendar: gregorian
description: days since 1900-01-01
9 global attributes: …Run Code Online (Sandbox Code Playgroud) 我有两个 netcdf 文件:rsds.nc名为rsns.nc. rsds.nc包含一个名为 rsds 的变量,并rsns.nc包含一个名为 rsns 的变量。现在,我想有上涌辐射rsus.nc通过内减去变量rsds.nc和rsns.nc分别。
我尝试了以下方法:
ncdiff rsds.nc rsns.nc rsus.nc
ncbo op_typ=diff rsds.nc rsns.nc rsus.nc
Run Code Online (Sandbox Code Playgroud)
他们都产生了一个,rsus.nc但是这个文件中的变量 rsus 丢失了。知道为什么会这样吗?
我有一个三维的大netcdf文件.我想替换LU_INDEXnetcdf文件中的变量所有值10与2.
我写这个python脚本这样做但它似乎不起作用.
filelocation = 'D:/dataset.nc'
ncdataset = nc.Dataset(filelocation,'r')
lat = ncdataset.variables['XLAT_M'][0,:,:]
lon = ncdataset.variables['XLONG_M'][0,:,:]
lu_index = ncdataset.variables['LU_INDEX'][0,:,:]
lu_index_new = lu_index
ncdataset.close()
nlat,nlon=lat.shape
for ilat in range(nlat):
for ilon in range(lon):
if lu_index == 10:
lu_index_new[ilat,ilon] = 2
newfilename = 'D:/dataset.new.nc'
copyfile(ncdataset,newfilename)
newfile = nc.Dataset(newfilename,'r+')
newfile.variables['LU_INDEX'][0,:,:] = lu_index_new
newfile.close()
Run Code Online (Sandbox Code Playgroud)
我收到错误:
The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Run Code Online (Sandbox Code Playgroud)
我对python不是很有经验,所以如果有更简单的方法,我们非常欢迎您发表评论.
您好,我有一个包含每日数据的netcdf文件。文件的形状为(5844,89,89),即16年数据。我试图从每日数据中获取每月平均值。我正在寻找类似的resample功能在熊猫数据框中运行。无论如何在python中做到这一点。据我所知,使用cdo和nco进行计算非常容易,但是我正在使用python。
我用来读取netcdf文件的示例代码是:
import netCDF4
from netCDF4 import Dataset
fh = Dataset(ncfile, mode='r')
time = fh.variables['time'][:]
lon = fh.variables['longitude'][:]
lat = fh.variables['latitude'][:]
data = fh.variables['t2m'][:]
data.shape
Run Code Online (Sandbox Code Playgroud) 我最近开始在 R 中使用 netcdf。示例数据在这里:
http://www.earthstat.org/data-download/ > 175 种作物的收获面积和产量 > 个别作物 > 大豆_HarvAreaYield2000_NetCDF
在这个文件夹中,有一个名为的 netcdf 文件 soybean_AreaYieldProduction.nc
这就是我打开 netcdf 的方式
library(ncdf4)
dat <- nc_open("soybean_AreaYieldProduction.nc")
print(soy)
1 variables (excluding dimension variables):
float soybeanData[longitude,latitude,level,time]
LayerDescriptions: struct(5).Data(:,:,1/2/3/4/5/6) to access data layer: 1=Harvested Area fraction, 2=Yield 3=Harvested Area data quality, 4=Yield data quality, 5=Harvested Area in hectares, 6= Production
Units: Harvested Area Fraction(1)=percent of gridcell that was harvested, Yield(2)=metric tons per hectare, Harvested Area Hectares(5)=total hectares harvested per gridcell, Production(6)=Metric Tons
DataQuality: In levels 3 …Run Code Online (Sandbox Code Playgroud) 我正在使用 xarray 将文本文件转换为 netCDF 格式。当我使用 netCDF4 格式和 Python3 时,它将字符串变量存储为字符串,但当我使用 Python2 时,它将它们存储为 n 维字符数组。我试图在编码中设置 dtype='str' 并且没有任何区别。有没有办法使用 Python2 使这些变量具有字符串数据类型?任何想法将不胜感激。
这是我的代码:
import pandas as pd
import xarray as xr
column_names = ['timestamp', 'air_temp', 'vtempdiff', 'rh', 'pressure', 'wind_dir', 'wind_spd']
df = pd.read_csv(args.input_file, skiprows = 1, header=None, names = column_names)
ds = xr.Dataset.from_dataframe(df)
encoding = {'timestamp': {'dtype': 'str'},
'air_temp': {'_FillValue': 9.96921e+36, 'dtype': 'f4'}
}
ds.to_netcdf(op_file.nc, format = 'NETCDF4', unlimited_dims={'time':True}, encoding = encoding)
Run Code Online (Sandbox Code Playgroud)
当我使用 Python3.6 对 op_file.nc 进行 ncdump 时,我得到:
netcdf op_file {
dimensions:
time = …Run Code Online (Sandbox Code Playgroud) 我有一个 .nc 文件作为 xarray 中的数据集打开,其结构如下:
ds
<xarray.Dataset>
Dimensions: (lat: 733, lon: 720, time: 204)
Coordinates:
* time (time) int16 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ...
Dimensions without coordinates: lat , lon
Data variables :
latitude (lat) float32 56.0 55.9917 55.9833 55.975 55.9667 55.9583 ...
longitude (lon) float32 -11.0 -10.9917 -10.9833 -10.975 -10.9667 ...
n2o (lat, lon, time) float64 nan nan nan nan nan nan nan nan …Run Code Online (Sandbox Code Playgroud)