问题描述
我是MATLAB和netCDF文件的新手,我认为自己陷入了一个简单的问题.我有一个带有5个变量的netCDF文件:纬度,经度,时间,风,mp2
I'm new to MATLAB and netCDF files and I think that I got stuck in an easy issue.I have a netCDF file with 5 variables: latitude, longitude, time, wind, mp2
我要提取具有以下格式的txt文件中的数据:
I want to extract data in a txt file with the following format:
latitude longitude time wind mp2
value value value value value
value value value value value
'''
value value value value value
直到现在,我已经使用ncread MATLAB命令存储了netCDF变量,如下所示:
Until now I have stored the netCDF variables using ncread MATLAB command like the following:
wind = ncread(filename, 'wind')
long = ncread(filename, 'long')
...
但是如何提取具有上述格式的数组/文件?
But how can I extract an array / file with the above format ?
推荐答案
我假设经度,纬度和时间是具有nx,ny和ntime元素的向量.
I am assuming that longitude, latitude and time are vectors with nx, ny and ntime elements.
这是使用Python的一个参数(例如wind)的初始解决方案.添加另一个参数也应该相对容易.脚本的第一部分只是生成一些示例数据:
Here is an initial solution for one parameter (for example wind) with Python. It should be relatively easy to add another parameter as well. The first part of the script is just to generate some example data:
#!/usr/bin/env ipython
import numpy as np
from netCDF4 import Dataset,num2date
# ---------------------
# let us generate random file:
fileout='test.nc'
nx,ny,ntime=5,10,15;
# ----------------------
timeout=np.linspace(0,ntime,ntime);
lon=np.linspace(9.0,30,nx);
lat=np.linspace(54.0,66.,ny);
wind=0.0+20.0*np.random.random((ntime,ny,nx));
mp2=np.random.random((ntime,ny,nx));
# ----------------------
ncout=Dataset(fileout,'w','NETCDF3');
ncout.createDimension('time',None);
ncout.createDimension('lon',nx);
ncout.createDimension('lat',ny);
lonvar=ncout.createVariable('lon','float32',('lon'));lonvar[:]=lon;
latvar=ncout.createVariable('lat','float32',('lat'));latvar[:]=lat;
timevar=ncout.createVariable('time','float64',('time'));timevar[:]=timeout;
wvar=ncout.createVariable('wind','float32',('time','lat','lon'));wvar[:]=wind
ncout.close()
# =============================================
def ncread(filename,varname):
ncin=Dataset(filename);
vardata = ncin.variables[varname][:];
ncin.close()
return vardata
# ---------------------------------------------
# Convert to text:
lonin=ncread(fileout,'lon'); # read longitude
latin=ncread(fileout,'lat'); # read latitude
timein=ncread(fileout,'time'); # read time
win=ncread(fileout,'wind'); # read wind
# ---------------------------------------------
latm,lonm,timem = np.meshgrid(latin,lonin,timein); # generate matrices of longitude,latitude and time with same dimensions as win
# ---------------------------------------------
dataout=np.concatenate((lonm.flatten()[:,np.newaxis],latm.flatten()[:,np.newaxis],timem.flatten()[:,np.newaxis],win.flatten()[:,np.newaxis]),axis=1) # make one matrice from all the data
np.savetxt('test.txt',dataout); # save data
# ==============================================
这是转换零件的MatLab解决方案:
and here is a MatLab solution for the converting part:
filename='test.nc'
timein = ncread(filename,'time');
lonin = ncread(filename,'lon');
latin = ncread(filename,'lat');
win = ncread(filename,'wind');
% -----------------------------
[latm,lonm,timem] = meshgrid(latin,lonin,timein);
dataout = [reshape(lonm,numel(win),1),reshape(latm,numel(win),1),reshape(timem,numel(win),1),reshape(win,numel(win),1)];
dlmwrite('test_matlab.txt',dataout);
这篇关于如何在MATLAB中的数组中匹配netCDF变量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!