Commit a211facf authored by PStar User Account's avatar PStar User Account
Browse files

changes to use matlab native netcdf

 Committer: PStar User Account <pstar@192.168.122.1>

 On branch dy113
 Changes to be committed:

	modified:   source/msubs/m_add_variable_name.m
	modified:   source/msubs/m_read_header.m
	modified:   source/msubs/m_unpack_dimnames.m
	modified:   source/msubs/m_unpack_varnames.m
	modified:   source/msubs/m_write_variable.m
	modified:   source/unfinished/m_ismstar.m
parent 07db814c
......@@ -18,7 +18,26 @@ if nargin < 3
end
metadata = nc_infoqdim(ncfile.name); %refresh metadata
if exist('ncinfo','file') == 2 % jc191 use matlab netcdf if available
% make metadata look the same as the old snctools nc_info
% dm is the matlab data structure; dr is the reconstructed one.
clear dm dr
dm = ncinfo(ncfile.name);
dr.Filename = ncfile.name;
dr.Attribute = dm.Attributes; % don't need in add_variable_name
dr.Dimension = dm.Dimensions(:);
for kl = 1:length(dm.Variables);
dr.Dataset(kl).Name = dm.Variables(kl).Name;
dr.Dataset(kl).Dimension = dm.Variables(kl).Dimensions.Name;
dr.Dataset(kl).Nctype = find(strcmp(dm.Variables(kl).Datatype,{'' 'char' '' '' '' 'double' ''}));
end
if isfield(dr,'Dataset'); dr.Dataset = dr.Dataset(:); end
metadata = dr;
else
metadata = nc_infoqdim(ncfile.name); %refresh metadata % command before jc191
end
ncfile.metadata = metadata;
dimnames = m_unpack_dimnames(ncfile);
......
......@@ -11,7 +11,27 @@ ncfile = m_resolve_filename(ncfile);
ncfile = m_ismstar(ncfile); % check it is an mstar file
metadata = nc_infoqatt(ncfile.name); %refresh metadata
if exist('ncinfo','file') == 2 % jc191 use matlab netcdf if available
% make metadata look the same as the old snctools nc_info
% dm is the matlab data structure; dr is the reconstructed one.
clear dm dr
dm = ncinfo(ncfile.name);
dr.Filename = ncfile.name;
dr.Attribute = dm.Attributes; % don't need in add_variable_name
dr.Dimension = dm.Dimensions(:);
for kl = 1:length(dm.Variables);
dr.Dataset(kl).Name = dm.Variables(kl).Name;
dr.Dataset(kl).Dimension = dm.Variables(kl).Dimensions.Name;
dr.Dataset(kl).Nctype = find(strcmp(dm.Variables(kl).Datatype,{'' 'char' '' '' '' 'double' ''}));
end
if isfield(dr,'Dataset'); dr.Dataset = dr.Dataset(:); end
metadata = dr;
else
metadata = nc_infoqatt(ncfile.name); %refresh metadata % command before jc191
end
ncfile.metadata = metadata;
var_names = m_unpack_varnames(ncfile);
dimnames = m_unpack_dimnames(ncfile);
......
......@@ -6,7 +6,25 @@ function dim_names = m_unpack_dimnames(ncfile)
% If the metadata isn't in the structure ncfile, then read it from the file
if ~isfield(ncfile,'metadata')
metadata = nc_info(ncfile.name);
if exist('ncinfo','file') == 2 % jc191 use matlab netcdf if available
% make metadata look the same as the old snctools nc_info
% dm is the matlab data structure; dr is the reconstructed one.
clear dm dr
dm = ncinfo(ncfile.name);
dr.Filename = ncfile.name;
dr.Attribute = dm.Attributes; % don't need in add_variable_name
dr.Dimension = dm.Dimensions(:);
for kl = 1:length(dm.Variables);
dr.Dataset(kl).Name = dm.Variables(kl).Name;
dr.Dataset(kl).Dimension = dm.Variables(kl).Dimensions.Name;
dr.Dataset(kl).Nctype = find(strcmp(dm.Variables(kl).Datatype,{'' 'char' '' '' '' 'double' ''}));
end
if isfield(dr,'Dataset'); dr.Dataset = dr.Dataset(:); end
metadata = dr;
else
metadata = nc_info(ncfile.name); % command before jc191
end
ncfile.metadata = metadata;
end
......
......@@ -3,36 +3,63 @@ function var_names = m_unpack_varnames(ncfile)
% Unpack variable names from netcdf file
%If the metadata isn't passed in, then read it from the file
ncfile.name
[ncid,MEXEC.status] = mexnc('open',ncfile.name,nc_nowrite_mode);
[ndims, nvars, ngatts, record_dimension, MEXEC.status] = mexnc('INQ', ncid);
var_names = cell(nvars,1);
for k = 1:nvars;
[varname, datatype, ndims, dims, natts, MEXEC.status] = mexnc('INQ_VAR', ncid, k-1);
var_names{k} = varname;
end;
MEXEC.status = mexnc('close',ncid);
% % if ~isfield(ncfile,'metadata')
% % metadata = nc_info(ncfile.name);
% % ncfile.metadata = metadata;
% % end
% %
% % metadata = ncfile.metadata;
% %
% % if isfield ( metadata, 'Dataset' )
% % num_vars = length(metadata.Dataset);
% % else
% % num_vars = 0;
% % end
% %
% % var_names = cell(num_vars,1);
% %
% % for k = 1:num_vars
% % var_names{k} = metadata.Dataset(k).Name;
% % end
% %
% % return
\ No newline at end of file
% % % % % % the version at the start of jc191 had been written to make
% direct call to mexnc, but I'm not sure why. There seems no reason to
% force the read of metadata if it has already been read.
% % % % %
% % % % % [ncid,MEXEC.status] = mexnc('open',ncfile.name,nc_nowrite_mode);
% % % % % [ndims, nvars, ngatts, record_dimension, MEXEC.status] = mexnc('INQ', ncid);
% % % % %
% % % % % var_names = cell(nvars,1);
% % % % %
% % % % % for k = 1:nvars;
% % % % % [varname, datatype, ndims, dims, natts, MEXEC.status] = mexnc('INQ_VAR', ncid, k-1);
% % % % % var_names{k} = varname;
% % % % % end;
% % % % %
% % % % % MEXEC.status = mexnc('close',ncid);
% jc191 reinstate previous logic, with faster call if varnames need to be
% read in.
% often, metadata will be passed in and does not need to be read again
if ~isfield(ncfile,'metadata')
if exist('ncinfo','file') == 2 % jc191 use matlab netcdf if available
% make metadata look the same as the old snctools nc_info
% dm is the matlab data structure; dr is the reconstructed one.
clear dm dr
dm = ncinfo(ncfile.name);
dr.Filename = ncfile.name;
dr.Attribute = dm.Attributes; % don't need in add_variable_name
dr.Dimension = dm.Dimensions(:);
for kl = 1:length(dm.Variables);
dr.Dataset(kl).Name = dm.Variables(kl).Name;
dr.Dataset(kl).Dimension = dm.Variables(kl).Dimensions.Name;
dr.Dataset(kl).Nctype = find(strcmp(dm.Variables(kl).Datatype,{'' 'char' '' '' '' 'double' ''}));
end
if isfield(dr,'Dataset'); dr.Dataset = dr.Dataset(:); end
metadata = dr;
else
metadata = nc_info(ncfile.name); % command before jc191
end
ncfile.metadata = metadata;
end
metadata = ncfile.metadata;
if isfield ( metadata, 'Dataset' )
num_vars = length(metadata.Dataset);
else
num_vars = 0;
end
var_names = cell(num_vars,1);
for k = 1:num_vars
var_names{k} = metadata.Dataset(k).Name;
end
return
\ No newline at end of file
......@@ -26,7 +26,25 @@ end
d1 = s(1);
d2 = s(2);
metadata = nc_infoqdim(ncfile.name); %refresh metadata
if exist('ncinfo','file') == 2 % jc191 use matlab netcdf if available
% make metadata look the same as the old snctools nc_info
% dm is the matlab data structure; dr is the reconstructed one.
clear dm dr
dm = ncinfo(ncfile.name);
dr.Filename = ncfile.name;
dr.Attribute = dm.Attributes; % don't need in add_variable_name
dr.Dimension = dm.Dimensions(:);
for kl = 1:length(dm.Variables);
dr.Dataset(kl).Name = dm.Variables(kl).Name;
dr.Dataset(kl).Dimension = dm.Variables(kl).Dimensions.Name;
dr.Dataset(kl).Nctype = find(strcmp(dm.Variables(kl).Datatype,{'' 'char' '' '' '' 'double' ''}));
end
if isfield(dr,'Dataset'); dr.Dataset = dr.Dataset(:); end
metadata = dr;
else
metadata = nc_infoqdim(ncfile.name); %refresh metadata % command before jc191
end
ncfile.metadata = metadata;
dimnames = m_unpack_dimnames(ncfile);
......
function ncfile = m_ismstar(ncfile,opt)
% function ncfile = m_ismstar(ncfile,opt)
function ncfile = m_ismstar(ncfile)
% function ncfile = m_ismstar(ncfile)
%
% Check if a file is an mstar NetCDF file
% v4 version by bak on jc191; Use matlab native netcdf;
% bak jc191 removed second argument 'opt' which was never used
if nargin ~= 1
error('Must supply precisely one argument to m_ismstar');
......@@ -9,6 +11,35 @@ end
ncfile.name = m_add_nc(ncfile.name);
% bak will this become v4, using matlab netcdf commands ? jc191: use a single try and matlab native netcdf to try to check
% for a mstar netcdf file
if exist('ncreadatt','file') == 2 % ncreadatt.m is an available command
try
s = ncreadatt(ncfile.name,'/','mstar_string');
catch
if exist(ncfile.name,'file') ~= 2
errstr0 = sprintf('\n%s',['The file ' ncfile.name ' does not exist']);
error(errstr0);
else
errstr0 = sprintf('\n%s',['The file ' ncfile.name ' exists but does not contain the mstar_string']);
error(errstr0);
end
end
if ~strcmp(s(1:5),'mstar')
errstr0 = sprintf('\n%s',['The file ' ncfile.name ' exists but the mstar_string contents are wrong']);
error(errstr0);
end
return
end
% bak jc191: old code follows here and should function as before if
% ncreadatt doesnt exist.
% if the matlab netcdf command readatt exists, then the code will have
% exited before this point, either with the catch case, or after
% successfully determining that it is an OK mstar file. If ncreadatt
% is not available, then we revert to the old snctools code of nc_info.
%check file exists
if exist(ncfile.name,'file') ~= 2
error(['Checking existence of mstar file: Filename ''' ncfile.name ''' not found']);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment