File: reflectancecalculator.cpp

package info (click to toggle)
gdal 3.11.3%2Bdfsg-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 89,016 kB
  • sloc: cpp: 1,165,048; ansic: 208,864; python: 26,958; java: 5,972; xml: 4,611; sh: 3,776; cs: 2,508; yacc: 1,306; makefile: 213
file content (150 lines) | stat: -rw-r--r-- 4,899 bytes parent folder | download | duplicates (2)
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
/******************************************************************************
 *
 * Purpose:  Implementation of ReflectanceCalculator class. Calculate
 *           reflectance values from radiance, for visual bands.
 * Author:   Bas Retsios, retsios@itc.nl
 *
 ******************************************************************************
 * Copyright (c) 2004, ITC
 *
 * SPDX-License-Identifier: MIT
 ******************************************************************************/

#include "cpl_port.h"  // Must be first.

#include "reflectancecalculator.h"
#include <cmath>
#include <cstdlib>

using namespace std;

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

ReflectanceCalculator::ReflectanceCalculator(const 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 l_rHourAngle)
{
    double rCosZen =
        (sin(phi) * sin(rDeclin) + cos(phi) * cos(rDeclin) * cos(l_rHourAngle));
    double zenithAngle = acos(rCosZen) * 180 / M_PI;
    return zenithAngle;
}

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;
}

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)
{
    bool fLeapYear = iDaysInMonth(2, iYear) == 29;

    if (fLeapYear)
        return 366;
    else
        return 365;
}

int ReflectanceCalculator::iDaysInMonth(int iMonth, int iYear)
{
    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;
}