Index: ../trunk-jpl/src/c/classes/Misfit.h
===================================================================
--- ../trunk-jpl/src/c/classes/Misfit.h	(revision 19062)
+++ ../trunk-jpl/src/c/classes/Misfit.h	(revision 19063)
@@ -14,7 +14,10 @@
 #include "./FemModel.h"
 #include "../modules/SurfaceAreax/SurfaceAreax.h"
 #include "../classes/Params/Parameters.h"
+#include "../classes/Inputs/Input.h"
+#include "../classes/gauss/Gauss.h"
 /*}}}*/
+IssmDouble OutputDefinitionsResponsex(FemModel* femmodel,int output_enum);
 
 class Misfit: public Object, public Definition{
 
@@ -26,6 +29,7 @@
 		int         observation_enum;
 		int         weights_enum;
 		char*       timeinterpolation;
+		bool        local;     
 		
 		IssmDouble  misfit; //value carried over in time.
 		int         lock; // if lock is on, we just return the value stored in "misfit".  this is used so we don't compute misfit past the final_time
@@ -39,12 +43,13 @@
 			this->observation_enum = UNDEF;
 			this->weights_enum = UNDEF;
 			this->timeinterpolation=NULL;
+			this->local=true;
 			this->misfit=0;
 			this->lock=0;
 
 		}
 		/*}}}*/
