function [x,y,netrain] = rainswathx(nt)
%
%  This script calculates the distribution of accumulated precipitation, in
%  mm, for a given individual storm.
%
%  nt is the track number of the storm
%
%  x and y are vectors containing the longitudes and latitudes of the grid
%
%  netrain is the storm total rainfall (mm) at each point on the grid
%
%  The grid resolution and dimensions can be set automatically or
%  specified in the swath map section of params.m.

%  Note:  If discontinuities appear in the result, increase the parameter
%  "timelength" in params.m.
%
%       January, 2009
%       Copyright 2009 WindRiskTech L.L.C.
%       Revised December 2020 to remove cos(latitude) factor from hy and to
%       allow for higher resolution topography
%
%-------------------------------------------------------------------------
%
params    %  Load parameters
%
load temp vstore vnet vsestore latstore longstore rmstore rmsestore ut vt
load temp shape city_radius clat clong T600store u850store v850store
if strcmp(shape,'poly')
    load temp polytype xint yint kint 
end    
load sorted %#ok<LOAD>
%
if strcmpi(topores,'high')
    load bathymetry_high
else
    load bathymetry
end    
[ntopo,~]=size(bathy);
topores=360/ntopo;
toporesi=1/topores;
%
pifac=acos(-1)/180;
knotfac=1852./3600;
sfac=1./(topores.*60.*1852);
m_to_mm=1000;
rowa_over_rowl=0.00117;
%
if bxmin < 0 %#ok<*NODEF>
    bxmin=bxmin+360;
end
if bxmax < 0
    bxmax=bxmax+360;
end    
%
[~,m]=size(vstore);
%
clear utd vtd
latsize=size(nonzeros(latstore(nt,:)));
utd(1,:)=ut(nt,1:latsize);
vtd(1,:)=vt(nt,1:latsize);
ush=0.*utd;
vsh=ush;
vdrift=1.5.*3600./1852;
vdrift=vdrift.*latstore(1,1)./(abs(latstore(1,1))+1e-8);
if exist('u850store','var') == 1
    ush(1,:)=5.*knotfac.*(utd-u850store(nt,1:latsize));
    vsh(1,:)=5.*knotfac.*(vtd-vdrift.*cos(pifac.*latstore(nt,1:latsize))- ...
        v850store(nt,1:latsize));
end    
lat(1,:)=latstore(nt,1:latsize);
long(1,:)=longstore(nt,1:latsize);
v(1,:)=vstore(nt,1:latsize);
vse(1,:)=vsestore(nt,1:latsize);
%  
% Scale and randomize radii of maximum wind
%
[nrm,mrm]=size(rmstore);
[~, jmaxd]=min(vstore,[],2);
jmaxd(vstore(:,end)~=0)=m;
jmaxd=jmaxd-1;
jmaxd=transpose(jmaxd);
rfac=zeros(nrm,mrm)+1;
if strcmp(randfac,'y')
    rfac=outer(nrm,mrm);
end  
%
rm(1,:)=transpose(nonzeros(magfac.*rfac(nt,:).*rmstore(nt,:)));
nrm=max(size(rm));
rmse(1,:)=magfac.*rfac(nt,1:nrm).*rmsestore(nt,1:nrm);
%
for i=1:jmaxd(nt)
    if long(1) > 200 && long(i) < 50
        long(i)=long(i)+360;
    end
end    
%
if strcmp(bound,'auto')
    if (strcmp(shape,'circ') && city_radius > 2000) || (strcmp(shape,'poly') && strcmp(polytype,'closed'))
        bxmin=min(nonzeros(long))-deltax;
        bxmax=max(nonzeros(long))+deltax;
        bymin=min(nonzeros(lat))-deltay;
        bymax=max(nonzeros(lat))+deltay;
    else    
        if strcmp(bound,'auto')
            if strcmp(shape,'circ')
                bxmin=clong-deltax;
                bxmax=clong+deltax;
                bymin=clat-deltay;
                bymax=clat+deltay;
            else
                [~,xsize]=size(xint);
                vtemp=0;
                ixmax=1; 
                for ix=1:xsize
                    if kint(nt,ix) > 0 && vnet(nt,kint(nt,ix)) > vtemp
                        vtemp=vnet(nt,kint(nt,ix));
                        ixmax=ix;
                    end
                end    
                xm=xint(nt,ixmax);
                ym=yint(nt,ixmax);
                bxmin=xm-deltax;
                bxmin=max(bxmin,0);
                bxmax=xm+deltax;
                bymin=ym-deltay;
                bymax=ym+deltay;
            end
        end  
    end
