在NetCDF文件中写入无限时间维度时,Matlab'NC_UNLIMITED在错误的索引中'

时间:2013-06-05 11:30:25

标签: matlab netcdf

我正在尝试编写一个具有无限时间维度的NetCDF文件,并且正在努力获得成功。我真的很感激一些帮助:)

这里出现错误信息:

  
     

使用netcdflib时出错   NetCDF库在执行' defVar'期间遇到错误。功能 -   ' NC_UNLIMITED在错误的索引(NC_EUNLIMPOS)'。

     

netcdf.defVar中的错误(第38行)

     
    

varid = netcdflib(' defVar',ncid,varname,xtype,dimids);

  
     

create_nc_for_HRLDAS_05_06_2013_unix_wong_100x120(第193行)出错

     
    

varid_Q2D = netcdf.defVar(ncid,' Q2D',' float',[Time west_east south_north]);

  
     

我附上了相关的脚本部分:

%%% WRITE NETCDF FILE FOR EVERY TIMESTEP
%
disp('Actually creating NetCDF files...')

for k = 1:length(daysfull);
%for k = 1:3

    fname_date_ex = cell2str(filename_datestring{k});
    fname_date = fname_date_ex(3:14);
    gatt_date_ex = cell2str(globalattribute_datestring{k});
    gatt_date = gatt_date_ex(2:20);

    %%%%%%%%%%%%%% Create new netcdf file ///
    %
    eval(['fileenc = ''' fname_date '.nc'';']);
    ncid = netcdf.create(fileenc,'CLOBBER');
    eval(['full_fname = ''' fileenc ''';']);

    %%%%%%%%%%%%%% Define dimensions ///
    %
    Time = netcdf.defDim(ncid,'Time',netcdf.getConstant('NC_UNLIMITED'));
    west_east = netcdf.defDim(ncid,'west_east',length(lon));
    south_north = netcdf.defDim(ncid,'south_north',length(lat));

    %%%%%%%%%%%%%% Define variables ///
    %
    %
%     varid_time = netcdf.defVar(ncid,'time','double',Time);
%     netcdf.putAtt(ncid,varid_time,'long_name','Time');
%     %
%     varid_lat = netcdf.defVar(ncid,'south_north','double',south_north);
%     netcdf.putAtt(ncid,varid_lat,'FieldType',104);
%     netcdf.putAtt(ncid,varid_lat,'MemoryOrder','XY');
%     netcdf.putAtt(ncid,varid_lat,'description','LATITUDE, SOUTH IS NEGATIVE');
%     netcdf.putAtt(ncid,varid_lat,'units','degree_north');
%     netcdf.putAtt(ncid,varid_lat,'stagger','');
%     %
%     varid_lon = netcdf.defVar(ncid,'west_east','double',west_east);
%     netcdf.putAtt(ncid,varid_lon,'FieldType',104);
%     netcdf.putAtt(ncid,varid_lon,'MemoryOrder','XY');
%     netcdf.putAtt(ncid,varid_lon,'description','LONGITUDE, WEST IS NEGATIVE');
%     netcdf.putAtt(ncid,varid_lon,'units','degree_east');
%     netcdf.putAtt(ncid,varid_lon,'stagger','');
    %
    varid_Q2D = netcdf.defVar(ncid,'Q2D','float',[Time west_east south_north]);
    netcdf.putAtt(ncid,varid_Q2D,'FieldType',104);
    netcdf.putAtt(ncid,varid_Q2D,'MemoryOrder','XY');
    netcdf.putAtt(ncid,varid_Q2D,'description','QV at 2 M');
    netcdf.putAtt(ncid,varid_Q2D,'units','kg kg-1');
    netcdf.putAtt(ncid,varid_Q2D,'stagger','');
    netcdf.putAtt(ncid,varid_Q2D,'coordinates','XLONG XLAT');
    netcdf.putAtt(ncid,varid_Q2D,'missing_value',-9999)
    %
    varid_T2D = netcdf.defVar(ncid,'T2D','float',[Time west_east south_north]);
    netcdf.putAtt(ncid,varid_T2D,'FieldType',104);
    netcdf.putAtt(ncid,varid_T2D,'MemoryOrder','XY');
    netcdf.putAtt(ncid,varid_T2D,'description','TEMP at 2 M');
    netcdf.putAtt(ncid,varid_T2D,'units','K');
    netcdf.putAtt(ncid,varid_T2D,'stagger','');
    netcdf.putAtt(ncid,varid_T2D,'coordinates','XLONG XLAT');
    netcdf.putAtt(ncid,varid_T2D,'missing_value',-9999)
    %
    varid_SWDOWN = netcdf.defVar(ncid,'SWDOWN','float',[Time west_east south_north]);
    netcdf.putAtt(ncid,varid_SWDOWN,'FieldType',104);
    netcdf.putAtt(ncid,varid_SWDOWN,'MemoryOrder','XY');
    netcdf.putAtt(ncid,varid_SWDOWN,'description','DOWNWARD SHORT WAVE FLUX AT GROUND SURFACE');
    netcdf.putAtt(ncid,varid_SWDOWN,'units','W m-2');
    netcdf.putAtt(ncid,varid_SWDOWN,'stagger','');
    netcdf.putAtt(ncid,varid_SWDOWN,'coordinates','XLONG XLAT');
    netcdf.putAtt(ncid,varid_SWDOWN,'missing_value',-9999)
    %
    varid_LWDOWN = netcdf.defVar(ncid,'LWDOWN','float',[Time west_east south_north]);
    netcdf.putAtt(ncid,varid_LWDOWN,'FieldType',104);
    netcdf.putAtt(ncid,varid_LWDOWN,'MemoryOrder','XY');
    netcdf.putAtt(ncid,varid_LWDOWN,'description','DOWNWARD LONG WAVE FLUX AT GROUND SURFACE');
    netcdf.putAtt(ncid,varid_LWDOWN,'units','W m-2');
    netcdf.putAtt(ncid,varid_LWDOWN,'stagger','');
    netcdf.putAtt(ncid,varid_LWDOWN,'coordinates','XLONG XLAT');
    netcdf.putAtt(ncid,varid_LWDOWN,'missing_value',-9999)
    %
    varid_U2D = netcdf.defVar(ncid,'U2D','float',[Time west_east south_north]);
    netcdf.putAtt(ncid,varid_U2D,'FieldType',104);
    netcdf.putAtt(ncid,varid_U2D,'MemoryOrder','XY');
    netcdf.putAtt(ncid,varid_U2D,'description','U at 10 M');
    netcdf.putAtt(ncid,varid_U2D,'units','m s-1');
    netcdf.putAtt(ncid,varid_U2D,'stagger','');
    netcdf.putAtt(ncid,varid_U2D,'coordinates','XLONG XLAT');
    netcdf.putAtt(ncid,varid_U2D,'missing_value',-9999)
    %
    varid_V2D = netcdf.defVar(ncid,'V2D','float',[Time west_east south_north]);
    netcdf.putAtt(ncid,varid_V2D,'FieldType',104);
    netcdf.putAtt(ncid,varid_V2D,'MemoryOrder','XY');
    netcdf.putAtt(ncid,varid_V2D,'description','V at 10 M');
    netcdf.putAtt(ncid,varid_V2D,'units','m s-1');
    netcdf.putAtt(ncid,varid_V2D,'stagger','');
    netcdf.putAtt(ncid,varid_V2D,'coordinates','XLONG XLAT');
    netcdf.putAtt(ncid,varid_V2D,'missing_value',-9999)
    %
    varid_PSFC = netcdf.defVar(ncid,'PSFC','float',[Time west_east south_north]);
    netcdf.putAtt(ncid,varid_PSFC,'FieldType',104);
    netcdf.putAtt(ncid,varid_PSFC,'MemoryOrder','XY');
    netcdf.putAtt(ncid,varid_PSFC,'description','SFC PRESSURE');
    netcdf.putAtt(ncid,varid_PSFC,'units','Pa');
    netcdf.putAtt(ncid,varid_PSFC,'stagger','');
    netcdf.putAtt(ncid,varid_PSFC,'coordinates','XLONG XLAT');
    netcdf.putAtt(ncid,varid_PSFC,'missing_value',-9999)
    %
    varid_RAINRATE = netcdf.defVar(ncid,'RAINRATE','double',[Time west_east south_north]);
    netcdf.putAtt(ncid,varid_RAINRATE,'FieldType',104);
    netcdf.putAtt(ncid,varid_RAINRATE,'MemoryOrder','XY');
    netcdf.putAtt(ncid,varid_RAINRATE,'description','TOTAL GRID SCALE PRECIPITATION RATE');
    netcdf.putAtt(ncid,varid_RAINRATE,'units','mm s-1');
    netcdf.putAtt(ncid,varid_RAINRATE,'stagger','');
    netcdf.putAtt(ncid,varid_RAINRATE,'coordinates','XLONG XLAT');
    netcdf.putAtt(ncid,varid_RAINRATE,'missing_value',-9999)
    %
    netcdf.endDef(ncid)


    %%%%%%%%%%%%%% Aggregate data ///
    %
    % store lwdown and swdown frame of interest in array
    lwdown_foi = squeeze(lwdown_bicubic_import_cltoft_permuted(k,:,:));
    swdown_foi = squeeze(swdown_bicubic_import_cltoft_permuted(k,:,:));

    % put variables into netcdf file
%     netcdf.putVar(ncid,varid_lon,0,length(lon),lon(1:length(lon)));
%     netcdf.putVar(ncid,varid_lat,0,length(lat),lat(1:length(lat)));
%     netcdf.putVar(ncid,varid_time,0,1,1);
%     
    netcdf.putVar(ncid,varid_Q2D, [0 0 0], [1 length(lon) length(lat)], squeeze(qa_import_permuted2(k,:,:)));
    netcdf.putVar(ncid,varid_T2D, [0 0 0], [1 length(lon) length(lat)], squeeze(ta_import_permuted2(k,:,:)));
    netcdf.putVar(ncid,varid_SWDOWN, [0 0 0], [1 length(lon) length(lat)], swdown_foi);
    netcdf.putVar(ncid,varid_LWDOWN, [0 0 0], [1 length(lon) length(lat)], lwdown_foi);
    netcdf.putVar(ncid,varid_U2D, [0 0 0], [1 length(lon) length(lat)], squeeze(ucomp_import_permuted2(k,:,:)));
    netcdf.putVar(ncid,varid_V2D, [0 0 0], [1 length(lon) length(lat)], squeeze(vcomp_import_permuted2(k,:,:)));
    netcdf.putVar(ncid,varid_PSFC, [0 0 0], [1 length(lon) length(lat)], squeeze(ps_import_permuted2(k,:,:)));
    netcdf.putVar(ncid,varid_RAINRATE, [0 0 0], [1 length(lon) length(lat)], squeeze(p_import_permuted2(k,:,:)));

    netcdf.close(ncid);

    %%%%%%%%%%%%%% Define global attributes ///

    eval(['copyfile(which(''' fname_date '.nc''),''current.nc'');']);
    fileattrib('current.nc','+w');
    ncwriteatt('current.nc','/','CREATION_DATE',datestr(now));
    ncwriteatt('current.nc','/','TITLE','OBSERVATIONAL FORCING DATA FROM WONG AND WATCH');
    ncwriteatt('current.nc','/','START_DATE', gatt_date);
    ncwriteatt('current.nc','/','SIMULATION_START_DATE', gatt_date);
    ncwriteatt('current.nc','/','WEST_EAST_GRID_DIMENSION',101);
    ncwriteatt('current.nc','/','SOUTH_NORTH_GRID_DIMENSION',121);
    ncwriteatt('current.nc','/','BOTTOM_TOP_GRID_DIMENSION',27);
    ncwriteatt('current.nc','/','DX',5.56e+03);
    ncwriteatt('current.nc','/','DY',5.56e+03);
    ncwriteatt('current.nc','/','GRIDTYPE','C');
    ncwriteatt('current.nc','/','DIFF_OPT',1);
    ncwriteatt('current.nc','/','KM_OPT',4);
    ncwriteatt('current.nc','/','DAMP_OPT',0);
    ncwriteatt('current.nc','/','DAMPCOEF',0.2);
    ncwriteatt('current.nc','/','KHDIF',0);
    ncwriteatt('current.nc','/','KVDIF',0);
    ncwriteatt('current.nc','/','MP_PHYSICS',6);
    ncwriteatt('current.nc','/','RA_LW_PHYSICS',1);
    ncwriteatt('current.nc','/','RA_SW_PHYSICS',1);
    ncwriteatt('current.nc','/','SF_SFCLAY_PHYSICS',1);
    ncwriteatt('current.nc','/','SF_SURFACE_PHYSICS',2);
    ncwriteatt('current.nc','/','BL_PBL_PHYSICS',1);
    ncwriteatt('current.nc','/','CU_PHYSICS',5);
    ncwriteatt('current.nc','/','SURFACE_INPUT_SOURCE',1);
    ncwriteatt('current.nc','/','SST_UPDATE',1);
    ncwriteatt('current.nc','/','GRID_FDDA',0);
    ncwriteatt('current.nc','/','GFDDA_INTERVAL_M',0);
    ncwriteatt('current.nc','/','GFDDA_END_H',0);
    ncwriteatt('current.nc','/','GRID_SFDDA',0);
    ncwriteatt('current.nc','/','SGFDDA_INTERVAL_M',0);
    ncwriteatt('current.nc','/','SGFDDA_END_H',0);
    ncwriteatt('current.nc','/','HYPSOMETRIC_OPT',2);
    ncwriteatt('current.nc','/','WEST_EAST_PATCH_START_UNSTAG',1);
    ncwriteatt('current.nc','/','WEST_EAST_PATCH_END_UNSTAG',100);
    ncwriteatt('current.nc','/','WEST_EAST_PATCH_START_STAG',1);
    ncwriteatt('current.nc','/','WEST_EAST_PATCH_END_STAG',101);
    ncwriteatt('current.nc','/','SOUTH_NORTH_PATCH_START_UNSTAG',1);
    ncwriteatt('current.nc','/','SOUTH_NORTH_PATCH_END_UNSTAG',120);
    ncwriteatt('current.nc','/','SOUTH_NORTH_PATCH_START_STAG',1);
    ncwriteatt('current.nc','/','SOUTH_NORTH_PATCH_END_STAG',121);
    ncwriteatt('current.nc','/','BOTTOM_TOP_PATCH_START_UNSTAG',1);
    ncwriteatt('current.nc','/','BOTTOM_TOP_PATCH_END_UNSTAG',26);
    ncwriteatt('current.nc','/','BOTTOM_TOP_PATCH_START_STAG',1);
    ncwriteatt('current.nc','/','BOTTOM_TOP_PATCH_END_STAG',27);
    ncwriteatt('current.nc','/','GRID_ID',1);
    ncwriteatt('current.nc','/','PARENT_ID',0);
    ncwriteatt('current.nc','/','I_PARENT_START',1);
    ncwriteatt('current.nc','/','J_PARENT_START',1);
    ncwriteatt('current.nc','/','PARENT_GRID_RATIO',1);
    ncwriteatt('current.nc','/','DT',60);
    ncwriteatt('current.nc','/','CEN_LAT',4);
    ncwriteatt('current.nc','/','CEN_LON',102);
    ncwriteatt('current.nc','/','TRUELAT1',4);
    ncwriteatt('current.nc','/','TRUELAT2',0);
    ncwriteatt('current.nc','/','MOAD_CEN_LAT',4);
    ncwriteatt('current.nc','/','STAND_LON',102);
    ncwriteatt('current.nc','/','POLE_LAT',90);
    ncwriteatt('current.nc','/','POLE_LON',0);
    ncwriteatt('current.nc','/','GMT',0);
    ncwriteatt('current.nc','/','JULYR',str2double(gatt_date(1:4)));
    ncwriteatt('current.nc','/','JULDAY',daysfull(k));
    ncwriteatt('current.nc','/','MAP_PROJ',6);
    ncwriteatt('current.nc','/','MMINLU','MODIFIED_IGBP_MODIS_NOAH');
    ncwriteatt('current.nc','/','NUM_LAND_CAT',21);
    ncwriteatt('current.nc','/','ISWATER',17);
    ncwriteatt('current.nc','/','ISLAKE',21);
    ncwriteatt('current.nc','/','ISICE',15);
    ncwriteatt('current.nc','/','ISURBAN',13);
    ncwriteatt('current.nc','/','ISOILWATER',14);
    eval(['movefile(''current.nc'',''../wrf_domain/noah_lsm_forcingfiles_05_06_2013/' fname_date '.LDASOUT_DOMAIN1'');']);

    %%%%%%%%%%%%%% Remove netcdf file in wong folder ///
    eval(['delete(''' fname_date '.nc'');']);
    eval(['disp(''' fname_date '.nc has been processed and stored...'')']);
end

disp('All done!')

错误索引(NC_EUNLIMPOS)' NC_UNLIMITED' NC_UNLIMPOS'意思?

1 个答案:

答案 0 :(得分:2)

无限维Time需要是Matlab中的最后一个维度(Matlab与大多数其他NetCDF实现相反)。因此,只需定义您的变量:

varid_Q2D = netcdf.defVar(ncid,'Q2D','float',[west_east south_north Time]);

你应该没事。

有关详细信息,请参阅此页:

http://www.mathworks.com/help/matlab/ref/netcdf.defvar.html