-		Misfit(char* in_name, int in_definitionenum, int in_model_enum, int in_observation_enum, char* in_timeinterpolation, int in_weights_enum){/*{{{*/
+		Misfit(char* in_name, int in_definitionenum, int in_model_enum, int in_observation_enum, char* in_timeinterpolation, bool in_local, int in_weights_enum){/*{{{*/
 
 			this->definitionenum=in_definitionenum;
 			this->name   = xNew<char>(strlen(in_name)+1);
@@ -56,6 +61,7 @@
 			this->model_enum=in_model_enum;
 			this->observation_enum=in_observation_enum;
 			this->weights_enum=in_weights_enum;
+			this->local=in_local;
 			
 			this->misfit=0;
 			this->lock=0;
@@ -75,6 +81,7 @@
 			_printf_("    observation_enum: " << observation_enum << " " << EnumToStringx(observation_enum) << "\n");
 			_printf_("    weights_enum: " << weights_enum << " " << EnumToStringx(weights_enum) << "\n");
 			_printf_("    timeinterpolation: " << timeinterpolation << "\n");
+			_printf_("    local: " << local << "\n");
 		}
 		/*}}}*/
 		void DeepEcho(void){/*{{{*/
@@ -90,7 +97,7 @@
 		}
 		/*}}}*/
 		Object* copy() {/*{{{*/
-			Misfit* mf = new Misfit(this->name,this->definitionenum, this->model_enum,this->observation_enum,this->timeinterpolation,this->weights_enum);
+			Misfit* mf = new Misfit(this->name,this->definitionenum, this->model_enum,this->observation_enum,this->timeinterpolation,this->local,this->weights_enum);
 			mf->misfit=this->misfit;
 			mf->lock=this->lock;
 			return (Object*) mf;
@@ -111,48 +118,76 @@
 		}
 		/*}}}*/
 		 IssmDouble Response(FemModel* femmodel){/*{{{*/
-
-			 int i;
-			 IssmDouble misfit_t=0.;
-			 IssmDouble all_misfit_t=0.;
+				 
+			 /*diverse: */
+			 IssmDouble time,starttime,finaltime;
 			 IssmDouble dt;
-			 IssmDouble area_t=0.;
-			 IssmDouble all_area_t;
-			 IssmDouble time,starttime,finaltime;
-
+			 
+			 /*recover time parameters: */
 			 femmodel->parameters->FindParam(&starttime,TimesteppingStartTimeEnum);
 			 femmodel->parameters->FindParam(&finaltime,TimesteppingFinalTimeEnum);
 			 femmodel->parameters->FindParam(&time,TimeEnum);
+			 femmodel->parameters->FindParam(&dt,TimesteppingTimeStepEnum);
 
-			 /*If we are locked, return time average: */
-			 if(this->lock)return misfit/(time-starttime);
+			 if (this->local){ /*local computation: {{{*/
 
+				 int i;
+				 IssmDouble misfit_t=0.;
+				 IssmDouble all_misfit_t=0.;
+				 IssmDouble area_t=0.;
+				 IssmDouble all_area_t;
 
-			 for(i=0;i<femmodel->elements->Size();i++){
-				 Element* element=(Element*)femmodel->elements->GetObjectByOffset(i);
-				 misfit_t+=element->Misfit(model_enum,observation_enum,weights_enum);
-				 area_t+=element->MisfitArea(weights_enum);
-			 }
+			
+				 /*If we are locked, return time average: */
+				 if(this->lock)return misfit/(time-starttime);
 
-			 ISSM_MPI_Allreduce ( (void*)&misfit_t,(void*)&all_misfit_t,1,ISSM_MPI_DOUBLE,ISSM_MPI_SUM,IssmComm::GetComm());
-			 ISSM_MPI_Allreduce ( (void*)&area_t,(void*)&all_area_t,1,ISSM_MPI_DOUBLE,ISSM_MPI_SUM,IssmComm::GetComm());
-			 area_t=all_area_t;
-			 misfit_t=all_misfit_t;
-			 
-			 /*Divide by surface area if not nill!: */
-			 if (area_t!=0) misfit_t=misfit_t/area_t;
+				 for(i=0;i<femmodel->elements->Size();i++){
+					 Element* element=(Element*)femmodel->elements->GetObjectByOffset(i);
+					 misfit_t+=element->Misfit(model_enum,observation_enum,weights_enum);
+					 area_t+=element->MisfitArea(weights_enum);
+				 }
 
-			 /*Recover delta_t: */
-			 femmodel->parameters->FindParam(&dt,TimesteppingTimeStepEnum);
+				 ISSM_MPI_Allreduce ( (void*)&misfit_t,(void*)&all_misfit_t,1,ISSM_MPI_DOUBLE,ISSM_MPI_SUM,IssmComm::GetComm());
+				 ISSM_MPI_Allreduce ( (void*)&area_t,(void*)&all_area_t,1,ISSM_MPI_DOUBLE,ISSM_MPI_SUM,IssmComm::GetComm());
+				 area_t=all_area_t;
+				 misfit_t=all_misfit_t;
+				 
+				 /*Divide by surface area if not nill!: */
+				 if (area_t!=0) misfit_t=misfit_t/area_t;
 
-			 /*Add this time's contribution to curent misfit: */
-			 misfit+=dt*misfit_t;
+				 /*Add this time's contribution to curent misfit: */
+				 misfit+=dt*misfit_t;
 
-			 /*Do we lock? i.e. are we at final_time? :*/
-			 if(time==finaltime)this->lock=1;
+				 /*Do we lock? i.e. are we at final_time? :*/
+				 if(time==finaltime)this->lock=1;
 
-			 /*What we return is the value of misfit / time: */
-			 return misfit/(time-starttime);
+				 /*What we return is the value of misfit / time: */
+				 return misfit/(time-starttime);
+			 } /*}}}*/
+			 else{ /*global computation: {{{ */
+				 
+				 IssmDouble model, observation;
+				 
+				 /*If we are locked, return time average: */
+				 if(this->lock)return misfit/(time-starttime);
+
+				 /*First, the global  model response: */
+				 model=OutputDefinitionsResponsex(femmodel,this->model_enum);
+				 /*Now, the observation is buried inside the elements, go fish it in the first element (cludgy, needs fixing): */
+				 Element* element=(Element*)femmodel->elements->GetObjectByOffset(0); _assert_(element);
+				 Input* input = element->GetInput(observation_enum); _assert_(input);
+				 input->GetInputAverage(&observation);
+				 
+				 /*Add this time's contribution to curent misfit: */
+				 misfit+=dt*(model-observation);
+				 
+				 /*Do we lock? i.e. are we at final_time? :*/
+				 if(time==finaltime)this->lock=1;
+				 
+				 /*What we return is the value of misfit / time: */
+				 return misfit/(time-starttime);
+			 } /*}}}*/
+
 		 }
 			/*}}}*/
 };
