util.hh 7.95 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
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
 * @brief mixingRatio Calculate the water vaper mixing ratio
 * @param temperature kelvin
 * @param pressure hPa
 * @param relHum %
 * @return
 */
Real mixingRatio(Real temperature, Real pressure, Real relHum);

/**
 * @brief virtualTemperature The temperature of dry air so that the its density
 * is equivalent to a sample of moist air at the same pressure.
 * @param temperature kelvin
 * @param pressure hPa
 * @param relHum %
 * @return
 */
Real virtualTemperature(Real temperature, Real pressure, Real relHum);

39
/**
40
 * @brief hpaToAltitude converts hectopascals or millibars to altitude (meters)
41
 * @param hpa hectorpascals in millibars
42
43
 * @param relHum in %
 * @param temperature in kelvin
LEFEBVREJP email's avatar
LEFEBVREJP email committed
44
 * @param msle mean sea level pressure (default=1013.25)
45
46
 * @return altitude in meters
 */
47
Real RADIX_PUBLIC hpaToAltitude(Real hpa, Real relHum, Real temperature,
LEFEBVREJP email's avatar
LEFEBVREJP email committed
48
                                Real msle = 1013.25);
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

/**
 * @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);

91
92
93
/**
 * @brief cspanf returns a value in the interval (begin,end]
 * This function is used to reduce periodic variables to a standard range.
94
95
 * It adjusts for the behaviour of the mod function which provides positive
 * results for position input and negative results for negative input.
96
97
98
99
100
 * @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
 */
101
Real RADIX_PUBLIC cspanf(Real value, Real begin, Real end);
102

103
104
105
106
107
108
109
110
/**
 * @brief haversine The Haversine formula for calculating great-circle
 * @param lat1
 * @param lon1
 * @param lat2
 * @param lon2
 * @return angle(radians) between two points
 */
111
Real RADIX_PUBLIC haversine(Real lat1, Real lon1, Real lat2, Real lon2);
112
113

/**
114
115
 * @brief greatCircleDistance Calculates the shortest distance between two
 * points on the surface of a sphere
116
117
118
119
 * @param lat1 Real point1 latitude
 * @param lon1 Real point1 longitude
 * @param lat2 Real point2 latitude
 * @param lon2 Real point2 longitude
120
121
122
 * @param radius
 * @return
 */
123
124
Real RADIX_PUBLIC greatCircleDistance(Real lat1, Real lon1, Real lat2,
                                      Real lon2, Real radius);
125

126
/**
127
128
 * @brief greatCircleVolume Calculates the volume giving latitude, longitude and
 * radius bounds.
129
130
131
132
133
134
135
136
 * @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
 */
137
138
Real RADIX_PUBLIC greatCircleVolume(Real lat1, Real lon1, Real lat2, Real lon2,
                                    Real r1, Real r2);
139

140
/**
141
142
 * @brief greatCircleArea Calculates the area (m^2) given rectanglar lat/lon
 * corners at radius height
143
144
145
146
147
148
149
 * @param lat1
 * @param lon1
 * @param lat2
 * @param lon2
 * @param r1
 * @return Area in square meters
 */
150
// Real greatCircleArea(Real lat1, Real lon1, Real lat2, Real lon2, Real r1);
151

152
153
154
155
156
157
/**
 * @brief cylinderVolume Calculates the volume of a cylinder
 * @param r radius of cylinder
 * @param h height of cylinder
 * @return Real
 */
158
Real RADIX_PUBLIC cylinderVolume(Real r, Real h);
159

160
161
162
std::vector<Real> RADIX_PUBLIC
getEqualAreaRadii(const std::vector<Real> &radii,
                  const std::vector<unsigned short> &subrings);
163

164
inline Real RADIX_PUBLIC toDegrees(Real radians) { return radians * 180 / PI; }
165

166
inline Real RADIX_PUBLIC toRadians(Real degrees) { return degrees * PI / 180; }
167
168

// 360 degrees = 1 turn, and limit to being less than a full turn
169
170
inline Real RADIX_PUBLIC toTurns(Real degrees)
{
171
172
173
174
175
176
177
178
  // 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;
179
180
181
}

/**
182
183
 * @brief lineIntersect determines the intersection distance from p1 along v1 to
 * line described p2->p3
184
185
186
187
 * @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
188
189
 * @return  Real - the track length t from p along v to the line described by
 * sp,ep t == maxReal if no intersection occurs
190
 */
191
192
193
Real RADIX_PUBLIC lineIntersect(const class Point3D &p, const class Vector3D &v,
                                const class Point3D &sp,
                                const class Point3D &ep);
194
195
196
197
198
199
200
201

/**
 * @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)
 */
202
203
204
205
template <typename T>
T RADIX_PUBLIC interpolate(const std::vector<T> &xvals,
                           const std::vector<T> &yvals, T x,
                           bool linear = false);
206
207

/**
208
209
 * @brief gammaRayAbsorptionInAir calculates the absorption of gamma rays as
 * they pass through air
210
 * @param energy - energy of the gamma ray in MeV
211
212
213
 * @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
214
 */
215
double RADIX_PUBLIC gammaRayAbsorptionInAir(double energy, bool linear = false);
216
217

/**
218
219
 * @brief gammaRayAttenuationInAir calculates the attenuation of gamma rays as
 * they pass through air
220
 * @param energy - energy of the gamma ray in MeV
221
222
223
 * @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
224
 */
225
226
double RADIX_PUBLIC gammaRayAttenuationInAir(double energy,
                                             bool linear = false);
227
228

/**
229
230
 * @brief exponentialIntegral calculates the exponential integral of the given
 * value
231
232
233
 * @param d - value of which the exponential integral is calculated
 * @return double - the exponential integral of the given value
 */
234
double RADIX_PUBLIC exponentialIntegral(double d);
235
}  // namespace radix
236
237

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

239
#endif /* RADIX_RADIXMATH_UTIL_H */