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

Last change on this file since 4189 was 4189, checked in by seroussi, 15 years ago

minor

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/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 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 ISSMASSERT(pfield);
55
56 /*Check type of field: */
57 if (mxIsDouble(pfield)){
58
59 /*could be DOUBLE, DOUBLEVEC or DOUBLEMAT, depends on dimensions: */
60 M=mxGetM(pfield);
61 N=mxGetN(pfield);
62
63 if (M==0 | N==0){
64 ISSMERROR("%s%i (%s) %s%i%s%i%s","array in parameters structure field ",count,name," is of size (",M,",",N,")");
65 }
66 if (M==1 && N==1){
67 /*we have a simple scalar: */
68 param= new DoubleParam(enum_type,*mxGetPr(pfield));
69 parameters->AddObject(param);
70
71 }
72 else{
73 if (N==1){
74
75 /*vector: */
76 param= new DoubleVecParam(enum_type,mxGetPr(pfield),M);
77 parameters->AddObject(param);
78
79 }
80 else{
81 /*matrix: first, transpose, then plug into Param */
82 matrix=mxGetPr(pfield);
83 tmatrix=(double*)xmalloc(M*N*sizeof(double));
84 for (i=0;i<M;i++){
85 for(j=0;j<N;j++){
86 *(tmatrix+N*i+j)=*(matrix+M*j+i);
87 }
88 }
89
90 param= new DoubleMatParam(enum_type,tmatrix,M,N);
91 parameters->AddObject(param);
92
93 /*Free ressources:*/
94 xfree((void**)&tmatrix);
95 }
96 }
97
98 }
99 else if (mxIsChar(pfield)){
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 /*string array: */
116 M=mxGetM(pfield);
117 stringarray=(char**)xmalloc(M*sizeof(char*));
118
119 for(i=0;i<M;i++){
120 char* descriptor=NULL;
121 pfield2=mxGetCell(pfield,i);
122 FetchData(&descriptor,pfield2);
123 stringarray[i]=descriptor;
124 }
125
126 param= new StringArrayParam(enum_type,stringarray,M);
127 parameters->AddObject(param);
128
129 /*Free ressources:*/
130 for(i=0;i<M;i++){
131 char* descriptor=stringarray[i];
132 xfree((void**)&descriptor);
133 }
134 xfree((void**)&stringarray);
135
136 }
137 else ISSMERROR("%s%i","unknow type in parameters structure field ",i);
138 }
139
140 /*Assign output pointers:*/
141 *pparameters=parameters;
142
143}
144#endif
Note: See TracBrowser for help on using the repository browser.