 clear all
function [x,y]=polarstereo_fwd(phi,lambda,a,e,phi_c,lambda_0)
%POLARSTEREO_FWD transforms lat/lon data to map coordinates for a polar stereographic system
%   [X,Y]=POLARSTEREO_FWD(LAT,LONG,EARTHRADIUS,ECCENTRICITY,LAT_TRUE,LON_POSY)
%   X and Y are the map coordinates (scalars, vectors, or matrices of equal size).
%   LAT and LON are in decimal degrees with negative numbers (-) for S and W.
%   EARTHRADIUS is the radius of the earth defined in the projection
%       (default is 6378137.0 m, WGS84)
%   ECCENTRICITY is the earth's misshapenness
%       (default is 0.08181919)
%   LAT_TRUE is the latitude of true scale in degrees, aka standard parallel
%       (default is -70). Note that some NSIDC data use 70 and some use 71.
%   LON_POSY is the meridian in degrees along the positive Y axis of the map
%       (default is 0)
%
%   The National Snow and Ice Data Center (NSIDC) and Scientific Committee
%   on Antarctic Research (SCAR) use a version of the polar stereographic
%   projection that Matlab does not have. This file does transformations to
%   map coordinates from geographic coordinates to facilitate
%   comparisons with other datasets.
%
%   Equations from: Map Projections - A Working manual - by J.P. Snyder. 1987
%   http://kartoweb.itc.nl/geometrics/Publications/Map%20Projections%20-%20A%20Working%20manual%20-%20by%20J.P.%20Snyder.pdf
%   See the section on Polar Stereographic, with a south polar aspect and
%   known phi_c not at the pole.
%
%   See also: MINVTRAN, MFWDTRAN.
%
%   Written by Andy Bliss, 9/12/2011
%   Changes since version 01:
%       1. Split into two functions and vectorized code.
%%%%%%%%%%%%%
%some standard info
%%%%%%%%%%%%%
%WGS84 - radius: 6378137.0 eccentricity: 0.08181919
%   in Matlab: axes2ecc(6378137.0, 6356752.3142)
%Hughes ellipsoid - radius: 6378.273 km eccentricity: 0.081816153
%   Used for SSM/I  http://nsidc.org/data/polar_stereo/ps_grids.html
%International ellipsoid (following Snyder) - radius: 6378388.0 eccentricity: 0.0819919
%{
%check the code using Snyder's example. Should get x=-1540033.6; y=-560526.4;
phi=-75; lambda=150;
[x,y]=polarstereo_fwd(phi,lambda,6378388.0,0.0819919,-71,-100);
x,y
%%%%%%%%%%%%
%check with AntDEM
%%%%%%%%%%%%
%http://nsidc.org/data/docs/daac/nsidc0304_0305_glas_dems.gd.html
% Center Point of Corner Grid Cell
%x	y	Latitude	Longitude
test=[-2812000.0  2299500.0   -57.3452815 -50.7255753
    2863500.0   2299500.0   -57.0043684 51.2342036
    -2812000.0  -2384000.0  -56.8847122 -130.2911169
    2863500.0   -2384000.0  -56.5495152  129.7789915];
[x,y]=polarstereo_fwd(test(:,3),test(:,4),6378137.0,axes2ecc(6378137.0, 6356752.3),-70,0);
figure,hold on,plot(test(:,1),test(:,2),'.'),plot(x,y,'r+')
[test(:,1) test(:,1)-x],[test(:,2) test(:,2)-y]
%error is less than half a meter (probably just round-off error).
%%%%%%%%%%%%
%check with Greenland
%%%%%%%%%%%%
%projected from the WGS 84 Ellipsoid, with 70° N as the latitude of true scale and a rotation of 45.
test=[-890000.0 -629000.0 79.9641229 -99.7495626 %center point of cell
    1720000.0 -629000.0 73.2101234 24.9126514
    -890000.0 -3410000.0 58.2706251 -59.6277136
    1720000.0 -3410000.0 55.7592932 -18.2336765];
[x,y]=polarstereo_fwd(test(:,3),test(:,4),6378273,0.081816153,70,-45); %slightly off
[x2,y2]=polarstereo_fwd(test(:,3),test(:,4),6378137.0,0.08181919,70,-45); %correct
figure,hold on,plot(test(:,1),test(:,2),'.'),plot(x,y,'r+'),plot(x2,y2,'gx')
[test(:,1) test(:,1)-x test(:,1)-x2],[test(:,2) test(:,2)-y test(:,2)-y2]
%error is less than half a meter (probably just round-off error).
%}
%%%%%%%%%%%%
%input checking
%%%%%%%%%%%%
if nargin < 3 || isempty(a)
    a=6378137.0; %radius of ellipsoid, WGS84
endif
if nargin < 4 || isempty(e)
    e=0.08181919; %eccentricity, WGS84
endif
if nargin < 5 || isempty(phi_c)
    phi_c=-70; %standard parallel, latitude of true scale
