SNOWPACK 20240425.b7bfb45
Utils.h
Go to the documentation of this file.
1/*
2 * SNOWPACK stand-alone
3 *
4 * Copyright WSL Institute for Snow and Avalanche Research SLF, DAVOS, SWITZERLAND
5*/
6/* This file is part of Snowpack.
7 Snowpack is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 Snowpack is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Snowpack. If not, see <http://www.gnu.org/licenses/>.
19*/
27#ifndef UTILS_H
28#define UTILS_H
29
31
32#include <cstdarg> // needed for va_list
33#include <string>
34#include <cstring>
35#include <vector>
36
37#ifdef _MSC_VER
38//Microsoft still does NOT support C99...
39//This replacement is not fully compatible, so
40//switch to a real, standard compliant compiler
41 #define snprintf _snprintf
42#endif
43
48namespace snowpack {
49std::string getLibVersion();
50}
51
52#ifdef GNU //in this case, GCC can check the format arguments for types, number, ...
53void prn_msg(const char *fileAndPath, const int theLine, const char *msg_type, const mio::Date& date_in, const char *format, ...)
54__attribute__ ((format (printf, 5, 6)));
55#else
56void prn_msg(const char *fileAndPath, const int theLine, const char *msg_type, const mio::Date& date_in, const char *format, ...);
57#endif
58
59bool booleanTime(const double& JulianDate, double days_between,
60 const double& start, const double& calculation_step_length);
61
62void deleteOldOutputFiles(const std::string& outdir, const std::string& experiment,
63 const std::string& stationID, const unsigned int& nSlopes,
64 const std::vector<std::string>& vecExtensions);
65
66void averageFluxTimeSeries(const size_t& n_steps, const bool& useCanopyModel,
67 SurfaceFluxes& Sdata, SnowStation& Xdata);
68
69void typeToCode(int *F1, int *F2, int *F3, int type);
70
71double unitConversion(const double val, char* unitIn, char* unitOut);
72
73bool massBalanceCheck(const SnowStation& Xdata, const SurfaceFluxes& Sdata, double& tot_mass_in);
74
75size_t findUpperNode(const double& z, const std::vector<NodeData>& Ndata, const size_t& nN);
76
77double forcedErosion(const double hs, SnowStation& Xdata);
78
79void deflateInflate(const CurrentMeteo& Mdata, SnowStation& Xdata, double& dhs_corr, double& mass_corr, const bool &prn_check);
80
81double logisticFunction(const double input, const double threshold, const double width);
82
83void cumulate(double& accu, const double value);
84
85void checkOldOutputFiles(const mio::Date& i_date, const std::string& stationID);
86
87double getPerpSensorPosition(const bool& useSoilLayers, const double& z_vert, const double& hs_ref, const double& Ground, const double& SlopeAngle);
88
95
96
97#define M_TO_CM( l ) ( (l) * 100. ) // meter to centimeters
98#define M_TO_MM( l ) ( (l) * 1000. ) // meter to millimeters
99#define CM_TO_M( l ) ( (l) / 100. ) // centimeter to meter
100#define MM_TO_M( l ) ( (l) / 1000. ) // millimeter to meter
101#define MM_TO_CM( l ) ( (l) / 10. ) // millimeter to centimeter
103#define D_TO_H( t ) ( (t) * 24. ) // day to hours
104#define D_TO_M( t ) ( (t) * 1440. ) // day to minutes
105#define D_TO_S( t ) ( (t) * 86400. ) // day to seconds
106#define H_TO_D( t ) ( (t) / 24.0 ) // hour to day
107#define H_TO_M( t ) ( (t) * 60. ) // hour to minutes
108#define H_TO_S( t ) ( (t) * 3600. ) // hour to seconds
109#define M_TO_D( t ) ( (t) / 1440. ) // minute to day
110#define M_TO_H( t ) ( (t) / 60. ) // minute to hour
111#define M_TO_S( t ) ( (t) * 60. ) // minute to seconds
112#define S_TO_D( t ) ( (t) / 86400. ) // second to day
113#define S_TO_H( t ) ( (t) / 3600. ) // second to hour
114#define S_TO_M( t ) ( (t) / 60. ) // second to minute
116
117
118#endif //End of Utils.h
void deflateInflate(const CurrentMeteo &Mdata, SnowStation &Xdata, double &dhs_corr, double &mass_corr, const bool &prn_check)
Deflates or inflates the snowpack for warning service purposes.
Definition: Utils.cc:483
double logisticFunction(const double input, const double threshold, const double width)
Logistic function.
Definition: Utils.cc:581
void cumulate(double &accu, const double value)
Cumulate if and only if value is defined.
Definition: Utils.cc:594
void deleteOldOutputFiles(const std::string &outdir, const std::string &experiment, const std::string &stationID, const unsigned int &nSlopes, const std::vector< std::string > &vecExtensions)
Delete old output files (*.sno, *.ini) from outdir.
Definition: Utils.cc:168
void averageFluxTimeSeries(const size_t &n_steps, const bool &useCanopyModel, SurfaceFluxes &Sdata, SnowStation &Xdata)
Averages energy fluxes.
Definition: Utils.cc:277
void typeToCode(int *F1, int *F2, int *F3, int type)
Decompose type in its constituents At present decomposition into Swiss numerical code TODO Adapt to ...
Definition: Utils.cc:298
size_t findUpperNode(const double &z, const std::vector< NodeData > &Ndata, const size_t &nN)
Returns number of lowest node above a given position z perpendicular to slope (m) .
Definition: Utils.cc:233
double getPerpSensorPosition(const bool &useSoilLayers, const double &z_vert, const double &hs_ref, const double &Ground, const double &SlopeAngle)
Returns sensor position perpendicular to slope (m). Negative vertical height indicates depth from eit...
Definition: Utils.cc:258
bool booleanTime(const double &JulianDate, double days_between, const double &start, const double &calculation_step_length)
Determines whether a certain time has been reached (e.g. to dump output) The function returns TRUE wh...
Definition: Utils.cc:138
void prn_msg(const char *fileAndPath, const int theLine, const char *msg_type, const mio::Date &date_in, const char *format,...)
Print a message to screen (see p.351-352[,469] in C: The Complete Reference) If date_in is Date(),...
Definition: Utils.cc:60
double unitConversion(const double val, char *unitIn, char *unitOut)
Performs simple unit conversion (supports temperature, prefixes and exponents)
Definition: Utils.cc:315
double forcedErosion(const double hs, SnowStation &Xdata)
Forced erosion of a missed event.
Definition: Utils.cc:448
bool massBalanceCheck(const SnowStation &Xdata, const SurfaceFluxes &Sdata, double &tot_mass_in)
Performs mass balance check either before or after a calculation time step.
Definition: Utils.cc:398
void checkOldOutputFiles(const mio::Date &i_date, const std::string &stationID)
CurrentMeteo is the class of interpolated meteo data for the current calculation time step It contai...
Definition: DataClasses.h:83
Definition: DataClasses.h:604
Definition: DataClasses.h:733
Return the library version.
Definition: Utils.cc:33
std::string getLibVersion()
Definition: Utils.cc:34