Index: /issm/trunk-jpl/src/c/classes/Inputs/TransientInput.cpp
===================================================================
--- /issm/trunk-jpl/src/c/classes/Inputs/TransientInput.cpp	(revision 27179)
+++ /issm/trunk-jpl/src/c/classes/Inputs/TransientInput.cpp	(revision 27180)
@@ -9,4 +9,5 @@
 #endif
 
+#include <numeric>
 #include "./TransientInput.h"
 #include "./TriaInput.h"
@@ -208,5 +209,5 @@
 
 	/*This is a new time step! we need to add it to the list*/
-	if(this->numtimesteps>0 && time<this->timesteps[this->numtimesteps-1]) _error_("timestep values must increase sequentially");
+	if(this->numtimesteps>0 && time<this->timesteps[this->numtimesteps-1]) _error_("timestep values must increase sequentially, here " << this->timesteps[this->numtimesteps-1] <<" is the last step but smaller than the preceding "<< time<<"\n");
 
 	IssmDouble *old_timesteps = NULL;
@@ -433,7 +434,8 @@
 
 	/*First, recover current time from parameters: */
-	bool linear_interp,cycle;
+	bool linear_interp,average,cycle;
 	IssmDouble dt;
 	this->parameters->FindParam(&linear_interp,TimesteppingInterpForcingEnum);
+	this->parameters->FindParam(&average,TimesteppingAverageForcingEnum);
 	this->parameters->FindParam(&cycle,TimesteppingCycleForcingEnum);
 	this->parameters->FindParam(&dt,TimesteppingTimeStepEnum);          /*transient core time step*/
@@ -471,6 +473,9 @@
 
 	/*Figure step out*/
-	int offset;
+	int offset, prevoffset;
 	if(!binary_search(&offset,time,this->timesteps,this->numtimesteps)){
+		_error_("Input not found (is TransientInput sorted ?)");
+	}
+	if(!binary_search(&prevoffset,time-dt,this->timesteps,this->numtimesteps)){
 		_error_("Input not found (is TransientInput sorted ?)");
 	}
@@ -488,4 +493,22 @@
 		this->current_step = 0.;
 		this->current_input = this->inputs[0]->copy();
+
+	}
+	else if(offset-prevoffset>1 && prevoffset >=0 && average){
+		/*get values for the last time: */
+		_assert_(time>=this->timesteps[offset]);
+
+		/*If already processed return*/
+		if(this->current_step==reCast<IssmDouble>(offset)) return;
+
+		/*Prepare input*/
+		if(this->current_input) delete this->current_input;
+		this->current_step  = reCast<IssmDouble>(offset);
+
+		this->current_input = this->inputs[prevoffset]->copy();
+		for(int i=prevoffset+1;i<offset;i++) {
+			this->current_input->AXPY(this->inputs[i],+1.0);
+		}
+		this->current_input->Scale(1./(offset-prevoffset));
 
 	}
@@ -535,4 +558,5 @@
 	IssmDouble  timespan,mid_step;
 	int         found,start_offset,end_offset,input_offset;
+
 
 	/*go through the timesteps, and grab offset for start and end*/
@@ -573,6 +597,5 @@
 			_assert_(dt>0.);
 		}
-		Input* stepinput=this->inputs[offset+1];
-
+		Input* stepinput=this->inputs[offset+1]->copy();
 		switch(averaging_method){
 			case 0: /*Arithmetic mean*/
@@ -608,4 +631,5 @@
 				_error_("averaging method is not recognised");
 		}
+		delete stepinput;
 		dtsum+=dt;
 		offset+=1;
