source: issm/trunk/src/c/io/FetchParams.cpp@ 4852

Last change on this file since 4852 was 4852, checked in by Eric.Larour, 15 years ago

New DoubleMatArrayParam object, to hold the qmu segments.

File size: 4.2 KB
Line 
1/* \file FetchParams.c:
2 * \brief: interface for fetching matlab parameters into a "C" dataset
3 */
4
5#ifdef HAVE_CONFIG_H
6 #include "config.h"
7#else
8#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
9#endif
10
11#ifdef _SERIAL_
12
13#include <mex.h>
14#include "./io.h"
15#include "../objects/objects.h"
16#include "../shared/shared.h"
17#include "../include/include.h"
18#include "../EnumDefinitions/EnumDefinitions.h"
19
20void FetchParams(Parameters** pparameters, DataHandle dataref){
21
22 int i,j;
23 int count;
24
25 /*output: */
26 Param* param=NULL;
27 Parameters* parameters=NULL;
28
29 /*intermediary: */
30 int M,N;
31 double* tmatrix=NULL;
32 double* matrix=NULL;
33 char** stringarray=NULL;
34 double** array=NULL;
35 int* mdims_array=NULL;
36 int* ndims_array=NULL;
37 int nfields;
38 char* name=NULL;
39 mxArray* pfield=NULL;
40 mxArray* pfield2=NULL;
41 int enum_type;
42
43
44 /*First, create parameters : */
45 parameters=new Parameters();
46
47 /*go through matlab params structure, and create Param object for each field: */
48
49 nfields=mxGetNumberOfFields(dataref);
50
51 for(count=0;count<nfields;count++){
52
53 /*Get i'th field: */
54 name=(char*)mxGetFieldNameByNumber(dataref,count);
55 enum_type=StringAsEnum(name);
56 pfield=mxGetFieldByNumber(dataref,0,count);
57 ISSMASSERT(pfield);
58
59 /*Check type of field: */
60 if (mxIsDouble(pfield)){
61
62 /*could be DOUBLE, DOUBLEVEC or DOUBLEMAT, depends on dimensions: */
63 M=mxGetM(pfield);
64 N=mxGetN(pfield);
65
66 if (M==0 | N==0){
67 ISSMERROR("%s%i (%s) %s%i%s%i%s","array in parameters structure field ",count,name," is of size (",M,",",N,")");
68 }
69 if (M==1 && N==1){
70 /*we have a simple scalar: */
71 param= new DoubleParam(enum_type,*mxGetPr(pfield));
72 parameters->AddObject(param);
73
74 }
75 else{
76 if (N==1){
77
78 /*vector: */
79 param= new DoubleVecParam(enum_type,mxGetPr(pfield),M);
80 parameters->AddObject(param);
81
82 }
83 else{
84 /*matrix: first, transpose, then plug into Param */
85 matrix=mxGetPr(pfield);
86 tmatrix=(double*)xmalloc(M*N*sizeof(double));
87 for (i=0;i<M;i++){
88 for(j=0;j<N;j++){
89 *(tmatrix+N*i+j)=*(matrix+M*j+i);
90 }
91 }
92
93 param= new DoubleMatParam(enum_type,tmatrix,M,N);
94 parameters->AddObject(param);
95
96 /*Free ressources:*/
97 xfree((void**)&tmatrix);
98 }
99 }
100
101 }
102 else if (mxIsChar(pfield)){
103 /* we have a string parameter:*/
104
105 int stringlen;
106 char* string=NULL;
107
108 stringlen = mxGetM(pfield)*mxGetN(pfield)+1;
109 string = (char*)xmalloc(sizeof(mxChar)*stringlen);
110 mxGetString(pfield,string,stringlen);
111
112 param= new StringParam(enum_type,string);
113 parameters->AddObject(param);
114
115 xfree((void**)&string);
116 }
117 else if (mxIsCell(pfield)){
118
119 /*This can be a string array, or a matrix array. Check the first
120 *element type to decide: */
121 pfield2=mxGetCell(pfield,0);
122 if (mxIsChar(pfield2)){
123
124 /*string array: */
125 M=mxGetM(pfield);
126 stringarray=(char**)xmalloc(M*sizeof(char*));
127
128 for(i=0;i<M;i++){
129 char* descriptor=NULL;
130 pfield2=mxGetCell(pfield,i);
131 FetchData(&descriptor,pfield2);
132 stringarray[i]=descriptor;
133 }
134
135 param= new StringArrayParam(enum_type,stringarray,M);
136 parameters->AddObject(param);
137
138 /*Free ressources:*/
139 for(i=0;i<M;i++){
140 char* descriptor=stringarray[i];
141 xfree((void**)&descriptor);
142 }
143 xfree((void**)&stringarray);
144
145 }
146 else{
147
148 /*matrix array: */
149 M=mxGetM(pfield);
150 array=(double**)xmalloc(M*sizeof(double*));
151 mdims_array=(int*)xmalloc(M*sizeof(int));
152 ndims_array=(int*)xmalloc(M*sizeof(int));
153
154 for(i=0;i<M;i++){
155 double* matrix=NULL;
156 int m,n;
157 pfield2=mxGetCell(pfield,i);
158 FetchData(&matrix,&m,&n,pfield2);
159 array[i]=matrix;
160 mdims_array[i]=m;
161 ndims_array[i]=n;
162 }
163
164 param= new DoubleMatArrayParam(enum_type,array,M,mdims_array,ndims_array);
165 parameters->AddObject(param);
166
167 /*Free ressources:*/
168 for(i=0;i<M;i++){
169 double* matrix=array[i];
170 xfree((void**)&matrix);
171 }
172 xfree((void**)&array);
173 xfree((void**)&mdims_array);
174 xfree((void**)&ndims_array);
175 }
176 }
177 else ISSMERROR("%s%i","unknow type in parameters structure field ",i);
178 }
179
180 /*Assign output pointers:*/
181 *pparameters=parameters;
182
183}
184#endif
Note: See TracBrowser for help on using the repository browser.