% touch_gmeta change a global attribute in a series of files % % touch_gmeta(gatt, newval, files, dryrun) % gatt = global attribute name % newval = new attribute value % files = cell array of file names % an easy way to just use a pattern: % d = dir('844*.nc'); % touch_gmeta('MOORING','844',{d.name},1) % dryrun = 1; test run first, set to zero to execute % % Written by Marinna Martini with guidance from Ellyn Montgomery function touch_gmeta(gatt, newval, files, dryrun) if ~exist('dryrun','var'), dryrun = 1; end if dryrun, disp('This is a dry run, nothing will be changed'); end % NOTE : this only works on files in the cwd- it doesn't traverse dirs to % find files to modify !! % ** needs to work with either nc or cdf if ~exist('files','var'), if ispc d1=dir('*.nc'); d2=dir('*.cdf'); d=[d1;d2]; for jj=1:length(d) fnames{jj}=d(jj).name; end else % for unix [stat,fnamesames]=unix('ls *'); knt=1; for ik=1:length(xx)-1 vv=char(deblank(fnamesames(xx(ik):xx(ik+1)-1))); if(~isempty(findstr(vv,'cdf')) || ~isempty(findstr(vv,'nc'))) fnames{knt}=deblank(fnamesames(xx(ik):xx(ik+1)-1)); knt=knt+1; else disp(['not processing ' vv]); end end end else fnames = files; end for ifile=1:length(fnames) nc = netcdf(fnames{ifile}, 'write'); if isempty(nc), disp('read of ncfile failed, try another name'), return, end eval(sprintf('gattobj = nc.%s(:)',gatt)) if ~isempty(gattobj), disp(sprintf('%s nc.%s(:) = newval;',fnames{ifile},gatt)); disp(newval) command = sprintf('nc.%s(:) = newval;',gatt); if ~dryrun, eval(command); end else disp(sprintf('%s not found in %s',gatt, fnames{ifile})) end close(nc) end