end
%
x=(bxmin:dellatlongs:bxmax);
y=(bymin:dellatlongs:bymax);
sx=max(size(x));
sy=max(size(y));
%  This statement added because in pointwshort it is assumed that if sx=sy
%  POI's are a vector rather than a grid.
if sx == sy
    sx=sx+1;
    x=[x bxmax+dellatlongs];
end    
%
h=zeros(sx,sy);
hx=zeros(sx,sy);
hy=zeros(sx,sy);
%
bathy=max(bathy,-1);
dhdx=sfac*(circshift(bathy,-1,1)-circshift(bathy,0,1));
dhdy=sfac*(circshift(bathy,-1,2)-circshift(bathy,0,2));
%
for i=1:sx
    plong=x(i);
    for j=1:sy
        plat=y(j);
        %
        ib=1+floor(toporesi.*plong);
        ibp=ib+1;
        if ibp > ntopo
            ibp=ibp-ntopo;
        end    
        ibs=1+floor(toporesi.*plong-0.5);
        ibsp=ibs+1;
        plongs=plong;
        if ibs < 0
            ibs=ntopo;
            plongs=plong+360;
        end    
        if ibsp > ntopo
            ibsp=ibsp-ntopo;
        end
        jb=1+floor(toporesi.*(plat+90));
        jbs=1+floor(toporesi.*(plat+90)-0.5);
        b1=bathy(ib,jb);
        b2=bathy(ib,jb+1);
        b3=bathy(ibp,jb);
        b4=bathy(ibp,jb+1);
        dely=1+toporesi.*(plat+90)-jb;
        delx=1+toporesi.*plong-ib;
        d1=(1.-delx).*(1.-dely);
        d2=dely.*(1.-delx);
        d3=delx.*(1.-dely);
        d4=delx.*dely;
        h(i,j)=exp(d1.*log(b1+11)+d2.*log(b2+11)+d3.*log(b3+11)+d4.*log(b4+11))-11;
        %
        b1=dhdx(ibs,jbs);
        b2=dhdx(ibs,jbs+1);
        b3=dhdx(ibsp,jbs);
        b4=dhdx(ibsp,jbs+1);
        dely=0.5+toporesi.*(plat+90)-jbs;
        delx=0.5+toporesi.*plongs-ibs;
        d1=(1.-delx).*(1.-dely);
        d2=dely.*(1.-delx);
        d3=delx.*(1.-dely);
        d4=delx.*dely;
        hx(i,j)=(b1.*d1+b2.*d2+b3.*d3+b4.*d4)./cos(pifac.*plat);
        %
        b1=dhdy(ibs,jbs);
        b2=dhdy(ibs,jbs+1);
        b3=dhdy(ibsp,jbs);
        b4=dhdy(ibsp,jbs+1);
        hy(i,j)=(b1.*d1+b2.*d2+b3.*d3+b4.*d4);    
        %
    end
end    
%
if exist('T600store','var')
    clear q900 q600
    [ q900, ~ ] = qs900b( T600store(nt,:), vstore(nt,:) );
    wq = pointwshortnq(lat,long,q900,v,rm,vse,rmse,utd,vtd,ush,vsh,y,x,h,hx,hy,timeres,wrad);
else
    w = pointwshortn(lat,long,v,rm,vse,rmse,utd,vtd,ush,vsh,y,x,h,hx,hy,timeres);
    wq=max(w-wrad,0).*q900;
    clear w
end    
temp=eprecip.*m_to_mm.*timeres.*3600.*rowa_over_rowl.*sum(wq,2);
netrain(:,:)=temp;
netrain=transpose(netrain);
%
end
