Index: ../trunk-jpl/src/m/contrib/gravity/vfsa_mpi.cpp
===================================================================
--- ../trunk-jpl/src/m/contrib/gravity/vfsa_mpi.cpp	(revision 18549)
+++ ../trunk-jpl/src/m/contrib/gravity/vfsa_mpi.cpp	(revision 18550)
@@ -132,7 +132,7 @@
 void GSLsquarefit(Matrix** pX,Matrix* A,Matrix* B);
 double signe(double a);
 void filtergrav(Matrix* A,Matrix* Ain,double ctr,double sd,int mx,int my);
-void newmodelgen(Matrix* m0,Matrix* m1,Matrix* bathy,Matrix* icethick,int mx,int my,double T,double ptval,double mmax,double mmax2,double ctr,double sd);
+void newmodelgen(Matrix* m0,Matrix* m1,Matrix* bathy,Matrix* icethick,int mx,int my,double T,double ptval,double mmax,double mmax2,double ctr,double sd, Matrix *landmask);
 double coolshed(double T0,double k,double c,double D);
 /*}}}*/
 
@@ -164,11 +164,11 @@
 	// Pobs->Echo();
 
 
-	Matrix *Pp=new Matrix(mx*my,2); /* data positions */
+	Matrix *Pp=new Matrix(mx*my,2); /* prisms positions */
 	makep(Pp,mx,my,dx,dy);
 	// Pp->Echo();
 
-	double  rhoi = 917;           /* ice density     */
+	double  rhoi = 890;           /* ice density     */
 	double  rhow = 1030;          /* water density   */
 	// double  rhos = 2013;		      /* sediment density */
 	double  rhoc = 2670;          /* bedrock density */
@@ -184,6 +184,8 @@
 	rho2->SetValue(0,0,rhoi-rhoc);
 	rho2->SetValue(0,1,rhow-rhoc);
 
+	double dlevel=860;         /* level of data acquisition */
+
 	double ctr=1;            /* parameter for filtering */
 	double sd=0.5;
 
@@ -194,7 +196,6 @@
 	//	xobs->Echo();
 	//	yobs->Echo();
 
-	double dlevel= 860;                /* mean level of data acquisition */
 
 	double mmax  = 1000;               /* max value for layer interfaces */
 	double mmax2 = 1000;
@@ -222,11 +223,21 @@
 	/*}}}*/     
 	/* load the data {{{*/
 
+	/*landmask */
+
+	ifstream file("landmask.txt");
+	Matrix * landmask= new Matrix(nx*ny,1);
+	double inputnumber;
+	for(int i=0;i<ny*nx; i++){ 
+		file >> inputnumber;
+		landmask->SetValue(i,0,inputnumber);
+	}
+	file.close();
+
 	/* Observed gravity anomaly */
 
 	ifstream file1("store_fa500_36s.txt");
 	Matrix * gobs= new Matrix(nx*ny,1);
