#include "su.h"
#include "segy.h"
#include "header.h"

#define MICRO 0.000001

char *sdoc[] = {
" Required parameters:						",
" Optional parameters: hdrwords -data -power			",
NULL};

segy tr;

/*
#include "hdr.h"
#include "bhdr.h"
#include "tapesegy.h"
#include "tapehdr.h"
#include "tapebhdr.h"
*/

void printheader_non(const segy *tp)
{
	int i;			/* index over header fields		*/
	int j;			/* index over non-null header fields	*/
	Value val;		/* value in header field		*/
	cwp_String type;	/* ... its data type			*/
	cwp_String key;		/* ... the name of the header field	*/
	Value zeroval;		 /* zero value to compare with		*/

	zeroval.l = 0;
	j = 0;
	for (i = 0; i < SU_NKEYS; i++) {
		gethval(tp, i, &val);
		key = getkey(i);
		type = hdtype(key);
		if (valcmp(type, val, zeroval)) { /* not equal to zero */
			(void) printf(" %s=", key);
			printfval(type, val);
			/* if ((++j % 6) == 0) putchar('\n'); */
		}
	}
	//putchar('\n');

	return;
}

int
main(int ac, char **av)
{
	cwp_String keyi;
	cwp_String typei;
	Value  vali;
	int indexi,i,all,prdata=0,prpower=0,prrms=0,prmax=0,itr,verbose=0,
		hbyte=-1;
	char      unass8[60],	*hd8;
	short int unass16[30],	*hd16;
	long int  unass32[15],	*hd32;
	float     unassf32[15],	*hdf32;
	double    unassf64[8],	*hdf64;
	int nonzunass;

	/* Hook up getpars */
	initargs(ac, av);
	requestdoc(1);

	getparint("verbose",&verbose);

	if(ac==1) {
		all = 1;
	} else {
		all = 0;
		if(verbose) {
			printf("c ");
			for(i=1;i<ac;i++)
				printf("%s\t",av[i]);
			printf("\n");
		}

		for(i=1;i<ac;i++) {
			if( !strcmp(av[i],"-data") )	prdata = 1;
			if( !strcmp(av[i],"-power") )	prpower = 1;
			if( !strcmp(av[i],"-rms") )	prrms = 1;
			if( !strcmp(av[i],"-max") )	prmax = 1;
			if( !strcmp(av[i],"-all") )	all = 1;
		}
	}
	if(prrms) prpower = 1;

	if(getparint("-hbyte",&hbyte)) all = 0;

	/* Loop over traces */
	itr = 0;
	while(vgettr(&tr)) {

		int prunass=0, prcvsta=0, psrcsta=0;

		hd8   = (char*)      &tr;
		hd16  = (short int*) &tr;
		hd32  = (long int*)  &tr;
		hdf32 = (float*)     &tr;
		hdf64 = (double*)    &tr;

		itr++;

		if(all) {
			printheader_non(&tr);
			//printf(" After printheader ");

			bcopy( (char*)&tr + 180 , (char*)unass8, 60);
			bcopy( (char*)&tr + 180 , (char*)unass16, 60);
			bcopy( (char*)&tr + 180 , (char*)unass32, 60);
			bcopy( (char*)&tr + 180 , (char*)unassf32, 60);
			nonzunass = 0;

			for(i=0;i<15;i++)
				if(unass32[i])
					nonzunass = 1;

			nonzunass = 0;

			if(nonzunass) {
				printf(
				"unass:\tint32\tint16\t\tint8\t\t\tfloat\n");
				for(i=0;i<15;i++) {
					if(unass32[i]) printf(
				"%2d:%10ld\t%5d %5d\t%3d %3d %3d %3d\t\t%g\n",
					i,unass32[i],
					unass16[2*i],unass16[2*i+1],
					unass8[4*i],unass8[4*i+1],
						unass8[4*i+2],unass8[4*i+3],
					unassf32[i]
					);
				}
			}
		} else for(i=1;i<ac;i++) {
			if(!strcmp(av[i],"srcsta")) {
				prcvsta = 1;
				continue;
			}
			if(!strcmp(av[i],"srcsta")) {
				prcvsta = 1;
				continue;
			}
			if(!strcmp(av[i],"unass")) {
				prunass = 0;
				continue;
			}
			if( *av[i]=='-') continue;
			/* should check for no = as well (with index?) */
			keyi = av[i];
			typei = hdtype(keyi);
			indexi = getindex(keyi);
			gethval(&tr, indexi, &vali);
			printfval(typei, vali);
			printf("\t");
		}
		if(psrcsta) {
			int *srcsta;
			srcsta = (int*)((char*)&tr + 220);
			/*if(*srcsta)*/ printf("srcsta = %d\n",*srcsta);
		}
		if(prunass) {
			long int *unassl;

			fprintf(stderr,"%s %d Should not be here if zero prunass=%d\n",
				__FILE__,__LINE__,prunass);

			unassl = (long int*) ( (char*)&tr + 180);
			for(i=0;i<15;i++) {
				if(unassl[i]) {
				printf("unassl[%d] = %ld\n",i,unassl[i]);
				}
			}
		}
		if(prdata) {
			float dt;
			if(tr.dt) dt = MICRO*i*tr.dt;
			if(tr.d1!=0.0) dt = tr.d1;
			if(dt==0.0) dt = 1.0;

			for(i=0;i<(int)tr.ns;i++) {
				printf("%g %g\n",tr.f1+i*dt,tr.data[i]);
			}
		}
		if(prpower) {
			float thepower=0.0;
			for(i=0;i<(int)tr.ns;i++)
				thepower += tr.data[i]*tr.data[i];
			if(prrms) {
				thepower = sqrt(thepower/tr.ns);
			}
			printf("%e\n", thepower);
		}
		if(prmax) {
			float themax,themin;
			themax = tr.data[0];
			themin = tr.data[0];
			for(i=1;i<(int)tr.ns;i++) {
				if(themax<tr.data[i]) themax=tr.data[i];
				if(themin>tr.data[i]) themin=tr.data[i];
			}
			printf("max=%e min=%e\n", themax,themin);
		}
		if(hbyte>=0 && hbyte<240) {
			printf("hbyte[%d]= %d %d %ld %e %e\n",hbyte,
				hd8[hbyte], hd16[hbyte/2], hd32[hbyte/4],
				hdf32[hbyte/4], hdf64[hbyte/8]);
		}
		printf("\n");
	}

	return EXIT_SUCCESS;
}
