Ice Sheet System Model  4.18
Code documentation
InterpFromMesh2d.cpp
Go to the documentation of this file.
1 
5 #include "./InterpFromMesh2d.h"
6 
7 void InterpFromMesh2dUsage(void){/*{{{*/
8  _printf0_(" usage:\n");
9  _printf0_(" data_prime=InterpFromMesh2d(index,x,y,data,x_prime,y_prime);\n");
10  _printf0_(" or data_prime=InterpFromMesh2d(index,x,y,data,x_prime,y_prime,default_value);\n");
11  _printf0_(" or data_prime=InterpFromMesh2d(index,x,y,data,x_prime,y_prime,default_value,contourname);\n");
12  _printf0_(" where:\n");
13  _printf0_(" x,y: coordinates of the nodes where data is defined\n");
14  _printf0_(" index: index of the mesh where data is defined\n");
15  _printf0_(" data - vector holding the data to be interpolated onto the points.\n");
16  _printf0_(" x_prime,y_prime: coordinates of the mesh vertices onto which we interpolate.\n");
17  _printf0_(" default_value: a scalar or vector of size length(x_prime).\n");
18  _printf0_(" contourname: linear interpolation will happen on all x_interp,y_interp inside the contour, default value will be adopted on the rest of the mesh.\n");
19  _printf0_(" data_prime: vector of prime interpolated data.\n");
20  _printf0_("\n");
21 }/*}}}*/
22 WRAPPER(InterpFromMesh2d_python){
23 
24  /*input: */
25  int* index_data=NULL;
26  int index_data_rows;
27  int dummy;
28  double* x_data=NULL;
29  int x_data_rows;
30  double* y_data=NULL;
31  int y_data_rows;
32  double* data=NULL;
33  int data_rows;
34  int data_cols;
35  double* x_prime=NULL;
36  double* y_prime=NULL;
37  int x_prime_rows;
38  int y_prime_rows;
39  double* default_values=NULL;
40  int num_default_values=0;
41 
42  /*contours*/
43  int i;
44  mxArray *matlabstructure = NULL;
45  Contour<double> **contours=NULL;
46  int numcontours;
47  Contour<double> *contouri=NULL;
48 
49  /*Intermediary*/
50  int nods_data;
51  int nels_data;
52  int nods_prime;
53 
54  /* output: */
55  IssmSeqVec<double> *data_prime = NULL;
56 
57  /*Boot module: */
58  MODULEBOOT();
59 
60  /*checks on arguments on the matlab side: */
61  if(nlhs!=NLHS){
63  _error_("InterpFromMeshToMesh2dUsage usage error");
64  }
65  if((nrhs!=6) && (nrhs!=7) && (nrhs!=8)){
67  _error_("InterpFromMeshToMesh2dUsage usage error");
68  }
69 
70  /*Input datasets: */
71  FetchData(&index_data,&index_data_rows,&dummy,INDEXHANDLE);
72  FetchData(&x_data,&x_data_rows,NULL,XHANDLE);
73  FetchData(&y_data,&y_data_rows,NULL,YHANDLE);
74  FetchData(&data,&data_rows,&data_cols,DATAHANDLE);
75  FetchData(&x_prime,&x_prime_rows,NULL,XPRIMEHANDLE);
76  FetchData(&y_prime,&y_prime_rows,NULL,YPRIMEHANDLE);
77 
78  if(nrhs>=7){
79  /*default values: */
80  FetchData(&default_values,&num_default_values,DEFAULTHANDLE);
81  }
82  else{
83  default_values=NULL;
84  num_default_values=0;
85  }
86 
87  if(nrhs>=8){
88 
89  #ifdef _HAVE_MATLAB_MODULES_
90  /*Call expread on filename to build a contour array in the matlab workspace: */
91  mexCallMATLAB( 1, &matlabstructure, 1, (mxArray**)&FILENAME, "expread");
92 
93  /*contours: */
94  numcontours=mxGetNumberOfElements(matlabstructure);
95  contours=xNew<Contour<double> *>(numcontours);
96  for(i=0;i<numcontours;i++){
97  //allocate
98  contouri=new Contour<double>();
99  //retrieve dimension of this contour.
100  contouri->nods=(int)mxGetScalar(mxGetField(matlabstructure,i,"nods"));
101  //set pointers.
102  contouri->x=mxGetPr(mxGetField(matlabstructure,i,"x"));
103  contouri->y=mxGetPr(mxGetField(matlabstructure,i,"y"));
104  *(contours+i)=contouri;
105  }
106  #else
107  _error_("not supported");
108  #endif
109  }
110  else{
111  numcontours=0;
112  contours=NULL;
113  }
114 
115  /*some checks*/
116  if (x_data_rows!=y_data_rows){
117  _error_("vectors x and y should have the same length!");
118  }
119  if (x_prime_rows!=y_prime_rows){
120  _error_("vectors x_prime and y_prime should have the same length!");
121  }
122 
123  /*get number of elements and number of nodes in the data*/
124  nels_data=index_data_rows;
125  nods_data=x_data_rows;
126  nods_prime=x_prime_rows;
127 
128  /* Run core computations: */
129  InterpFromMesh2dx(&data_prime,index_data,x_data,y_data,nods_data,nels_data,data,data_rows,x_prime,y_prime,nods_prime,default_values,num_default_values,contours,numcontours);
130 
131  /*Write data: */
132  WriteData(DATAPRIME,data_prime);
133 
134  /*end module: */
135  xDelete<int>(index_data);
136  xDelete<double>(x_data);
137  xDelete<double>(y_data);
138  xDelete<double>(data);
139  xDelete<double>(x_prime);
140  xDelete<double>(y_prime);
141  xDelete<double>(default_values);
142 
143  /*end module: */
144  MODULEEND();
145 }
_printf0_
#define _printf0_(StreamArgs)
Definition: Print.h:29
WriteData
void WriteData(IssmPDouble **pmatrix, int *pnel, int *matrix, int M, int N)
Definition: WriteJavascriptData.cpp:16
IssmSeqVec
Definition: IssmSeqVec.h:31
Contour
Definition: Contour.h:15
InterpFromMesh2d.h
: prototype for Data Interpolation mex module.
FetchData
void FetchData(char **pstring, char *stringin)
Definition: FetchJavascriptData.cpp:16
InterpFromMesh2dUsage
void InterpFromMesh2dUsage(void)
Definition: InterpFromMesh2d.cpp:7
Contour::nods
int nods
Definition: Contour.h:20
InterpFromMesh2dx
int InterpFromMesh2dx(IssmSeqVec< IssmPDouble > **pdata_prime, int *index_data, double *x_data, double *y_data, int nods_data, int nels_data, double *data, int data_length, double *x_prime, double *y_prime, int nods_prime, double *default_values, int num_default_values, Contour< IssmPDouble > **contours, int numcontours)
Definition: InterpFromMesh2dx.cpp:11
WRAPPER
WRAPPER(InterpFromMesh2d_python)
Definition: InterpFromMesh2d.cpp:22
NLHS
#define NLHS
Definition: BamgConvertMesh.h:50
Contour::y
doubletype * y
Definition: Contour.h:22
_error_
#define _error_(StreamArgs)
Definition: exceptions.h:49
Contour::x
doubletype * x
Definition: Contour.h:21