File: reflectancecalculator.cpp

package info (click to toggle)
gdal 1.10.1%2Bdfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 84,320 kB
  • ctags: 74,726
  • sloc: cpp: 677,199; ansic: 162,820; python: 13,816; cs: 11,163; sh: 10,446; java: 5,279; perl: 4,429; php: 2,971; xml: 1,500; yacc: 934; makefile: 494; sql: 112
file content (162 lines) | stat: -rw-r--r-- 5,627 bytes parent folder | download | duplicates (3)
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
/******************************************************************************
 * $Id: reflectancecalculator.cpp 15066 2008-07-28 20:21:59Z mloskot $
 *
 * Purpose:  Implementation of ReflectanceCalculator class. Calculate
 *           reflectance values from radiance, for visual bands.
 * Author:   Bas Retsios, retsios@itc.nl
 *
 ******************************************************************************
 * Copyright (c) 2004, ITC
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 ******************************************************************************/

#include "reflectancecalculator.h"
#include <cmath>
#include <cstdlib>
using namespace std;

#define M_PI        3.14159265358979323846

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

ReflectanceCalculator::ReflectanceCalculator(std::string sTimeStamp, double rRTOA)
: m_rRTOA(rRTOA)
{
  std::string sYear (sTimeStamp.substr(0, 4));
  std::string sMonth (sTimeStamp.substr(4, 2));
  std::string sDay (sTimeStamp.substr(6, 2));
  std::string sHours (sTimeStamp.substr(8, 2));
  std::string sMins (sTimeStamp.substr(10, 2));

  m_iYear = atoi(sYear.c_str());
  int iMonth = atoi(sMonth.c_str());
  m_iDay = atoi(sDay.c_str());
	for (int i = 1; i < iMonth; ++i)
		m_iDay += iDaysInMonth(i, m_iYear);
  int iHours = atoi(sHours.c_str());
  int iMins = atoi(sMins.c_str());

	m_rHours = iHours + iMins / 60.0;
}

ReflectanceCalculator::~ReflectanceCalculator()
{

}

double ReflectanceCalculator::rGetReflectance(double rRadiance, double rLat, double rLon) const
{
  double phi = rLat * M_PI / 180;
  double lam = rLon * M_PI / 180;
  double rSunDist = rSunDistance();
  double ReflectanceNumerator = rRadiance*rSunDist*rSunDist;
  double zenithAngle = rZenithAngle(phi, rDeclination(), rHourAngle(rLon));
  double ReflectanceDenominator = m_rRTOA*cos(zenithAngle*M_PI/180);
  double Reflectance = ReflectanceNumerator / ReflectanceDenominator;
  return Reflectance;
}

double ReflectanceCalculator::rZenithAngle(double phi, double rDeclin, double rHourAngle) const
{
  double rCosZen = (sin(phi) * sin(rDeclin) + cos(phi)
          * cos(rDeclin) * cos(rHourAngle));
  double zenithAngle = acos(rCosZen) * 180 / M_PI;
  return zenithAngle;
}

const double ReflectanceCalculator::rDeclination() const
{
  double rJulianDay = m_iDay - 1;
  double yearFraction = (rJulianDay + m_rHours / 24) / iDaysInYear(m_iYear);
  double T = 2 * M_PI * yearFraction;
  
  double declin = 0.006918 - 0.399912 * cos(T) + 0.070257 * sin(T)
          - 0.006758 * cos(2 * T) + 0.000907 * sin(2 * T)
          - 0.002697 * cos(3 * T) + 0.00148 * sin(3 * T);
  return declin;
}

double ReflectanceCalculator::rHourAngle(double rLon) const
{
	// In: rLon (in degrees)
	// Out: hourAngle (in radians)
  double rJulianDay = m_iDay - 1;
  double yearFraction = (rJulianDay + m_rHours / 24) / iDaysInYear(m_iYear);
  double T = 2 * M_PI * yearFraction;

  double EOT2 = 229.18 * (0.000075 + 0.001868 * cos(T)- 0.032077 * sin(T));
  double EOT3 = 229.18 * (- 0.014615 * cos(2 * T) - 0.040849 * sin(2 * T));
  double EOT = EOT2 + EOT3;
  double TimeOffset = EOT + (4. * rLon);
  // True solar time in minutes:
  double TrueSolarTime = m_rHours * 60 + TimeOffset;
  // Solar hour angle in degrees and in radians:
  double HaDegr = (TrueSolarTime / 4. - 180.);
  double hourAngle = HaDegr * M_PI / 180;
  return hourAngle;
}

const double ReflectanceCalculator::rSunDistance() const
{
  int iJulianDay = m_iDay - 1;
  double theta = 2*M_PI *(iJulianDay - 3) / 365.25;
	// rE0 is the inverse of the square of the sun-distance ratio
	double rE0 = 1.000110 + 0.034221*cos(theta)+0.00128*sin(theta) + 0.000719*cos(2*theta)+0.000077*sin(2*theta);
  // The calculated distance is expressed as a factor of the "average sun-distance" (on 1 Jan approx. 0.98, on 1 Jul approx. 1.01)
  return 1 / sqrt(rE0);
}

int ReflectanceCalculator::iDaysInYear(int iYear) const
{
  bool fLeapYear = iDaysInMonth(2, iYear) == 29;
  
  if (fLeapYear)
      return 366;
  else
      return 365;
}

int ReflectanceCalculator::iDaysInMonth(int iMonth, int iYear) const
{
  int iDays;

  if ((iMonth == 4) || (iMonth == 6) || (iMonth == 9) || (iMonth == 11))
    iDays = 30;
  else if (iMonth == 2)
  {
    iDays = 28;
    if (iYear % 100 == 0) // century year
    {
      if (iYear % 400 == 0) // century leap year
        ++iDays;
    }
    else
    {
      if (iYear % 4 == 0) // normal leap year
        ++iDays;
    }
  }
  else
    iDays = 31;

  return iDays;
}