/matlab/old/ReadData.c

http://github.com/Yniold/liftsrc · C · 568 lines · 398 code · 104 blank · 66 comment · 71 complexity · b3a3979a8ffe2e3af36044dcf7747512 MD5 · raw file

  1. /*=================================================================
  2. *
  3. * READData.c
  4. *
  5. * Read binary data from instrument software
  6. * The calling syntax is:
  7. *
  8. * [yp] = ReadData('filename')
  9. *
  10. *=================================================================*/
  11. /* $Revision: 1.3 $ */
  12. #include <math.h>
  13. #include <stdio.h>
  14. #include <time.h>
  15. /* #define D_HEADER 1 /* will print with mexPrintf the position of each row */
  16. /*#define UNIX */ /* has to be set for unix compilation */
  17. #ifdef UNIX
  18. #include <stdint.h>
  19. #else
  20. typedef unsigned short uint16_t;
  21. typedef unsigned uint32_t;
  22. struct timeval {
  23. long tv_sec;
  24. long tv_usec;
  25. };
  26. #endif
  27. #include "mex.h"
  28. #include "../include/elekIO.h"
  29. /* Input Arguments */
  30. #define T_IN prhs[0]
  31. #define Y_IN prhs[1]
  32. /* Output Arguments */
  33. #define YP_OUT plhs[0]
  34. #if !defined(MAX)
  35. #define MAX(A, B) ((A) > (B) ? (A) : (B))
  36. #endif
  37. #if !defined(MIN)
  38. #define MIN(A, B) ((A) < (B) ? (A) : (B))
  39. #endif
  40. #define PI 3.14159265
  41. static double mu = 1/82.45;
  42. static double mus = 1 - 1/82.45;
  43. static void ReadData(
  44. double yp[],
  45. double *t,
  46. double y[]
  47. )
  48. {
  49. double r1,r2;
  50. r1 = sqrt((y[0]+mu)*(y[0]+mu) + y[2]*y[2]);
  51. r2 = sqrt((y[0]-mus)*(y[0]-mus) + y[2]*y[2]);
  52. /* Print warning if dividing by zero. */
  53. if (r1 == 0.0 || r2 == 0.0 ){
  54. mexWarnMsgTxt("Division by zero!\n");
  55. }
  56. yp[0] = y[1];
  57. yp[1] = 2*y[3]+y[0]-mus*(y[0]+mu)/(r1*r1*r1)-mu*(y[0]-mus)/(r2*r2*r2);
  58. yp[2] = y[3];
  59. yp[3] = -2*y[1] + y[2] - mus*y[2]/(r1*r1*r1) - mu*y[2]/(r2*r2*r2);
  60. return;
  61. }
  62. void mexFunction( int nlhs, mxArray *plhs[],
  63. int nrhs, const mxArray*prhs[] )
  64. {
  65. /* unsigned int m,n; */
  66. unsigned short *z,*ptrData;
  67. char *input_buf;
  68. int buflen;
  69. int status;
  70. int i,j,k;
  71. int Card;
  72. FILE *fp;
  73. long flen;
  74. long nelements;
  75. int dims[2];
  76. unsigned char *Databuf;
  77. long count;
  78. struct tm *ptrTmZeit;
  79. time_t Seconds;
  80. struct elekStatusType *elekStatus;
  81. /* Check for proper number of arguments */
  82. if (nrhs != 1) {
  83. mexErrMsgTxt("Two input arguments required.");
  84. } else if (nlhs > 1) {
  85. mexErrMsgTxt("Too many output arguments.");
  86. }
  87. /* Input must be a string. */
  88. if (mxIsChar(prhs[0]) != 1)
  89. mexErrMsgTxt("Input must be a string.");
  90. /* Input must be a row vector. */
  91. if (mxGetM(prhs[0]) != 1)
  92. mexErrMsgTxt("Input must be a row vector.");
  93. /* Get the length of the input string. */
  94. buflen = (mxGetM(prhs[0]) * mxGetN(prhs[0])) + 1;
  95. /* Allocate memory for input and output strings. */
  96. input_buf = mxCalloc(buflen, sizeof(char));
  97. /* Copy the string data from prhs[0] into a C string
  98. * input_buf. If the string array contains several rows,
  99. * they are copied, one column at a time, into one long
  100. * string array. */
  101. status = mxGetString(prhs[0], input_buf, buflen);
  102. if (status != 0)
  103. mexWarnMsgTxt("Not enough space. String is truncated.");
  104. fp=fopen(input_buf,"r");
  105. if (fp==NULL)
  106. mexErrMsgTxt("can't open file");
  107. fseek(fp,0,SEEK_END);
  108. flen=ftell(fp);
  109. /* we have to allocate some space, use mxCalloc, matlab is cleaning up memory upon exit */
  110. Databuf=mxCalloc(flen, sizeof(char));
  111. nelements=flen/sizeof(struct elekStatusType);
  112. /* go to the beginning */
  113. fseek(fp,0,SEEK_SET);
  114. status=fread(Databuf,sizeof(struct elekStatusType),nelements,fp);
  115. fclose(fp);
  116. elekStatus=Databuf;
  117. dims[0]= nelements;
  118. dims[1]= 6; /* Jahr JulTag Stunde Minute Sekunde Mikrosek */
  119. dims[1]= dims[1] + ADC_CHANNEL_COUNTER_CARD+1+
  120. MAX_COUNTER_CHANNEL*(5+MAX_COUNTER_TIMESLOT+COUNTER_MASK_WIDTH);
  121. dims[1]= dims[1] + 12; /* etalon data type */
  122. dims[1]= dims[1] + MAX_ADC_CARD*(1+MAX_ADC_CHANNEL_PER_CARD*3+MAX_ADC_CHANNEL_PER_CARD*1);
  123. dims[1]= dims[1] + 2; /* mfc data */
  124. dims[1]= dims[1] + 2; /* valve data */
  125. dims[1]= dims[1] + MAX_DCDC4_CHANNEL_PER_CARD; /* dcdc data */
  126. dims[1]= dims[1] + 4 + MAX_TEMP_SENSOR*3; /* temp data */
  127. dims[1]= dims[1] + 1; /* instrument flags */
  128. dims[1]= dims[1] + 3; /* extra reserve */
  129. plhs[0] = mxCreateNumericArray(2,dims,mxUINT16_CLASS,mxREAL);
  130. /* Assign pointers to each input and output. */
  131. z = mxGetPr(plhs[0]);
  132. ptrData=z;
  133. count=0;
  134. /* Date */
  135. for (i=0; i<nelements;i++) {
  136. Seconds=elekStatus[i].TimeOfDay.tv_sec;
  137. ptrTmZeit=localtime(&Seconds);
  138. *(z+count++)=ptrTmZeit->tm_year;
  139. }
  140. for (i=0; i<nelements;i++) {
  141. Seconds=elekStatus[i].TimeOfDay.tv_sec;
  142. ptrTmZeit=localtime(&Seconds);
  143. *(z+count++)=ptrTmZeit->tm_yday;
  144. }
  145. for (i=0; i<nelements;i++) {
  146. Seconds=elekStatus[i].TimeOfDay.tv_sec;
  147. ptrTmZeit=localtime(&Seconds);
  148. *(z+count++)=ptrTmZeit->tm_hour;
  149. }
  150. for (i=0; i<nelements;i++) {
  151. Seconds=elekStatus[i].TimeOfDay.tv_sec;
  152. ptrTmZeit=localtime(&Seconds);
  153. *(z+count++)=ptrTmZeit->tm_min;
  154. }
  155. for (i=0; i<nelements;i++) {
  156. Seconds=elekStatus[i].TimeOfDay.tv_sec;
  157. ptrTmZeit=localtime(&Seconds);
  158. *(z+count++)=ptrTmZeit->tm_sec;
  159. }
  160. for (i=0; i<nelements;i++) {
  161. *(z+count++)=elekStatus[i].TimeOfDay.tv_usec/1000;
  162. }
  163. /******************* Counter Card ***************************/
  164. /* mexPrintf("%d %d %d\n",sizeof(uint16_t),sizeof(struct timeval),sizeof(struct elekStatusType));
  165. mexPrintf("%d/%d %d %d %d\n",count,dims[0]*dims[1],dims[0],dims[1],ADC_CHANNEL_COUNTER_CARD);
  166. */
  167. #ifdef D_HEADER
  168. mexPrintf("ccADC %f\n",1+(double)count/(double)nelements);
  169. #endif
  170. for (j=0;j<ADC_CHANNEL_COUNTER_CARD;j++) {
  171. for (i=0; i<nelements;i++) {
  172. *(z+count++)=elekStatus[i].CounterCard.ADCData[j];
  173. /* mexPrintf("%d %",elekStatus[i].CounterCard.ADCData[j]); */
  174. }
  175. }
  176. #ifdef D_HEADER
  177. mexPrintf("ccMasterDelay %d\n",1+count/nelements);
  178. #endif
  179. for (i=0; i<nelements;i++) {
  180. *(z+count++)=elekStatus[i].CounterCard.MasterDelay;
  181. }
  182. for (j=0;j<MAX_COUNTER_CHANNEL;j++) {
  183. #ifdef D_HEADER
  184. mexPrintf("ccShiftDelay #%d %d\n",j,1+count/nelements);
  185. #endif
  186. for (i=0; i<nelements;i++) {
  187. *(z+count++)=elekStatus[i].CounterCard.Channel[j].ShiftDelay;
  188. }
  189. #ifdef D_HEADER
  190. mexPrintf("ccGateDelay #%d %d\n",j,1+count/nelements);
  191. #endif
  192. for (i=0; i<nelements;i++) {
  193. *(z+count++)=elekStatus[i].CounterCard.Channel[j].GateDelay;
  194. }
  195. #ifdef D_HEADER
  196. mexPrintf("ccGateWidth #%d %d\n",j,1+count/nelements);
  197. #endif
  198. for (i=0; i<nelements;i++) {
  199. *(z+count++)=elekStatus[i].CounterCard.Channel[j].GateWidth;
  200. }
  201. #ifdef D_HEADER
  202. mexPrintf("ccData #%d %d\n",j,1+count/nelements);
  203. #endif
  204. for (k=0; k<MAX_COUNTER_TIMESLOT;k++) {
  205. for (i=0; i<nelements;i++) {
  206. *(z+count++)=elekStatus[i].CounterCard.Channel[j].Data[k];
  207. }
  208. }
  209. #ifdef D_HEADER
  210. mexPrintf("ccMask #%d %d\n",j,1+count/nelements);
  211. #endif
  212. for (k=0; k<COUNTER_MASK_WIDTH;k++) {
  213. for (i=0; i<nelements;i++) {
  214. *(z+count++)=elekStatus[i].CounterCard.Channel[j].Mask[k];
  215. }
  216. }
  217. #ifdef D_HEADER
  218. mexPrintf("ccCounts #%d %d\n",j,1+count/nelements);
  219. #endif
  220. for (i=0; i<nelements;i++) {
  221. *(z+count++)=elekStatus[i].CounterCard.Channel[j].Counts;
  222. }
  223. #ifdef D_HEADER
  224. mexPrintf("ccPulses #%d %d\n",j,1+count/nelements);
  225. #endif
  226. for (i=0; i<nelements;i++) {
  227. *(z+count++)=elekStatus[i].CounterCard.Channel[j].Pulses;
  228. }
  229. }
  230. /******************* etalon ***************************/
  231. #ifdef D_HEADER
  232. mexPrintf("etaSetPosLow %d\n",1+count/nelements);
  233. #endif
  234. /* 0*/
  235. for (i=0; i<nelements;i++) {
  236. *(z+count++)=elekStatus[i].EtalonData.Set.PositionWord.Low;
  237. }
  238. #ifdef D_HEADER
  239. mexPrintf("etaSetPosHigh %d\n",1+count/nelements);
  240. #endif
  241. /* 1*/
  242. for (i=0; i<nelements;i++) {
  243. *(z+count++)=elekStatus[i].EtalonData.Set.PositionWord.High;
  244. }
  245. #ifdef D_HEADER
  246. mexPrintf("etaCurPosLow %d\n",1+count/nelements);
  247. #endif
  248. /* 2*/
  249. for (i=0; i<nelements;i++) {
  250. *(z+count++)=elekStatus[i].EtalonData.Current.PositionWord.Low;
  251. }
  252. #ifdef D_HEADER
  253. mexPrintf("etaCurPosHigh %d\n",1+count/nelements);
  254. #endif
  255. /* 3*/
  256. for (i=0; i<nelements;i++) {
  257. *(z+count++)=elekStatus[i].EtalonData.Current.PositionWord.High;
  258. }
  259. #ifdef D_HEADER
  260. mexPrintf("etaEncoderPosLow %d\n",1+count/nelements);
  261. #endif
  262. /* 4*/
  263. for (i=0; i<nelements;i++) {
  264. *(z+count++)=elekStatus[i].EtalonData.Encoder.PositionWord.Low;
  265. }
  266. #ifdef D_HEADER
  267. mexPrintf("etaEncoderPosHigh %d\n",1+count/nelements);
  268. #endif
  269. /* 5*/
  270. for (i=0; i<nelements;i++) {
  271. *(z+count++)=elekStatus[i].EtalonData.Encoder.PositionWord.High;
  272. }
  273. #ifdef D_HEADER
  274. mexPrintf("etaIndexPosLow %d\n",1+count/nelements);
  275. #endif
  276. /* 6*/
  277. for (i=0; i<nelements;i++) {
  278. *(z+count++)=elekStatus[i].EtalonData.Index.PositionWord.Low;
  279. }
  280. #ifdef D_HEADER
  281. mexPrintf("etaIndexPosHigh %d\n",1+count/nelements);
  282. #endif
  283. /* 7*/
  284. for (i=0; i<nelements;i++) {
  285. *(z+count++)=elekStatus[i].EtalonData.Index.PositionWord.High;
  286. }
  287. #ifdef D_HEADER
  288. mexPrintf("etaSetSpd %d\n",1+count/nelements);
  289. #endif
  290. /* 8*/
  291. for (i=0; i<nelements;i++) {
  292. *(z+count++)=elekStatus[i].EtalonData.SetSpeed;
  293. }
  294. #ifdef D_HEADER
  295. mexPrintf("etaSetAcclSpd %d\n",1+count/nelements);
  296. #endif
  297. /* 9*/
  298. for (i=0; i<nelements;i++) {
  299. *(z+count++)=elekStatus[i].EtalonData.SetAccl;
  300. }
  301. #ifdef D_HEADER
  302. mexPrintf("etaCurSpd %d\n",1+count/nelements);
  303. #endif
  304. /* 10*/
  305. for (i=0; i<nelements;i++) {
  306. *(z+count++)=elekStatus[i].EtalonData.CurSpeed;
  307. }
  308. #ifdef D_HEADER
  309. mexPrintf("etaStatus %d\n",1+count/nelements);
  310. #endif
  311. /* 11*/
  312. for (i=0; i<nelements;i++) {
  313. *(z+count++)=elekStatus[i].EtalonData.Status;
  314. }
  315. /* mexPrintf("%d/%d\n",count,dims[1]); */
  316. /******************* ADC Cards ***************************/
  317. for (k=0; k<MAX_ADC_CARD; k++) {
  318. #ifdef D_HEADER
  319. mexPrintf("NumSamples %d\n",1+count/nelements);
  320. #endif
  321. for (i=0; i<nelements;i++)
  322. *(z+count++)=elekStatus[i].ADCCard[k].NumSamples;
  323. for (j=0;j<MAX_ADC_CHANNEL_PER_CARD;j++) {
  324. #ifdef D_HEADER
  325. mexPrintf("adcData #%d.%d %d\n",k,j,1+count/nelements);
  326. #endif
  327. for (i=0; i<nelements;i++)
  328. *(z+count++)=elekStatus[i].ADCCard[k].ADCChannelData[j].ADCData;
  329. #ifdef D_HEADER
  330. mexPrintf("adcSumDat %d\n",1+count/nelements);
  331. #endif
  332. for (i=0; i<nelements;i++)
  333. *(z+count++)=elekStatus[i].ADCCard[k].ADCChannelData[j].SumDat;
  334. #ifdef D_HEADER
  335. mexPrintf("adcSumSqr %d\n",1+count/nelements);
  336. #endif
  337. for (i=0; i<nelements;i++)
  338. *(z+count++)=elekStatus[i].ADCCard[k].ADCChannelData[j].SumSqr;
  339. }
  340. for (j=0;j<MAX_ADC_CHANNEL_PER_CARD;j++) {
  341. #ifdef D_HEADER
  342. mexPrintf("adcConfig #%d.%d %d\n",k,j,1+count/nelements);
  343. #endif
  344. for (i=0; i<nelements;i++)
  345. *(z+count++)=elekStatus[i].ADCCard[k].ADCChannelConfig[j].ADCChannelConfig;
  346. }
  347. }
  348. /* mexPrintf("%d/%d %d %d\n",count,dims[0]*dims[1],dims[0],dims[1]);
  349. */
  350. /******************* MFC Card ***************************/
  351. /* we are only interested in Data of Card 0 */
  352. Card=0;
  353. #ifdef D_HEADER
  354. mexPrintf("MFCSetFlow %d\n",1+count/nelements);
  355. #endif
  356. for (i=0; i<nelements;i++) {
  357. *(z+count++)=elekStatus[i].MFCCard[Card].MFCChannelData[0].SetFlow;
  358. }
  359. #ifdef D_HEADER
  360. mexPrintf("MFCFlow %d\n",1+count/nelements);
  361. #endif
  362. for (i=0; i<nelements;i++) {
  363. *(z+count++)=elekStatus[i].MFCCard[Card].MFCChannelData[0].Flow;
  364. }
  365. /******************* Valve Cards ***************************/
  366. /* for (k=0; k<MAX_VALVE_CARD; k++) { */
  367. /* we are only interested in Data of Card 0 */
  368. Card=0;
  369. #ifdef D_HEADER
  370. mexPrintf("ValveVolt %d\n",1+count/nelements);
  371. #endif
  372. for (i=0; i<nelements;i++) {
  373. *(z+count++)=elekStatus[i].ValveCard[Card].ValveVolt;
  374. }
  375. #ifdef D_HEADER
  376. mexPrintf("Valve %d\n",1+count/nelements);
  377. #endif
  378. for (i=0; i<nelements;i++) {
  379. *(z+count++)=elekStatus[i].ValveCard[Card].Valve;
  380. }
  381. /* }*/
  382. /******************* DCD4 Card ***************************/
  383. /* we are only interested in Data of Card 0 */
  384. Card=0;
  385. for (j=0; j<MAX_DCDC4_CHANNEL_PER_CARD; j++) {
  386. #ifdef D_HEADER
  387. mexPrintf("DCDC4channel #%d %d\n",j,1+count/nelements);
  388. #endif
  389. for (i=0; i<nelements;i++) {
  390. *(z+count++)=elekStatus[i].DCDC4Card[Card].Channel[j];
  391. }
  392. }
  393. /******************* TempSensor Card ***************************/
  394. /* we are only interested in Data of Card 0 */
  395. Card=0;
  396. #ifdef D_HEADER
  397. mexPrintf("TempMissed #%d %d\n",j,1+count/nelements);
  398. #endif
  399. for (i=0; i<nelements;i++) {
  400. *(z+count++)=elekStatus[i].TempSensCard[Card].NumMissed;
  401. }
  402. #ifdef D_HEADER
  403. mexPrintf("TempNumber #%d %d\n",j,1+count/nelements);
  404. #endif
  405. for (i=0; i<nelements;i++) {
  406. *(z+count++)=elekStatus[i].TempSensCard[Card].NumSensor;
  407. }
  408. #ifdef D_HEADER
  409. mexPrintf("TempErrCRC #%d %d\n",j,1+count/nelements);
  410. #endif
  411. for (i=0; i<nelements;i++) {
  412. *(z+count++)=elekStatus[i].TempSensCard[Card].NumErrCRC;
  413. }
  414. #ifdef D_HEADER
  415. mexPrintf("TempNoResponse #%d %d\n",j,1+count/nelements);
  416. #endif
  417. for (i=0; i<nelements;i++) {
  418. *(z+count++)=elekStatus[i].TempSensCard[Card].NumErrNoResponse;
  419. }
  420. for (j=0; j<MAX_TEMP_SENSOR; j++) {
  421. #ifdef D_HEADER
  422. mexPrintf("Temp #%d %d\n",j,1+count/nelements);
  423. #endif
  424. /* we represent the temperature in 1/100K) */
  425. for (i=0; i<nelements;i++) {
  426. *(z+count++)=(uint16_t) ( (elekStatus[i].TempSensCard[Card].TempSensor[j].Field.TempFrac*100.0/16.0+
  427. elekStatus[i].TempSensCard[Card].TempSensor[j].Field.TempMain*100)+27320);
  428. }
  429. #ifdef D_HEADER
  430. mexPrintf("Temp #%d valid_CRCerr_noresp_alarm %d\n",j,1+count/nelements);
  431. #endif
  432. for (i=0; i<nelements;i++) {
  433. *(z+count++)=elekStatus[i].TempSensCard[Card].TempSensor[j].Word.WordTemp & 17;
  434. }
  435. #ifdef D_HEADER
  436. mexPrintf("Temp #%d ID %d\n",j,1+count/nelements);
  437. #endif
  438. for (i=0; i<nelements;i++) {
  439. *(z+count++)=elekStatus[i].TempSensCard[Card].TempSensor[j].Word.WordID[1];
  440. }
  441. }
  442. /******************* Instrument Flags ***************************/
  443. #ifdef D_HEADER
  444. mexPrintf("EtalonAction %d\n",1+count/nelements);
  445. #endif
  446. for (i=0; i<nelements;i++) {
  447. *(z+count++)=elekStatus[i].InstrumentFlags.EtalonAction;
  448. }
  449. #ifdef D_HEADER
  450. mexPrintf("etaOnlinePosLow %d\n",1+count/nelements);
  451. #endif
  452. /* 6*/
  453. for (i=0; i<nelements;i++) {
  454. *(z+count++)=elekStatus[i].EtalonData.Online.PositionWord.Low;
  455. }
  456. #ifdef D_HEADER
  457. mexPrintf("etaOnlinePosHigh %d\n",1+count/nelements);
  458. #endif
  459. /* 7*/
  460. for (i=0; i<nelements;i++) {
  461. *(z+count++)=elekStatus[i].EtalonData.Online.PositionWord.High;
  462. }
  463. return;
  464. }