5 #include <gsl/gsl_blas.h>
6 #include <gsl/gsl_linalg.h>
7 #include <gsl/gsl_matrix_double.h>
21 #define RADEG 57.29577951
29 int latlon_interp_dist(
size_t num_gridlines,
double *dist_ai,
float *lati,
float *loni,
double *dist,
float *lati2,
float *loni2){
32 std::vector<double> dscan;
33 std::vector<double> latpix,lonpix;
41 for(
size_t i=0;
i<num_gridlines-1;
i++){
42 dscan.push_back(dist_ai[
i]);
44 latpix.push_back(lati[
i]);
45 if (loni[
i]<0.0) loni[
i]+=360;
46 lonpix.push_back(loni[
i]);
51 for(
size_t k=0;
k<num_gridlines-1;
k++){
53 s1.set_boundary(tk::spline::second_deriv, 0.0,
54 tk::spline::second_deriv, 0.0);
55 s2.set_boundary(tk::spline::second_deriv, 0.0,
56 tk::spline::second_deriv, 0.0);
58 s1.set_points(dscan,latpix);
59 s2.set_points(dscan,lonpix);
64 if (loni2[
k]>180.) loni2[
k]-=360;
76 int latlon_interp2(
size_t num_gridlines,
size_t num_pixels,
double *gtime,
float *lati,
float *loni,
double *gtime2,
float *lati2,
float *loni2){
79 std::vector<double> tscan;
80 std::vector<double> latpix,lonpix;
87 for(
size_t i=0;
i<num_gridlines;
i++){
88 tscan.push_back(gtime[
i]);
90 latpix.push_back(lati[
i]);
91 if (loni[
i]<0.0) loni[
i]+=360;
92 lonpix.push_back(loni[
i]);
97 for(
size_t k=0;
k<num_gridlines;
k++){
99 s1.set_points(tscan,latpix);
100 s2.set_points(tscan,lonpix);
101 lati2[
k]=s1(gtime2[
k]);
102 loni2[
k]=s2(gtime2[
k]);
103 if (loni2[
k]>180.) loni2[
k]-=360;
116 int latlon_interp1pix(
size_t num_gridlines,
size_t gd_index,
double *tgrid,
float *lat_nad,
float *lon_nad,
float *latipix,
float *lonipix){
119 std::vector<double> tscan;
120 std::vector<double> latpix,lonpix;
122 cout<<
"interpolating 1 nadir position at the time..latlon_interp1pix."<<endl;
124 for(
size_t i=0;
i<num_gridlines;
i++){
125 tscan.push_back(tgrid[
i]);
127 latpix.push_back(lat_nad[
i]);
128 if (lon_nad[
i]<0.0) lon_nad[
i]+=360;
129 lonpix.push_back(lon_nad[
i]);
134 s1.set_boundary(tk::spline::second_deriv, 0.0,
135 tk::spline::second_deriv, 0.0);
136 s2.set_boundary(tk::spline::second_deriv, 0.0,
137 tk::spline::second_deriv, 0.0);
139 s1.set_points(tscan,latpix);
140 s2.set_points(tscan,lonpix);
141 cout<<
"ok filling latpix/lonpix vectors..."<<endl;
142 cout<<
"gd.."<<gd_index<<
"time.."<<tgrid[gd_index]<<
"latnad.."<<lat_nad[gd_index]<<
"lonnad.."<<lon_nad[gd_index]<<
"lat.."<<s1(tgrid[gd_index])<<
"..."<<
"lon.."<<s2(tgrid[gd_index])<<endl;
145 *latipix=s1(tgrid[gd_index]);
146 *lonipix=s2(tgrid[gd_index]);
147 if (*lonipix>180.) *lonipix-=360;
158 int latlon_interp_vec(
size_t n_orb_rec,
size_t num_gridlines,
double *torb,
double *latorb,
double *lonorb,
double *tmgv,
float *lati,
float *loni){
160 double elon1,elon2,elat1,elat2,thres=5;
161 std::vector<double> tscan,tlat,tlon;
162 std::vector<double> latpix,lonpix,latclean,lonclean;
163 std::vector<int> latidx,lonidx;
164 double latemp=0.,lontemp=0.;
168 for(
size_t i=0;
i<n_orb_rec;
i++){
169 tscan.push_back(torb[
i]);
171 latpix.push_back(latemp);
174 lontemp=lonorb[
i]+360;}
175 else lontemp=lonorb[
i];
177 lonpix.push_back(lontemp);
181 for(
size_t k=0;
k<num_gridlines;
k++){
183 s1.set_boundary(tk::spline::second_deriv, 0.0,
184 tk::spline::second_deriv, 0.0);
185 s2.set_boundary(tk::spline::second_deriv, 0.0,
186 tk::spline::second_deriv, 0.0);
188 s1.set_points(tscan,latpix);
189 s2.set_points(tscan,lonpix);
192 if (loni[
k]>180.) loni[
k]-=360;
199 if(
k>=1 &&
k+1<num_gridlines){
200 elon1=
abs(loni[
k]-loni[
k-1]);
201 elon2=
abs(loni[
k+1]-loni[
k]);
202 elat1=
abs(lati[
k]-lati[
k-1]);
203 elat2=
abs(lati[
k+1]-lati[
k]);
205 if(
abs(100*elon2/elon1)<=100+thres &&
abs(100*elon2/elon1)>=100-thres){
206 tlon.push_back(tmgv[
k]);
208 lonclean.push_back(loni[
k]);}
209 if(
abs(100*elat2/elat1)<=100+thres &&
abs(100*elat2/elat1)>=100-thres){
210 tlat.push_back(tmgv[
k]);
212 latclean.push_back(lati[
k]);}
236 int latlon_interp(
size_t n_orb_rec,
size_t num_gridlines,
size_t num_pixels,
double *torb,
float **
lat,
float **
lon,
double *
time,
float *lati,
float *loni){
238 double elon1,elon2,elat1,elat2,thres=5;
240 std::vector<double> tscan,tlat,tlon;
241 std::vector<double> latpix,lonpix,latclean,lonclean;
243 std::vector<int> latidx,lonidx;
250 int j=(num_pixels-1)/2;
251 cout<<
"interpolating based on nad pixels....index #.."<<
j<<endl;
252 for(
size_t i=0;
i<n_orb_rec;
i++){
253 tscan.push_back(torb[
i]);
255 latpix.push_back(
lat[
i][
j]);
258 lonpix.push_back(
lon[
i][
j]);
262 for(
size_t k=0;
k<num_gridlines;
k++){
264 s1.set_boundary(tk::spline::second_deriv, 0.0,
265 tk::spline::second_deriv, 0.0);
266 s2.set_boundary(tk::spline::second_deriv, 0.0,
267 tk::spline::second_deriv, 0.0);
269 s1.set_points(tscan,latpix);
270 s2.set_points(tscan,lonpix);
273 if (loni[
k]>180.) loni[
k]-=360;
280 if(
k>=1 &&
k+1<num_gridlines){
281 elon1=
abs(loni[
k]-loni[
k-1]);
282 elon2=
abs(loni[
k+1]-loni[
k]);
283 elat1=
abs(lati[
k]-lati[
k-1]);
284 elat2=
abs(lati[
k+1]-lati[
k]);
286 if(
abs(100*elon2/elon1)<=100+thres &&
abs(100*elon2/elon1)>=100-thres){
287 tlon.push_back(
time[
k]);
289 lonclean.push_back(loni[
k]);}
290 if(
abs(100*elat2/elat1)<=100+thres &&
abs(100*elat2/elat1)>=100-thres){
291 tlat.push_back(
time[
k]);
293 latclean.push_back(lati[
k]);}
459 double a0[3], a1[3], a2[3], a3[3];
474 for (
size_t i = 0;
i < sdim;
i++) {
476 for (
size_t j = ind;
j < n_orb_rec;
j++) {
484 double dt = torb[ind + 1] - torb[ind];
485 for (
size_t j = 0;
j < 3;
j++) {
487 a1[
j] =
v[ind][
j] * dt;
488 a2[
j] = 3 *
p[ind + 1][
j] - 3 *
p[ind][
j] - 2 *
v[ind][
j] * dt -
490 a3[
j] = 2 *
p[ind][
j] - 2 *
p[ind + 1][
j] +
v[ind][
j] * dt +
495 double x = (
time[
i] - torb[ind]) / dt;
498 for (
size_t j = 0;
j < 3;
j++) {
499 posi[
i][
j] = a0[
j] + a1[
j] *
x + a2[
j] * x2 + a3[
j] * x3;
500 veli[
i][
j] = (a1[
j] + 2 * a2[
j] *
x + 3 * a3[
j] * x2) / dt;
540 double a0[3], a1[3], a2[3], a3[3];
555 for (
size_t i = 0;
i < sdim;
i++) {
557 for (
size_t j = ind;
j < n_orb_rec;
j++) {
565 double dt = torb[ind + 1] - torb[ind];
566 for (
size_t j = 0;
j < 3;
j++) {
568 a1[
j] =
v[ind][
j] * dt;
569 a2[
j] = 3 *
p[ind + 1][
j] - 3 *
p[ind][
j] - 2 *
v[ind][
j] * dt -
571 a3[
j] = 2 *
p[ind][
j] - 2 *
p[ind + 1][
j] +
v[ind][
j] * dt +
576 double x = (
time[
i] - torb[ind]) / dt;
579 for (
size_t j = 0;
j < 3;
j++) {
580 posi[
i][
j] = a0[
j] + a1[
j] *
x + a2[
j] * x2 + a3[
j] * x3;
581 veli[
i][
j] = (a1[
j] + 2 * a2[
j] *
x + 3 * a3[
j] * x2) / dt;
606 double xnut[3][3], ut1utc;
611 double day =
idy + (sec + ut1utc) / 86400;
612 double gha, gham[3][3];
615 gham[0][0] = cos(gha /
RADEG);
616 gham[1][1] = cos(gha /
RADEG);
618 gham[0][1] = sin(gha /
RADEG);
619 gham[1][0] = -sin(gha /
RADEG);
627 gsl_matrix_view A = gsl_matrix_view_array(&gham[0][0], 3, 3);
628 gsl_matrix_view B = gsl_matrix_view_array(&xnut[0][0], 3, 3);
629 gsl_matrix *
C = gsl_matrix_alloc(3, 3);
631 gsl_blas_dgemm(CblasNoTrans, CblasTrans, 1.0, &A.matrix, &B.matrix, 0.0,
C);
633 gsl_matrix_view D = gsl_matrix_view_array(&j2mod[0][0], 3, 3);
634 gsl_matrix *E = gsl_matrix_alloc(3, 3);
635 gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0,
C, &D.matrix, 0.0, E);
636 double *ptr_E = gsl_matrix_ptr(E, 0, 0);
638 memcpy(ecmat, ptr_E, 9 *
sizeof(
double));
661 double t =
jday(iyear, 1, iday) - (
double)2451545.5 + sec / 86400;
664 double zeta0 =
t * (2306.2181 + 0.302 *
t + 0.018 *
t *
t) /
RADEG / 3600;
665 double thetap =
t * (2004.3109 - 0.4266 *
t - 0.04160 *
t *
t) /
RADEG / 3600;
666 double xip =
t * (2306.2181 + 1.095 *
t + 0.018 *
t *
t) /
RADEG / 3600;
668 j2mod[0][0] = -sin(zeta0) * sin(xip) + cos(zeta0) * cos(xip) * cos(thetap);
669 j2mod[0][1] = -cos(zeta0) * sin(xip) - sin(zeta0) * cos(xip) * cos(thetap);
670 j2mod[0][2] = -cos(xip) * sin(thetap);
671 j2mod[1][0] = sin(zeta0) * cos(xip) + cos(zeta0) * sin(xip) * cos(thetap);
672 j2mod[1][1] = cos(zeta0) * cos(xip) - sin(zeta0) * sin(xip) * cos(thetap);
673 j2mod[1][2] = -sin(xip) * sin(thetap);
674 j2mod[2][0] = cos(zeta0) * sin(thetap);
675 j2mod[2][1] = -sin(zeta0) * sin(thetap);
676 j2mod[2][2] = cos(thetap);
685 double t =
jday(iyear, 1, iday) - (
double)2451545.5;
687 double xls, gs, xlm,
omega;
688 double dpsi, eps, epsm;
692 xnut[0][0] = cos(dpsi /
RADEG);
693 xnut[1][0] = -sin(dpsi /
RADEG) * cos(epsm /
RADEG);
694 xnut[2][0] = -sin(dpsi /
RADEG) * sin(epsm /
RADEG);
695 xnut[0][1] = sin(dpsi /
RADEG) * cos(eps /
RADEG);
696 xnut[1][1] = cos(dpsi /
RADEG) * cos(eps /
RADEG) * cos(epsm /
RADEG) +
698 xnut[2][1] = cos(dpsi /
RADEG) * cos(eps /
RADEG) * sin(epsm /
RADEG) -
700 xnut[0][2] = sin(dpsi /
RADEG) * sin(eps /
RADEG);
701 xnut[1][2] = cos(dpsi /
RADEG) * sin(eps /
RADEG) * cos(epsm /
RADEG) -
703 xnut[2][2] = cos(dpsi /
RADEG) * sin(eps /
RADEG) * sin(epsm /
RADEG) +
720 xls = (
double)280.46592 + ((
double)0.9856473516) *
t;
721 xls = fmod(xls, (
double)360);
724 gs = (
double)357.52772 + ((
double)0.9856002831) *
t;
725 gs = fmod(gs, (
double)360);
728 xlm = (
double)218.31643 + ((
double)13.17639648) *
t;
729 xlm = fmod(xlm, (
double)360);
739 double &dpsi,
double &eps,
double &epsm) {
752 1.3187 * sin(2. * xls /
RADEG) + 0.1426 * sin(gs /
RADEG) -
753 0.2274 * sin(2. * xlm /
RADEG);
756 epsm = (
double)23.439291 - ((
double)3.560e-7) *
t;
759 double deps = 9.2025 * cos(
omega /
RADEG) + 0.5736 * cos(2. * xls /
RADEG);
762 eps = epsm + deps / 3600;
773 static int32_t ijd[25000];
774 static double ut1[25000];
775 string utcpole =
"$OCVARROOT/var/modis/utcpole.dat";
776 static bool first =
true;
783 ifstream utcpfile(
utcpole.c_str());
784 if (utcpfile.is_open()) {
785 getline(utcpfile,
line);
786 getline(utcpfile,
line);
787 while (getline(utcpfile,
line)) {
790 istr.str(
line.substr(0, 5));
793 istr.str(
line.substr(44, 9));
801 cout <<
utcpole.c_str() <<
" not found" << endl;
807 int mjd =
jday(iyear, 1, iday) - 2400000;
830 double fday =
day - iday;
832 double t =
jd - (
double)2451545.5 + fday;
835 double gmst = (
double)100.4606184 + (
double)0.9856473663 *
t +
839 double xls, gs, xlm,
omega;
840 double dpsi, eps, epsm;
845 gha = gmst + dpsi * cos(eps /
RADEG) + fday * 360;
846 gha = fmod(gha, (
double)360);
853 if ( (*sValue).find_first_of(
"$" ) == string::npos)
return 0;
854 string::size_type posEndIdx = (*sValue).find_first_of(
"/" );
855 if ( posEndIdx == string::npos)
return 0;
856 char *envVar_str = getenv((*sValue).substr( 1, posEndIdx-1 ).c_str());
857 if (envVar_str == 0x0) {
858 printf(
"Environment variable: %s not defined.\n", envVar_str);
861 *sValue = envVar_str + (*sValue).substr( posEndIdx);