File: matlab_func_link.c

package info (click to toggle)
ferret-vis 7.6.0-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 218,500 kB
  • sloc: fortran: 234,502; ansic: 51,843; csh: 2,516; makefile: 1,613; sh: 1,571; pascal: 569; sed: 184; lisp: 122; awk: 26
file content (147 lines) | stat: -rw-r--r-- 3,722 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "engine.h"
#define  BUFSIZE 8192
#define NaN (0.0/0.0)		/* Anyone know a better way? */

static void convertToDouble(float *data, double *ddata, float bad_flag,
			    int ysize, int zsize, int maxy, int maxz);
static void convertToFloat(double *ddata, float *data, float bad_flag,
			    int ysize, int zsize, int maxy, int maxz);


static Engine *ep = 0;
int matlab_func_demo_(int *getFunction,
		      float *sal, float *temp, float *pres, float *res,
			 int *ysize, int *zsize, float *bad_flag,
		         int *maxy, int *maxz){
  mxArray *mSal = NULL, *mTemp = NULL, *mPres = NULL, *mRes = NULL;
  double *dSal, *dTemp, *dPres, *dRes;
  char buffer[BUFSIZE], cbuffer[BUFSIZE];
  int size = *ysize * *zsize;
  static int choice = 0;

				/* Open MATLAB engine */
  if (ep == 0){
    if (!(ep = engOpen("\0"))) {
      fprintf(stderr, "\nCan't start MATLAB engine\n");
      return EXIT_FAILURE;
    }
  }

				/* Set up the MATLAB output buffer */
  engOutputBuffer(ep, buffer, BUFSIZE);

  mSal = mxCreateDoubleMatrix(*ysize, *zsize, mxREAL);
  mxSetName(mSal, "SAL");
  dSal = mxGetPr(mSal);
  mTemp = mxCreateDoubleMatrix(*ysize, *zsize, mxREAL);
  mxSetName(mTemp, "TEMP");
  dTemp = mxGetPr(mTemp);
  mPres = mxCreateDoubleMatrix(1, *zsize, mxREAL);
  mxSetName(mPres, "PRES");
  dPres = mxGetPr(mPres);

				/* Convert from float->double */
  convertToDouble(temp, dTemp, *bad_flag, *ysize, *zsize, *maxy, *maxz);
  convertToDouble(sal, dSal, *bad_flag, *ysize, *zsize, *maxy, *maxz);
  convertToDouble(pres, dPres, *bad_flag, *zsize, 1, *zsize, 1);

  /* Place the variables into the MATLAB workspace */
  engPutArray(ep, mSal);
  engPutArray(ep, mPres);
  engPutArray(ep, mTemp);

  /* Get the function choice */
  if (*getFunction){
    choice = 0;
    while (choice < 1 || choice > 5){
      printf("1) sw_adtg (adiabatic temperature gradient)\n");
      printf("2) sw_cndr (conductivity ratio)\n");
      printf("3) sw_cp (heat capacity)\n");
      printf("4) sw_dens (density)\n");
      printf("5) sw_gpan (geopotential anomaly)\n");
      printf("Select a function: ");
      scanf("%d", &choice);
    }
  }


  sprintf(cbuffer, "res = matlab_func_test(%d,SAL,TEMP,PRES);", choice);
  engEvalString(ep, cbuffer);
  /*
   * Echo the output from the command.  First two characters are
   * always the double prompt (>>).
   */
  if (strlen(buffer) > 1)
    printf("%s", buffer+2);

  /* Get the result array */
  mRes = engGetArray(ep,"res");
  assert(mRes);
  dRes = mxGetPr(mRes);
  convertToFloat(dRes, res, *bad_flag, *ysize , *zsize, *maxy, *maxz);
 

  mxDestroyArray(mSal);
  mxDestroyArray(mPres);
  mxDestroyArray(mTemp);
  mxDestroyArray(mRes);
  
#if 0
   engClose(ep);
#endif
	
  return EXIT_SUCCESS;
}

static void convertToFloat(double *ddata, float *data, float bad_flag,
			    int ysize, int zsize, int maxy, int maxz)
{

  int i=0,j,k;
  for (k=0; k < zsize; ++k){
    for (j=0; j < ysize; ++j){
      double theData = ddata[i++];
      int index = k * maxy + j;
      if (isnan(theData)){
	data[index] = bad_flag;
      } else {
	data[index] = theData;
      }
#if 0
      printf("Converting: %d %lf -> %f\n", i, theData, data[i]);
#endif
    }
  }
}

static void convertToDouble(float *data, double *ddata, float bad_flag,
			    int ysize, int zsize, int maxy, int maxz)
{

  int i=0,j,k;
  for (k=0; k < zsize; ++k){
    for (j=0; j < ysize; ++j){
      int index = k * maxy + j;
      float theData = data[index];
      if (theData == bad_flag){
	ddata[i++] = NaN;
      } else {
	ddata[i++] = theData;
      }
#if 0
      printf("Converting: %d %f -> %lf\n", i-1, theData, ddata[i-1]);
#endif
    }
  }
}