-	double inputnumber;
 	for(int i=0;i<ny*nx; i++){ 
 		file1 >> inputnumber;
 		gobs->SetValue(i,0, inputnumber*1e-5);
@@ -260,8 +271,8 @@
 
 
 	ifstream file4("store_flag_eval500.txt");
-	Matrix * evalid= new Matrix(mx*my,1);
-	for(int s=0;s<mx*my; s++){ 
+	Matrix * evalid= new Matrix(nx*ny,1);
+	for(int s=0;s<nx*ny; s++){ 
 		file4 >> inputnumber;
 		evalid->SetValue(s,0,inputnumber);
 	}
@@ -345,7 +356,7 @@
 
 		/* update model and calculate energy */
 
-		newmodelgen(m_old,m_new,bathy,icethick,mx,my,Tm,ptval,mmax,mmax2,ctr,sd);  /* new model */
+		newmodelgen(m_old,m_new,bathy,icethick,mx,my,Tm,ptval,mmax,mmax2,ctr,sd, landmask);  /* new model */
 		E_new=misfit(m_new,evalid,gobs,dlevel,Pobs,xobs,yobs,Pp,rho1,rho2,dx,dy,dn,nx,ny,mx,my, my_rank, num_procs); /* new energy */
 		dE=E_new-E_old;                                        /* energy difference */
 
@@ -507,6 +518,8 @@
 					m1->SetValue(i,j,m->GetValue(i,j));
 					m2->SetValue(i,j,i*1e-10);
 				}
+				m1->SetValue(i,j,m1->GetValue(i,j));
+				m2->SetValue(i,j,m2->GetValue(i,j));
 			}
 			else{
 				m1->SetValue(i,j,1e-10);
@@ -618,9 +631,9 @@
 	Matrix* g2=new Matrix(nx*ny,1);
 	Matrix* g=new Matrix(nx*ny,1);
 	Matrix* gcalgr=new Matrix(ny,nx);
-	Matrix* gcalvec=new Matrix(mx*my,1);
-	Matrix* df=new Matrix(mx*my,1);
-	Matrix* G=new Matrix(mx*my,3);
+	Matrix* gcalvec=new Matrix(nx*ny,1);
+	Matrix* df=new Matrix(nx*ny,1);
+	Matrix* G=new Matrix(nx*ny,3);
 	double a=0;
 	double b=0;
 	double e=0;
@@ -629,8 +642,8 @@
 	plouff(g2,Pobs,Pp,m2,rho2,dx,dy,dn,mx*my,nx*ny,3, my_rank, num_procs);
 	g->MatrixSum(g1,g2);
 	vec2gridsimple(g,gcalgr,nx,ny);
-	reshape(gcalgr,gcalvec,mx,my);
-	for (int i=0;i<mx*my;i++){
+	reshape(gcalgr,gcalvec,nx,ny);
+	for (int i=0;i<nx*ny;i++){
 		df->SetValue(i,0,evalid->GetValue(i,0)*(gobs->GetValue(i,0)-gcalvec->GetValue(i,0)));
 		G->SetValue(i,0,evalid->GetValue(i,0)*Pobs->GetValue(i,0));
 		G->SetValue(i,1,evalid->GetValue(i,0)*Pobs->GetValue(i,1));
@@ -639,13 +652,13 @@
 	Matrix* M = NULL;
 	GSLsquarefit(&M,G,df);
 
-	for (int i=0;i<my;i++){
-		for(int j=0;j<mx;j++){
+	for (int i=0;i<ny;i++){
+		for(int j=0;j<nx;j++){
 			gcalgr->SetValue(i,j,gcalgr->GetValue(i,j)+xobs->GetValue(i,j)*M->GetValue(0,0)+yobs->GetValue(i,j)*M->GetValue(1,0)+M->GetValue(2,0));
 		}
 	}
-	reshape(gcalgr,g,mx,my);
-	for (int i=0;i<mx*my;i++){
+	reshape(gcalgr,g,nx,ny);
+	for (int i=0;i<nx*ny;i++){
 		a=a+fabs(evalid->GetValue(i,0)*(gobs->GetValue(i,0)-g->GetValue(i,0)));
 		b=b+fabs(evalid->GetValue(i,0)*(gobs->GetValue(i,0)+g->GetValue(i,0)));
 	}
@@ -664,7 +677,7 @@
 
 	return e;
 }/*}}}*/
-void newmodelgen(Matrix* m0,Matrix* m1,Matrix* bathy,Matrix* icethick,int mx,int my,double T,double ptval,double mmax,double mmax2,double ctr,double sd){/*{{{*/
+void newmodelgen(Matrix* m0,Matrix* m1,Matrix* bathy,Matrix* icethick,int mx,int my,double T,double ptval,double mmax,double mmax2,double ctr,double sd, Matrix *landmask){/*{{{*/
 	Matrix* m1gr=new Matrix(my,mx);
 	Matrix* m1grsm=new Matrix(my,mx);
 	Matrix* m1col=new Matrix(mx*my,1);
@@ -678,18 +691,33 @@
 	nptflag->MatrixSum(icethick,bathy);
 	/* first layer: ice */
 	for (int i=0;i<mx*my;i++){
-		if(nptflag->GetValue(i,0)==0){
-			u=double(rand())/double(RAND_MAX);
-			y=signe(u-0.5)*T*(pow(1+1/T,fabs(2*u-1))-1);
-			m1->SetValue(i,1,m0->GetValue(i,1)+y*ptval);
-			m1->SetValue(i,2,m1->GetValue(i,1)+1e-10);
-			if(m1->GetValue(i,1)<=m1->GetValue(i,0)){
-				m1->SetValue(i,1,m1->GetValue(i,0)+1e-10);
+		if(landmask->GetValue(i,0)==2){
+			if(nptflag->GetValue(i,0)==0){
+				u=double(rand())/double(RAND_MAX);
+				y=signe(u-0.5)*T*(pow(1+1/T,fabs(2*u-1))-1);
+				m1->SetValue(i,1,m0->GetValue(i,1)+y*ptval);
 				m1->SetValue(i,2,m1->GetValue(i,1)+1e-10);
+				if(m1->GetValue(i,1)<=m1->GetValue(i,0)){
+					m1->SetValue(i,1,m1->GetValue(i,0)+1e-10);
+					m1->SetValue(i,2,m1->GetValue(i,1)+1e-10);
+				}
+				if(m1->GetValue(i,1)>=m1->GetValue(i,0)+mmax){
+					m1->SetValue(i,1,m1->GetValue(i,0)+mmax);
+					m1->SetValue(i,2,m1->GetValue(i,1)+1e-10);
+				}
 			}
-			if(m1->GetValue(i,1)>=m1->GetValue(i,0)+mmax){
-				m1->SetValue(i,1,m1->GetValue(i,0)+mmax);
-				m1->SetValue(i,2,m1->GetValue(i,1)+1e-10);
+		}
+		else if(landmask->GetValue(i,0)==0){
+			if(nptflag->GetValue(i,0)==0){
+				u=double(rand())/double(RAND_MAX);
+				y=signe(u-0.5)*T*(pow(1+1/T,fabs(2*u-1))-1);
+				m1->SetValue(i,2,m0->GetValue(i,2)+y*ptval);
+				if(m1->GetValue(i,2)<=m1->GetValue(i,0)){
+					m1->SetValue(i,2,m1->GetValue(i,0)+1e-10);
+				}
+				if(m1->GetValue(i,2)>=m1->GetValue(i,0)+mmax2){
+					m1->SetValue(i,2,m1->GetValue(i,0)+mmax2);
+				}
 			}
 		}
 	}
@@ -704,18 +732,52 @@
 	reshape(m1grsm2,m1col2,mx,my);
 
 	for (int i=0;i<mx*my;i++){
-		if(nptflag->GetValue(i,0)==0){
-			m1->SetValue(i,1,m1col->GetValue(i,0));
-			m1->SetValue(i,2,m1col2->GetValue(i,0));
-			if(m1->GetValue(i,1)<m1->GetValue(i,0)){
-				m1->SetValue(i,1,m1->GetValue(i,0)+1e-10);
-				m1->SetValue(i,2,m1->GetValue(i,1)+1e-10);
+		if(landmask->GetValue(i,0)==2){
+			if(nptflag->GetValue(i,0)==0){
+				m1->SetValue(i,1,m1col->GetValue(i,0));
+				m1->SetValue(i,2,m1col2->GetValue(i,0));
+				if(m1->GetValue(i,1)<=m1->GetValue(i,0)){
+					m1->SetValue(i,1,m1->GetValue(i,0)+1e-10);
+					m1->SetValue(i,2,m1->GetValue(i,1)+1e-10);
+				}
+				if(fabs(m1->GetValue(i,2)-m1->GetValue(i,1))>1){
+					m1->SetValue(i,2,m1->GetValue(i,1)+1e-10);
+				}
 			}
+			else{
+				m1->SetValue(i,1,m0->GetValue(i,1));
+				m1->SetValue(i,2,m0->GetValue(i,2));
+			}
 		}
-		else{
-			m1->SetValue(i,1,m0->GetValue(i,1));
-			m1->SetValue(i,2,m0->GetValue(i,2));
+		else if(landmask->GetValue(i,0)==0){
+			if(nptflag->GetValue(i,0)==0){
+				m1->SetValue(i,2,m1col2->GetValue(i,0));
+				if(m1->GetValue(i,2)<=m1->GetValue(i,0)){
+					m1->SetValue(i,2,m1->GetValue(i,0)+1e-10);
+				}
+				if(fabs(m1->GetValue(i,0)-m1->GetValue(i,1))>1){
+					m1->SetValue(i,1,m1->GetValue(i,0)+1e-10);
+				}
+			}
+			else{
+				m1->SetValue(i,1,m0->GetValue(i,1));
+				m1->SetValue(i,2,m0->GetValue(i,2));
+			}
 		}
+		else {
+			if(nptflag->GetValue(i,0)==0){
+				if(fabs(m1->GetValue(i,0)-m1->GetValue(i,1))>1){
+					m1->SetValue(i,1,m1->GetValue(i,0));
+				}
+				if(fabs(m1->GetValue(i,0)-m1->GetValue(i,2))>1){
+					m1->SetValue(i,2,m1->GetValue(i,0));
+				}
+			}
+			else{
+				m1->SetValue(i,1,m0->GetValue(i,1));
+				m1->SetValue(i,2,m0->GetValue(i,2));
+			}
+		}
 	}
 
 				/* second layer: water */
