在 MATLAB 中将一组字符串保存到 NetCDF
Save a set of strings into NetCDF in MATLAB
我有一个字符串列表,我想使用 MATLAB 将其保存到 netCDF 中。但是,我在创建文件时遇到问题。
method_ID
是一个像这样的字符数组:
'55-059-0019-88101-1'
'55-059-0019-88101-1'
'55-059-0019-88101-1'
'55-059-0019-88101-1'
'55-059-0019-88101-1'
'55-059-0019-88101-1'
'55-059-0019-88101-1'
'55-059-0019-88101-1'
'55-059-0019-88101-1'
'55-059-0019-88101-1'
我尝试执行以下操作,但它会将字符串保存为一个长字符串而不是 10 个字符串。
filename = ['PM25_24hr_',num2str(years(y)),'_EPA_AQS.nc'];
ncfilename = ['/data3/jg3223/PM25/netCDF/',filename];
method_ID = char(data_PM25{:,1});
% Create the file, overwriting if already exists (Clobber) and
% 64-bit offset (Allow easier creation of files and variables which are larger
% than two gigabytes.)
mode = bitor(netcdf.getConstant('CLOBBER'),netcdf.getConstant('64BIT_OFFSET'));
ncid = netcdf.create(ncfilename,mode);
% Define (file) global attributes
globalVarId=netcdf.getConstant('GLOBAL');
netcdf.putAtt(ncid,globalVarId,'name','PM25_24hr_AQS');
[a,b] = size(method_ID);
IDDimId = netcdf.defDim(ncid,'method_ID',a*b); % I know this size is wrong, but using `length(method_ID)` runs a size error later on
% Define variables, starting with coordinate variables,
% with appropriate names, types, and dimensions
IDVarId = netcdf.defVar(ncid,'method_ID','char',IDDimId);
% Exit the "define mode", i.e., structure creation mode
netcdf.endDef(ncid);
method ID
netcdf.putVar(ncid,IDVarId,method_ID);
% Close the netCDF file
netcdf.close(ncid);
如果您使用 "classic strings",您应该定义两个维度。
可以使用两种形式的 netCDF 存储字符串,"classic strings" 和 "arrays of strings" (https://www.unidata.ucar.edu/software/netcdf/netcdf-4/newdocs/netcdf-c/Strings.html)。 "Classic strings" 不需要 netCDF 版本 4.0+,以下答案基于此。
%% Solution using classic string (without NETCDF-4)
ncfilename ='./AQS.nc';
uint_method=uint8('55-059-0019-88101-1');
method_ID = char(repmat(uint_method,10,1));
[a,b] = size(method_ID); %a=10, b=19
mode = bitor(netcdf.getConstant('CLOBBER'),netcdf.getConstant('64BIT_OFFSET'));
ncid = netcdf.create(ncfilename,mode);
globalVarId=netcdf.getConstant('GLOBAL');
netcdf.putAtt(ncid,globalVarId,'name','PM25_24hr_AQS');
%IDDimId = netcdf.defDim(ncid,'method_ID',a*b);
% IDDimId = netcdf.defDim(ncid,'method_ID',length(method_ID)); % ERROR: input elements does not match the variable size
% IDVarId = netcdf.defVar(ncid,'method_ID','char',IDDimId);
IDDim1 = netcdf.defDim(ncid,'charlen',b);
IDDim2 = netcdf.defDim(ncid,'methods',a);
IDVarId = netcdf.defVar(ncid,'method_ID','char',[IDDim1 IDDim2]);
netcdf.endDef(ncid);
netcdf.putVar(ncid,IDVarId,method_ID'); %transpose
netcdf.close(ncid);
您将获得:
$ ncdump AQS.nc
netcdf AQS {
dimensions:
charlen = 19 ;
methods = 10 ;
variables:
char method_ID(methods, charlen) ;
// global attributes:
:name = "PM25_24hr_AQS" ;
data:
method_ID =
"55-059-0019-88101-1",
"55-059-0019-88101-1",
"55-059-0019-88101-1",
"55-059-0019-88101-1",
"55-059-0019-88101-1",
"55-059-0019-88101-1",
"55-059-0019-88101-1",
"55-059-0019-88101-1",
"55-059-0019-88101-1",
"55-059-0019-88101-1" ;
}
我有一个字符串列表,我想使用 MATLAB 将其保存到 netCDF 中。但是,我在创建文件时遇到问题。
method_ID
是一个像这样的字符数组:
'55-059-0019-88101-1'
'55-059-0019-88101-1'
'55-059-0019-88101-1'
'55-059-0019-88101-1'
'55-059-0019-88101-1'
'55-059-0019-88101-1'
'55-059-0019-88101-1'
'55-059-0019-88101-1'
'55-059-0019-88101-1'
'55-059-0019-88101-1'
我尝试执行以下操作,但它会将字符串保存为一个长字符串而不是 10 个字符串。
filename = ['PM25_24hr_',num2str(years(y)),'_EPA_AQS.nc'];
ncfilename = ['/data3/jg3223/PM25/netCDF/',filename];
method_ID = char(data_PM25{:,1});
% Create the file, overwriting if already exists (Clobber) and
% 64-bit offset (Allow easier creation of files and variables which are larger
% than two gigabytes.)
mode = bitor(netcdf.getConstant('CLOBBER'),netcdf.getConstant('64BIT_OFFSET'));
ncid = netcdf.create(ncfilename,mode);
% Define (file) global attributes
globalVarId=netcdf.getConstant('GLOBAL');
netcdf.putAtt(ncid,globalVarId,'name','PM25_24hr_AQS');
[a,b] = size(method_ID);
IDDimId = netcdf.defDim(ncid,'method_ID',a*b); % I know this size is wrong, but using `length(method_ID)` runs a size error later on
% Define variables, starting with coordinate variables,
% with appropriate names, types, and dimensions
IDVarId = netcdf.defVar(ncid,'method_ID','char',IDDimId);
% Exit the "define mode", i.e., structure creation mode
netcdf.endDef(ncid);
method ID
netcdf.putVar(ncid,IDVarId,method_ID);
% Close the netCDF file
netcdf.close(ncid);
如果您使用 "classic strings",您应该定义两个维度。
可以使用两种形式的 netCDF 存储字符串,"classic strings" 和 "arrays of strings" (https://www.unidata.ucar.edu/software/netcdf/netcdf-4/newdocs/netcdf-c/Strings.html)。 "Classic strings" 不需要 netCDF 版本 4.0+,以下答案基于此。
%% Solution using classic string (without NETCDF-4)
ncfilename ='./AQS.nc';
uint_method=uint8('55-059-0019-88101-1');
method_ID = char(repmat(uint_method,10,1));
[a,b] = size(method_ID); %a=10, b=19
mode = bitor(netcdf.getConstant('CLOBBER'),netcdf.getConstant('64BIT_OFFSET'));
ncid = netcdf.create(ncfilename,mode);
globalVarId=netcdf.getConstant('GLOBAL');
netcdf.putAtt(ncid,globalVarId,'name','PM25_24hr_AQS');
%IDDimId = netcdf.defDim(ncid,'method_ID',a*b);
% IDDimId = netcdf.defDim(ncid,'method_ID',length(method_ID)); % ERROR: input elements does not match the variable size
% IDVarId = netcdf.defVar(ncid,'method_ID','char',IDDimId);
IDDim1 = netcdf.defDim(ncid,'charlen',b);
IDDim2 = netcdf.defDim(ncid,'methods',a);
IDVarId = netcdf.defVar(ncid,'method_ID','char',[IDDim1 IDDim2]);
netcdf.endDef(ncid);
netcdf.putVar(ncid,IDVarId,method_ID'); %transpose
netcdf.close(ncid);
您将获得:
$ ncdump AQS.nc
netcdf AQS {
dimensions:
charlen = 19 ;
methods = 10 ;
variables:
char method_ID(methods, charlen) ;
// global attributes:
:name = "PM25_24hr_AQS" ;
data:
method_ID =
"55-059-0019-88101-1",
"55-059-0019-88101-1",
"55-059-0019-88101-1",
"55-059-0019-88101-1",
"55-059-0019-88101-1",
"55-059-0019-88101-1",
"55-059-0019-88101-1",
"55-059-0019-88101-1",
"55-059-0019-88101-1",
"55-059-0019-88101-1" ;
}