9#include <unordered_map>
13#ifndef TIFFTAG_GDAL_NODATA
14#define TIFFTAG_GDAL_NODATA 42113
23TIFF* GeoTiffOpen(
const char*
const filename,
const char*
const mode);
28int sxprintf(
char* buffer,
size_t N,
const char* format, va_list* args);
29int sxprintf(
char* buffer,
size_t N,
const char* format, ...);
34template <std::
size_t N>
35int sxprintf(
char (&buffer)[N],
const char* format, va_list* args)
38 return sxprintf(&buffer[0], N, format, args);
40template <std::
size_t N>
41int sxprintf(
char (&buffer)[N],
const char* format, ...)
45 va_start(args, format);
46 auto r = sxprintf(buffer, format, &args);
63[[nodiscard]] DurationSize to_time(
const T day,
const int hour)
noexcept
65 return day + hour / (1.0 * DAY_HOURS);
72[[nodiscard]]
constexpr DurationSize to_time(
const size_t t_index)
noexcept
74 return static_cast<DurationSize
>(t_index) / DAY_HOURS;
84[[nodiscard]]
size_t time_index(
const T day,
const int hour)
noexcept
86 return static_cast<size_t>(day) * DAY_HOURS + hour;
96[[nodiscard]]
size_t time_index(
const T day,
98 const Day min_date)
noexcept
100 return time_index(day, hour) -
static_cast<size_t>(DAY_HOURS) * min_date;
107[[nodiscard]]
constexpr size_t time_index(
const DurationSize time)
noexcept
109 return static_cast<size_t>(time * DAY_HOURS);
117[[nodiscard]]
constexpr size_t time_index(
const DurationSize time,
118 const Day min_date)
noexcept
120 return time_index(time) -
static_cast<size_t>(DAY_HOURS) * min_date;
129[[nodiscard]] T no_convert(
int value,
int)
noexcept
131 return static_cast<T
>(value);
138[[nodiscard]]
constexpr MathSize fix_radians(
const MathSize theta)
140 if (theta > M_2_X_PI)
142 return theta - M_2_X_PI;
146 return theta + M_2_X_PI;
155[[nodiscard]]
constexpr MathSize to_radians(
const MathSize degrees)
noexcept
157 return fix_radians(degrees / M_RADIANS_TO_DEGREES);
163static constexpr MathSize RAD_360 = to_radians(360);
167static constexpr MathSize RAD_270 = to_radians(270);
171static constexpr MathSize RAD_180 = to_radians(180);
175static constexpr MathSize RAD_090 = to_radians(90);
181[[nodiscard]]
constexpr MathSize to_degrees(
const MathSize radians)
183 return fix_radians(radians) * M_RADIANS_TO_DEGREES;
190[[nodiscard]]
constexpr MathSize to_heading(
const MathSize azimuth)
192 return fix_radians(azimuth + RAD_180);
207std::basic_istream<Elem, Traits>& getline(
208 std::basic_istream<Elem, Traits>* stream,
209 std::basic_string<Elem, Traits, Alloc>* str,
210 const Elem delimiter)
213 return getline(*stream, *str, delimiter);
220[[nodiscard]]
bool directory_exists(
const char* dir)
noexcept;
226[[nodiscard]]
bool file_exists(
const char* path)
noexcept;
234void read_directory(
const bool for_files,
237 const string& match);
244void read_directory(
const string& name,
246 const string& match);
253void read_directory(
const bool for_files,
261void read_directory(
const string& name, vector<string>* v);
268[[nodiscard]] vector<string> find_rasters(
const string& dir,
int year);
273void make_directory(
const char* dir)
noexcept;
278void make_directory_recursive(
const char* dir)
noexcept;
285[[nodiscard]]
inline constexpr MathSize sq(
const T& x)
287 return static_cast<MathSize
>(x) *
static_cast<MathSize
>(x);
296template <
unsigned int N,
class T>
297[[nodiscard]]
constexpr T pow_int(
const T& base)
303 ? pow_int<N / 2, T>(base) * pow_int<N / 2, T>(base)
304 : base * pow_int<(N - 1) / 2, T>(base) * pow_int<(N - 1) / 2, T>(base);
312template <
unsigned int N,
class T>
313[[nodiscard]]
constexpr T bit_mask()
315 return static_cast<T
>(pow_int<N, int64_t>(2) - 1);
323template <
unsigned int N>
324[[nodiscard]] MathSize round_to_precision(
const MathSize value)
noexcept
329 static const auto b = pow_int<N, int64_t>(10);
330 return round(value * b) / b;
341tm to_tm(
const int year,
352DurationSize to_time(
const tm& t);
362DurationSize to_time(
const int year,
373void read_date(istringstream* iss,
string* str, tm* t);
393 explicit UsageCount(
string for_what)
noexcept;
413[[nodiscard]] typename std::vector<T>::iterator
414 insert_sorted(std::vector<T>* vec, T const& item)
416 return vec->insert(std::upper_bound(vec->begin(), vec->end(), item), item);
425void insert_unique(std::vector<T>* vec, T
const& item)
427 const auto i = std::lower_bound(vec->begin(), vec->end(), item);
428 if (i == vec->end() || *i != item)
430 vec->insert(i, item);
445template <
typename T,
typename V>
446T binary_find(
const T lower,
448 const MathSize value,
449 const std::function<V(T)>& fct)
451 const auto mid = lower + (upper - lower) / 2;
456 if (fct(mid) < value)
458 return binary_find(lower, mid, value, fct);
460 return binary_find(mid + 1, upper, value, fct);
472template <
typename T,
typename V>
473T binary_find_checked(
const T lower,
475 const MathSize value,
476 const std::function<V(T)>& fct)
478 if (fct(lower) < value)
482 if (fct(upper) >= value)
486 return binary_find(lower, upper, value, fct);
495void month_and_day(
const int year,
const size_t day_of_year,
size_t* month,
size_t* day_of_month);
501[[nodiscard]]
bool is_leap_year(
const int year);
508[[nodiscard]]
string make_timestamp(
const int year,
const DurationSize time);
515[[nodiscard]]
inline MathSize ellipse_angle(
const MathSize length_to_breadth,
516 const MathSize theta)
518 return (util::fix_radians(
519 atan2(sin(theta) / length_to_breadth,
Provides the ability to determine how many times something is used during a simulation.
Definition Util.h:378
UsageCount & operator++() noexcept
Increment operator.
Definition Util.cpp:230
UsageCount(string for_what) noexcept
Constructor.
Definition Util.cpp:226
atomic< size_t > count_
How many times this has been used.
Definition Util.h:382
string for_what_
What this is tracking usage of.
Definition Util.h:386