【发布时间】: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