Ice Sheet System Model  4.18
Code documentation
Xy2llx.cpp
Go to the documentation of this file.
1 
4 #include "../../shared/shared.h"
5 #include "./latlong.h"
6 #include <math.h>
7 
8 int Xy2llx(double* lat, double* lon, double* x, double* y, int ncoord, int sgn){
9 /* This is a cpp conversion of the following:
10 %XY2LL - converts xy to lat long
11 %
12 % Converts Polar Stereographic (X,Y) coordinates for the polar regions to
13 % latitude and longitude Stereographic (X,Y) coordinates for the polar
14 % regions.
15 % Author: Michael P. Schodlok, December 2003 (map2xy.m)
16 %
17 % Usage:
18 % [lat,lon] = xy2ll(x,y,sgn);
19 % [lat,lon] = xy2ll(x,y,sgn,central_meridian,standard_parallel);
20 %
21 % - sgn = Sign of latitude +1 : north latitude (default is mer=45 lat=70)
22 % -1 : south latitude (default is mer=0 lat=71)
23 */
24  double central_meridian,standard_parallel;
25 
26  Xy2lldef(&central_meridian,&standard_parallel,sgn);
27 
28  return(Xy2llx(lat,lon,x,y,ncoord,sgn,central_meridian,standard_parallel));
29 }
30 
31 int Xy2llx(double* lat, double* lon, double* x, double* y, int ncoord, int sgn, double central_meridian, double standard_parallel){
32 /* This is a cpp conversion of the following:
33 %XY2LL - converts xy to lat long
34 %
35 % Converts Polar Stereographic (X,Y) coordinates for the polar regions to
36 % latitude and longitude Stereographic (X,Y) coordinates for the polar
37 % regions.
38 % Author: Michael P. Schodlok, December 2003 (map2xy.m)
39 %
40 % Usage:
41 % [lat,lon] = xy2ll(x,y,sgn);
42 % [lat,lon] = xy2ll(x,y,sgn,central_meridian,standard_parallel);
43 %
44 % - sgn = Sign of latitude +1 : north latitude (default is mer=45 lat=70)
45 % -1 : south latitude (default is mer=0 lat=71)
46 */
47 
48  int i,iret=0;
49  double delta,slat;
50  double re,ex2,ex;
51  double sl,rho,cm,T,chi;
52 
53  if((sgn!=1) && (sgn!=-1)) _error_("Sign should be either +1 or -1.\n");
54 
55  delta = central_meridian;
56  slat = standard_parallel;
57 
58  /* Radius of the earth in meters */
59  re = 6378.273e+3;
60  /* Eccentricity of the Hughes ellipsoid squared */
61  ex2 = 0.006693883;
62  /* Eccentricity of the Hughes ellipsoid */
63  ex = sqrt(ex2);
64 
65  /* loop over all the coordinate pairs */
66  for(i=0; i<ncoord; i++){
67  sl = slat*PI/180.;
68  cm = cos(sl)/sqrt(1.0-ex2*(pow(sin(sl),2)));
69  rho= sqrt(pow(x[i],2) + pow(y[i],2));
70  T = tan((PI/4.0) - (sl/2.0))/pow(((1.0-ex*sin(sl))/(1.0+ex*sin(sl))),(ex/2.0));
71 
72  if(fabs(slat-90.) < 1.e-5)
73  T =rho*sqrt(pow((1.+ex),(1.+ex))*pow((1.-ex),(1.-ex)))/2./re;
74  else
75  T =rho*T/(re*cm);
76 
77  chi = (PI / 2.0) - 2.0 * atan(T);
78  lat[i] = chi + ((ex2 / 2.0) + (5.0 * pow(ex2,2.0) / 24.0) + (pow(ex2,3.0) / 12.0)) *
79  sin(2.0 * chi) + ((7.0 * pow(ex2,2.0) / 48.0) + (29.0 * pow(ex2,3.0) / 240.0)) *
80  sin(4.0 * chi) + (7.0 * pow(ex2,3.0) / 120.0) * sin(6.0 * chi) ;
81 
82  lat[i] = (double)sgn * lat[i];
83  lon[i] = atan2((double)sgn * x[i],-(double)sgn * y[i]);
84  lon[i] = (double)sgn * lon[i];
85 
86  if(rho <= 0.1){
87  lat[i] = 90. * (double)sgn;
88  lon[i] = 0.0;
89  iret=1;
90  }
91 
92  lon[i] = lon[i] * 180. / PI;
93  lat[i] = lat[i] * 180. / PI;
94  lon[i] = lon[i] - delta;
95  }
96 
97  return(iret);
98 }
99 
100 void Xy2lldef(double* pdelta, double* pslat, int sgn){
101 /* This is a cpp conversion of the following:
102 %XY2LL - converts xy to lat long
103 %
104 % Converts Polar Stereographic (X,Y) coordinates for the polar regions to
105 % latitude and longitude Stereographic (X,Y) coordinates for the polar
106 % regions.
107 % Author: Michael P. Schodlok, December 2003 (map2xy.m)
108 %
109 % Usage:
110 % [lat,lon] = xy2ll(x,y,sgn);
111 % [lat,lon] = xy2ll(x,y,sgn,central_meridian,standard_parallel);
112 %
113 % - sgn = Sign of latitude +1 : north latitude (default is mer=45 lat=70)
114 % -1 : south latitude (default is mer=0 lat=71)
115 */
116  bool flag=true;
117 
118  /* Get central_meridian and standard_parallel depending on hemisphere */
119  if (sgn == 1) {
120  *pdelta= 45;
121  *pslat = 70;
122  if(flag) _printf0_("Warning: expecting coordinates in polar stereographic (Std Latitude: 70N Meridian: 45).\n");
123  }
124  else if (sgn == -1) {
125  *pdelta= 0;
126  *pslat = 71;
127  if(flag) _printf0_("Warning: expecting coordinates in polar stereographic (Std Latitude: 71S Meridian: 0).\n");
128  }
129  else _error_("Sign should be either +1 or -1.\n");
130 
131  return;
132 }
_printf0_
#define _printf0_(StreamArgs)
Definition: Print.h:29
Xy2lldef
void Xy2lldef(double *pdelta, double *pslat, int sgn)
Definition: Xy2llx.cpp:100
Xy2llx
int Xy2llx(double *lat, double *lon, double *x, double *y, int ncoord, int sgn)
Definition: Xy2llx.cpp:8
latlong.h
prototypes for latlong.h
PI
const double PI
Definition: constants.h:11
_error_
#define _error_(StreamArgs)
Definition: exceptions.h:49