function nfild = EPIC_interp(inc,outc) % EPIC_interp: replace pads in an EPIC netCDF file. % Fill data gaps marked by NaN's with % linearly interpolated values. % Fran Hotchkiss April 13, 1998 % Modified June 2, 1998, to interpolate 5000 % records at a time, because interp1.m % was taking forever. Fran Hotchkiss % Script changed to a function, June 3, 1998. FH % Now it will interpolate pad values as well. % Fran Hotchkiss 26-Mar-2001 % Copy inc to outc. result=fcopy(inc,outc) % Open outc. nc = netcdf(outc, 'write'); if isempty(nc), return, end % Load time variables and make monotonic time base. time = nc{'time'}(:); time2 = nc{'time2'}(:); msperday = 24*60*60*1000%; t = time + time2/msperday; % Cycle through variables. nfild = 0%; fillflag = 0%; theVars = var(nc); theRecdim = recdim(nc); for i = 1:length(theVars) thisVar = name(theVars{i}) % Is it a function of time? theDims = dim(theVars{i}); if length(theDims) > 1 if isequal(name(theDims{1}),name(theRecdim)) message = 'thisVar is a function of time' % Does it have pad values? ivar = nc{name(theVars{i})} ivar = autonan(ivar,1); data = ivar(:); if max(isnan(data)) message = 'thisVar has pad values' % Fill pad values. DoMore = 1; ntotal = 0; startrec = 1; while DoMore stoprec = startrec + 5000 if stoprec > length(data) stoprec = length(data) DoMore = 0 end if max(isnan(data(startrec:stoprec))) [yi,ni] = fillpads (data(startrec:stoprec),t(startrec:stoprec)); ntotal = ntotal + ni; ivar(startrec:stoprec) = yi(:); end startrec = startrec + 4000; end nfild = nfild + ntotal % Update attributes. comment = [num2str(ntotal) ' pad values interpolated.'] ivar.interp_note = comment; % Does it still have pad values? if max(isnan(yi)) message = 'not all pad values interpolated' fillflag = 1%; end end end end end message = 'done cycling through all variables' % Update global attributes. nc.FILL_FLAG = fillflag; nc.VAR_FILL = ncchar('NaN'); nc.CREATION_DATE = ncchar(datestr(now,0)); history =' pad values replaced by linear interpolations using EPIC_interp.m :'; history =[num2str(nfild) history nc.history(:)]; nc.history = history; ncclose