READSST

Reads SST fileds at 500mb (Z500) for the
the region chosen
function [z,ind,fcos]=readsst(flat,flon,varargin)
%Input:
%         flat      lat for field  [-20 20]
%         flon      lon for field  [150 270]
%         season    Season to be processed '
%
%Usage:
% [s,inds,fcos]=readsst([-90 90],[0 360],'season','jfm','scaling','no');


%Set def,aults
%
choice = 1;
scaling = 'yes';
sesint=4;

for i = 1:length(varargin)

    if ischar(varargin{i});
        option=varargin{i};
        switch option;
            case 'season'
                switch varargin{i+1}
                    case 'jfm'
                        choice = 1;
                    case 'amj'
                        choice = 2;
                    case 'jas'
                        choice = 3;
                    case 'ond'
                        choice = 4;
                    case 'all'
                        choice=1;  sesint=1;
                    otherwise;
                        error('Wrong season specified in readsst');
                end
            case 'scale'
                scaling = 'yes';
        end
    end
end



% choose format
%
if min(flon) < 0
    format='wardinv';
else
    format='ward';
end
%
sea=infile([ ], './TS0.1.408.dat', ...
    './TS0.1.408.ctl','sst',0,format,0,34);

%
%
% Define area of EOF for sst, assumes
% column 2 and 3 are lat/lon
%
% compute eof of the seasonal mean over the area
%
siz=size(sea);
lat1=sea(:,2) > flat(1);
lat2=sea(:,2) < flat(2);
lon1=sea(:,3) >= flon(1);
lon2=sea(:,3) <= flon(2);
mask=lat1.*lat2.*lon1.*lon2;
%
%  compute landsea mask
[x,y,lsm]=makeggrid('T30');
% lsmmask=reshape(flipud((1-(lsm+1)/2))',[1 96*48])';
lsmmask=reshape(((1-(lsm+1)/2))',[1 96*48])';
clear x y;
%
mask=mask.*lsmmask;
ind=find(mask == 1 );



%
% cut the region
%
%  Scale field with cos(lat)
%
% Choose scaling

switch scaling
    case 'yes';
        fcos= cos(sea(ind,2)*pi/180.) ;
    case 'no';
        fcos=1;
end


%
z=[];
fcut=zeros(size(sea(ind,4:siz(2))));
for icos=4:siz(2)
    fcut(:,icos-3)=fcos.*sea(ind,icos);
end
sizfc=size(fcut);
z= [ z fcut(:,choice:sesint:sizfc(2)) ];


return