From 4261478a5d2914ca85f6335e0a0ae8e1f5da01cb Mon Sep 17 00:00:00 2001
From: Mauricio Zambrano-Bigiarini <hzambran@users.noreply.github.com>
Date: Wed, 17 Oct 2012 16:12:46 +0000
Subject: [PATCH] =?UTF-8?q?hydroPSO:=20new=20output=20file=20'BestMod=1BOH?=
 =?UTF-8?q?el=5Fout.txt'?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 DESCRIPTION   |  4 ++--
 NEWS          |  3 +++
 R/PSO_v2012.R | 32 ++++++++++++++++++++++----------
 3 files changed, 27 insertions(+), 12 deletions(-)

diff --git a/DESCRIPTION b/DESCRIPTION
index 0cdcac4..e6ad88e 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-11
-Date: 2012-10-15
+Version: 0.1-58-12
+Date: 2012-10-17
 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 ee1e325..528a9cd 100755
--- a/NEWS
+++ b/NEWS
@@ -8,6 +8,9 @@ NEWS/ChangeLog for hydroPSO
                                * new confinement of the velocity ( V[t+1] = -0.5 * V[t], when x[t+1] > x_max | x[t+1] < x_min )
                                * optional normalisation of parameter values
         
+                            -) now it handles models with sub-daily time step and with sub-daily observations (thanks to Olda Rakovec !)
+                            -) new output file 'BestModel_out.txt', with the model outputs corresponding to the best parameter set. 
+                               Only available when 'fn.name=="hydromod"'
                             -) new 'normalise' parameter for the 'control' variable, in order to improve the performance when the search space is not 
                                a hypercube
                             -) argument 'method' now allows the following 2 new values: 'spso2007' and 'spso2011', which set the values of the PSO 
diff --git a/R/PSO_v2012.R b/R/PSO_v2012.R
index b92080c..6deb5eb 100755
--- a/R/PSO_v2012.R
+++ b/R/PSO_v2012.R
@@ -2899,32 +2899,44 @@ hydroPSO <- function(
       model.FUN.args <- modifyList(model.FUN.args, 
 				   list(param.values=out[["par"]])
 				   ) 
-      hydromod.out   <- do.call(model.FUN, as.list(model.FUN.args))   
+      hydromod.out   <- do.call(model.FUN, as.list(model.FUN.args))              
 
+      # Writing observations and best model output
       if ("obs" %in% names(model.FUN.args)) {      
          ifelse(date.fmt.exists, date.fmt <- model.FUN.args[["date.fmt"]], date.fmt <- "%Y-%m-%d")         
          if ( gof.Ini.exists | gof.Fin.exists ) 
              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 )      
-        fname <- paste(file.path(drty.out), "/", "Observations.txt", sep="") 	
-	obs <- model.FUN.args[["obs"]] 
+                     subdaily.date.fmt <- TRUE, subdaily.date.fmt <- FALSE )      
+                     
+        obs <- model.FUN.args[["obs"]]
+        sim <- hydromod.out[["sim"]]  
+        
+        obs.fname <- paste(file.path(drty.out), "/", "Observations.txt", sep="") 
+        sim.fname <- paste(file.path(drty.out), "/", "BestModel_out.txt", sep="") 	
+	 
         if (is.zoo(obs)) {
           if (gof.Ini.exists) {
-            ifelse(subdaily, gof.Ini <- as.POSIXct(model.FUN.args[["gof.Ini"]], format=date.fmt),
-                             gof.Ini <- as.Date(model.FUN.args[["gof.Ini"]], format=date.fmt) )
+            ifelse(subdaily.date.fmt, gof.Ini <- as.POSIXct(model.FUN.args[["gof.Ini"]], format=date.fmt),
+                                      gof.Ini <- as.Date(model.FUN.args[["gof.Ini"]], format=date.fmt) )
             obs <- window(obs, start=gof.Ini)
+            sim <- window(sim, start=gof.Ini)
           } # IF end
           if (gof.Fin.exists) {
-            ifelse(subdaily, gof.Fin <- as.POSIXct(model.FUN.args[["gof.Fin"]], format=date.fmt),
-                             gof.Fin <- as.Date(model.FUN.args[["gof.Fin"]], format=date.fmt) )
+            ifelse(subdaily.date.fmt, gof.Fin <- as.POSIXct(model.FUN.args[["gof.Fin"]], format=date.fmt),
+                                      gof.Fin <- as.Date(model.FUN.args[["gof.Fin"]], format=date.fmt) )
             obs <- window(obs, end=gof.Fin)
+            sim <- window(sim, end=gof.Fin)
           } # IF end
-          write.zoo(x=obs, file=fname)
+          write.zoo(x=obs, file=obs.fname)
+          write.zoo(x=sim, file=sim.fname)
         } else {
             obs <- cbind(1:length(obs), obs)
-            write.table(obs, file=fname, col.names=FALSE, row.names=FALSE, sep="  ", quote=FALSE)
+            write.table(obs, file=obs.fname, col.names=FALSE, row.names=FALSE, sep="  ", quote=FALSE)
+            
+            sim <- cbind(1:length(sim), sim)
+            write.table(obs, file=sim.fname, col.names=FALSE, row.names=FALSE, sep="  ", quote=FALSE)
           } # ELSE end
           
       } # IF end
-- 
GitLab