Skip to content
Snippets Groups Projects
Commit 8066dd64 authored by Olivier Sauter's avatar Olivier Sauter
Browse files

fix reading netcdf with units not defined (e.g. fi cdf), add shot as input

parent b7fdd59a
No related branches found
No related tags found
1 merge request!181fix reading netcdf with units not defined (e.g. fi cdf), add shot as input
Pipeline #245443 passed
function cdf2mat_out = cdf2mat(pfname) function cdf2mat_out = cdf2mat(pfname,varargin)
% cdf2mat_out = cdf2mat(pfname,varargin)
% %
% reads all variables and coordinates from netcdf % reads all variables and coordinates from netcdf
% some trials with netcdf, using 50725c01.cdf as test % some trials with netcdf, using 50725c01.cdf as test
...@@ -7,6 +8,9 @@ function cdf2mat_out = cdf2mat(pfname) ...@@ -7,6 +8,9 @@ function cdf2mat_out = cdf2mat(pfname)
% %
% Uses matlab netcdf.open, ncinfo, netcdf.inqVarIDs, netcd.getVar, etc % Uses matlab netcdf.open, ncinfo, netcdf.inqVarIDs, netcd.getVar, etc
% %
% varargin{1}: shot number to add (in particular when cdf file does not have it defined
% shot from file: allinfo=ncinfo(pfname);if ~isempty(allinfo.Attributes),top_attr_names = {allinfo.Attributes(:).Name};allinfo.Attributes(strmatch('shot',top_attr_names,'exact')).Value, else, disp('no Attributes'),end
%
% %
if ~exist(pfname,'file') if ~exist(pfname,'file')
...@@ -33,6 +37,7 @@ if length(allvarnames) ~= length(allvarids) ...@@ -33,6 +37,7 @@ if length(allvarnames) ~= length(allvarids)
end end
[varnames_sorted,~]=sort(allvarnames); [varnames_sorted,~]=sort(allvarnames);
cdf2mat_out.allvarnames_sorted = varnames_sorted;
% to find a variable: % to find a variable:
% strmatch('GFUN',allvarnames,'exact') % strmatch('GFUN',allvarnames,'exact')
...@@ -56,6 +61,8 @@ for i=1:length(coordnames_sorted) ...@@ -56,6 +61,8 @@ for i=1:length(coordnames_sorted)
for ij=1:length(fields_variables_to_copy) for ij=1:length(fields_variables_to_copy)
matcdf.coords(i).(fields_variables_to_copy{ij}) = allinfo.Variables(matcdf.coords(i).index_allvarnames).(fields_variables_to_copy{ij}); matcdf.coords(i).(fields_variables_to_copy{ij}) = allinfo.Variables(matcdf.coords(i).index_allvarnames).(fields_variables_to_copy{ij});
end end
matcdf.coords(i).units = '';
matcdf.coords(i).long_name = [matcdf.coords(i).name ' empty'];
for jj=1:numel(allinfo.Variables(matcdf.coords(i).index_allvarnames).Attributes) for jj=1:numel(allinfo.Variables(matcdf.coords(i).index_allvarnames).Attributes)
if strcmp(lower(allinfo.Variables(matcdf.coords(i).index_allvarnames).Attributes(jj).Name),'units') if strcmp(lower(allinfo.Variables(matcdf.coords(i).index_allvarnames).Attributes(jj).Name),'units')
matcdf.coords(i).units = strtrim(allinfo.Variables(matcdf.coords(i).index_allvarnames).Attributes(jj).Value); matcdf.coords(i).units = strtrim(allinfo.Variables(matcdf.coords(i).index_allvarnames).Attributes(jj).Value);
...@@ -80,6 +87,7 @@ for i=1:length(varnames_sorted) ...@@ -80,6 +87,7 @@ for i=1:length(varnames_sorted)
matcdf.vars(i).name = varnames_sorted{i}; matcdf.vars(i).name = varnames_sorted{i};
matcdf.vars(i).index_allvarnames = strmatch(matcdf.vars(i).name,allvarnames,'exact'); matcdf.vars(i).index_allvarnames = strmatch(matcdf.vars(i).name,allvarnames,'exact');
matcdf.vars(i).varid = allvarids(matcdf.vars(i).index_allvarnames); matcdf.vars(i).varid = allvarids(matcdf.vars(i).index_allvarnames);
% if i==strmatch('ZEFFC',varnames_sorted,'exact'), keyboard; end
if ~isempty(matcdf.vars(i).index_allvarnames) if ~isempty(matcdf.vars(i).index_allvarnames)
if strcmp(allinfo.Variables(matcdf.vars(i).index_allvarnames).Datatype,'single') if strcmp(allinfo.Variables(matcdf.vars(i).index_allvarnames).Datatype,'single')
matcdf.vars(i).data = netcdf.getVar(funnetcdf,matcdf.vars(i).varid,'double'); matcdf.vars(i).data = netcdf.getVar(funnetcdf,matcdf.vars(i).varid,'double');
...@@ -89,6 +97,8 @@ for i=1:length(varnames_sorted) ...@@ -89,6 +97,8 @@ for i=1:length(varnames_sorted)
for ij=1:length(fields_variables_to_copy) for ij=1:length(fields_variables_to_copy)
matcdf.vars(i).(fields_variables_to_copy{ij}) = allinfo.Variables(matcdf.vars(i).index_allvarnames).(fields_variables_to_copy{ij}); matcdf.vars(i).(fields_variables_to_copy{ij}) = allinfo.Variables(matcdf.vars(i).index_allvarnames).(fields_variables_to_copy{ij});
end end
matcdf.vars(i).units = '';
matcdf.vars(i).long_name = [matcdf.vars(i).name ' empty'];
for jj=1:numel(allinfo.Variables(matcdf.vars(i).index_allvarnames).Attributes) for jj=1:numel(allinfo.Variables(matcdf.vars(i).index_allvarnames).Attributes)
if strcmp(lower(allinfo.Variables(matcdf.vars(i).index_allvarnames).Attributes(jj).Name),'units') if strcmp(lower(allinfo.Variables(matcdf.vars(i).index_allvarnames).Attributes(jj).Name),'units')
matcdf.vars(i).units = strtrim(allinfo.Variables(matcdf.vars(i).index_allvarnames).Attributes(jj).Value); matcdf.vars(i).units = strtrim(allinfo.Variables(matcdf.vars(i).index_allvarnames).Attributes(jj).Value);
...@@ -128,7 +138,11 @@ if ~isempty(allinfo.Attributes) ...@@ -128,7 +138,11 @@ if ~isempty(allinfo.Attributes)
ij = strmatch('shot',top_attr_names,'exact'); ij = strmatch('shot',top_attr_names,'exact');
cdf2mat_out.shot = allinfo.Attributes(ij).Value; cdf2mat_out.shot = allinfo.Attributes(ij).Value;
else else
cdf2mat_out.shot = NaN; if nargin>1 && isnumeric(varargin{1})
cdf2mat_out.shot = varargin{1};
else
cdf2mat_out.shot = NaN;
end
end end
cdf2mat_out.fname = pfname; cdf2mat_out.fname = pfname;
[a1,a2,a3]=fileparts(pfname); [a1,a2,a3]=fileparts(pfname);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment