使用matlab将Netcdf变量导出到文本

时间:2014-05-21 19:10:44

标签: matlab netcdf

我是netcdf文件的新手,并且一直在尝试使用MATLAB将netcdf .nc格式中的一个变量导出到文本文件(ascii等)。

我可以使用nc_dump阅读netcdf文件,并使用nc_varget获取变量。
但是当我尝试保存变量(它是4D double)时,它不会保存4D文件,而是只保存该特定变量中的值。

我的代码:

nc_dump('filename.nc')
var1 = nc_varget('filename.nc','var1');
var2 = nc_varget('filename.nc','var2');
var3 = nc_varget('filename.nc','var3');
var4 = nc_varget('filename.nc','var4');
var5 = nc_varget('filename.nc','var5');
for i = 1:length(var1)
for j = 1:length(var2)
for k = 1:length(var3)
for l = 1:length(var4)
for m = 1:length(var5)
data(i,j,k,l,m) = [var1(i), var2(j), var3(k), var4(l), var5(i,j,k,l)];
end
end
end
end
end
dlmwrite(‘data.txt’, data)

nc_dump的输出:

nc_dump('geoschem_nestedNA_10_20041010.nc')

NetCDF-3 Classic geoschem_nestedNA_10_20041010.nc {

dimensions:
lon = 151 ;
lat = 121 ;
lev = 37 ;
time = UNLIMITED ; (7 currently)


variables:

    // Preference 'PRESERVE_FVD':  false,
// dimensions consistent with ncBrowse, not with native MATLAB netcdf package.
single lon(lon), shape = [151]
    lon:long_name = "Longitude" ;
    lon:units = "degrees_east" ;
single lat(lat), shape = [121]
    lat:long_name = "Latitude" ;
    lat:units = "degrees_north" ;
single lev(lev), shape = [37]
    lev:long_name = "Eta Centers" ;
    lev:units = "sigma_level" ;
single time(time), shape = [7]
    time:long_name = "Time" ;
    time:units = "hours since 1985-1-1 00:00:0.0" ;
    time:delta_t = "0000-00-00 03:00:00" ;
single IJ_AVG_S__NOx(time,lev,lat,lon), shape = [7 37 121 151]
    IJ_AVG_S__NOx:long_name = "NOx tracer" ;
    IJ_AVG_S__NOx:units = "ppbv" ;
single IJ_AVG_S__ACET(time,lev,lat,lon), shape = [7 37 121 151]
    IJ_AVG_S__ACET:long_name = "ACET tracer" ;
    IJ_AVG_S__ACET:units = "ppbC" ;
single IJ_AVG_S__SO2(time,lev,lat,lon), shape = [7 37 121 151]
    IJ_AVG_S__SO2:long_name = "SO2 tracer" ;
    IJ_AVG_S__SO2:units = "ppbv" ;
single IJ_AVG_S__SO4(time,lev,lat,lon), shape = [7 37 121 151]
    IJ_AVG_S__SO4:long_name = "SO4 tracer" ;
    IJ_AVG_S__SO4:units = "ppbv" ;
single IJ_AVG_S__NH4(time,lev,lat,lon), shape = [7 37 121 151]
    IJ_AVG_S__NH4:long_name = "NH4 tracer" ;
    IJ_AVG_S__NH4:units = "ppbv" ;
single IJ_AVG_S__NIT(time,lev,lat,lon), shape = [7 37 121 151]
    IJ_AVG_S__NIT:long_name = "NIT tracer" ;
    IJ_AVG_S__NIT:units = "ppbv" ;
single IJ_AVG_S__BCPI(time,lev,lat,lon), shape = [7 37 121 151]
    IJ_AVG_S__BCPI:long_name = "BCPI tracer" ;
    IJ_AVG_S__BCPI:units = "ppbv" ;
single IJ_AVG_S__OCPI(time,lev,lat,lon), shape = [7 37 121 151]
    IJ_AVG_S__OCPI:long_name = "OCPI tracer" ;
    IJ_AVG_S__OCPI:units = "ppbv" ;
single IJ_AVG_S__BCPO(time,lev,lat,lon), shape = [7 37 121 151]
    IJ_AVG_S__BCPO:long_name = "BCPO tracer" ;
    IJ_AVG_S__BCPO:units = "ppbv" ;
single IJ_AVG_S__OCPO(time,lev,lat,lon), shape = [7 37 121 151]
    IJ_AVG_S__OCPO:long_name = "OCPO tracer" ;
    IJ_AVG_S__OCPO:units = "ppbv" ;
single IJ_AVG_S__ALPH(time,lev,lat,lon), shape = [7 37 121 151]
    IJ_AVG_S__ALPH:long_name = "ALPH tracer" ;
    IJ_AVG_S__ALPH:units = "ppbv" ;
single IJ_AVG_S__LIMO(time,lev,lat,lon), shape = [7 37 121 151]
    IJ_AVG_S__LIMO:long_name = "LIMO tracer" ;
    IJ_AVG_S__LIMO:units = "ppbv" ;
single IJ_AVG_S__ALCO(time,lev,lat,lon), shape = [7 37 121 151]
    IJ_AVG_S__ALCO:long_name = "ALCO tracer" ;
    IJ_AVG_S__ALCO:units = "ppbv" ;
single IJ_AVG_S__SOG1(time,lev,lat,lon), shape = [7 37 121 151]
    IJ_AVG_S__SOG1:long_name = "SOG1 tracer" ;
    IJ_AVG_S__SOG1:units = "ppbv" ;
single IJ_AVG_S__SOG2(time,lev,lat,lon), shape = [7 37 121 151]
    IJ_AVG_S__SOG2:long_name = "SOG2 tracer" ;
    IJ_AVG_S__SOG2:units = "ppbv" ;
single IJ_AVG_S__SOG3(time,lev,lat,lon), shape = [7 37 121 151]
    IJ_AVG_S__SOG3:long_name = "SOG3 tracer" ;
    IJ_AVG_S__SOG3:units = "ppbv" ;
single IJ_AVG_S__SOG4(time,lev,lat,lon), shape = [7 37 121 151]
    IJ_AVG_S__SOG4:long_name = "SOG4 tracer" ;
    IJ_AVG_S__SOG4:units = "ppbv" ;
single IJ_AVG_S__SOG5(time,lev,lat,lon), shape = [7 37 121 151]
    IJ_AVG_S__SOG5:long_name = "SOG5 tracer" ;
    IJ_AVG_S__SOG5:units = "ppbv" ;
single IJ_AVG_S__SOA1(time,lev,lat,lon), shape = [7 37 121 151]
    IJ_AVG_S__SOA1:long_name = "SOA1 tracer" ;
    IJ_AVG_S__SOA1:units = "ppbv" ;
single IJ_AVG_S__SOA2(time,lev,lat,lon), shape = [7 37 121 151]
    IJ_AVG_S__SOA2:long_name = "SOA2 tracer" ;
    IJ_AVG_S__SOA2:units = "ppbv" ;
single IJ_AVG_S__SOA3(time,lev,lat,lon), shape = [7 37 121 151]
    IJ_AVG_S__SOA3:long_name = "SOA3 tracer" ;
    IJ_AVG_S__SOA3:units = "ppbv" ;
single IJ_AVG_S__SOA4(time,lev,lat,lon), shape = [7 37 121 151]
    IJ_AVG_S__SOA4:long_name = "SOA4 tracer" ;
    IJ_AVG_S__SOA4:units = "ppbv" ;
single IJ_AVG_S__SOA5(time,lev,lat,lon), shape = [7 37 121 151]
    IJ_AVG_S__SOA5:long_name = "SOA5 tracer" ;
    IJ_AVG_S__SOA5:units = "ppbv" ;
single IJ_AVG_S__DST1(time,lev,lat,lon), shape = [7 37 121 151]
    IJ_AVG_S__DST1:long_name = "DST1 tracer" ;
    IJ_AVG_S__DST1:units = "ppbv" ;
single IJ_AVG_S__DST2(time,lev,lat,lon), shape = [7 37 121 151]
    IJ_AVG_S__DST2:long_name = "DST2 tracer" ;
    IJ_AVG_S__DST2:units = "ppbv" ;
single IJ_AVG_S__DST3(time,lev,lat,lon), shape = [7 37 121 151]
    IJ_AVG_S__DST3:long_name = "DST3 tracer" ;
    IJ_AVG_S__DST3:units = "ppbv" ;
single IJ_AVG_S__DST4(time,lev,lat,lon), shape = [7 37 121 151]
    IJ_AVG_S__DST4:long_name = "DST4 tracer" ;
    IJ_AVG_S__DST4:units = "ppbv" ;
single IJ_AVG_S__SALA(time,lev,lat,lon), shape = [7 37 121 151]
    IJ_AVG_S__SALA:long_name = "SALA tracer" ;
    IJ_AVG_S__SALA:units = "ppbv" ;
single IJ_AVG_S__SALC(time,lev,lat,lon), shape = [7 37 121 151]
    IJ_AVG_S__SALC:long_name = "SALC tracer" ;
    IJ_AVG_S__SALC:units = "ppbv" ;
single DAO_FLDS__PARDF(time,lev,lat,lon), shape = [7 37 121 151]
    DAO_FLDS__PARDF:long_name = "GMAO PARDF field" ;
    DAO_FLDS__PARDF:units = "W/m2" ;
single DAO_FLDS__PARDR(time,lev,lat,lon), shape = [7 37 121 151]
    DAO_FLDS__PARDR:long_name = "GMAO PARDR field" ;
    DAO_FLDS__PARDR:units = "W/m2" ;
single DAO_FLDS__TS(time,lev,lat,lon), shape = [7 37 121 151]
    DAO_FLDS__TS:long_name = "GMAO TS field" ;
    DAO_FLDS__TS:units = "K" ;
single TIME_SER__D_LAI(time,lev,lat,lon), shape = [7 37 121 151]
    TIME_SER__D_LAI:long_name = "Daily LAI" ;
    TIME_SER__D_LAI:units = "m2/m2" ;
single BIOGSRCE__ISOP(time,lev,lat,lon), shape = [7 37 121 151]
    BIOGSRCE__ISOP:long_name = "ISOP emissions" ;
    BIOGSRCE__ISOP:units = "atomC/cm2/s" ;
single BIOGSRCE__MONOT(time,lev,lat,lon), shape = [7 37 121 151]
    BIOGSRCE__MONOT:long_name = "MONOT emissions" ;
    BIOGSRCE__MONOT:units = "atomC/cm2/s" ;
single BIOGSRCE__MBO(time,lev,lat,lon), shape = [7 37 121 151]
    BIOGSRCE__MBO:long_name = "MBO emissions" ;
    BIOGSRCE__MBO:units = "atomC/cm2/s" ;
single BIOGSRCE__APINE(time,lev,lat,lon), shape = [7 37 121 151]
    BIOGSRCE__APINE:long_name = "APINE emissions" ;
    BIOGSRCE__APINE:units = "atomC/cm2/s" ;
single BIOGSRCE__BPINE(time,lev,lat,lon), shape = [7 37 121 151]
    BIOGSRCE__BPINE:long_name = "BPINE emissions" ;
    BIOGSRCE__BPINE:units = "atomC/cm2/s" ;
single BIOGSRCE__LIMON(time,lev,lat,lon), shape = [7 37 121 151]
    BIOGSRCE__LIMON:long_name = "LIMON emissions" ;
    BIOGSRCE__LIMON:units = "atomC/cm2/s" ;
single BIOGSRCE__SABIN(time,lev,lat,lon), shape = [7 37 121 151]
    BIOGSRCE__SABIN:long_name = "SABIN emissions" ;
    BIOGSRCE__SABIN:units = "atomC/cm2/s" ;
single BIOGSRCE__MYRCN(time,lev,lat,lon), shape = [7 37 121 151]
    BIOGSRCE__MYRCN:long_name = "MYRCN emissions" ;
    BIOGSRCE__MYRCN:units = "atomC/cm2/s" ;
single BIOGSRCE__CAREN(time,lev,lat,lon), shape = [7 37 121 151]
    BIOGSRCE__CAREN:long_name = "CAREN emissions" ;
    BIOGSRCE__CAREN:units = "atomC/cm2/s" ;
single BIOGSRCE__OCIMN(time,lev,lat,lon), shape = [7 37 121 151]
    BIOGSRCE__OCIMN:long_name = "OCIMN emissions" ;
    BIOGSRCE__OCIMN:units = "atomC/cm2/s" ;


//global attributes:
    :Title = "COARDS/netCDF file created by BPCH2COARDS (GAMAP v2-03+)" ;
    :Model = "GEOS5_47L" ;
    :Delta_Lon = 0.666667 f;
    :Delta_Lat = 0.500000 f;
    :NLayers = 47 d;
    :Start_Date = 20041010 d;
    :Start_Time = 0 d;
    :End_Date = 20041010 d;
    :End_Time = 21 d;
    :Delta_Time = 3 d;
    :Conventions = "COARDS" ;

可以在以下链接找到数据文件: https://www.dropbox.com/s/8fnpw71yc60fm6z/geoschem_nestedNA_10_20041010.nc

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

我迟到了,但我认为这段代码更新应该有效。 而不是使用

数据(i,j,k,l,m)= [var1(i),var2(j),var3(k),var4(l),var5(i,j,k,l)]; < / p>

创建了一个多维矩阵,我想创建一个填充了每个变量的表。

这可能不是最有效的代码(我是非常新的,昨天第一次打开了Matlab新版本),但我认为这段代码有效。

创建数据集,然后覆盖数据集,以便不必连续重新分配内存

for n = 2008:2010
name = sprintf('tmax_daily_%d-%d.nc', n, n );
name2 = sprintf('tmax_daily_%d.txt', n);
nc= netcdf.open(name)
data=zeros(2000000000,5);
long3=netcdf.getVar(nc,0);
long=mod((long3+180),360)-180;
lat=netcdf.getVar(nc,1);
height=netcdf.getVar(nc,2);
time=netcdf.getVar(nc,3);
tmax=netcdf.getVar(nc,4);
 row=1;
for i=1:length(long)
for j=1:length(lat)
for k=1:length(height)
for l=1:length(time)
for m=1:length(tmax)
data(row,1)=[long(i)];
data(row,2)=[lat(j)];
data(row,3)=[height(k)];
data(row,4)=[time(l)];
data(row,5)=[tmax(i,j,k,l)];
row=row+1;
end
end
end
end
end

csvwrite(name2,data)
fprintf('%d done\n',n);
clear
end