source: issm/trunk-jpl/src/modules/KMLMeshWrite/KMLMeshWrite.cpp@ 12032

Last change on this file since 12032 was 11933, checked in by Eric.Larour, 13 years ago

New FetchData and WriteData interface

File size: 6.0 KB
Line 
1/*\file KMLMeshWrite.c
2 *\brief: KML mesh writer module.
3 */
4#include "./KMLMeshWrite.h"
5
6void mexFunction( int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]){
7
8 int i,j,nnodes=0,verbose=1;
9
10 /*input: */
11 char* name=NULL;
12
13 char* notes=NULL;
14 const mxArray* notesi;
15 mwIndex nindex;
16
17 int* elem=NULL;
18 int melem=0,nelem=0;
19
20 int* nodecon=NULL;
21 int mncon=0,nncon=0;
22
23 double* lat=NULL;
24 int mlat=0,nlat=0,llat=0;
25
26 double* lng=NULL;
27 int mlng=0,nlng=0,llng=0;
28
29 int nparts=0;
30
31 int* part=NULL;
32 int mprt=0,nprt=0,lprt=0;
33
34 double* data=NULL;
35 int mdata=0,ndata=0;
36
37 double* cmap=NULL;
38 int mcmap=0,ncmap=0;
39
40 char* filnam=NULL;
41
42 FILE* fid=NULL;
43
44 Options* options=NULL;
45
46 /* output: */
47 int ierror=0;
48
49 /*Boot module: */
50 MODULEBOOT();
51
52 /*checks on arguments on the matlab side: */
53 if (nlhs > NLHS) {
54 KMLMeshWriteUsage();
55 _error_("KMLMeshWrite usage error");
56 }
57 if (nrhs < NRHS) {
58 KMLMeshWriteUsage();
59 _error_("KMLMeshWrite usage error");
60 }
61
62 /*Input datasets: */
63 if (verbose) printf("Fetching inputs:\n");
64 FetchData(&name,NAME);
65 if (verbose) printf(" name =\"%s\"\n",name);
66
67/* notes is typically a cell array of character strings */
68 if (mxIsCell(NOTES)) {
69 for (nindex=0; nindex<mxGetNumberOfElements(NOTES); nindex++) {
70 notesi=mxGetCell(NOTES,nindex);
71 if (notesi && mxIsChar(notesi) && mxGetNumberOfElements(notesi)) {
72 if (!notes) {
73 notes=(char *) xmalloc((mxGetNumberOfElements(notesi)+1)*sizeof(char));
74 mxGetString(notesi,notes,mxGetNumberOfElements(notesi)+1);
75 }
76 else {
77/* note that strlen does not include trailing null */
78 notes=(char *) xrealloc(notes,(strlen(notes)+1+mxGetNumberOfElements(notesi)+1)*sizeof(char));
79 strcat(notes,"\n");
80 mxGetString(notesi,&notes[strlen(notes)],mxGetNumberOfElements(notesi)+1);
81 }
82 }
83 }
84 }
85 else
86 FetchData(&notes,NOTES);
87 if (verbose) printf(" notes =\"%s\"\n",notes);
88
89 FetchData(&elem,&melem,&nelem,ELEMHANDLE);
90 if (verbose) printf(" elem =size [%d x %d]\n",melem,nelem);
91 FetchData(&nodecon,&mncon,&nncon,NODECONHANDLE);
92 if (verbose) printf(" nodecon=size [%d x %d]\n",mncon,nncon);
93 FetchData(&lat,&mlat,&nlat,LATHANDLE);
94 llat=mlat*nlat;
95 if (verbose) printf(" lat =length [%d]\n",llat);
96 FetchData(&lng,&mlng,&nlng,LNGHANDLE);
97 llng=mlng*nlng;
98 if (verbose) printf(" lng =length [%d]\n",llng);
99 FetchData(&part,&mprt,&nprt,PARTHANDLE);
100 lprt=mprt*nprt;
101 if (verbose) printf(" part =length [%d]\n",lprt);
102 FetchData(&data,&mdata,&ndata,DATAHANDLE);
103 if (verbose) printf(" data =size [%d x %d]\n",mdata,ndata);
104 FetchData(&cmap,&mcmap,&ncmap,CMAPHANDLE);
105 if (verbose) printf(" cmap =size [%d x %d]\n",mcmap,ncmap);
106 FetchData(&filnam,FILENAME);
107 if (verbose) printf(" filnam =\"%s\"\n",filnam);
108
109 if (verbose) printf("Parsing options:\n");
110 options=new Options(NRHS,nrhs,prhs);
111// if (options->Size()) for(i=0;i<options->Size();i++) ((Option*)options->GetObjectByOffset(i))->DeepEcho();
112
113 /*some checks*/
114 if (verbose) printf("Checking inputs:\n");
115
116 for (i=0; i<melem*nelem; i++)
117 if (elem[i] > nnodes)
118 nnodes=elem[i];
119 if (verbose) printf(" nnodes =%d\n",nnodes);
120 if (part)
121 for (i=0; i<lprt; i++)
122 if (part[i]+1 > nparts)
123 nparts=part[i]+1;
124 if (verbose) printf(" nparts =%d\n",nparts);
125
126 if (nodecon && (mncon != nnodes))
127 _error_("Nodal connectivity table, if supplied, must be supplied for all nodes.");
128 else if (!nodecon)
129 mncon=nnodes;
130 if ((llat != nnodes) || (llng != nnodes) || (llat != llng))
131 _error_("Latitude and longitude vectors must be supplied for all nodes.");
132 if (part && (lprt != nnodes))
133 _error_("Partitioning vector, if supplied, must be supplied for all nodes.");
134 if (data && !((mdata == nnodes) || (mdata == melem)))
135 _error_("Data matrix, if supplied, must be supplied for all nodes or all elements.");
136 if (cmap && (ncmap != 3))
137 _error_("Colormap matrix, if supplied, must have three columns for rgb.");
138
139 if (!strlen(filnam))
140 strcpy(filnam,"stdout");
141
142 if (verbose) printf("Opening file \"%s\".\n",filnam);
143 fid=fopen(filnam,"w");
144
145 /* Run core computations: */
146 if (verbose) printf("Calling core:\n");
147 KMLMeshWritex(&ierror,
148 name,
149 notes,
150 elem,melem,nelem,
151 nodecon,mncon,nncon,
152 lat,lng,
153 part,
154 data,mdata,ndata,
155 cmap,mcmap,ncmap,
156 fid);
157
158 if (verbose) printf("Closing file \"%s\".\n",filnam);
159 fclose(fid);
160
161 /*Write data: */
162 WriteData(ERRORFLAG,ierror);
163
164 /*Clean-up*/
165 delete options;
166 if (mxIsCell(NOTES) && notes) xfree((void**)&notes);
167
168 /*end module: */
169 MODULEEND();
170}
171
172void KMLMeshWriteUsage(void)
173{
174 _printf_(true,"KMLMeshWrite - KML mesh writer module:\n");
175 _printf_(true,"\n");
176 _printf_(true," This module writes the mesh of a model as KML polygons into the specified KML file.\n");
177 _printf_(true,"\n");
178 _printf_(true," Usage:\n");
179 _printf_(true," ierror=KMLMeshWrite(name,notes,elem,nodecon,lat,long,part,data,cmap,kmlfile);\n");
180 _printf_(true,"\n");
181 _printf_(true," name model name (string, may be empty)\n");
182 _printf_(true," notes model notes (string or cell array of strings, may be empty)\n");
183 _printf_(true," elem elements (double array)\n");
184 _printf_(true," nodecon nodal connectivity array (double array, may be empty)\n");
185 _printf_(true," lat nodal latititudes (double vector)\n");
186 _printf_(true," long nodal longitudes (double vector)\n");
187 _printf_(true," part nodal partitions (double vector, may be empty)\n");
188 _printf_(true," data nodal or element data (double vector, may be empty)\n");
189 _printf_(true," cmap color map (double nx3 array, may be empty)\n");
190 _printf_(true," kmlfile KML file name (string)\n");
191 _printf_(true,"\n");
192 _printf_(true," ierror error flag (double, non-zero for error)\n");
193 _printf_(true,"\n");
194 _printf_(true," Example:\n");
195 _printf_(true," KMLMeshWrite(md.name,md.notes,md.elements,md.nodeconnectivity,md.lat,md.long,md.part,md.fm_criterion,options.cmap,filekml);\n");
196 _printf_(true,"\n");
197}
198
Note: See TracBrowser for help on using the repository browser.