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

Last change on this file since 13173 was 13173, checked in by jschierm, 13 years ago

CHG: Removed xrealloc from KMLMeshWrite.cpp.

File size: 5.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 char* notes=NULL;
13 char* notes2=NULL;
14 const mxArray* notesi;
15 mwIndex nindex;
16 int* elem=NULL;
17 int melem=0,nelem=0;
18 int* nodecon=NULL;
19 int mncon=0,nncon=0;
20 double* lat=NULL;
21 int mlat=0,nlat=0,llat=0;
22 double* lng=NULL;
23 int mlng=0,nlng=0,llng=0;
24 int nparts=0;
25 int* part=NULL;
26 int mprt=0,nprt=0,lprt=0;
27 double* data=NULL;
28 int mdata=0,ndata=0;
29 double* cmap=NULL;
30 int mcmap=0,ncmap=0;
31 char* filnam=NULL;
32 FILE* fid=NULL;
33 Options* options=NULL;
34
35 /* output: */
36 int ierror=0;
37
38 /*Boot module: */
39 MODULEBOOT();
40
41 /*checks on arguments on the matlab side: */
42 if (nlhs > NLHS) {
43 KMLMeshWriteUsage(); _error_("KMLMeshWrite usage error");
44 }
45 if (nrhs < NRHS) {
46 KMLMeshWriteUsage(); _error_("KMLMeshWrite usage error");
47 }
48
49 /*Input datasets: */
50 FetchData(&name,NAME);
51
52/* notes is typically a cell array of character strings */
53 if (mxIsCell(NOTES)) {
54 for (nindex=0; nindex<mxGetNumberOfElements(NOTES); nindex++) {
55 notesi=mxGetCell(NOTES,nindex);
56 if (notesi && mxIsChar(notesi) && mxGetNumberOfElements(notesi)) {
57 if (!notes) {
58 notes=xNew<char>(mxGetNumberOfElements(notesi)+1);
59 mxGetString(notesi,notes,mxGetNumberOfElements(notesi)+1);
60 }
61 else {
62/* note that strlen does not include trailing null */
63 notes2=xNew<char>(strlen(notes)+1+mxGetNumberOfElements(notesi)+1);
64 memcpy(notes2,notes,(strlen(notes)+1)*sizeof(char));
65 xDelete<char>(notes);
66 notes=notes2;
67 notes2=NULL;
68// notes=(char*)xrealloc(notes,(strlen(notes)+1+mxGetNumberOfElements(notesi)+1)*sizeof(char));
69 strcat(notes,"\n");
70 mxGetString(notesi,&notes[strlen(notes)],mxGetNumberOfElements(notesi)+1);
71 }
72 }
73 }
74 }
75 else
76 FetchData(&notes,NOTES);
77 FetchData(&elem,&melem,&nelem,ELEMHANDLE);
78 FetchData(&nodecon,&mncon,&nncon,NODECONHANDLE);
79 FetchData(&lat,&mlat,&nlat,LATHANDLE);
80 llat=mlat*nlat;
81 FetchData(&lng,&mlng,&nlng,LNGHANDLE);
82 llng=mlng*nlng;
83 FetchData(&part,&mprt,&nprt,PARTHANDLE);
84 lprt=mprt*nprt;
85 FetchData(&data,&mdata,&ndata,DATAHANDLE);
86 FetchData(&cmap,&mcmap,&ncmap,CMAPHANDLE);
87 FetchData(&filnam,FILENAME);
88 FetchData(&options,NRHS,nrhs,prhs);
89
90 /*some checks*/
91 for (i=0; i<melem*nelem; i++) if(elem[i]>nnodes) nnodes=elem[i];
92 if(part) for (i=0; i<lprt; i++) if (part[i]+1 > nparts) nparts=part[i]+1;
93
94 if (nodecon && (mncon != nnodes))
95 {_error_("Nodal connectivity table, if supplied, must be supplied for all nodes.");}
96 else if (!nodecon)
97 mncon=nnodes;
98 if ((llat != nnodes) || (llng != nnodes) || (llat != llng))
99 _error_("Latitude and longitude vectors must be supplied for all nodes.");
100 if (part && (lprt != nnodes))
101 _error_("Partitioning vector, if supplied, must be supplied for all nodes.");
102 if (data && !((mdata == nnodes) || (mdata == melem)))
103 _error_("Data matrix, if supplied, must be supplied for all nodes or all elements.");
104 if (cmap && (ncmap != 3))
105 _error_("Colormap matrix, if supplied, must have three columns for rgb.");
106 if (!strlen(filnam))
107 strcpy(filnam,"stdout");
108
109 /* Run core computations: */
110 fid=fopen(filnam,"w");
111 KMLMeshWritex(&ierror,name,notes,elem,melem,nelem,nodecon,mncon,nncon,lat,lng,part,data,mdata,ndata,cmap,mcmap,ncmap,fid);
112 fclose(fid);
113
114 /*Write data: */
115 WriteData(ERRORFLAG,ierror);
116
117 /*Clean-up*/
118 delete options;
119 if (mxIsCell(NOTES) && notes) xfree((void**)&notes);
120
121 /*end module: */
122 MODULEEND();
123}
124
125void KMLMeshWriteUsage(void){
126 _pprintLine_("KMLMeshWrite - KML mesh writer module:");
127 _pprintLine_("");
128 _pprintLine_(" This module writes the mesh of a model as KML polygons into the specified KML file.");
129 _pprintLine_("");
130 _pprintLine_(" Usage:");
131 _pprintLine_(" ierror=KMLMeshWrite(name,notes,elem,nodecon,lat,long,part,data,cmap,kmlfile);");
132 _pprintLine_("");
133 _pprintLine_(" name model name (string, may be empty)");
134 _pprintLine_(" notes model notes (string or cell array of strings, may be empty)");
135 _pprintLine_(" elem elements (double array)");
136 _pprintLine_(" nodecon nodal connectivity array (double array, may be empty)");
137 _pprintLine_(" lat nodal latititudes (double vector)");
138 _pprintLine_(" long nodal longitudes (double vector)");
139 _pprintLine_(" part nodal partitions (double vector, may be empty)");
140 _pprintLine_(" data nodal or element data (double vector, may be empty)");
141 _pprintLine_(" cmap color map (double nx3 array, may be empty)");
142 _pprintLine_(" kmlfile KML file name (string)");
143 _pprintLine_("");
144 _pprintLine_(" ierror error flag (double, non-zero for error)");
145 _pprintLine_("");
146 _pprintLine_(" Example:");
147 _pprintLine_(" KMLMeshWrite(md.name,md.notes,md.elements,md.nodeconnectivity,md.lat,md.long,md.part,md.fm_criterion,options.cmap,filekml);");
148 _pprintLine_("");
149}
Note: See TracBrowser for help on using the repository browser.