如何将二维数据转换为三维,其中三维作为时间具有单个值?

时间:2019-01-22 14:25:50

标签: python-3.x netcdf4

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

答案 0 :(得分:0)

实际上,您的时间维度没有长度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)