[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] | 20 | void 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;
|
---|
[4852] | 34 | double** array=NULL;
|
---|
| 35 | int* mdims_array=NULL;
|
---|
| 36 | int* ndims_array=NULL;
|
---|
[2333] | 37 | int nfields;
|
---|
| 38 | char* name=NULL;
|
---|
| 39 | mxArray* pfield=NULL;
|
---|
| 40 | mxArray* pfield2=NULL;
|
---|
[3673] | 41 | int enum_type;
|
---|
[2333] | 42 |
|
---|
| 43 |
|
---|
| 44 | /*First, create parameters : */
|
---|
[3673] | 45 | parameters=new Parameters();
|
---|
[2333] | 46 |
|
---|
[3673] | 47 | /*go through matlab params structure, and create Param object for each field: */
|
---|
[2333] | 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);
|
---|
[3673] | 55 | enum_type=StringAsEnum(name);
|
---|
[2333] | 56 | pfield=mxGetFieldByNumber(dataref,0,count);
|
---|
[4189] | 57 | ISSMASSERT(pfield);
|
---|
| 58 |
|
---|
[2333] | 59 | /*Check type of field: */
|
---|
| 60 | if (mxIsDouble(pfield)){
|
---|
[4189] | 61 |
|
---|
[2333] | 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){
|
---|
[3570] | 67 | ISSMERROR("%s%i (%s) %s%i%s%i%s","array in parameters structure field ",count,name," is of size (",M,",",N,")");
|
---|
[2333] | 68 | }
|
---|
| 69 | if (M==1 && N==1){
|
---|
| 70 | /*we have a simple scalar: */
|
---|
[3673] | 71 | param= new DoubleParam(enum_type,*mxGetPr(pfield));
|
---|
[2333] | 72 | parameters->AddObject(param);
|
---|
| 73 |
|
---|
| 74 | }
|
---|
| 75 | else{
|
---|
| 76 | if (N==1){
|
---|
| 77 |
|
---|
| 78 | /*vector: */
|
---|
[3673] | 79 | param= new DoubleVecParam(enum_type,mxGetPr(pfield),M);
|
---|
[2333] | 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 |
|
---|
[3673] | 93 | param= new DoubleMatParam(enum_type,tmatrix,M,N);
|
---|
[2333] | 94 | parameters->AddObject(param);
|
---|
| 95 |
|
---|
| 96 | /*Free ressources:*/
|
---|
| 97 | xfree((void**)&tmatrix);
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | }
|
---|
| 102 | else if (mxIsChar(pfield)){
|
---|
[2337] | 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 |
|
---|
[3673] | 112 | param= new StringParam(enum_type,string);
|
---|
[2333] | 113 | parameters->AddObject(param);
|
---|
| 114 |
|
---|
[2337] | 115 | xfree((void**)&string);
|
---|
[2333] | 116 | }
|
---|
| 117 | else if (mxIsCell(pfield)){
|
---|
| 118 |
|
---|
[4852] | 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 |
|
---|
[2333] | 145 | }
|
---|
[4852] | 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));
|
---|
[2333] | 153 |
|
---|
[4852] | 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);
|
---|
[2333] | 175 | }
|
---|
| 176 | }
|
---|
[3570] | 177 | else ISSMERROR("%s%i","unknow type in parameters structure field ",i);
|
---|
[2333] | 178 | }
|
---|
| 179 |
|
---|
| 180 | /*Assign output pointers:*/
|
---|
| 181 | *pparameters=parameters;
|
---|
| 182 |
|
---|
| 183 | }
|
---|
| 184 | #endif
|
---|