如何将二维数据转换为三维数据,三维数据为单值时间?

How to convert a two dimensional data into three dimensional with third dimension as time with single value?

我有来自 quickscat 的每日风力数据 ftp://ftp.ifremer.fr/ifremer/cersat/products/gridded/mwf-quikscat/data/daily 问题是纬向风和经向风是二维的,即它们仅包含 (lon, lat) 作为维度而不包含 (time, lon,lat) 作为维度。文件包含有关时间作为变量和维度的所有信息。我尝试将所有维度和变量数据从输入文件复制到输出文件,但出了点问题。它成功地复制了纬度、经度和时间,但没有复制风的值。在源文件中风是二维的,但我希望输出文件中的风是三维的,时间是第三个 dimension.Anyway 时间维度的长度=1

import netCDF4 as nc
import numpy as np
import os
in_path = '2000'
out_path = '2000_new'

files = os.listdir(in_path)
fd=0
for names in files:
   # print(names)
    x_file = os.path.join(in_path,names)
    y_file = os.path.join(out_path,names)
    fd +=1
    i_file = nc.Dataset(x_file, 'r')
    z_w = i_file.variables['zonal_wind_speed'][:,:]
    m_w = i_file.variables['meridional_wind_speed'][:,:]
    y = i_file.variables['latitude'][:]
    x = i_file.variables['longitude'][:]
    t = i_file.variables['time'][:]


    os.system("'rm y_file")
    o_file = nc.Dataset(y_file, 'w', format='NETCDF4')
    latitude = o_file.createDimension('latitude', y.size)
    longitude = o_file.createDimension('longitude', x.size)
    time = o_file.createDimension('time',None)

    var = o_file.createVariable('latitude','f4',('latitude'), zlib=True)
    o_file.variables['latitude'].units = 'degree_north'
    o_file.variables['latitude'].long_name ='latitude'
    o_file.variables['latitude'].axis = 'X'

    var = o_file.createVariable('longitude','f4',('longitude'), zlib=True)
    o_file.variables['longitude'].units = 'degree_east'
    o_file.variables['longitude'].long_name = 'longitude'
    o_file.variables['longitude'].axis = 'Y'

    var = o_file.createVariable('time','d',('time'), zlib=True)
    o_file.variables['time'].long_name = 'time'
    o_file.variables['time'].units = "hours since 1900-1-1 0:0:0"
    o_file.variables['time'].calendar = 'standard'
    o_file.variables['time'].axis = 'T'

    var = o_file.createVariable('u','f4',('time','latitude','longitude'),fill_value=-1.e+23, zlib=True)
    o_file.variables['u'].long_name='zonal wind speed component'
    o_file.variables['u'].units = 'meter second-1'
    o_file.variables['u'].coordinates = 'longitude latitude'
    o_file.variables['u'].time = 'time'


    var = o_file.createVariable('v','f4',('time','latitude','longitude'),fill_value=-1.e+23, zlib = True)
    o_file.variables['v'].long_name = 'meridional wind speed component'
    o_file.variables['v'].units = 'meter second-1'
    o_file.variables['v'].coordinates = 'longitude latitude'
    o_file.variables['v'].time = 'time'


    o_file.variables['latitude'][:] = y
    o_file.variables['longitude'][:] =x
    o_file.variables['time'][:] = t
    o_file.variables['u'] = z_w
    o_file.variables['v'] = m_w





    i_file.close()
    o_file.close()

其实你的时间维度的长度不是1,它是无限长的。如果你真的想让它的长度为1,你需要使用

#time = o_file.createDimension('time',None)
time = o_file.createDimension('time',1)

相反。 然后,要将第一个(也是唯一一个)时间索引的所有数据设置为您的值,请使用

o_file.variables['u'][0] = z_w
o_file.variables['v'][0] = m_w

如果您最终在文件中多次保存,请将 0 替换为您正在复制的数据的适当索引。

或者,因为时间维度现在的长度为 1,您也可以使用 numpy.expand_dims

复制它
o_file.variables['u'][:] = np.expand_dims(z_w, 0)
o_file.variables['v'][:] = np.expand_dims(m_w, 0)