添加时间维度和相应netcdf文件的新变量——地球科学堆栈交换江南电子竞技平台江南体育网页版 最近30从www.hoelymoley.com 2023 - 07 - 08 - t02:18:03z //www.hoelymoley.com/feeds/question/12512 https://creativecommons.org/licenses/by-sa/4.0/rdf //www.hoelymoley.com/q/12512 3 将时间维度和相应的新变量添加到netcdf文件 感觉 //www.hoelymoley.com/users/11304 2017 - 10 - 20 - t13:48:59z 2017 - 11 - 22 - t16:33:12z < p >我已经创建了一个x, y netcdf文件,现在想添加额外的时间能量变量。这为每个网格单元能量时间价值。< / p > < p >我的脚本在Python中是这样的:< / p > < pre > <代码> value = loadtxt (“countries.txt skiprows = 0) % = loadtxt国家数据(“energy.txt”, skiprows = 0) %能源< /代码> < / pre > < p > <代码>能量。txt > < /代码文件:< / p > < pre > <代码>[国家id]…51 52 54 55 56 59 60 62…[时间步1]…649年南南南1083 9979南13602…[时间步2]…5743 43444 6365 1283 9062 4026南南…(时间步n)…………………………< /代码> < / pre > < p >等(直到87648步),感兴趣的细胞在哪里第一行:< / p > < p > <代码> cellsOfInt =数据(0:)< /代码> < / p > < ul > <李>我有一些细胞nan值以来没有值的时间步骤。
  • Each row correspond to one time step so for cell of interest 51, in the first time step we have nan value, in the second time step we have 5743 and so on till 87648.
  • All cells of interest are in countries.txt file that is 950 X 1000 with cells of interest ids or nan when there is no value.
  • My code:

    y = nc.createDimension("y", 950) #y x = nc.createDimension("x", 1000) #x time = nc.createDimension("time", 87648) #time latitude=nc.createVariable("y", "f8", ("y", )) longitude=nc.createVariable("x", "f8", ("x", )) time = nc.createVariable("time","f8",("time",)) countries = nc.createVariable("countries", "f8", ("y", "x")); energy = nc.createVariable("energy", "f8", ("time", "y", "x"), fill_value=-9999, chunksizes=(1, 950, 1000)); time.standard_name='time' time.units ='hours since 2006-01-01 00:00:00.0' time.calendar='proleptic_gregorian' lats = np.arange(5497500,747500,-5000) #y lons = np.arange(2502500,7502500,5000) #x # Fill variables latitude[:] = lats longitude[:] = lons countries [:]=value 

    UNTIL HERE WORKS FINE I have my 2d country but then it does not give any result for energy:

    for i in xrange(0,950): #rows for j in xrange(0,1000): #columns for n in xrange(0,87648): #rows for m in xrange(0,1): #columns if data[1,m] == countries[i,j]: #correspondance nc.variables["energy"][n]=data[n,m] 

    Any idea how to fix this so I can have in country.netcdf file time dimension with energy values at each time step? Thanks a lot!!

    //www.hoelymoley.com/questions/12512/-/12526 # 12526 1 由丹尼尔回答。heydebreck添加时间维度和相应的新变量netcdf文件 daniel.heydebreck //www.hoelymoley.com/users/5594 2017 - 10 - 23 - t16:07:38z 2017 - 10 - 23 - t16:07:38z < p >有两个问题(和一个优化)/上面的代码:< / p > < ol > <李>数组索引开始<代码> 0 < /代码>(<代码> 1 > < /代码)在Python中。因此,它应该是<代码>如果数据[0,m] = =国家(i, j): < /代码>因为你感兴趣的第一行,李< / > <李> <代码> m > < /代码从<代码> 0 < /代码> 1 (m的<代码> xrange(0,1): > < /代码),但应该从<代码> 0 <代码> / <代码> number_of_countries > < /代码,以便所有列的能量迭代。李李< / > < >的<代码> n < /代码>和<代码> m > < /代码循环似乎是完全无效的。更好的是:<代码> m…:如果数据[1 m] = =…:n…:nc.variables…= ....> < /代码或更换后一个for循环<代码>数控。变量(“能源”)[:,i, j] =数据(:,m) < /代码> < /李> < / ol > < p >这可能工作(请改善= >社区Wiki)但我不能测试它丢失的输入数据:< / p > < pre > <代码> y =数控。createDimension (" y ", 950) # y x =数控。createDimension (“x”, 1000) # x时间=数控。createDimension(“时间”,87648)#时间纬度=数控。createVariable (“y”、“f8”(“y”))经度=数控。createVariable (“x”、“f8”(“x”))时间= nc.createVariable(“时间”、“f8”(“时间”))国家=数控。createVariable(“国家”、“f8”(" y "、" x ")); energy = nc.createVariable("energy", "f8", ("time", "y", "x"), fill_value=-9999, chunksizes=(1, 950, 1000)); time.standard_name='time' time.units ='hours since 2006-01-01 00:00:00.0' time.calendar='proleptic_gregorian' lats = np.arange(5497500,747500,-5000) #y lons = np.arange(2502500,7502500,5000) #x # Fill variables latitude[:] = lats longitude[:] = lons countries [:]=value for i in xrange(0,950): # grid rows for j in xrange(0,1000): # grid columns for m in xrange(0,int(max(countries))): # countries if data[0,m] == countries[i,j]: #correspondance nc.variables["energy"][:,i,j]=data[1:87648,m]
    Baidu
    map