function Xout = avgoasis(Xin, Lon, TimeLim) % AVGOASIS - Create a daily average of data in oasis files % % Use as: avgoasis(X, Lon, TimeLim) % avgoasis(X) % No accounting offset, use data between 10am and 2pm % avgoasis(X,[], TimeLim)% No accounting offset, specify TimeLims % avgoasis(X,[]) % No accounting offset, use data between 10am and 2pm % % Inputs: X = matrix of data (from READOASIS), 1st column is julian day, % last column is year. % Lon = Longitude of readings (scalar) used to calculate the offset % from GMT. Default = 'no GMT offset % TimeLims = 2 element vector of hours to average between. Default = [10 14] % This feature is used for spectral data. % It may also be a scalar (fractional day) in which case the Local noon is calcualted by % using NOON_. Then TimeLim specifies the time on either side of noon to average between. % % Outputs: Xo = daily average of column wise data. % % Requires: GMTOFFSET, JUL2DATE, Oceans toolbox % Brian Schlining % 17 May 1999 if nargin < 2 | isempty(flag) | isnan(flag) Lon = 0; end %========================================= % Error Checking on the TimeLims argument %========================================= if nargin < 3 % Default StartTime = 10; EndTime = 14; else if length(TimeLim) > 1 if TimeLim(1) > TimeLim(2) error('AVGOASIS: TimeLims must be [smaller larger]') end if TimeLim(2) > 24 | TimeLim(1) < 0 error('AVGOASIS: Invalid Time limit') end StartTime = TimeLim(1); EndTime = TimeLim(2); Flag = 0; else Flag = 1; end end switch Lon case 0 Offset = 0; otherwise Offset = gmtoffset(Lon) % Calculate the ofset in days from GMT end %=================================== % Get the date number of each point %=================================== d = jul2date(Xin(:,1),Xin(:,end)); % Convert Julian day to matlab's format GoodDate = find(~isnan(d)); d = d(GoodDate); Xin = Xin(GoodDate,:); [d, DateOrder] = sortrows(d); % Make sure the data is sorted by time Xin = Xin(DateOrder,:); [Year, Month, Day, Hour, Minute, Second] = datevec(d + Offset); n = 1; % Counter for the start of each 15 minute data set m = 1; % The position we're writing the data to [NumOfPoints NumOfFields] = size(Xin); Xout = ones(size(Xin))*NaN; % Preallocate memory fprintf(1,'(AVGOASIS)') OK = 1; while OK %====================================================== % Create a daily average for data between 10am and 2pm %====================================================== % Find all data points between 10:00 and 14:00 for each day j = find(Year == Year(n) & Month == Month(n) & Day == Day(n) & ... Hour >= StartTime & Hour <= EndTime); % Move pointer to the next day k = find(Year == Year(n) & Month == Month(n) & Day == Day(n)); if ~isempty(j) % Get the data that occurs during each minute, take its average and then % write it out to the new structure, OUT. if length(j) == 1 Xout(m,:) = Xin(j,:); else Xout(m,:) = nanmean(Xin(j,:)); end n = n + length(k); m = m + 1; elseif isempty(j) & ~isempty(k) n = n + length(k)'; else n = n + 1; end if ~rem(m,200) fprintf(1,'.') end if n > NumOfPoints fprintf(1,'\n') % Remove all the trailing NaN's before returning OUT NotNaN = find(~isnan(Xout(:,1))); Xout = Xout(NotNaN,:); return end end