Skip to content

Commit

Permalink
Dolfyn: Read data attributes into attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
simmsa committed Jul 23, 2024
1 parent e0e2c41 commit b6f1970
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions mhkit/dolfyn/io/read_netcdf.m
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,18 @@
end
if ~isempty(attrs)
for ii = 1:numel(attrs)
if strcmpi(attrs(ii).Name,'units')
ds.(name).units = attrs(ii).Value;
% Specify what we are accessing
this_name = attrs(ii).Name;
this_field_name = stringToValidFieldname(this_name);
this_value = attrs(ii).Value;
% Specifically assign units above attributes
% Not sure if this is technically correct
if strcmpi(this_name, 'units')
ds.(name).units = this_value;
end

% Assign all attributes to `attrs`
ds.(name).attrs.(this_field_name) = this_value;
end
end
end
Expand All @@ -140,5 +149,22 @@
ds.coord_sys = ds.attrs.coord_sys;
ds.time = ds.coords.time;

end
function fieldname = stringToValidFieldname(str)
% Ensure first character is a letter
if ~isletter(str(1))
str = ['_' str];
end

% Trim leading and trailing whitespace
str = strtrim(str);

% Replace double underscores
str = strrep(str, '__', '');

% % Replace invalid characters with underscore
str = regexprep(str, '[^a-zA-Z0-9_]', '_');

fieldname = str;
end

end

0 comments on commit b6f1970

Please sign in to comment.