Index: /issm/trunk-jpl/src/c/Makefile.am
===================================================================
--- /issm/trunk-jpl/src/c/Makefile.am	(revision 20060)
+++ /issm/trunk-jpl/src/c/Makefile.am	(revision 20061)
@@ -550,4 +550,6 @@
 			./modules/ExpToLevelSetx/ExpToLevelSetxt.cpp\
 			./modules/ContourToNodesx/ContourToNodesx.cpp\
+			./modules/DistanceToMaskBoundaryx/DistanceToMaskBoundaryx.cpp\
+			./modules/DistanceToMaskBoundaryx/DistanceToMaskBoundaryxt.cpp\
 			./modules/NodeConnectivityx/NodeConnectivityx.cpp\
 			./modules/ElementConnectivityx/ElementConnectivityx.cpp\
Index: /issm/trunk-jpl/src/c/modules/DistanceToMaskBoundaryx/DistanceToMaskBoundaryx.cpp
===================================================================
--- /issm/trunk-jpl/src/c/modules/DistanceToMaskBoundaryx/DistanceToMaskBoundaryx.cpp	(revision 20061)
+++ /issm/trunk-jpl/src/c/modules/DistanceToMaskBoundaryx/DistanceToMaskBoundaryx.cpp	(revision 20061)
@@ -0,0 +1,35 @@
+/*! \file  DistanceToMaskBoundaryx.c
+ */
+
+#ifdef HAVE_CONFIG_H
+	#include <config.h>
+#else
+#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
+#endif
+
+#include "./DistanceToMaskBoundaryx.h"
+
+int DistanceToMaskBoundaryx(double** pdistance,double* x, double* y, double* mask, int nods) {
+
+	/*output: */
+	double*  distance;
+
+	/*initialize: */
+	distance=xNew<IssmDouble>(nods);
+
+	/*initialize thread parameters: */
+	DistanceToMaskBoundaryxThreadStruct gate;
+	gate.distance    = distance;
+	gate.x         = x;
+	gate.y         = y;
+	gate.mask         = mask;
+	gate.nods      = nods;
+
+	/*launch the thread manager with DistanceToMaskBoundaryxt as a core: */
+	LaunchThread(DistanceToMaskBoundaryxt,(void*)&gate,_NUMTHREADS_);
+
+	/*Assign output pointers: */
+	*pdistance=distance;
+
+	return 1;
+}
Index: /issm/trunk-jpl/src/c/modules/DistanceToMaskBoundaryx/DistanceToMaskBoundaryx.h
===================================================================
--- /issm/trunk-jpl/src/c/modules/DistanceToMaskBoundaryx/DistanceToMaskBoundaryx.h	(revision 20061)
+++ /issm/trunk-jpl/src/c/modules/DistanceToMaskBoundaryx/DistanceToMaskBoundaryx.h	(revision 20061)
@@ -0,0 +1,27 @@
+/*
+	DistanceToMaskBoundaryx.h
+*/
+
+#ifndef _DISTANCETOMASKBOUNDARYX_H
+#define _DISTANCETOMASKBOUNDARYX_H
+
+#include "../../shared/shared.h"
+#include "../../classes/classes.h"
+
+/*threading: */
+typedef struct{
+
+	int       nods;
+	double   *distance;
+	double   *x;
+	double   *y;
+	double   *mask;
+
+} DistanceToMaskBoundaryxThreadStruct;
+
+/* local prototypes: */
+int DistanceToMaskBoundaryx(double** pdistance,double* x, double* y, double* mask, int nods);
+
+void* DistanceToMaskBoundaryxt(void* vDistanceToMaskBoundaryxThreadStruct);
+
+#endif /* _DISTANCETOMASKBOUNDARYX_H */
Index: /issm/trunk-jpl/src/c/modules/DistanceToMaskBoundaryx/DistanceToMaskBoundaryxt.cpp
===================================================================
--- /issm/trunk-jpl/src/c/modules/DistanceToMaskBoundaryx/DistanceToMaskBoundaryxt.cpp	(revision 20061)
+++ /issm/trunk-jpl/src/c/modules/DistanceToMaskBoundaryx/DistanceToMaskBoundaryxt.cpp	(revision 20061)
@@ -0,0 +1,80 @@
+/*!\file:  DistanceToMaskBoundaryxt.cpp
+ * \brief  "thread" core code 
+ */ 
+
+#ifdef HAVE_CONFIG_H
+	#include <config.h>
+#else
+#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
+#endif
+
+#include "./DistanceToMaskBoundaryx.h"
+
+void* DistanceToMaskBoundaryxt(void* vpthread_handle){
+
+	/*gate variables :*/
+	DistanceToMaskBoundaryxThreadStruct *gate        = NULL;
+	pthread_handle             *handle      = NULL;
+	int  i,j,i0,i1;
+
+	/*recover handle and gate: */
+	handle          = (pthread_handle*)vpthread_handle;
+	gate            = (DistanceToMaskBoundaryxThreadStruct*)handle->gate;
+	int my_thread   = handle->id;
+	int num_threads = handle->num;
+
+	/*recover parameters :*/
+	int       nods      = gate->nods;
+	double   *distance    = gate->distance;
+	double   *x         = gate->x;
+	double   *y         = gate->y;
+	double   *mask         = gate->mask;
+
+	/*distribute indices across threads :*/
+	PartitionRange(&i0,&i1,nods,num_threads,my_thread);
+
+	/*Loop through vertices: */
+	for(i=i0;i<i1;i++){
+
+		IssmDouble d0=pow(10,10);
+
+		IssmDouble xi,yi;
+		
+		//recover vertex position: 
+		xi=x[i];  yi=y[i];
+
+		//figure out if we are inside the mask, or outside: 
+		if(mask[i]==1){
+			//we are inside, look for nearest vertex that is outside the mask: 
+			for(j=0;j<nods;j++){
+				if(j==i)continue;
+				if (mask[j]==0){
+					IssmDouble xj,yj,deltaphi,deltalambda,d;
+					xj=x[j]; yj=y[j];
+					/*figure  out the distance to xi,yi in lat,long mode, using the greatest circle distance:*/
+					deltaphi=fabs(xj-xi); deltalambda=fabs(yj-yi);
+					d=2*asin(sqrt(pow(sin(deltaphi/2),2)+cos(xi)*cos(xj)*pow(sin(deltalambda/2),2)));
+					if(d<d0)d0=d;
+				}
+			}
+			distance[i]=d0;
+		}
+		else{
+			//we are outside, look for nearest vertex that is inside the mask: 
+			for(j=0;j<nods;j++){
+				if(j==i)continue;
+				if (mask[j]==1){
+					IssmDouble xj,yj,deltaphi,deltalambda,d;
+					xj=x[j]; yj=y[j];
+					/*figure  out the distance to xi,yi in lat,long mode, using the greatest circle distance:*/
+					deltaphi=fabs(xj-xi); deltalambda=fabs(yj-yi);
+					d=2*asin(sqrt(pow(sin(deltaphi/2),2)+cos(xi)*cos(xj)*pow(sin(deltalambda/2),2)));
+					if(d<d0)d0=d;
+				}
+			}
+			distance[i]=d0;
+		}
+	}
+
+	return NULL;
+}
Index: /issm/trunk-jpl/src/wrappers/DistanceToMaskBoundary/DistanceToMaskBoundary.cpp
===================================================================
--- /issm/trunk-jpl/src/wrappers/DistanceToMaskBoundary/DistanceToMaskBoundary.cpp	(revision 20061)
+++ /issm/trunk-jpl/src/wrappers/DistanceToMaskBoundary/DistanceToMaskBoundary.cpp	(revision 20061)
@@ -0,0 +1,55 @@
+/*\file DistanceToMaskBoundary.c
+ *\brief: compute distance from any point in a mesh to a mask boundary
+ */
+
+#include "./DistanceToMaskBoundary.h"
+
+void DistanceToMaskBoundaryUsage(void){/*{{{*/
+	_printf0_("DISTANCETOMASKBOUNDARYUSAGE - compute distance from any point in a mesh to a mask boundary\n");
+	_printf0_("\n");
+	_printf0_("   This function is a multi-threaded mex file\n");
+	_printf0_("\n");
+	_printf0_("   Usage:\n");
+	_printf0_("      [distance]=DistanceToMaskBoundary(x,y,mask)\n");
+	_printf0_("\n");
+	_printf0_("      x,y,mask: mesh vertices with corresponding mask values. \n");
+	_printf0_("      distance: distance from x,y to the mask transition between 0 and 1\n");
+	_printf0_("\n");
+}/*}}}*/
+
+WRAPPER(DistanceToMaskBoundary){
+
+	/*input datasets: */
+	double* x=NULL;
+	double* y=NULL;
+	double* mask=NULL;
+	int     nods;
+
+	/* output datasets: */
+	double* distance=NULL;
+
+	/*Boot module: */
+	MODULEBOOT();
+
+	/*checks on arguments on the matlab side: */
+	#ifdef _HAVE_MATLAB_MODULES_
+	CheckNumMatlabArguments(nlhs,NLHS,nrhs,NRHS,__FUNCT__,&DistanceToMaskBoundaryUsage);
+	#endif
+
+	/*Input datasets: */
+	FetchData(&x,&nods,NULL,X);
+	FetchData(&y,NULL,NULL,Y);
+	FetchData(&mask,NULL,NULL,MASK);
+
+	/*Call core of computation: */
+	DistanceToMaskBoundaryx(&distance,x,y,mask,nods);
+
+	/*Write results: */
+	WriteData(DISTANCE,distance,nods);
+
+	/*Free ressources: */
+	//let matlab do this.
+
+	/*end module: */
+	MODULEEND();
+}
Index: /issm/trunk-jpl/src/wrappers/DistanceToMaskBoundary/DistanceToMaskBoundary.h
===================================================================
--- /issm/trunk-jpl/src/wrappers/DistanceToMaskBoundary/DistanceToMaskBoundary.h	(revision 20061)
+++ /issm/trunk-jpl/src/wrappers/DistanceToMaskBoundary/DistanceToMaskBoundary.h	(revision 20061)
@@ -0,0 +1,54 @@
+/*
+	DistanceToMaskBoundary.h
+*/
+
+#ifndef _DISTANCETOMASKBOUNDARY_H
+#define _DISTANCETOMASKBOUNDARY_H
+
+#ifdef HAVE_CONFIG_H
+	#include <config.h>
+#else
+	#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
+#endif
+
+/*For python modules: needs to come before header files inclusion*/
+#ifdef _HAVE_PYTHON_
+#define PY_ARRAY_UNIQUE_SYMBOL PythonIOSymbol
+#endif
+
+#include "../bindings.h"
+#include "../../c/main/globals.h"
+#include "../../c/toolkits/toolkits.h"
+#include "../../c/modules/modules.h"
+#include "../../c/shared/shared.h"
+#include "../../c/shared/io/io.h"
+#include "../../c/shared/Enum/Enum.h"
+
+#undef __FUNCT__ 
+#define __FUNCT__  "DistanceToMaskBoundary"
+
+#ifdef _HAVE_MATLAB_MODULES_
+/* serial input macros: */
+#define X            prhs[0]
+#define Y            prhs[1]
+#define MASK         prhs[2]
+/* serial output macros: */
+#define DISTANCE (mxArray**)&plhs[0]
+#endif
+
+#ifdef _HAVE_PYTHON_MODULES_
+/* serial input macros: */
+#define X            PyTuple_GetItem(args,0)
+#define Y            PyTuple_GetItem(args,1)
+#define MASK            PyTuple_GetItem(args,2)
+/* serial output macros: */
+#define DISTANCE output,0
+#endif
+
+/* serial arg counts: */
+#undef NLHS
+#define NLHS  1
+#undef NRHS
+#define NRHS  3
+
+#endif  /* _DISTANCETOMASKBOUNDARY_H*/
Index: /issm/trunk-jpl/src/wrappers/matlab/Makefile.am
===================================================================
--- /issm/trunk-jpl/src/wrappers/matlab/Makefile.am	(revision 20060)
+++ /issm/trunk-jpl/src/wrappers/matlab/Makefile.am	(revision 20061)
@@ -42,4 +42,5 @@
 						 ContourToMesh.la\
 						 ContourToNodes.la\
+						 DistanceToMaskBoundary.la\
 						 ElementConnectivity.la\
 						 EnumToString.la\
@@ -161,4 +162,7 @@
 ContourToNodes_la_LIBADD = ${deps} $(MPILIB) $(PETSCLIB) $(GSLLIB) $(PROJ4LIB)
 
+DistanceToMaskBoundary_la_SOURCES = ../DistanceToMaskBoundary/DistanceToMaskBoundary.cpp
+DistanceToMaskBoundary_la_LIBADD = ${deps} $(MPILIB) $(PETSCLIB) $(GSLLIB) $(PROJ4LIB)
+
 ElementConnectivity_la_SOURCES = ../ElementConnectivity/ElementConnectivity.cpp
 ElementConnectivity_la_LIBADD = ${deps} $(MPILIB) $(PETSCLIB) $(GSLLIB) $(PROJ4LIB)
