function theResult = timterp(src, dst) % timterp -- Interpolation of time data. % timterp(src, dst) interpolates the "src" data % at the times corresponding to the "dst" data. % The "src" and "dst" are NetCDF variables, both % of whose leftmost dimension represents the same % kind of time, possibly EPIC time with "time" % (Julian Day) augmented by "time2" (ms). % The result is a Matlab array of interpolated values, % oriented in the same fashion as the "dst" data. If % no output argument is given, the result is silently % assigned to "ans" in the caller's workspace. % Fran Hotchkiss and Chuck Denham, USGS. % Version of 22-Dec-1997 14:31:10. if nargin < 2, help(mfilename), return, end SEC_PER_DAY = 86400; % 24 * 60 * 60. MSEC_PER_DAY = 86400000; % 24 * 60 * 60 * 1000. % Time for "src". fsrc = parent(src); d = dim(src); tsrc = fsrc{'time'}; t2src = fsrc{'time2'}; if ~isempty(t2src) % EPIC. tsrc = ep_datenum([tsrc(:) t2src(:)]); % Decimal days. elseif ~isempty(tsrc.base_date) % Seconds + base_date. b = tsrc.base_date(:); t2src = datenum(b(1), b(2), b(3), b(4), b(5), b(6)); tsrc = t2src + tsrc(:) / SEC_PER_DAY; else tsrc = tsrc(:); % Unknown units. end % Time for "dst". fdst = parent(dst); d = dim(dst); tdst = fdst{'time'}; t2dst = fdst{'time2'}; if ~isempty(t2dst) % EPIC. tdst = ep_datenum([tdst(:) t2dst(:)]); % Decimal days. elseif ~isempty(tdst.base_date) % Seconds + base_date. b = tdst.base_date(:); t2dst = datenum(b(1), b(2), b(3), b(4), b(5), b(6)); tdst = t2dst + tdst(:) / SEC_PER_DAY; else tdst = tdst(:); % Unknown units. end % Set-up for Matlab "interp1" function. x = tsrc; y = src(:); xi = tdst; yi = interp1(x, y, xi, 'linear'); % Output. if nargout > 0 theResult = yi; else assignin('caller', 'ans', yi) end