/************************************************************
*     20/9/2005 modified by david fedele to read buffer-data
*     instead raw-data-file 
**************************************************************
 * include needed system headers
 */
#include <stdio.h>       /* include standard i/o library */
#include <stdlib.h>      /* include standard library */
#include <string.h>      /* include string library */
#include <unistd.h>      /* include unix standard library */
#include <sys/types.h>   /* */
#include <sys/stat.h>    /* */
#include <fcntl.h>       /* */
#include <errno.h>       /* error simbol definitions */
#include <time.h>        /* system time definitions */
#include <math.h>        /* math library */
/************************************************************
 *     20/9/2005 modified by david fedele                     */
/*void readped_(float *ped_buf, int *error, int *fd)*/
void getbytes_(int *nbytes, char *buffer, int *p, unsigned int *value);


void readped_(float *ped_buf, int *error,char *buffer, int *len,int *curpos)
/**************************************************************/

{
   
    int i;
    unsigned int decimals,units;
    unsigned int word;
    unsigned int ped_buf_temp[1024];
    
    *error=0;
/************************************************************
 *     20/9/2005 modified by david fedele                     */
 /*    int nread; */

/* /\*** read the buffer ***\/ */
/*     nread=read(*fd, ped_buf_temp, 2*1024); */
/*     if( nread==0 ) { */
/* 	printf("readped: Unexpected end of file %d at %d \n",*fd,nread); */
/* 	*error=1; */
/* 	return ; */
/*     } */
/*     if ( nread != 2*1024) { */
/* 	printf("readped: Error on reading: nread = %d \n",nread); */
/* 	*error=-1; */
/*  	return ;  */
/*     } */


/* /\*** correct byte ***\/ //endianess... */
/*     for(i=0; i<1024; i++) { */
/* 	word = ped_buf_temp[i]; */
/* 	ped_buf_temp[i] = ( (word&0x00ff) << 8 ) | ( (word&0xff00) >> 8 ); */
/*     } */

    int nbytes2=2;
    char *buff=buffer;
    
    if( *curpos+(2*1024)-1>*len ) {
	printf("readped: Unexpected end of file\n");
	*error=1;
	return ;
    }
    
    for(i=0; i<1024; i++)
	getbytes_(&nbytes2,buff,curpos,&ped_buf_temp[i]);
    
    
/*****************************************************************/

    /***interpreta*/
    for(i=0; i<1024; i++) {

	word = ped_buf_temp[i];
	units = ( (word >> 4) & 0x0fff ) ;
	decimals = (word & 0x000f) ;
	ped_buf[i] = (float)units + (float)decimals / 16.;
//	printf("%i %i %i %f \n",i,units,decimals,ped_buf[i]);
    }


//    for( i=0; i<1024; i++) printf("%x \n",ped_buf[i]);//???

    return; 
}
