aboutsummaryrefslogtreecommitdiff
path: root/src/PJ_misrsom.c
blob: 19518a1db90e0240f1c00d17cc75532a1f067aa7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/******************************************************************************
 * This implements Space Oblique Mercator (SOM) projection, used by the
 * Multi-angle Imaging SpectroRadiometer (MISR) products, from the NASA EOS Terra
 * platform.
 *
 * The code is identical to that of Landsat SOM (PJ_lsat.c) with the following
 * parameter changes:
 *
 *   inclination angle = 98.30382 degrees
 *   period of revolution = 98.88 minutes
 *   ascending longitude = 129.3056 degrees - (360 / 233) * path_number
 *
 * and the following code change:
 *
 *   P->rlm = PI * (1. / 248. + .5161290322580645);
 *
 * changed to:
 *
 *   P->rlm = 0
 *
 *****************************************************************************/
/* based upon Snyder and Linck, USGS-NMD */
#define PROJ_PARMS__ \
    double a2, a4, b, c1, c3; \
    double q, t, u, w, p22, sa, ca, xj, rlm, rlm2;
#define PJ_LIB__
#include <projects.h>
PROJ_HEAD(misrsom, "Space oblique for MISR")
        "\n\tCyl, Sph&Ell\n\tpath=";
#define TOL 1e-7
#define PI_HALFPI 4.71238898038468985766
#define TWOPI_HALFPI 7.85398163397448309610

