Skip to content

Commit

Permalink
POSIXct dates
Browse files Browse the repository at this point in the history
  • Loading branch information
miquelcaceres committed Jun 3, 2024
1 parent fdff233 commit 4b7db98
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Package: medfate
Type: Package
Title: Mediterranean Forest Simulation
Version: 4.3.2
Date: 2024-05-31
Date: 2024-06-03
Authors@R: c(
person('Miquel', 'De Cáceres', role=c('aut','cre', 'cph'),
email='miquelcaceres@gmail.com', comment = c(ORCID = "0000-0001-7132-2080")),
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Default control parameters changed
- Subdaily soil temperature change limited for stability with thin first layer
- Column 'Strict' added to SpParamsDefinition
- Detection of POSIXct date columns

# Version 4.3.1
- Functions forest2spwbInput/forest2growthInput deprecated. Preferred function names are spwbInput/growthInput.
Expand Down
4 changes: 4 additions & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -3405,6 +3405,10 @@ soil_temperatureChange <- function(widths, Temp, sand, clay, W, Theta_SAT, Theta
.Call(`_medfate_temperatureChange`, widths, Temp, sand, clay, W, Theta_SAT, Theta_FC, Gdown, tstep)
}

.getWeatherDates <- function(meteo) {
.Call(`_medfate_getWeatherDates`, meteo)
}

#' Single-day simulation
#'
#' Function \code{spwb_day} performs water balance for a single day and \code{growth_day}
Expand Down
21 changes: 21 additions & 0 deletions inst/include/medfate_RcppExports.h
Original file line number Diff line number Diff line change
Expand Up @@ -1218,6 +1218,27 @@ namespace medfate {
return Rcpp::as<NumericVector >(rcpp_result_gen);
}

inline CharacterVector _getWeatherDates(DataFrame meteo) {
typedef SEXP(*Ptr__getWeatherDates)(SEXP);
static Ptr__getWeatherDates p__getWeatherDates = NULL;
if (p__getWeatherDates == NULL) {
validateSignature("CharacterVector(*_getWeatherDates)(DataFrame)");
p__getWeatherDates = (Ptr__getWeatherDates)R_GetCCallable("medfate", "_medfate__getWeatherDates");
}
RObject rcpp_result_gen;
{
RNGScope RCPP_rngScope_gen;
rcpp_result_gen = p__getWeatherDates(Shield<SEXP>(Rcpp::wrap(meteo)));
}
if (rcpp_result_gen.inherits("interrupted-error"))
throw Rcpp::internal::InterruptedException();
if (Rcpp::internal::isLongjumpSentinel(rcpp_result_gen))
throw Rcpp::LongjumpException(rcpp_result_gen);
if (rcpp_result_gen.inherits("try-error"))
throw Rcpp::exception(Rcpp::as<std::string>(rcpp_result_gen).c_str());
return Rcpp::as<CharacterVector >(rcpp_result_gen);
}

inline List spwb_day(List x, CharacterVector date, NumericVector meteovec, double latitude, double elevation, double slope = NA_REAL, double aspect = NA_REAL, double runon = 0.0, Nullable<NumericVector> lateralFlows = R_NilValue, double waterTableDepth = NA_REAL, bool modifyInput = true) {
typedef SEXP(*Ptr_spwb_day)(SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP,SEXP);
static Ptr_spwb_day p_spwb_day = NULL;
Expand Down
37 changes: 37 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5027,6 +5027,40 @@ RcppExport SEXP _medfate_temperatureChange(SEXP widthsSEXP, SEXP TempSEXP, SEXP
UNPROTECT(1);
return rcpp_result_gen;
}
// getWeatherDates
CharacterVector getWeatherDates(DataFrame meteo);
static SEXP _medfate_getWeatherDates_try(SEXP meteoSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::traits::input_parameter< DataFrame >::type meteo(meteoSEXP);
rcpp_result_gen = Rcpp::wrap(getWeatherDates(meteo));
return rcpp_result_gen;
END_RCPP_RETURN_ERROR
}
RcppExport SEXP _medfate_getWeatherDates(SEXP meteoSEXP) {
SEXP rcpp_result_gen;
{
Rcpp::RNGScope rcpp_rngScope_gen;
rcpp_result_gen = PROTECT(_medfate_getWeatherDates_try(meteoSEXP));
}
Rboolean rcpp_isInterrupt_gen = Rf_inherits(rcpp_result_gen, "interrupted-error");
if (rcpp_isInterrupt_gen) {
UNPROTECT(1);
Rf_onintr();
}
bool rcpp_isLongjump_gen = Rcpp::internal::isLongjumpSentinel(rcpp_result_gen);
if (rcpp_isLongjump_gen) {
Rcpp::internal::resumeJump(rcpp_result_gen);
}
Rboolean rcpp_isError_gen = Rf_inherits(rcpp_result_gen, "try-error");
if (rcpp_isError_gen) {
SEXP rcpp_msgSEXP_gen = Rf_asChar(rcpp_result_gen);
UNPROTECT(1);
Rf_error("%s", CHAR(rcpp_msgSEXP_gen));
}
UNPROTECT(1);
return rcpp_result_gen;
}
// spwbDay
List spwbDay(List x, CharacterVector date, NumericVector meteovec, double latitude, double elevation, double slope, double aspect, double runon, Nullable<NumericVector> lateralFlows, double waterTableDepth, bool modifyInput);
static SEXP _medfate_spwbDay_try(SEXP xSEXP, SEXP dateSEXP, SEXP meteovecSEXP, SEXP latitudeSEXP, SEXP elevationSEXP, SEXP slopeSEXP, SEXP aspectSEXP, SEXP runonSEXP, SEXP lateralFlowsSEXP, SEXP waterTableDepthSEXP, SEXP modifyInputSEXP) {
Expand Down Expand Up @@ -5647,6 +5681,7 @@ static int _medfate_RcppExport_validate(const char* sig) {
signatures.insert("NumericVector(*soil_thermalConductivity)(DataFrame,String)");
signatures.insert("NumericVector(*soil_temperatureGradient)(NumericVector,NumericVector)");
signatures.insert("NumericVector(*soil_temperatureChange)(NumericVector,NumericVector,NumericVector,NumericVector,NumericVector,NumericVector,NumericVector,double,double)");
signatures.insert("CharacterVector(*.getWeatherDates)(DataFrame)");
signatures.insert("List(*spwb_day)(List,CharacterVector,NumericVector,double,double,double,double,double,Nullable<NumericVector>,double,bool)");
signatures.insert("List(*.defineSPWBDailyOutput)(double,double,double,double,CharacterVector,List)");
signatures.insert("void(*.fillSPWBDailyOutput)(List,List,List,int)");
Expand Down Expand Up @@ -5715,6 +5750,7 @@ RcppExport SEXP _medfate_RcppExport_registerCCallable() {
R_RegisterCCallable("medfate", "_medfate_soil_thermalConductivity", (DL_FUNC)_medfate_thermalConductivity_try);
R_RegisterCCallable("medfate", "_medfate_soil_temperatureGradient", (DL_FUNC)_medfate_temperatureGradient_try);
R_RegisterCCallable("medfate", "_medfate_soil_temperatureChange", (DL_FUNC)_medfate_temperatureChange_try);
R_RegisterCCallable("medfate", "_medfate_.getWeatherDates", (DL_FUNC)_medfate_getWeatherDates_try);
R_RegisterCCallable("medfate", "_medfate_spwb_day", (DL_FUNC)_medfate_spwbDay_try);
R_RegisterCCallable("medfate", "_medfate_.defineSPWBDailyOutput", (DL_FUNC)_medfate_defineSPWBDailyOutput_try);
R_RegisterCCallable("medfate", "_medfate_.fillSPWBDailyOutput", (DL_FUNC)_medfate_fillSPWBDailyOutput_try);
Expand Down Expand Up @@ -5984,6 +6020,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_medfate_thermalConductivity", (DL_FUNC) &_medfate_thermalConductivity, 2},
{"_medfate_temperatureGradient", (DL_FUNC) &_medfate_temperatureGradient, 2},
{"_medfate_temperatureChange", (DL_FUNC) &_medfate_temperatureChange, 9},
{"_medfate_getWeatherDates", (DL_FUNC) &_medfate_getWeatherDates, 1},
{"_medfate_spwbDay", (DL_FUNC) &_medfate_spwbDay, 11},
{"_medfate_defineSPWBDailyOutput", (DL_FUNC) &_medfate_defineSPWBDailyOutput, 6},
{"_medfate_fillSPWBDailyOutput", (DL_FUNC) &_medfate_fillSPWBDailyOutput, 4},
Expand Down
18 changes: 18 additions & 0 deletions src/spwb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,23 @@
#include <meteoland.h>
using namespace Rcpp;

// [[Rcpp::export(".getWeatherDates")]]
CharacterVector getWeatherDates(DataFrame meteo){
CharacterVector dateStrings;
String dateColumnName = NA_STRING;
bool is_date_column = false;
if(meteo.containsElementNamed("dates")) {
dateColumnName = "dates";
is_date_column = true;
} else if(meteo.containsElementNamed("date")) {
dateColumnName = "date";
is_date_column = true;
} else if(meteo.containsElementNamed("Dates")) {
dateColumnName = "Dates";
is_date_column = true;
} else if(meteo.containsElementNamed("Date")) {
dateColumnName = "Date";
is_date_column = true;
}
if(is_date_column){
RObject vector = Rcpp::as<Rcpp::RObject>(meteo[dateColumnName]);
Expand All @@ -50,8 +57,19 @@ CharacterVector getWeatherDates(DataFrame meteo){
dS[i] = d.format("%Y-%m-%d");
}
dateStrings = dS;
} else if(vector.inherits("POSIXct")) {
DatetimeVector datetimeVector = Rcpp::as<Rcpp::DatetimeVector>(vector);
CharacterVector dS(datetimeVector.size(), NA_STRING);
for(int i=0;i< datetimeVector.size();i++) {
Datetime dt = datetimeVector[i];
Date d(dt.getYear(), dt.getMonth(), dt.getDay());
dS[i] = d.format("%Y-%m-%d");
}
dateStrings = dS;
} else if(is<StringVector>(vector)) {
dateStrings = Rcpp::as<Rcpp::StringVector>(vector);
} else {
stop("Could not parse date column.");
}
} else {
dateStrings = meteo.attr("row.names");
Expand Down

0 comments on commit 4b7db98

Please sign in to comment.