我有一个文件夹,包含2006 - 2001年的NetCDF文件,十年块(2011-2020,2021-2030等).
我想创建一个新的NetCDF文件,其中包含连接在一起的所有这些文件.到目前为止,我已阅读过文件:
ds = xarray.open_dataset('Path/to/file/20062010.nc')
ds1 = xarray.open_dataset('Path/to/file/20112020.nc')
etc.
Run Code Online (Sandbox Code Playgroud)
然后像这样合并这些:
dsmerged = xarray.merge([ds,ds1])
Run Code Online (Sandbox Code Playgroud)
这是有效的,但是很笨重,并且必须有一种更简单的方法来自动化这个过程,因为我将为许多不同文件夹的文件夹执行此操作.有没有更有效的方法来做到这一点?
编辑:
尝试使用glob加入这些文件:
for filename in glob.glob('path/to/file/.*nc'):
dsmerged = xarray.merge([filename])
Run Code Online (Sandbox Code Playgroud)
给出错误:
AttributeError: 'str' object has no attribute 'items'
Run Code Online (Sandbox Code Playgroud)
这只是读取文件名的文本,而不是实际文件本身,所以它不能合并它.如何打开,存储为变量,然后合并而不是一点一点地进行合并?
采用看起来像这样的数据框,包含2005年某些日期的数据和每个日期的测量数据.
df <- data.frame("date" = c('2005-04-04','2005-04-19', '2005-04-26', '2005-05-05',
'2005-05-12', '2005-05-25', '2005-06-02', '2005-06-16', '2005-07-07', '2005-07-14',
'2005-07-21', '2005-08-04'), "numbers" = c(90,50,50,48,44,37,34,30,36,31,49,54))
Run Code Online (Sandbox Code Playgroud)
我希望在一年中的每一天基于此创建1:365的值序列,主要是为了创建一个新的数据框,从2005年1月1日到2005年12月31日,其中已经填充了来自a的值.样条函数拟合这些现有的12个值.
当我尝试使用以下方法时:
numbers <- df$numbers
x = spline(1:365, numbers)
Run Code Online (Sandbox Code Playgroud)
我明白了
xy.coords中的错误(x,y,setLab = FALSE):'x'和'y'长度不同'
我不确定出了什么问题.
以每月的温度数据多年的光栅文件,它具有通过连接访问的名称names(object)按以下格式"Jan.1981",两年与下面的代码工作"Feb.1981"等(例如文件位置 -添加的所有文件太大了
使用以下代码读入并写入NetCDF:
#Load Packages
library(raster)
library(ncdf4)
#Read in temperature files
r1 <- brick('TavgM_1981.grd')
r2 <- brick('TavgM_1982.grd')
#stack them together
TempStack = stack(r1, r2)
#set the coordinate system (as it was missing)
crs(TempStack) <- ('+proj=lcc +lat_1=53.5 +lat_2=53.5 +lat_0=46.834 +lon_0=5 +x_0=1488375 +y_0=-203375 +datum=WGS84 +to_meter=2500 +no_defs +ellps=WGS84 +towgs84=0,0,0')
#reproject to get in lat/lon instead of meters
TempStack<-projectRaster(TempStack, crs=CRS("+init=epsg:4326"))
#Extract monthly data names to assign to netCDf later
names <- names(TempStack)
#write the raster file to NetCDF
writeRaster(TempStack, "Temp.nc", overwrite=TRUE, …Run Code Online (Sandbox Code Playgroud) 这是代码的简化版本,它解释了我要做什么:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
dates = pd.date_range('20070101',periods=1000)
df = pd.DataFrame(np.random.randn(1000), index = dates, columns =list ('A'))
plt.plot(df['A'])
Run Code Online (Sandbox Code Playgroud)
这导致了这个图表:
我想使用日期时间索引中的年份作为此图上 x 轴的标签,而不是数据点数/天数。我想要基于日期时间索引的2007、2008、2009 等(因为这会根据我的输入数据而有所不同)。
我已经为此查看了每个帮助站点,但似乎没有任何效果,我可能遗漏了一些非常明显的内容,对此我深表歉意,但我无法弄清楚。
编辑
用于说明错误的新代码:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
dates = pd.date_range('20070101',periods=1000)
df = pd.DataFrame(np.random.randn(1000), columns =list ('A'))
df['date'] = dates
def get_season(row):
if row['date'].month >= 3 and row['date'].month <= 5:
return 'spring'
elif row['date'].month >= 6 …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) 我编写了一个混乱的函数,它根据数据帧的长度计算数据帧中的年数(假设数据帧具有一年中每一天的值)。
它工作得很好,但是有很多代码可以变得更智能(但我不知道如何......)
这是函数,它只持续 10 年,我希望它适用于任何大小的数据集。我可以通过复制和粘贴并进一步添加总数来进一步扩展它,但必须有一种更智能的方法来编写此代码。
def numyears(x):
if len(x.index) <= 366:
return 1
elif len(x.index) <= 732:
return 2
elif len(x.index) <= 1098:
return 3
elif len(x.index) <= 1464:
return 4
elif len(x.index) <= 1830:
return 5
elif len(x.index) <= 2196:
return 6
elif len(x.index) <= 2562:
return 7
elif len(x.index) <= 2928:
return 8
elif len(x.index) <= 3294:
return 9
elif len(x.index) <= 3660:
return 10
else:
return 'ERROR'
Run Code Online (Sandbox Code Playgroud)