READZ500

Reads Height fields at 500mb (Z500) for the
the region chosen
%Input:
%         flat      lat for field  [-20 20]
%         flon      lon for field  [150 270]
%         season    Season to be processed '
%

function [z,ind,fcos]=readz500(flat,flon,varargin)
%Usage:
% [z,ind,fcos]=readz500([-90 90],[0 360],'season','jfm','scaling','no');


%Set defaults
%
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 readz500');
                end
            case 'scaling'
                scaling = 'yes';
        end
    end
end



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


siz=size(field);
lat1=field(:,2) > flat(1);
lat2=field(:,2) < flat(2);
lon1=field(:,3) >= flon(1);
lon2=field(:,3) <= flon(2);
maskf=lat1.*lat2.*lon1.*lon2;
ind=find(maskf == 1 );

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

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


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

return