Example #1
0
DllExport long   
SfAllMotorPos ( SpecFile *sf, long index, double **retpos, int *error )
{
     char **lines;
     char  *thisline,
           *endline;

     double *posarr;

     static double pos[200];
     static char   posstr[40];

     char  *ptr;

     long      motct = 0;
     long      no_lines;
     short     i,j;

#ifndef _GNU_SOURCE
#ifdef PYMCA_POSIX
	char *currentLocaleBuffer;
	char localeBuffer[21];
#endif
#endif

     if (sfSetCurrent(sf,index,error) == -1) {
         *retpos = (double *) NULL;
          return(0);
     }

    /*
     * if motors position for this scan have already been read
     */
     if (sf->motor_pos != (double *)NULL) {
         posarr = (double *)malloc(sizeof(double) * sf->no_motor_pos);
         for (i=0;i<sf->no_motor_pos;i++) {
             posarr[i] = sf->motor_pos[i];
         }
        *retpos = posarr;
         return(sf->no_motor_pos);
     }

    /*
     * else
     */
     no_lines =  SfHeader(sf, index,"P",&lines,error);

     if (no_lines == -1 || no_lines == 0 ) {
         *retpos = (double *) NULL;
          return(-1);
     }

     motct = 0;
#ifndef _GNU_SOURCE
#ifdef PYMCA_POSIX
	currentLocaleBuffer = setlocale(LC_NUMERIC, NULL);
	strcpy(localeBuffer, currentLocaleBuffer);
	setlocale(LC_NUMERIC, "C\0");
#endif
#endif
     for (j=0;j<no_lines;j++) {
         thisline = lines[j] + 4;
         endline  = thisline + strlen(thisline);
         for(ptr=thisline;*ptr == ' ';ptr++);
         for (i=0;ptr < endline -1;ptr++,i++) {
            if (*ptr==' ') { 
               posstr[i] = '\0';

               pos[motct]  = PyMcaAtof(posstr);

               motct++; 
               i=-1; 
               for(;*(ptr+1) == ' ' && ptr < endline -1;ptr++);
            } else {
               posstr[i] = *ptr;
            }
         }
         if (*ptr != ' ') {
            posstr[i]   = *ptr; 
            i++;
         }
         posstr[i]   = '\0';
         pos[motct]  = PyMcaAtof(posstr);

         motct++; 

	 }

#ifndef _GNU_SOURCE
#ifdef PYMCA_POSIX
	setlocale(LC_NUMERIC, localeBuffer);
#endif
#endif

     /*
      * Save in specfile structure
      */
      sf->no_motor_pos = motct;
      sf->motor_pos    = (double *)malloc(sizeof(double) * motct);
      memcpy(sf->motor_pos,pos,motct * sizeof(double));

     /*
      * and return
      */
      posarr = (double *) malloc ( sizeof(double) * motct ) ;
      memcpy(posarr,pos,motct * sizeof(double));

     *retpos = posarr;

      return( motct );
}
Example #2
0
/*********************************************************************
 *   Function:        int SfGetMca(sf, index, number, data, error)
 *
 *   Description:    Gets data.
 *   Parameters:
 *        Input :    (1) File pointer
 *            (2) Index
 *        Output:
 *            (3) Data array
 *            (4) Data info : [0] => no_lines
 *                    [1] => no_columns
 *                    [2] = ( 0 ) => regular
 *                          ( 1 ) => not regular !
 *            (5) error number
 *   Returns:
 *            (  0 ) => OK
 *                ( -1 ) => errors occured
 *   Possible errors:
 *            SF_ERR_MEMORY_ALLOC
 *            SF_ERR_FILE_READ
 *            SF_ERR_SCAN_NOT_FOUND
 *            SF_ERR_LINE_NOT_FOUND
 *
 *   Remark:  The memory allocated should be freed by the application
 *
 *********************************************************************/
