function cleanup_ysi_tb(ncfilename) % cleanup_ysi_tb: creates a consistent timebase to use in netCDF file % ysi's are patched together from smaller parts so there are usually gaps, % but they aren't filled with NaN, so the timebase isn't uniform % this program adds the missing time steps, and fills all variables at % those times with fill_Value % etm mod 02/17/11 %%% START USGS BOILERPLATE ------------- % These programs are intened for us in adjusting metadata terms in % netCDF files from the USGS CMGP Oceanographic time-series data % archive % % Program written Matlab 7.6.0.342 (R2008a) % Program ran on Linux PC with RHEL4 and on Windows XP PC. % % "Although this program has been used by the USGS, no warranty, % expressed or implied, is made by the USGS or the United States % Government as to the accuracy and functioning of the program % and related program material nor shall the fact of distribution % constitute any such warranty, and no responsibility is assumed % by the USGS in connection therewith." %%% END USGS BOILERPLATE -------------- perloc=findstr('.',ncfilename); new_nm=[ncfilename(1:perloc-1) '_new.cdf']; % open input file nc = netcdf(ncfilename); % open the a (file named b) if isempty(nc), return, end % and the NEW EMPTY output file to take trimmed data outc = netcdf(new_nm,'clobber'); %% set the dimensions (only applies to coordinate variables) outc('time') = 0; outc('depth')=length(nc{'depth'}(:)); outc('lon') = 1; outc('lat') = 1; %copy the info over - this is a way of DEFINING the variables % this happens in the loop below : copy(nc{'time'},outc,0,1); % have to copy the structure of the variable into the new array with % size 0 initially, then copy new contents to determine the size. vnam=ncnames(var(nc)); % use the names from nc, but get the empty variables from ep-standard. eps = netcdf('ep_standard.nc'); if isempty(eps) [epsfile,epspath] = uigetfile('*.nc','Choose epic standard file'); eps = netcdf(epsfile); if isempty(eps) disp (['Cannot open epic standard file ' epsfile]); disp (['Execution ends.']); ncclose; return; end end for ik=1:length(vnam)% create the non-record variables epvar=eps{vnam{ik}}; if isempty(epvar) %get from nc, if not in eps eval(['copy(nc{''' char(vnam(ik)) '''},outc,0,1);']) else copy(epvar,outc,0,1); end % eval(['copy(nc{''' char(vnam(ik)) '''},outc,0,1);']) end close(eps) % endef must be used to terminate the variable definition % and the termination *MUST* come before values are written to any % variable! endef(outc) % need to copy over all the global attributes here atn=ncnames(att(nc)); for j=1:length(atn) eval( ['copy(nc.' char(atn(j)) ',outc);']) end %% Global attributes: lfeed = char(10); outc.CREATION_DATE = ncchar(datestr(now,0)); history = ['timebase made even by cleaup_ysy_tb.m:' nc.history(:)]; ifeed = findstr(history,lfeed); history(ifeed) = ':'; outc.history = ncchar(history); % NOW set the values of some of the coordinate vars, but don't do time yet! outc{'lat'}(:)=nc{'lat'}(:); outc{'lon'}(:)=nc{'lon'}(:); outc{'depth'}(:)=nc{'depth'}(:); tt=nc{'time'}(:)+(nc{'time2'}(:)/86400000); if isnumeric(nc.DELTA_T(:)) dtjd=(nc.DELTA_T(:)./86400); else dtjd=(str2num(nc.DELTA_T(:)))/86400; end tgap=find(diff(tt)> dtjd*1.25); % not sure why, but the threshold needed to be a bit bigger % here we want to insert the shorter strings into the first nlen items % later we use nctrim to get rid of the trailing irrelevant junk nlen=length(tgap) tnew=tt(1:tgap(1)); for ik=1:nlen tdiff=(tt(tgap(ik)+1)-tt(tgap(ik))); nskip=round(tdiff/dtjd); new_times=tt(tgap(ik))+dtjd:dtjd:tt(tgap(ik))+(nskip-1)*dtjd; if (new_times(end)-tt(tgap(ik)+1) ==0) new_times(end)=[]; end eval(['nf' num2str(ik) '=ones(length(new_times),1).*nc.VAR_FILL(:);']) tnew=[tnew; new_times']; if ik==nlen tnew=[tnew; tt(tgap(ik)+1:end)]; else tnew=[tnew; tt(tgap(ik)+1:tgap(ik+1))]; end clear nskip new_times tdiff end % time part works to here % put the new time into the coordinate variable % eptime expects a matlab time string, so we must convert to get it back % ntime=ep_time(tnew); % nl=length(ntime); % outc{'time'}(1:nl)=ntime(:,1); % outc{'time2'}(1:nl)=ntime(:,2); t1=floor(tnew); t2=rem(tnew,floor(tnew)); nl=length(t1); outc{'time'}(1:nl)=t1; outc{'time2'}(1:nl)=t2.*86400000; for ik=[6:length(vnam)] %copy the attributes of each variable over eval(['copy(nc{''' char(vnam(ik)) ''' }, outc{''' char(vnam(ik)) ''' },0,1);']) if strmatch(vnam(ik),'time_cf') tmp=linspace(nc{'time_cf'}(1), nc{'time_cf'}(end),length(t1)); else tmp=ones(size(tnew)); eval(['ori_dat = nc{''' char(vnam(ik)) '''}(:);']) % separate indecies are needed because tmp will index by all the flens idx1=tgap(1);tidx1=idx1;tidx2=tidx1; tmp(1:tidx1)=ori_dat(1:idx1); flentot=0; for jj=1:nlen eval(['flen=length(nf' num2str(jj) ');']); flentot=flentot+flen; tidx1=tidx2; tidx2=tidx1+flen; [tidx1+1 tidx2]; eval(['tmp(tidx1+1:tidx2)=nf' num2str(jj) ';']); if jj