util.hh 7.43 KB
Newer Older
1
2
3
4
5
6
7
8
/*
 * File:   Util.h
 * Author: a16
 *
 * Created on January 17, 2013, 9:27 AM
 */

#ifndef RADIX_RADIXMATH_UTIL_H
9
#define RADIX_RADIXMATH_UTIL_H
10
11
12
13

#include "radixbug/bug.hh"
#include "radixmath/constants.hh"

14
15
#include "radixdl/visibility.hh"

16
17
#include <vector>

18
19
namespace radix
{
20
/**
21
 * @brief hpaToAltitude converts hectopascals or millibars to altitude (meters)
22
 * @param hpa hectorpascals in millibars
LEFEBVREJP email's avatar
LEFEBVREJP email committed
23
24
 * @param temperature in kelvin (default=288.15)
 * @param msle mean sea level pressure (default=1013.25)
25
26
 * @return altitude in meters
 */
LEFEBVREJP email's avatar
LEFEBVREJP email committed
27
28
Real RADIX_PUBLIC hpaToAltitude(Real hpa, Real temperature = 288.15,
                                Real msle = 1013.25);
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

/**
 * @brief absoluteTemperature Get the temperature of an air parcel given its
 * potential temperature and pressure
 * @param potentialTemp Potential temperature in Kelvin
 * @param pressure Pressure in mb
 * @return Absolute temperature (K)
 */
Real RADIX_PUBLIC absoluteTemperature(Real potentialTemp, Real pressure);

/**
 * @brief saturationVaporPressure Calculate the saturation pressure of an air
 * parcel given its absolute temperature and pressure
 * @param absTemp Absolute temperature in Kelvin
 * @param pressure Pressure in mb
 * @return Saturation vapour pressure (mb)
 */
Real RADIX_PUBLIC saturationVaporPressure(Real absTemp, Real pressure);

/**
 * @brief mixingRatioToRelativeHumidity Get the relative humidity of an air
 * parcel given a mixing ratio, temperature and pressure
 * @param mixingRatio Mixing ratio (g/g)
 * @param absTemp Absolute temperature (K)
 * @param pressure Pressure (mb)
 * @return Relative humidity (%)
 */
Real RADIX_PUBLIC mixingRatioToRelativeHumidity(Real mixingRatio, Real absTemp,
                                                Real pressure);

/**
 * @brief specificHumidityToRelativeHumidity Get the relative humidity of an air
 * parcel given a specific humidity, temperature and pressure
 * @param specificHumidity Specific humidity (g/kg)
 * @param absTemp Absolute temperature (K)
 * @param pressure Pressure (mb)
 * @return Relative humidity (%)
 */
Real RADIX_PUBLIC specificHumidityToRelativeHumidity(Real specificHumidity,
                                                     Real absTemp,
                                                     Real pressure);

71
72
73
/**
 * @brief cspanf returns a value in the interval (begin,end]
 * This function is used to reduce periodic variables to a standard range.
74
75
 * It adjusts for the behaviour of the mod function which provides positive
 * results for position input and negative results for negative input.
76
77
78
79
80
 * @param value number to be reduced to the span
 * @param begin first value of the span
 * @param end last value of the span
 * @return Real the reduced value
 */
81
Real RADIX_PUBLIC cspanf(Real value, Real begin, Real end);
82

83
84
85
86
87
88
89
90
/**
 * @brief haversine The Haversine formula for calculating great-circle
 * @param lat1
 * @param lon1
 * @param lat2
 * @param lon2
 * @return angle(radians) between two points
 */
91
Real RADIX_PUBLIC haversine(Real lat1, Real lon1, Real lat2, Real lon2);
92
93

/**
94
95
 * @brief greatCircleDistance Calculates the shortest distance between two
 * points on the surface of a sphere
96
97
98
99
 * @param lat1 Real point1 latitude
 * @param lon1 Real point1 longitude
 * @param lat2 Real point2 latitude
 * @param lon2 Real point2 longitude
100
101
102
 * @param radius
 * @return
 */
103
104
Real RADIX_PUBLIC greatCircleDistance(Real lat1, Real lon1, Real lat2,
                                      Real lon2, Real radius);
105

106
/**
107
108
 * @brief greatCircleVolume Calculates the volume giving latitude, longitude and
 * radius bounds.
109
110
111
112
113
114
115
116
 * @param lat1 Real min latitude
 * @param lon1 Real min longitude
 * @param lat2 Real max latitude
 * @param lon2 Real max longitude
 * @param r1 Real min radius
 * @param r2 Real max radius
 * @return Volume in cubic meters
 */
117
118
Real RADIX_PUBLIC greatCircleVolume(Real lat1, Real lon1, Real lat2, Real lon2,
                                    Real r1, Real r2);
119

120
/**
121
122
 * @brief greatCircleArea Calculates the area (m^2) given rectanglar lat/lon
 * corners at radius height
123
124
125
126
127
128
129
 * @param lat1
 * @param lon1
 * @param lat2
 * @param lon2
 * @param r1
 * @return Area in square meters
 */
130
// Real greatCircleArea(Real lat1, Real lon1, Real lat2, Real lon2, Real r1);
131

132
133
134
135
136
137
/**
 * @brief cylinderVolume Calculates the volume of a cylinder
 * @param r radius of cylinder
 * @param h height of cylinder
 * @return Real
 */
138
Real RADIX_PUBLIC cylinderVolume(Real r, Real h);
139

140
141
142
std::vector<Real> RADIX_PUBLIC
getEqualAreaRadii(const std::vector<Real> &radii,
                  const std::vector<unsigned short> &subrings);
143

144
inline Real RADIX_PUBLIC toDegrees(Real radians) { return radians * 180 / PI; }
145

146
inline Real RADIX_PUBLIC toRadians(Real degrees) { return degrees * PI / 180; }
147
148

// 360 degrees = 1 turn, and limit to being less than a full turn
149
150
inline Real RADIX_PUBLIC toTurns(Real degrees)
{
151
152
153
154
155
156
157
158
  // Convert to revolutions
  double turns = degrees / 360;
  // Remove extra turns, rotating into (-1, 1)
  turns -= std::floor(turns);
  // Convert negative turns to positive turns
  if (turns < 0) turns += 1.0;
  radix_ensure(turns >= 0 && turns < 1);
  return turns;
159
160
161
}

/**
162
163
 * @brief lineIntersect determines the intersection distance from p1 along v1 to
 * line described p2->p3
164
165
166
167
 * @param p - the origin of the line
 * @param v - the direction vector to determine intersection with.
 * @param sp - a start point on the line to intersect
 * @param ep - a end point on the line to intersect
168
169
 * @return  Real - the track length t from p along v to the line described by
 * sp,ep t == maxReal if no intersection occurs
170
 */
171
172
173
Real RADIX_PUBLIC lineIntersect(const class Point3D &p, const class Vector3D &v,
                                const class Point3D &sp,
                                const class Point3D &ep);
174
175
176
177
178
179
180
181

/**
 * @brief interpolate interpolates the given data vectors
 * @param xvals - a list of bin boundaries in the x-dimension
 * @param yvals - a list of y-dimension values at each x-value
 * @param x - an x-value at which to interpolate its associated y-value
 * @param linear - whether to interpolate in a linear fashion (false for log)
 */
182
183
184
185
template <typename T>
T RADIX_PUBLIC interpolate(const std::vector<T> &xvals,
                           const std::vector<T> &yvals, T x,
                           bool linear = false);
186
187

/**
188
189
 * @brief gammaRayAbsorptionInAir calculates the absorption of gamma rays as
 * they pass through air
190
 * @param energy - energy of the gamma ray in MeV
191
192
193
 * @param linear - whether to interpolate across energies in a linear fashion
 * (false for log) return double - the absorption of gamma rays of a given
 * energy as they pass through air
194
 */
195
double RADIX_PUBLIC gammaRayAbsorptionInAir(double energy, bool linear = false);
196
197

/**
198
199
 * @brief gammaRayAttenuationInAir calculates the attenuation of gamma rays as
 * they pass through air
200
 * @param energy - energy of the gamma ray in MeV
201
202
203
 * @param linear - whether to interpolate across energies in a linear fashion
 * (false for log) return double - the attentuation of gamma rays of a given
 * energy as they pass through air
204
 */
205
206
double RADIX_PUBLIC gammaRayAttenuationInAir(double energy,
                                             bool linear = false);
207
208

/**
209
210
 * @brief exponentialIntegral calculates the exponential integral of the given
 * value
211
212
213
 * @param d - value of which the exponential integral is calculated
 * @return double - the exponential integral of the given value
 */
214
double RADIX_PUBLIC exponentialIntegral(double d);
215
}  // namespace radix
216
217

#include "radixmath/util.i.hh"
218

219
#endif /* RADIX_RADIXMATH_UTIL_H */