【问题标题】:How to convert a two dimensional data into three dimensional with third dimension as time with single value?如何将二维数据转换为具有第三维的三维作为具有单值的时间?
【发布时间】:2019-05-17 15:14:11
【问题描述】:

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

【问题讨论】:

    标签: python-3.x netcdf4


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-30
      • 2010-11-26
      • 1970-01-01
      • 2019-03-14
      相关资源
      最近更新 更多