DllExport int
SfGetMca( SpecFile *sf, long index, long number, double **retdata, int *error )
{
     double  *data  = NULL;
     long     headersize;
     int                  old_fashion;
     static char*         last_from = NULL;
     static char*         last_pos = NULL;
     static long          last_number = 0;
     long int             scanno = 0;
     static long int last_scanno = 0;
     char *ptr,
          *from,
          *to;

     char    strval[100];
     double  val;

     int     i,spect_no=0;
     long    vals;

     long    blocks=1,
             initsize=1024;
#ifndef _GNU_SOURCE
#ifdef PYMCA_POSIX
	char *currentLocaleBuffer;
	char localeBuffer[21];
#endif
#endif

     headersize = ((SpecScan *)sf->current->contents)->data_offset
                - ((SpecScan *)sf->current->contents)->offset;

     scanno = ((SpecScan *)sf->current->contents)->scan_no;

    /*
     *  check that mca number is available
     */
    if (number < 1) {
        *error = SF_ERR_MCA_NOT_FOUND;
        *retdata = (double *)NULL;
         return(-1);
    }

    /*
     * Get MCA info from header
     */

     from = sf->scanbuffer + headersize;
     to   = sf->scanbuffer + ((SpecScan *)sf->current->contents)->size;

     old_fashion = 1;
     if (last_scanno == scanno)
     {
         if (last_from == from)
         {
            /* same scan as before */
            if (number > last_number)
            {
                spect_no = last_number;
                old_fashion = 0;
            }
         }
    }
    if (old_fashion)
    {
        last_scanno = scanno;
	    last_from = from;
        spect_no   = 0;
        last_pos  = from;
    }
     /*
      * go and find the beginning of spectrum
      */
     ptr = last_pos;

     if ( *ptr == '@' ) {
         spect_no++;
         ptr++;
         last_pos = ptr;
     }

     while ( spect_no != number  && ptr < to ) {
            if (*ptr == '@') spect_no++;
            ptr++;
            last_pos = ptr;
     }
     ptr++;

     if ( spect_no != number ) {
        *error = SF_ERR_MCA_NOT_FOUND;
        *retdata = (double *)NULL;
         return(-1);
     }
     last_number = spect_no;
    /*
     * Calculate size and book memory
     */
     initsize = 2048;

     i    = 0;
     vals = 0;

     /*
      * Alloc memory
      */
     if ((data = (double *)malloc (sizeof(double) * initsize)) == (double *)NULL) {
         *error = SF_ERR_MEMORY_ALLOC;
          return(-1);
     }

    /*
     * continue
     */
#ifndef _GNU_SOURCE
#ifdef PYMCA_POSIX
	currentLocaleBuffer = setlocale(LC_NUMERIC, NULL);
	strcpy(localeBuffer, currentLocaleBuffer);
	setlocale(LC_NUMERIC, "C\0");
#endif
#endif
     for ( ;(*(ptr+1) != '\n' || (*ptr == MCA_CONT)) && ptr < to - 1 ; ptr++)
     {
         if (*ptr == ' ' || *ptr == '\t' || *ptr == '\\' || *ptr == '\n') {
             if ( i ) {
                if ( vals%initsize  == 0 ) {
                    blocks++;
                    if ((data = (double *)realloc (data, sizeof(double) * blocks * initsize))
                                       == (double *)NULL) {
                          *error = SF_ERR_MEMORY_ALLOC;
#ifndef _GNU_SOURCE
#ifdef PYMCA_POSIX
	setlocale(LC_NUMERIC, localeBuffer);
#endif
#endif
							return(-1);
                    }

                }
                strval[i] = '\0';
                i = 0;
                val = PyMcaAtof(strval);
                data[vals] = val;
                vals++;
             }
         } else if (isnumber(*ptr)) {
             strval[i] = *ptr;
             i++;
         }
	 }

     if (isnumber(*ptr)) {
       strval[i]    = *ptr;
       strval[i+1]  = '\0';
       val = PyMcaAtof(strval);
       data[vals] = val;
       vals++;
     }
#ifndef _GNU_SOURCE
#ifdef PYMCA_POSIX
	setlocale(LC_NUMERIC, localeBuffer);
#endif
#endif

    *retdata = data;

     return( vals );
}