From 8066dd645617d7febcabd239cc60727efe0bfbe5 Mon Sep 17 00:00:00 2001 From: Olivier Sauter <olivier.sauter@epfl.ch> Date: Sun, 9 Feb 2025 09:30:34 +0100 Subject: [PATCH] fix reading netcdf with units not defined (e.g. fi cdf), add shot as input --- matlab/CHDF/cdf2mat.m | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/matlab/CHDF/cdf2mat.m b/matlab/CHDF/cdf2mat.m index c9d7a14e..270ded91 100644 --- a/matlab/CHDF/cdf2mat.m +++ b/matlab/CHDF/cdf2mat.m @@ -1,4 +1,5 @@ -function cdf2mat_out = cdf2mat(pfname) +function cdf2mat_out = cdf2mat(pfname,varargin) +% cdf2mat_out = cdf2mat(pfname,varargin) % % reads all variables and coordinates from netcdf % some trials with netcdf, using 50725c01.cdf as test @@ -7,6 +8,9 @@ function cdf2mat_out = cdf2mat(pfname) % % 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') @@ -33,6 +37,7 @@ if length(allvarnames) ~= length(allvarids) end [varnames_sorted,~]=sort(allvarnames); +cdf2mat_out.allvarnames_sorted = varnames_sorted; % to find a variable: % strmatch('GFUN',allvarnames,'exact') @@ -56,6 +61,8 @@ for i=1:length(coordnames_sorted) 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}); 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) 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); @@ -80,6 +87,7 @@ for i=1:length(varnames_sorted) matcdf.vars(i).name = varnames_sorted{i}; matcdf.vars(i).index_allvarnames = strmatch(matcdf.vars(i).name,allvarnames,'exact'); 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 strcmp(allinfo.Variables(matcdf.vars(i).index_allvarnames).Datatype,'single') matcdf.vars(i).data = netcdf.getVar(funnetcdf,matcdf.vars(i).varid,'double'); @@ -89,6 +97,8 @@ for i=1:length(varnames_sorted) 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}); 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) 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); @@ -128,7 +138,11 @@ if ~isempty(allinfo.Attributes) ij = strmatch('shot',top_attr_names,'exact'); cdf2mat_out.shot = allinfo.Attributes(ij).Value; else - cdf2mat_out.shot = NaN; + if nargin>1 && isnumeric(varargin{1}) + cdf2mat_out.shot = varargin{1}; + else + cdf2mat_out.shot = NaN; + end end cdf2mat_out.fname = pfname; [a1,a2,a3]=fileparts(pfname); -- GitLab