#include <vxWorks.h>
#include <string.h>
#include <types.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <stdioLib.h>

#include <dbEvent.h>
#include <dbDefs.h>
#include <dbCommon.h>
#include <recSup.h>
#include <genSubRecord.h>

long scalerN_init( struct genSubRecord *pgsub )
{
  return( 0 );
}

long scalerN_proc( struct genSubRecord *pgsub )
{
/* BK this routine performs the scaler summing and profiles for a up to 16 channel array */
	long	*ptr[16], *out[19];
	int	nelm,iscal,jchan,nscal;
	long	totsum,sum;
/*	long	sum_arr[100];
	float	pos_arr[100];*/
	float	pos;
	float *pos_out;

	ptr[0]=  (long *)pgsub->a;	/* input arrays*/
	ptr[1]=  (long *)pgsub->b;
	ptr[2]=  (long *)pgsub->c;
	ptr[3]=  (long *)pgsub->d;
	ptr[4]=  (long *)pgsub->e;
	ptr[5]=  (long *)pgsub->f;
	ptr[6]=  (long *)pgsub->g;
	ptr[7]=  (long *)pgsub->h;
        ptr[8]=  (long *)pgsub->i;
        ptr[9]=  (long *)pgsub->j;
        ptr[10]=  (long *)pgsub->k;
        ptr[11]=  (long *)pgsub->l;
        ptr[12]=  (long *)pgsub->m;
        ptr[13]=  (long *)pgsub->n;
        ptr[14]=  (long *)pgsub->o;
        ptr[15]=  (long *)pgsub->p;
        
	out[0]=  (long *)pgsub->vala;	/* output (0 - 7) A to H are for sums of input arrays*/
	out[1]=  (long *)pgsub->valb;
	out[2]=  (long *)pgsub->valc;
	out[3]=  (long *)pgsub->vald;
	out[4]=  (long *)pgsub->vale;
	out[5]=  (long *)pgsub->valf;
	out[6]=  (long *)pgsub->valg;
	out[7]=  (long *)pgsub->valh;
        out[8]=  (long *)pgsub->vali;
        out[9]=  (long *)pgsub->valj;
        out[10]=  (long *)pgsub->valk;
        out[11]=  (long *)pgsub->vall;
        out[12]=  (long *)pgsub->valm;
        out[13]=  (long *)pgsub->valn;
        out[14]=  (long *)pgsub->valo;
        out[15]=  (long *)pgsub->valp;
	out[16]=  (long *)pgsub->valq;	/* output (16) I are for across sums of input arrays*/
	pos_out=  (float *)pgsub->valr;	/* output (17) J is for weighted mean of inputs, array of same length*/
        out[17]=  (long *)pgsub->vals;	/* output (18) K is profile array, dim max 16*/
        out[18]=  (long *)pgsub->valt;	/* output (19) L is total sum of input arrays*/

	nelm= pgsub->noa;
	nscal= pgsub->nou;
	totsum=0;
	for(iscal=0; iscal<nscal; iscal++)
		{
			sum=0;
			for(jchan=0; jchan < nelm; jchan++)
				{
					sum += *(ptr[iscal]++);
				}
			*out[iscal] = sum;
                        *(out[17]++) = sum;
			totsum += sum;
		}
        *out[18] = totsum;

	ptr[0]=  (long *)pgsub->a;
	ptr[1]=  (long *)pgsub->b;
	ptr[2]=  (long *)pgsub->c;
	ptr[3]=  (long *)pgsub->d;
	ptr[4]=  (long *)pgsub->e;
	ptr[5]=  (long *)pgsub->f;
	ptr[6]=  (long *)pgsub->g;
	ptr[7]=  (long *)pgsub->h;
        ptr[8]=  (long *)pgsub->i;
        ptr[9]=  (long *)pgsub->j;
        ptr[10]=  (long *)pgsub->k;
        ptr[11]=  (long *)pgsub->l;
        ptr[12]=  (long *)pgsub->m;
        ptr[13]=  (long *)pgsub->n;
        ptr[14]=  (long *)pgsub->o;
        ptr[15]=  (long *)pgsub->p;
        
	for(jchan=0; jchan < nelm; jchan++)
		{
			sum = 0;
			pos = 0.;
			for(iscal=0; iscal<nscal; iscal++)
			{
			sum += *(ptr[iscal]);
			pos += iscal * (*(ptr[iscal]++));
			}
			if( sum > 0 )
				pos = pos / sum;
			else
				pos = (nscal-1)/2.;
			*(out[16]++) = sum;
			*pos_out++ = pos;
		}

  return(totsum);
}