endif
if nargin < 6 || isempty(lambda_0)
    lambda_0=0; %meridian along positive Y axis
endif
%convert to radians
phi=deg2rad(phi);
phi_c=deg2rad(phi_c);
lambda=deg2rad(lambda);
lambda_0=deg2rad(lambda_0);
%if the standard parallel is in S.Hemi., switch signs.
if phi_c < 0
    pm=-1; %plus or minus, north lat. or south
    phi=-phi;
    phi_c=-phi_c;
    lambda=-lambda;
    lambda_0=-lambda_0;
else
    pm=1;
endif
%this is not commented very well. See Snyder for details.
t=tan(pi/4-phi/2)./((1-e*sin(phi))./(1+e*sin(phi))).^(e/2);
% t_alt=sqrt((1-sin(phi))./(1+sin(phi)).*((1+e*sin(phi))./(1-e*sin(phi))).^e);
t_c=tan(pi/4 - phi_c/2)./((1-e*sin(phi_c))./(1+e*sin(phi_c))).^(e/2);
m_c=cos(phi_c)./sqrt(1-e^2*(sin(phi_c)).^2);
rho=a*m_c*t/t_c; %true scale at lat phi_c
m=cos(phi)./sqrt(1-e^2*(sin(phi)).^2);
x=pm*rho.*sin(lambda-lambda_0);
y=-pm*rho.*cos(lambda - lambda_0);
k=rho./(a*m);

end
xw=0;
xe=360;
ys=-70;
yn=-30;
LONG=-180:0.25:180;
nlon=length(LONG);
EARTHRADIUS=6378137.0;
ECCENTRICITY=0.08181919;
LAT_TRUE=-71;
LON_POSY=0;
lat=-70:0.25:-30;

load coastline
xc=C.coastlon;
yc=C.coastlat;
xc=xc(yc<=-30);
yc=yc(yc<=-30);
[Xc,Yc]=polarstereo_fwd(yc,xc,EARTHRADIUS,ECCENTRICITY,LAT_TRUE,LON_POSY);

spec='bz';
name='ACC_final_';
load ([name spec '_field_and_sem'])
bz=ogmf;
%XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[Xr,Yr]=meshgrid(LONG,lat);
bz(:,nlon)=(bz(:,nlon-1)+bz(:,1))/2; bz(:,1)=bz(:,nlon);
bzn(:,1:(nlon-1)/2+1)=bz(:,(nlon-1)/2+1:nlon);
bzn(:,(nlon-1)/2+2:nlon)=bz(:,1:(nlon-1)/2);
bzn(:,nlon)=(bzn(:,nlon-1)+bzn(:,1))/2; bzn(:,1)=bzn(:,nlon);
[X,Y]=polarstereo_fwd(Yr,Xr,EARTHRADIUS,ECCENTRICITY,LAT_TRUE,LON_POSY);
%B=zeros(61,nlon);
%B(1:20,:)=-0.2; %nonzero small background over Antarctica for smooth picture
%B(21:61,:)=bzn;
B=bzn;
B(:,nlon)=(B(:,nlon-1)+B(:,1))/2;
B(:,   1)=(B(:,nlon)+B(:,2))/2;
##bm=mean(B');
##for j=1:161
##B(j,:)=B(j,:)-bm(j);
##endfor
load ACC_zero_bz_line  %% Xn Yn Xs Ys

figure
colormap('jet')
contourf(X,Y,B,30)
axis off
hold on
plot(Xc,Yc,'k.')
minx=min(min(X));
maxx=max(max(X));
miny=min(min(Y));
maxy=max(max(Y));

line([minx maxx],[0 0],'Color','k')
line([0, 0],[minx maxx],'Color','k')
text(-80000,1.06*maxy,'0^o','FontSize',10)
text(-400000,1.05*miny,'180^o','FontSize',10)
text(1.20*minx,20000,'90^oW','FontSize',10)
text(1.02*maxx,20000,'90^oE','FontSize',10)
text(1.5*maxx,20000,'nT','FontSize',12)
colorbar

##Xn(720:800)=Xn(800);
##Yn(720:800)=Yn(800);
##plot(Xn,Yn,'w.')
##hold on
##%plot(Xn(720:800),Yn(720:800),'w.')
##Xs(1:721)=Xs(721); Ys(1:721)=Ys(721);
##Xs(900:nlon)=Xs(nlon);  Ys(900:nlon)=Ys(nlon);
##plot(Xs,Ys,'w.')
##%plot(Xs(1:721),Ys(1:721),'w.')
##%plot(Xs(900:nlon),Ys(900:nlon),'w.')

Xn(710:800)=Xn(800);
Yn(710:800)=Yn(800);
Xn(end)=Xs(721);
plot(Xn,Yn,'w.')
%plot(Xs(721:930),Ys(721:930),'w.')

print -djpg ACC_final_bz_plot


