From d090973420d3b7c33efbeb347e8bd2e36964745d Mon Sep 17 00:00:00 2001
From: Mauricio Zambrano-Bigiarini <hzambran@users.noreply.github.com>
Date: Mon, 15 Oct 2012 14:30:58 +0000
Subject: [PATCH] hydromod.R: sub-daily capable

---
 DESCRIPTION  |  4 ++--
 NEWS         |  3 ++-
 R/hydromod.R | 34 ++++++++++++++++++++++++----------
 3 files changed, 28 insertions(+), 13 deletions(-)

diff --git a/DESCRIPTION b/DESCRIPTION
index 5e07faa..0cdcac4 100755
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -1,8 +1,8 @@
 Package: hydroPSO
 Type: Package
 Title: Model-Independent Particle Swarm Optimisation for Environmental Models
-Version: 0.1-58-10
-Date: 2012-09-28
+Version: 0.1-58-11
+Date: 2012-10-15
 Authors@R: c(person("Mauricio", "Zambrano-Bigiarini", email="mzb.devel@gmail.com", role=c("aut","cre")), person("Rodrigo", "Rojas", email="Rodrigo.RojasMujica@gmail.com", role=c("ctb")) )
 Maintainer: Mauricio Zambrano-Bigiarini <mzb.devel@gmail.com>
 Description: This package implements a state-of-the-art version of the Particle Swarm Optimisation (PSO) algorithm (SPSO-2011 and SPSO-2007 capable), with a special focus on the calibration of environmental models. hydroPSO is model-independent, allowing the user to easily interface any model code with the calibration engine (PSO). It includes a series of controlling options and PSO variants to fine-tune the performance of the calibration engine to different calibration problems. An advanced sensitivity analysis function together with user-friendly plotting summaries facilitate the interpretation and assessment of the calibration results. Bugs reports/comments/questions are very welcomed.
diff --git a/NEWS b/NEWS
index 24d80ef..8e8f317 100755
--- a/NEWS
+++ b/NEWS
@@ -70,7 +70,8 @@ NEWS/ChangeLog for hydroPSO
         o 'plot_out'      : -) argument 'MinMax' is not required any more
                             -) argument 'sim' may also be 'integer' (before it had to be 'numeric' != 'integer')
                             
-        o 'hydromod'      : -) if the 'hydroGOF' package is not available, a simple correlation plot is produced between observations and 
+        o 'hydromod'      : -) now it is able to handle sub-daily models
+                            -) if the 'hydroGOF' package is not available, a simple correlation plot is produced between observations and 
                                the best simulation
                             -) improved documentation        
         
diff --git a/R/hydromod.R b/R/hydromod.R
index d4d8ff4..9b42985 100755
--- a/R/hydromod.R
+++ b/R/hydromod.R
@@ -22,6 +22,7 @@
 # Updates: 20-Dec-2010                                                         #
 #          19-Jan-2011 ; 22-Jan-2011 ; 02-Feb-2011 ; 11-May-2011               #
 #          13-Jan-2012 ; 16-Jan-2012 ; 23-Jan-2012 ; 02-May-2012 ; 12-Oct-2012 #
+#          15-Oct-2012                                                         #
 ################################################################################
 hydromod <- function(
                      param.values,                 # Numeric vector with the paramter values that will be used in the input files of the hydrological model
@@ -150,25 +151,38 @@ hydromod <- function(
   # In case different dates are given for 'sim', 'obs', 'gof.Ini', gof.Fin', 
   # 'sim' and 'obs' are subset to the time period selected for the GoF function 
   
+  if ( !missing(gof.Ini) | !missing(gof.Fin) ) 
+    ifelse ( grepl("%H", date.fmt, fixed=TRUE) | grepl("%M", date.fmt, fixed=TRUE) |
+             grepl("%S", date.fmt, fixed=TRUE) | grepl("%I", date.fmt, fixed=TRUE) |
+             grepl("%p", date.fmt, fixed=TRUE) | grepl("%X", date.fmt, fixed=TRUE),
+             subdaily <- TRUE, subdaily <- FALSE )
+  
   if (!missing(gof.Ini) ) {
+      ifelse(subdaily, gof.Ini <- as.POSIXct(gof.Ini, format=date.fmt),
+                       gof.Ini <- as.Date(gof.Ini, format=date.fmt) )
+                   
       if (!zoo::is.zoo(obs)) {
-        stop( "Invalid argument: 'obs' must be a zoo or xts object to use 'gof.Ini' !" )
-      } else obs <- window(obs, start=as.Date(gof.Ini, format=date.fmt) )
+        stop( "Invalid argument: 'obs' must be a zoo or xts object to use 'gof.Ini' !")
+      } else obs <- zoo:window.zoo(obs, start=gof.Ini) 
       if (!zoo::is.zoo(sim)) {
-        stop( "Invalid argument: 'sim' must be a zoo or xts object to use 'gof.Ini' !" )
-      } else sim <- window(sim, start=as.Date(gof.Ini, format=date.fmt) )
+        stop( "Invalid argument: 'sim' must be a zoo or xts object to use 'gof.Ini' !")
+      } else sim <- window(sim, start=gof.Ini)
+                    
   } # IF end 
    
   if (!missing(gof.Fin) ) {
+    ifelse(subdaily, gof.Fin <- as.POSIXct(gof.Fin, format=date.fmt),
+                     gof.Fin <- as.Date(gof.Fin, format=date.fmt) )
+                       
     if (gof.Fin < gof.Ini) {
       stop( "Invalid argument: 'gof.Fin < gof.Ini' (", gof.Fin, " < ", gof.Ini, ")" )
     } else { 
-           if (!zoo::is.zoo(obs)) {
-             stop( "Invalid argument: 'obs' must be a zoo or xts object to use 'gof.Fin' !" )
-           } else obs <- window(obs, end=as.Date(gof.Fin, format=date.fmt) )
-           if (!zoo::is.zoo(sim)) {
-             stop( "Invalid argument: 'sim' must be a zoo or xts object to use 'gof.Fin' !" )
-           } else sim <- window(sim, end=as.Date(gof.Fin, format=date.fmt) )
+             if (!zoo::is.zoo(obs)) {
+               stop( "Invalid argument: 'obs' must be a zoo or xts object to use 'gof.Fin' !" )
+             } else obs <- window(obs, end=gof.Fin)
+             if (!zoo::is.zoo(sim)) {
+               stop( "Invalid argument: 'sim' must be a zoo or xts object to use 'gof.Fin' !" )
+             } else sim <- window(sim, end=gof.Fin)
            } # IF end
   } # IF end  
   
-- 
GitLab