PageRenderTime 72ms CodeModel.GetById 15ms app.highlight 52ms RepoModel.GetById 1ms app.codeStats 0ms

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