%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% SUB-FUNCTIONS %% %% USED BY: SURFBET2OUTCROPS %% %% INTBET2OUTCROPS %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % USE DIRECTLY AT YOUR OWN RISK ! % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Master sub-function: function varargout = subfct_getsurf(CHP,Y,X,LIMITS) % Limits: %disp(strcat('Limits: ',num2str(LIMITS))); O = LIMITS(1); MY = sort( LIMITS(2:3) ); MX = sort( LIMITS(4:5) ); % Compute the surface: [S Smat dS] = getsurf(Y,X,O,MY,MX,CHP); % Outputs: switch nargout case 1 varargout(1) = {S}; case 2 varargout(1) = {S}; varargout(2) = {Smat}; case 3 varargout(1) = {S}; varargout(2) = {Smat}; varargout(3) = {dS}; end %switch nargout %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % This function computes the surface limited southward by % MY(1), northward by iso-O (or MY(2) if iso-O reaches it), % eastward by MX(1), westward by MX(2) % % Updates: % 20060615: add a default meridional gradient (negative) % test on x,y limits % function varargout = getsurf(Y,X,O,MY,MX,CHP) %% Dim: ny = length(Y); nx = length(X); %disp(num2str([ny nx])); %% Indices: iymin = max( find( YMY(2) ) ); if isempty(iymax),iymax=ny;end; ixmin = max( find( XMX(2) ) ); if isempty(ixmax),ixmax=nx;end; %disp(num2str([iymin iymax ixmin ixmax])); %% 1- determine the 2D matrix of surface elements defined by % the grid: dS = getdS(Y,X); %% 2- compute the 2D surface matrix where 1 means dS must be % counted and 0 must not: S = ones(ny,nx); % initialy keep all points % Exclude northward iso-O limits: % NB: here the test depends on the meridional gradient of CHP % if CHP increase (resp. decreases) with LAT then we must % keep lower (resp. higher) values than iso-O limit % a: determine test type: N = iymax - iymin + 1; % Number of Y points in the domain CHPsouth = nanmean(nanmean(squeeze(CHP(iymin:iymin+fix(N/2),ixmin:ixmax)))); CHPnorth = nanmean(nanmean(squeeze(CHP(iymin+fix(N/2):iymax,ixmin:ixmax)))); SNgrad = (CHPnorth - CHPsouth)./abs(CHPnorth - CHPsouth); if isnan(SNgrad), SNgrad=-1; end % Assume negative gradient by default %disp(strcat('Northward gradient sign is:',num2str(SNgrad))); switch SNgrad case 1, testype = 'le'; % Less than or equal case -1, testype = 'ge'; % Greater than or equal end %switch % b: exclude points S = double(feval(testype,CHP,O)); % Exclude southward limit: S(1:iymin,:) = zeros(iymin,nx); % Exclude northward limit: S(iymax:ny,:) = zeros((ny-iymax)+1,nx); % Exclude westward limit: S(:,1:ixmin) = zeros(ny,ixmin); % Exclude eastward limit: S(:,ixmax:nx) = zeros(ny,(nx-ixmax)+1); %% 3- Then compute the surface by summing dS elements % for non nul S points Skeep = S.*dS; Skeep = sum(sum(sum(Skeep))); %% 4- Outputs: switch nargout case 1 varargout(1) = {Skeep}; % Surface single value case 2 varargout(1) = {Skeep}; varargout(2) = {S}; % Logical S matrix with included/excluded points case 3 varargout(1) = {Skeep}; varargout(2) = {S}; varargout(3) = {dS}; % Surface elements matrix end %switch nargout %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % This function computes the 2D dS surface elements. function ds = getdS(Y,X); ny = length(Y); nx = length(X); %%% Compute the DY: % Assuming Y is independant of ix: d = m_lldist([1 1]*X(1),Y); dy = [d(1)/2 (d(2:length(d))+d(1:length(d)-1))/2 d(length(d))/2]; dy = meshgrid(dy,X)'; %%% Compute the DX: clear d for iy = 1 : ny d(:,iy) = m_lldist(X,Y([iy iy])); end dx = [d(1,:)/2 ; ( d(2:size(d,1),:) + d(1:size(d,1)-1,:) )./2 ; d(size(d,1),:)/2]; dx = dx'; %% Compute the horizontal DS surface element: ds = dx.*dy;