function [dxout,dyout,azmout,rgeout,varout] = do_polar_conversion2(dxin,dyin,varin,azmin,radin)

% Usage: [dxout,dyout,azmout,rgeout,varout] = do_polar_conversion2(dxin,dyin,varin,azmin,radin);
%
% Written by Altug Aksoy, October 2011
%
% Convert any given 2d variable defined on Cartesian coordinates to
% polar coordinates at - this version does not require a WRF file to read
% latitude/longitude information, but requires to supply (dx,dy) Cartesian
% distance vectors or arrays on which the variable varin is valid.
%
% dxin:     X distance Cartesian coordinate (km) for varin
% dyin:     Y distance Cartesian coordinate (km) for varin
% varin:    Input 2d variable on Cartesian grid
% azmin:    Azimuth values in degrees (0-360) - 1d vector or 2d mesh values
% radin:    Radial values in km - 1d vector or 2d mesh values
%
% Output variables:
% dxout:    x distance in polar coordinates
% dyout:    y distance in polar coordinates
% azmout:   Azimuth (degrees) in polar coordinates
% rgeout:   Range (km) in polar coordinates
% varout:   Varible in polar coordinates

dxout  = [];
dyout  = [];
azmout = [];
rgeout = [];
varout = [];

if (nargin ~= 5)
    warning('Following error occurred:')
    disp(['This function expects 5 input arguments as follows:']);
    disp(['    [dxout,dyout,azmout,rgeout,varout] = do_polar_conversion2(dxin,dyin,varin,azmin,radin);']);
    return
end


% Convert dx/dy vectors to 2-d gridded Cartesian arrays
if (isvector(dxin) & ~isvector(dyin)) | (~isvector(dxin) & isvector(dyin))
    warning('Error in do_polar_conversion2: dxin and dyin should both be vectors or 2d mesh arrays.')
    return
elseif isvector(dxin)
    % One is vector -> both are vectors -> Convert to mesh arrays here
    [dy,dx] = ndgrid(dyin,dxin);
else
    % Use specified arrays
    dx = dxin;
    dy = dyin;
end

% Convert azimuth and radial vectors to 2-d gridded Cartesian arrays
if (isvector(azmin) & ~isvector(radin)) | (~isvector(azmin) & isvector(radin))
    warning('Error in do_polar_conversion: azmin and radin should both be vectors or 2d mesh arrays.')
    return
elseif isvector(azmin)
    % One is vector -> both are vectors -> Convert to mesh arrays here
    [azmout,rgeout]   = meshgrid(deg2rad(azmin),radin);
    [distlon,distlat] = pol2cart(azmout,rgeout);
else
    % Use specified arrays
    [distlon,distlat] = pol2cart(deg2rad(azmin),radin);
end

% Interpolate to the gridded polar-equivalent Cartesian distances
varout = interp2(dx,dy,varin,distlon,distlat);

% Interpolate grid distances to polar coordinates
dxout  = griddata(dx,dy,dx,distlon,distlat);
dyout  = griddata(dx,dy,dy,distlon,distlat);