static void
seraz0(double lam, double mult, PJ *P) {
    double sdsq, h, s, fc, sd, sq, d__1;

    lam *= DEG_TO_RAD;
    sd = sin(lam);
    sdsq = sd * sd;
    s = P->p22 * P->sa * cos(lam) * sqrt((1. + P->t * sdsq) / ((
            1. + P->w * sdsq) * (1. + P->q * sdsq)));
    d__1 = 1. + P->q * sdsq;
    h = sqrt((1. + P->q * sdsq) / (1. + P->w * sdsq)) * ((1. +
            P->w * sdsq) / (d__1 * d__1) - P->p22 * P->ca);
    sq = sqrt(P->xj * P->xj + s * s);
    P->b += fc = mult * (h * P->xj - s * s) / sq;
    P->a2 += fc * cos(lam + lam);
    P->a4 += fc * cos(lam * 4.);
    fc = mult * s * (h + P->xj) / sq;
    P->c1 += fc * cos(lam);
    P->c3 += fc * cos(lam * 3.);
}
FORWARD(e_forward); /* ellipsoid */
    int l, nn;
    double lamt, xlam, sdsq, c, d, s, lamdp, phidp, lampp, tanph,
                lamtp, cl, sd, sp, fac, sav, tanphi;

    if (lp.phi > HALFPI)
        lp.phi = HALFPI;
    else if (lp.phi < -HALFPI)
        lp.phi = -HALFPI;
    lampp = lp.phi >= 0. ? HALFPI : PI_HALFPI;
    tanphi = tan(lp.phi);
    for (nn = 0;;) {
            sav = lampp;
            lamtp = lp.lam + P->p22 * lampp;
            cl = cos(lamtp);
            if (fabs(cl) < TOL)
                lamtp -= TOL;
            fac = lampp - sin(lampp) * (cl < 0. ? -HALFPI : HALFPI);
            for (l = 50; l; --l) {
                    lamt = lp.lam + P->p22 * sav;
                    if (fabs(c = cos(lamt)) < TOL)
                        lamt -= TOL;
                    xlam = (P->one_es * tanphi * P->sa + sin(lamt) * P->ca) / c;
                    lamdp = atan(xlam) + fac;
                    if (fabs(fabs(sav) - fabs(lamdp)) < TOL)
                        break;
                    sav = lamdp;
            }
            if (!l || ++nn >= 3 || (lamdp > P->rlm && lamdp < P->rlm2))
                    break;
            if (lamdp <= P->rlm)
                lampp = TWOPI_HALFPI;
            else if (lamdp >= P->rlm2)
                lampp = HALFPI;
    }
    if (l) {
            sp = sin(lp.phi);
            phidp = aasin(P->ctx,(P->one_es * P->ca * sp - P->sa * cos(lp.phi) *
                    sin(lamt)) / sqrt(1. - P->es * sp * sp));
            tanph = log(tan(FORTPI + .5 * phidp));
            sd = sin(lamdp);
            sdsq = sd * sd;
            s = P->p22 * P->sa * cos(lamdp) * sqrt((1. + P->t * sdsq)
                     / ((1. + P->w * sdsq) * (1. + P->q * sdsq)));
            d = sqrt(P->xj * P->xj + s * s);
            xy.x = P->b * lamdp + P->a2 * sin(2. * lamdp) + P->a4 *
                    sin(lamdp * 4.) - tanph * s / d;
            xy.y = P->c1 * sd + P->c3 * sin(lamdp * 3.) + tanph * P->xj / d;
    } else
            xy.x = xy.y = HUGE_VAL;
    return xy;
}
INVERSE(e_inverse); /* ellipsoid */
    int nn;
    double lamt, sdsq, s, lamdp, phidp, sppsq, dd, sd, sl, fac, scl, sav, spp;

    lamdp = xy.x / P->b;
    nn = 50;
    do {
            sav = lamdp;
            sd = sin(lamdp);
            sdsq = sd * sd;
            s = P->p22 * P->sa * cos(lamdp) * sqrt((1. + P->t * sdsq)
                     / ((1. + P->w * sdsq) * (1. + P->q * sdsq)));
            lamdp = xy.x + xy.y * s / P->xj - P->a2 * sin(
                    2. * lamdp) - P->a4 * sin(lamdp * 4.) - s / P->xj * (
                    P->c1 * sin(lamdp) + P->c3 * sin(lamdp * 3.));
            lamdp /= P->b;
    } while (fabs(lamdp - sav) >= TOL && --nn);
    sl = sin(lamdp);
    fac = exp(sqrt(1. + s * s / P->xj / P->xj) * (xy.y -
            P->c1 * sl - P->c3 * sin(lamdp * 3.)));
    phidp = 2. * (atan(fac) - FORTPI);
    dd = sl * sl;
    if (fabs(cos(lamdp)) < TOL)
        lamdp -= TOL;
    spp = sin(phidp);
    sppsq = spp * spp;
    lamt = atan(((1. - sppsq * P->rone_es) * tan(lamdp) *
            P->ca - spp * P->sa * sqrt((1. + P->q * dd) * (
            1. - sppsq) - sppsq * P->u) / cos(lamdp)) / (1. - sppsq
            * (1. + P->u)));
    sl = lamt >= 0. ? 1. : -1.;
    scl = cos(lamdp) >= 0. ? 1. : -1;
    lamt -= HALFPI * (1. - scl) * sl;
    lp.lam = lamt - P->p22 * lamdp;
    if (fabs(P->sa) < TOL)
        lp.phi = aasin(P->ctx,spp / sqrt(P->one_es * P->one_es + P->es * sppsq));
    else
            lp.phi = atan((tan(lamdp) * cos(lamt) - P->ca * sin(lamt)) /
                    (P->one_es * P->sa));
    return lp;
}
FREEUP; if (P) pj_dalloc(P); }
ENTRY0(misrsom)
    int path;
    double lam, alf, esc, ess;

    path = pj_param(P->ctx, P->params, "ipath").i;
    if (path <= 0 || path > 233) E_ERROR(-29);
    P->lam0 = DEG_TO_RAD * 129.3056 - TWOPI / 233. * path;
    alf = 98.30382 * DEG_TO_RAD;
    P->p22 = 98.88 / 1440.0;

    P->sa = sin(alf);
    P->ca = cos(alf);
    if (fabs(P->ca) < 1e-9)
        P->ca = 1e-9;
    esc = P->es * P->ca * P->ca;
    ess = P->es * P->sa * P->sa;
    P->w = (1. - esc) * P->rone_es;
    P->w = P->w * P->w - 1.;
    P->q = ess * P->rone_es;
    P->t = ess * (2. - P->es) * P->rone_es * P->rone_es;
    P->u = esc * P->rone_es;
    P->xj = P->one_es * P->one_es * P->one_es;
    P->rlm = 0;
    P->rlm2 = P->rlm + TWOPI;
    P->a2 = P->a4 = P->b = P->c1 = P->c3 = 0.;
    seraz0(0., 1., P);
    for (lam = 9.; lam <= 81.0001; lam += 18.)
        seraz0(lam, 4., P);
    for (lam = 18; lam <= 72.0001; lam += 18.)
        seraz0(lam, 2., P);
    seraz0(90., 1., P);
    P->a2 /= 30.;
    P->a4 /= 60.;
    P->b /= 30.;
    P->c1 /= 15.;
    P->c3 /= 45.;
    P->inv = e_inverse; P->fwd = e_forward;
ENDENTRY(P)