/* Some needed defined constants                            */
#define PIC_X 101
#define PIC_Y 101
#define NO_VALUE 100.0

output_velocities(fname,velocities,pic_x,pic_y)
double velocities[PIC_X][PIC_Y][2];
unsigned char fname[100];
int pic_x,pic_y;
{
float x,y;
float temp[PIC_X][PIC_Y][2];
int i,j,k,bytes,no_novals,no_vals,fdf,n;

n = 0;
for(i=0;i<pic_x;i++)
for(j=0;j<pic_y;j++)
	{
	if(velocities[i][j][0]!=NO_VALUE && velocities[i][j][1]!=NO_VALUE)
		{
		temp[i][j][0] =    velocities[i][j][0];
		temp[i][j][1] =   -velocities[i][j][1]; 
		}
	else temp[i][j][0] = temp[i][j][1] = NO_VALUE;
	}

if((fdf = creat(fname,0755))==NULL)
	{
	printf("Fatal error in creating file %s\n",fname);
	exit(1);
	}
/* original size */
y = pic_x;
x = pic_y;
write(fdf,&x,4);
write(fdf,&y,4);

/* size of result data */
y = pic_x-2*n;
x = pic_y-2*n;
write(fdf,&x,4);
write(fdf,&y,4);

/* offset to start of data */
y = n;
x = n;
write(fdf,&x,4);
write(fdf,&y,4);
bytes = 24;

no_novals = no_vals = 0;
for(i=n;i<pic_x-n;i++)
for(j=n;j<pic_y-n;j++)
	{
	if(velocities[i][j][0] != NO_VALUE && 
	   velocities[i][j][1] != NO_VALUE)
		{
		no_vals++;
		}
	else
		{
		no_novals++;
		}
	}
for(i=n;i<pic_x-n;i++)
	{
	bytes += write(fdf,&temp[i][n][0],(pic_y-2*n)*8);
	}
close(fdf);
printf("\nVelocities written to file: %s\n",fname);
printf("%d bytes output from output_velocities\n",bytes);
printf("Number of positions with velocity: %d\n",no_vals);
printf("Number of positions without velocity: %d\n",no_novals);
printf("Percentage of velocities: %f\n",no_vals/(1.0*(no_vals+no_novals))*100.0);
fflush(stdout);
}

