问题
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
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
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 ?
回答1:
I am assuming that longitude, latitude and time are vectors with nx, ny and ntime elements.
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
# ==============================================
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);
来源:https://stackoverflow.com/questions/55626985/how-to-match-netcdf-variables-values-in-an-array-in-matlab