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
RevLine 
[2333]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"
[3685]15#include "../objects/objects.h"
[2333]16#include "../shared/shared.h"
[3775]17#include "../include/include.h"
[3685]18#include "../EnumDefinitions/EnumDefinitions.h"
[2333]19
[3713]20void FetchParams(Parameters** pparameters, DataHandle dataref){
[2333]21
[3673]22 int i,j;
[3685]23 int count;
[2333]24
25 /*output: */
26 Param* param=NULL;
[3673]27 Parameters* parameters=NULL;
[2333]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;
[3673]38 int enum_type;
[2333]39
40
41 /*First, create parameters : */
[3673]42 parameters=new Parameters();
[2333]43
[3673]44 /*go through matlab params structure, and create Param object for each field: */
[2333]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);
[3673]52 enum_type=StringAsEnum(name);
[2333]53 pfield=mxGetFieldByNumber(dataref,0,count);
[4189]54 ISSMASSERT(pfield);
55
[2333]56 /*Check type of field: */
57 if (mxIsDouble(pfield)){
[4189]58
[2333]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){
[3570]64 ISSMERROR("%s%i (%s) %s%i%s%i%s","array in parameters structure field ",count,name," is of size (",M,",",N,")");
[2333]65 }
66 if (M==1 && N==1){
67 /*we have a simple scalar: */
[3673]68 param= new DoubleParam(enum_type,*mxGetPr(pfield));
[2333]69 parameters->AddObject(param);
70
71 }
72 else{
73 if (N==1){
74
75 /*vector: */
[3673]76 param= new DoubleVecParam(enum_type,mxGetPr(pfield),M);
[2333]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
[3673]90 param= new DoubleMatParam(enum_type,tmatrix,M,N);
[2333]91 parameters->AddObject(param);
92
93 /*Free ressources:*/
94 xfree((void**)&tmatrix);
95 }
96 }
97
98 }
99 else if (mxIsChar(pfield)){
[2337]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
[3673]109 param= new StringParam(enum_type,string);
[2333]110 parameters->AddObject(param);
111
[2337]112 xfree((void**)&string);
[2333]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
[3673]126 param= new StringArrayParam(enum_type,stringarray,M);
[2333]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 }
[3570]137 else ISSMERROR("%s%i","unknow type in parameters structure field ",i);
[2333]138 }
139
140 /*Assign output pointers:*/
141 *pparameters=parameters;
142
143}
144#endif
Note: See TracBrowser for help on using the repository browser.