将变量存储在嵌套的循环python的新维度上
我是Python的新手,试图从Matlab转移到
我正在使用for Loop读取多个.NC文件,并试图沿着新的记录维度保存它们。下面的代码部分。 J
和j1
的大小为4x30x30,我试图将j
的数据存储在变量appn
中3 e
沿0 dim,因此appn
是一个变量,大小为3x4x30x30。
在Matlab中很简单,但在Python中无法找到一种方法
import os, sys
import netCDF4
from mpl_toolkits.basemap import Basemap
import numpy as np
import matplotlib.pyplot as plt
from cdo import *
import xarray as xr
cdo=Cdo()
indices = ["T"]
models = ["MJ", "MK", "ML"]
seasons =["JJA", "DJF"]
period =["base", "proj"]
exp = ["ssp1", "ssp2", "ssp3"]
odir = "/outs_pytry/season/"
top ="/outs_pytry/monmean_1/"
os.makedirs(odir, exist_ok=True)
#appn=[]
appnx=[]
#pr_arr = np.zeros([models,nlat,nlon], dtype='f4')
#pr_arr = np.zeros([], dtype='f4')
j=[]
for m in models:
folder = "%s"%(top)
if m in ["ML"]:
run = "r1i1p1f2"
else:
run = "r1i1p1f1"
for i in indices:
for e in exp:
origfi1 = '%s%s_%s_%s_%s_base.nc'%(folder,i, m, e, run)
origfi2 = '%s%s_%s_%s_%s_proj.nc'%(folder,i, m, e, run)
k=cdo.timselmean(3,11,9, input="%s"%origfi1, output="%s%s_%s_%s_%s_DJF_proj.nc"%(odir,i,m,e,run), returnCdf=True)
k1=cdo.timselmean(3,5,9, input="%s"%origfi1, output="%s%s_%s_%s_%s_JJA_proj.nc"%(odir,i,m,e,run), returnCdf=True)
j=k.variables["T"][:]
j1=k1.variables["T"][:]
lat=k.variables["lat"]
lon=k.variables["lon"]
#appn=np.zeros([3,4,lon,lat], dtype='f4')
datain = np.array(j)
#Confused with how to store data in appn , so that it has a fourth dimension of size 'e'?
appn(e,:,:,:) =datain
appn.append(datain)
I am new to Python- trying to move from Matlab
I am reading multiple .nc files using for loop and am trying to save them along a new record dimension.A section of the code below. j
and j1
has a size of 4x30x30 and I am trying to store data of j
in variable appn
for the 3 e
along the 0th dim, such that appn
is a variable with size 3x4x30x30.
It is simple in Matlab, but could not figure out a way in Python
import os, sys
import netCDF4
from mpl_toolkits.basemap import Basemap
import numpy as np
import matplotlib.pyplot as plt
from cdo import *
import xarray as xr
cdo=Cdo()
indices = ["T"]
models = ["MJ", "MK", "ML"]
seasons =["JJA", "DJF"]
period =["base", "proj"]
exp = ["ssp1", "ssp2", "ssp3"]
odir = "/outs_pytry/season/"
top ="/outs_pytry/monmean_1/"
os.makedirs(odir, exist_ok=True)
#appn=[]
appnx=[]
#pr_arr = np.zeros([models,nlat,nlon], dtype='f4')
#pr_arr = np.zeros([], dtype='f4')
j=[]
for m in models:
folder = "%s"%(top)
if m in ["ML"]:
run = "r1i1p1f2"
else:
run = "r1i1p1f1"
for i in indices:
for e in exp:
origfi1 = '%s%s_%s_%s_%s_base.nc'%(folder,i, m, e, run)
origfi2 = '%s%s_%s_%s_%s_proj.nc'%(folder,i, m, e, run)
k=cdo.timselmean(3,11,9, input="%s"%origfi1, output="%s%s_%s_%s_%s_DJF_proj.nc"%(odir,i,m,e,run), returnCdf=True)
k1=cdo.timselmean(3,5,9, input="%s"%origfi1, output="%s%s_%s_%s_%s_JJA_proj.nc"%(odir,i,m,e,run), returnCdf=True)
j=k.variables["T"][:]
j1=k1.variables["T"][:]
lat=k.variables["lat"]
lon=k.variables["lon"]
#appn=np.zeros([3,4,lon,lat], dtype='f4')
datain = np.array(j)
#Confused with how to store data in appn , so that it has a fourth dimension of size 'e'?
appn(e,:,:,:) =datain
appn.append(datain)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在Python中,您可以将其附加到列表词典或列表中。如果您想将所有词典附加到appn appn appn appn,则如果限制了变量,则可以这样做:{“ e”:e,** datain}。
如果您只想附加一个要使用的值的数组:[e,*datain.values()]来创建列表,然后将其附加。我在下面举了一个例子
in python you can append to a list dictionaries or lists. if you want to append to appn a dictionary with all if detains variables, plus "e" you can do it like this: {"e":e,**datain}.
if you only want to append an array of the values to appn use: [e,*datain.values()] to create a list and then append it. I put an example below