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

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

More commits

File size: 3.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/macros.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 int nfields;
35 char* name=NULL;
36 mxArray* pfield=NULL;
37 mxArray* pfield2=NULL;
38 int enum_type;
39
40
41 /*First, create parameters : */
42 parameters=new Parameters();
43
44 /*go through matlab params structure, and create Param object for each field: */
45
46 nfields=mxGetNumberOfFields(dataref);
47
48 for(count=0;count<nfields;count++){
49
50 /*Get i'th field: */
51 name=(char*)mxGetFieldNameByNumber(dataref,count);
52 enum_type=StringAsEnum(name);
53 pfield=mxGetFieldByNumber(dataref,0,count);
54
55 /*Check type of field: */
56 if (mxIsDouble(pfield)){
57 /*could be DOUBLE, DOUBLEVEC or DOUBLEMAT, depends on dimensions: */
58
59 M=mxGetM(pfield);
60 N=mxGetN(pfield);
61
62 if (M==0 | N==0){
63 ISSMERROR("%s%i (%s) %s%i%s%i%s","array in parameters structure field ",count,name," is of size (",M,",",N,")");
64 }
65 if (M==1 && N==1){
66 /*we have a simple scalar: */
67 param= new DoubleParam(enum_type,*mxGetPr(pfield));
68 parameters->AddObject(param);
69
70 }
71 else{
72 if (N==1){
73
74 /*vector: */
75 param= new DoubleVecParam(enum_type,mxGetPr(pfield),M);
76 parameters->AddObject(param);
77
78 }
79 else{
80 /*matrix: first, transpose, then plug into Param */
81 matrix=mxGetPr(pfield);
82 tmatrix=(double*)xmalloc(M*N*sizeof(double));
83 for (i=0;i<M;i++){
84 for(j=0;j<N;j++){
85 *(tmatrix+N*i+j)=*(matrix+M*j+i);
86 }
87 }
88
89 param= new DoubleMatParam(enum_type,tmatrix,M,N);
90 parameters->AddObject(param);
91
92 /*Free ressources:*/
93 xfree((void**)&tmatrix);
94 }
95 }
96
97 }
98 else if (mxIsChar(pfield)){
99
100 /* we have a string parameter:*/
101
102 int stringlen;
103 char* string=NULL;
104
105 stringlen = mxGetM(pfield)*mxGetN(pfield)+1;
106 string = (char*)xmalloc(sizeof(mxChar)*stringlen);
107 mxGetString(pfield,string,stringlen);
108
109 param= new StringParam(enum_type,string);
110 parameters->AddObject(param);
111
112 xfree((void**)&string);
113 }
114 else if (mxIsCell(pfield)){
115
116 /*string array: */
117 M=mxGetM(pfield);
118 stringarray=(char**)xmalloc(M*sizeof(char*));
119
120 for(i=0;i<M;i++){
121 char* descriptor=NULL;
122 pfield2=mxGetCell(pfield,i);
123 FetchData(&descriptor,pfield2);
124 stringarray[i]=descriptor;
125 }
126
127 param= new StringArrayParam(enum_type,stringarray,M);
128 parameters->AddObject(param);
129
130 /*Free ressources:*/
131 for(i=0;i<M;i++){
132 char* descriptor=stringarray[i];
133 xfree((void**)&descriptor);
134 }
135 xfree((void**)&stringarray);
136
137 }
138 else ISSMERROR("%s%i","unknow type in parameters structure field ",i);
139 }
140
141 /*Assign output pointers:*/
142 *pparameters=parameters;
143
144}
145#endif
Note: See TracBrowser for help on using the repository browser.