Esempio n. 1
0
int fgettra(FILE *fp, segy *tp, int itr)
{
	int nread;
	if(lastfp != fp)  searchlist(fp);  /* search for match */
		
	
	if(infoptr == (struct insegyinfo *) NULL) {
		/* get first trace */
		if(0 >= fgettr(fp, tp)) return(0); /* error return */

		switch(infoptr->ftype) {
		case TTY:
			warn("stdin not redirected");
		break;
		case DISK:	/* correct */
		break;
		default:
			err("%s: input must be disk file",__FILE__);
		}
	


		efseeko(fp,(off_t) 0LL,SEEK_END);
		if( in_line_hdr ){
			infoptr->ntr = (off_t)((eftello(fp)-3600)/infoptr->nsegy);
			efseeko(fp, (off_t) 3600+infoptr->nsegy,SEEK_SET);
		}else{
			infoptr->ntr = (off_t)(eftello(fp)/infoptr->nsegy);
			efseeko(fp, (off_t) infoptr->nsegy,SEEK_SET);
		}
	} /* end first entry initialization */
	
	/* Check on requested trace number */
	if(itr >= infoptr->ntr)
		err("%s: trying to read off end of file",__FILE__);
	
	/* Position file pointer at start of requested trace */
	if( in_line_hdr ){
		efseeko(fp, (off_t) 3600+itr*infoptr->nsegy,SEEK_SET);
	}else{
		efseeko(fp, (off_t) itr*infoptr->nsegy,SEEK_SET);
	}
	
	nread=fgettr(fp, tp); /* let fgettr do the work */
	if(nread != infoptr->nsegy)
		err("%s: read %d bytes in trace of %d bytes",
		    __FILE__,nread,infoptr->nsegy);
	
	if(tp->ns != infoptr->nsfirst)
		warn("%s: header ns field = %d differs from first trace = %d",
		     __FILE__,tp->ns,infoptr->nsfirst);
	
	return(infoptr->ntr);
}
Esempio n. 2
0
int
main (int argc, char **argv)
{
	int n1,n2,i2;
	float f1,f2,d1,d2,*x;
	char *label2="Trace",label[256];
	FILE *infp=stdin,*outfp=stdout;

	/* hook up getpar to handle the parameters */
	initargs(argc,argv);
	requestdoc(0);

	/* get optional parameters */
	if (!getparint("n1",&n1)) {
		if (efseeko(infp,(off_t) 0,SEEK_END)==-1)
			err("input file size is unknown; specify n1!\n");
		if ((n1=((int) (eftello(infp)/((off_t) sizeof(float)))))<=0)
			err("input file size is unknown; specify n1!\n");
		efseeko(infp,(off_t) 0,SEEK_SET);
	}

	if (!getparfloat("d1",&d1)) d1 = 1.0;
	if (!getparfloat("f1",&f1)) f1 = d1;
	if (!getparint("n2",&n2)) n2 = -1;
	if (!getparfloat("d2",&d2)) d2 = 1.0;
	if (!getparfloat("f2",&f2)) f2 = d2;
	getparstring("label2",&label2);

	/* allocate space */
	x = ealloc1float(n1);

	/* loop over 2nd dimension */
	for (i2=0; i2<n2 || n2<0; i2++) {

		/* read input array, watching for end of file */
		if (efread(x,sizeof(float),n1,infp)!=n1) break;
			
		/* make plot label */
		sprintf(label,"%s %0.4g",label2,f2+i2*d2);

		/* plot the array */
		prp1d(outfp,label,n1,d1,f1,x);
	}
	
	return(CWP_Exit());
}
Esempio n. 3
0
static
int fgettr_internal(FILE *fp, segy *tp, cwp_Bool fixed_length) {
   int nread;  /* bytes seen by fread calls  */
   unsigned char buf[240];  /* buffer for test for line header */

   /* search linked list for possible alternative */
   if(fp != lastfp)  searchlist(fp);

   if (infoptr == ((struct insegyinfo *) NULL)) {
	/* initialize new segy input stream */
	unsigned int databytes; /* bytes from nsfirst   */

	/* allocate new segy input information table */
	*oldinfoptr = (struct insegyinfo *)
	 malloc(sizeof(struct insegyinfo));
	infoptr = *oldinfoptr;
	infoptr->nextinfo = (struct insegyinfo *) NULL;
	infoptr->infp = fp;  /* save FILE * ptr */
	infoptr->itr = 0;
	infoptr->ntr = -1;
	
	switch (infoptr->ftype = filestat(fileno(fp))) {
	 case DIRECTORY:
	    err("%s: segy input can't be a directory", __FILE__);

	 case TTY:
	    err("%s: segy input can't be tty", __FILE__);

	 default:
	    /* all others are ok */
	    break;
	}

/*--------------------------------------------------------------------*\
   Check for the presence of a line header and set a flag if one is
   found. The decision of what to do will be delayed until the call
   to fputtr(). This allows us to accept data w/ or w/o a line
   header.

   Reginald H. Beardsley [email protected]
\*--------------------------------------------------------------------*/

	/* Attempt to get a text header */

	nread=efread(buf ,1 ,HDRBYTES ,infoptr->infp);

	switch( nread ){

	 case 0:   
	    return 0; /* no traces; trap in mains */

	 default:  

	    if (nread < HDRBYTES ){
		return 0; 

	    }else if( isascii_txt( buf ,HDRBYTES  )
		  || isebcdic_txt( buf ,HDRBYTES  ) ){
		in_line_hdr = 1;
		memcpy( su_text_hdr ,buf ,HDRBYTES );
		nread += efread(&(su_text_hdr[HDRBYTES]) ,1 
			,3200-HDRBYTES ,infoptr->infp);

	    }else{
		in_line_hdr=0;
		memcpy( tp ,buf ,HDRBYTES );

	    }
	}		

	
	if( in_line_hdr ){

	 /* Get the binary header */
	 nread = efread(&su_binary_hdr, 1, sizeof(bhed), infoptr->infp);
	 switch( nread ){
	    case 0:   
		return 0; /* no traces; trap in mains */

		default:  
		 if (nread != sizeof(su_binary_hdr)){
		    err("%s:%d bad binary header" , __FILE__ ,__LINE__ );
		 }
	 }	 

	 /* Get the first trace header */
	 nread = efread(tp, 1, HDRBYTES, infoptr->infp);
	 switch( nread ){
	    case 0:   
		return 0; /* no traces; trap in mains */

	    default:  
		if (nread != HDRBYTES){ 
		  err("%s: bad first header", __FILE__);
		}
	 }	 


	}

	/* Have the header, now for the data */
	infoptr->nsfirst = tp->ns;
	if (infoptr->nsfirst > SU_NFLTS){
	 err("%s: unable to handle %d > %d samples per trace",
		__FILE__, infoptr->nsfirst, SU_NFLTS);
	}

	switch (tp->trid) {
	 case CHARPACK:
	    infoptr->bytesper = sizeof(char); break;
	 case SHORTPACK:
	    infoptr->bytesper = 2*sizeof(char); break;
	 default:
	    infoptr->bytesper = sizeof(float); break;
	}

	databytes = infoptr->bytesper * tp->ns;

	infoptr->nsegy = HDRBYTES + databytes;


	/* Inconvenient to bump nread here; do it in the switch */
	nread = dataread(tp, infoptr, fixed_length);
	
	switch (nread) {
	 case 0:   
	    err("%s: no data on first trace", __FILE__);

	 default:  
	    if (nread != databytes){
		err("%s: first trace: " "read only %d bytes of %u",
		  __FILE__, nread, databytes);

	    }else{
		nread += HDRBYTES;
	    }
	}
	
	
	if (infoptr->ftype == DISK) { /* compute ntr */
	 efseeko(fp, (off_t) 0LL,SEEK_END);

	 if( in_line_hdr ){
	    infoptr->ntr = (eftello(fp)-3600)/infoptr->nsegy;
	    efseeko(fp, (off_t) 3600+infoptr->nsegy,SEEK_SET);

	 }else{
	    infoptr->ntr = eftello(fp)/infoptr->nsegy;
	    efseeko(fp, (off_t) infoptr->nsegy ,SEEK_SET);

	 }
	 
	}

   }else{

	/* not first trace */

/*--------------------------------------------------------------------*\
   A number of programs seek on the input file using either fseek(3c)
   or rewind(3c) and then expect to read trace data.  As a consequence
   we need to check and offset the filepointer if needed.
\*--------------------------------------------------------------------*/

	if( in_line_hdr && ftello( infoptr->infp ) == 0 ){

	 fseeko( infoptr->infp ,(off_t)3600L ,SEEK_SET );

	}

	nread = (int) efread(tp, 1, HDRBYTES, infoptr->infp);

	switch( nread ){

	 case 0: 
	    lastfp = infoptr->infp;
	    return 0; /* finished */

	 default:  
	    if (nread != HDRBYTES){
		err("%s: on trace #%ld read %d bytes expected %d bytes",
		  __FILE__,(infoptr->itr)+1,nread,HDRBYTES);
	    }
	}

	nread += dataread(tp, infoptr, fixed_length);

	if (fixed_length && (tp->ns != infoptr->nsfirst)){
	 err("%s: on trace #%ld number of samples in header (%d) differs from number for first trace (%d)"
		,__FILE__, (infoptr->itr)+1, tp->ns,

		infoptr->nsfirst);
	}
   }

   ++(infoptr->itr);
   lastfp = infoptr->infp;
   return (nread);
}
Esempio n. 4
0
static
int fgettr_internal(FILE *fp, segy *tp, cwp_Bool fixed_length)
{
	int nread;		/* bytes seen by fread calls	*/
	off_t curoff;

	if(fp != lastfp)  /* search linked list for possible alternative */
			    searchlist(fp);

	if (infoptr == ((struct insegyinfo *) NULL)) {
	/* initialize new segy input stream */
		unsigned int databytes;	/* bytes from nsfirst		*/

		/* allocate new segy input information table */
		*oldinfoptr = (struct insegyinfo *)
			malloc(sizeof(struct insegyinfo));
		infoptr = *oldinfoptr;
		infoptr->nextinfo = (struct insegyinfo *) NULL;
		/* save FILE * ptr */
		infoptr->infp = fp;
		infoptr->itr = 0;
		infoptr->ntr = -1;
		/* allocate XDR struct and associate FILE * ptr */
		infoptr->segy_xdr = (XDR *) malloc(sizeof(XDR));

		switch (infoptr->ftype = filestat(fileno(fp))) {
		case DIRECTORY:
			err("%s: segy input can't be a directory", __FILE__);
		case TTY:
			err("%s: segy input can't be tty", __FILE__);
		break;
		default: /* the rest are ok */
		break;
		}
		/* xdrstdio_create(infoptr->segy_xdr,fp,XDR_DECODE); */
		infoptr->buf = ealloc1(sizeof(segy),sizeof(char));
		xdrmem_create(infoptr->segy_xdr, infoptr->buf, sizeof(segy), XDR_DECODE);
		infoptr->bufstart = xdr_getpos(infoptr->segy_xdr);

		/* retrieve segy trace header */
		nread = efread(infoptr->buf ,1 ,HDRBYTES ,infoptr->infp);

		if(nread != HDRBYTES || FALSE == xdrhdrsub(infoptr->segy_xdr,tp))
			err("%s: bad first header", __FILE__);
		
		/* Have the header, now for the data */
		infoptr->nsfirst = tp->ns;
		if (infoptr->nsfirst > SU_NFLTS)
			err("%s: unable to handle %d > %d samples per trace",
				    __FILE__, infoptr->nsfirst, SU_NFLTS);

		switch(tp->trid) {
		case CHARPACK:
		   infoptr->bytesper = sizeof(char); break;
		case SHORTPACK:
		   infoptr->bytesper = 2*sizeof(char); break;
		default:
		   infoptr->bytesper = BYTES_PER_XDR_UNIT; break;
		}

		databytes = infoptr->bytesper * tp->ns;

		infoptr->nsegy = databytes + HDRBYTES;

		nread = dataread(infoptr, tp, fixed_length);


		switch (nread) {
		case 0:   err("%s: no data on first trace", __FILE__);
		default:  if (nread != databytes)
				 err("%s: first trace: tried to read %d bytes "
				     "read %d bytes",
					__FILE__, databytes, nread);
			else nread += HDRBYTES;
		}

		if(infoptr->ftype == DISK) { /* compute ntr */
		    curoff = eftello(fp); 
		    efseeko(fp,(off_t) 0,SEEK_END);
		    infoptr->ntr = eftello(fp)/infoptr->nsegy;
		    efseeko(fp, curoff, SEEK_SET); /* restore location */
		    }


	} else { /* Not first entry */

		  xdr_setpos(infoptr->segy_xdr, infoptr->bufstart);
		  nread = efread(infoptr->buf ,1 ,HDRBYTES ,infoptr->infp);
		  if ( nread != HDRBYTES || FALSE == xdrhdrsub(infoptr->segy_xdr,tp)) nread=0;
		  if(nread == HDRBYTES)
			nread += dataread(infoptr, tp, fixed_length);
		  if (nread <= 0) {
			   lastfp = infoptr->infp;
			   return 0;
		  }



		if (fixed_length && (tp->ns != infoptr->nsfirst))
			err("%s: on trace #%ld, "
			    "number of samples in header (%d) "
			    "differs from number for first trace (%d)", 
			     __FILE__, infoptr->itr, tp->ns, infoptr->nsfirst);
	}

	++(infoptr->itr);
	lastfp = infoptr->infp;
	return (nread);
}
Esempio n. 5
0
int
main (int argc, char **argv)
{
	int n1,n2,n1tic,n2tic,nfloats,bbox[4],
		i1,i2,grid1,grid2,style,
		i3,n3,notitle2=0,
		n1c,n2c,n1s,n2s,i1beg,i1end,i2beg,i2end,i1c,i2c,
		n1dsp=0,n2dsp=0,n3dsp=0,loopdsp,
		nz,iz,i1step,i2step,verbose;
	float labelsize,titlesize,perc,clip,bperc,wperc,bclip,wclip,
		d1,f1,d2,f2,*z,*temp,zscale,zoffset,zi,
		f3,d3,f3s,
		xbox,ybox,wbox,hbox,
		x1beg,x1end,x2beg,x2end,
		x1min,x1max,x2min,x2max,
		d1num,f1num,d2num,f2num,
		p1beg,p1end,p2beg,p2end,matrix[6],
		d1s,d2s;
	unsigned char *cz,*czp,*sz;
	char *label1="",*label2="",*title="",*title2="",
		*labelfont="Helvetica",*titlefont="Helvetica-Bold",
		*styles="seismic",*grid1s="none",*grid2s="none";
	char title2s[80],c80[80];
	FILE *infp=stdin;

	/* initialize getpar */
	initargs(argc,argv);
	requestdoc(1);

	/* get parameters describing 1st dimension sampling */
	if (!getparint("n1",&n1)) err("must specify n1!\n");
	if (!getparfloat("d1",&d1)) d1 = 1.0;
	if (!getparfloat("f1",&f1)) f1 = 0.0;

	/* get parameters describing 2nd dimension sampling */
	if (!getparint("n2",&n2)) {
		if (efseeko(infp,(off_t) 0,SEEK_END)!=0)
			err("must specify n2 if in a pipe!\n");
		nfloats = (int) (eftello(infp)/((off_t) sizeof(float)));
		efseeko(infp,(off_t) 0,SEEK_SET);
		n2 = nfloats/n1;
		n3 = 1;
	}
	if (!getparfloat("d2",&d2)) d2 = 1.0;
	if (!getparfloat("f2",&f2)) f2 = 0.0;

	/* get parameters describing 3rd dimension sampling */
	if (!getparint("n3",&n3)) {
		if (efseeko(infp,(off_t) 0,SEEK_END)!=0)
			err("must specify n3 if in a pipe!\n");
		nfloats = (int) (eftello(infp)/((off_t) sizeof(float)));
		efseeko(infp,(off_t) 0,SEEK_SET);
		n3 = nfloats/n1/n2;
	}
	if (!getparfloat("d3",&d3)) d3 = 1.0;
	if (!getparfloat("f3",&f3)) f3 = d3;

	/* set up desired looping mode */
	if (!getparint("loopdsp",&loopdsp)) loopdsp = 3;
	if (loopdsp == 3) {
		n1dsp = n1;
		n2dsp=n2;
		n3dsp=n3;
	} else if (loopdsp == 1) {
		n1dsp = n2;
		n2dsp=n3;
		n3dsp=n1;
	} else if (loopdsp == 2) {
		n1dsp = n1;
		n2dsp=n3;
		n3dsp=n2;
	}

	x1min = (d1>0.0)?f1:f1+(n1dsp-1)*d1;
	x1max = (d1<0.0)?f1:f1+(n1dsp-1)*d1;
	x2min = (d2>0.0)?f2:f2+(n2dsp-1)*d2;
	x2max = (d2<0.0)?f2:f2+(n2dsp-1)*d2;

	/* read binary data to be plotted */
	nz = n1*n2*n3;
	z = ealloc1float(nz);
	if (fread(z,sizeof(float),nz,infp)!=nz)
		err("error reading input file!\n");

	/* if necessary, determine clips from percentiles */
	if (getparfloat("clip",&clip)) {
		bclip = clip;
		wclip = -clip;
	}
	if ((!getparfloat("bclip",&bclip) || !getparfloat("wclip",&wclip)) &&
		!getparfloat("clip",&clip)) {
		perc = 100.0;  getparfloat("perc",&perc);
		temp = ealloc1float(nz);
		for (iz=0; iz<nz; iz++)
			temp[iz] = z[iz];
		if (!getparfloat("bclip",&bclip)) {
			bperc = perc;	getparfloat("bperc",&bperc);
			iz = (nz*bperc/100.0);
			if (iz<0) iz = 0;
			if (iz>nz-1) iz = nz-1;
			qkfind(iz,nz,temp);
			bclip = temp[iz];
		}
		if (!getparfloat("wclip",&wclip)) {
			wperc = 100.0-perc;  getparfloat("wperc",&wperc);
			iz = (nz*wperc/100.0);
			if (iz<0) iz = 0;
			if (iz>nz-1) iz = nz-1;
			qkfind(iz,nz,temp);
			wclip = temp[iz];
		}
		free1float(temp);
	}

	/* transpose data if needed */
	if (loopdsp == 1) {
		temp = ealloc1float(nz);
		for (iz=0;iz<nz;iz++) temp[iz] = z[iz];
		for (i3=0;i3<n3;i3++) {
			for (i2=0;i2<n2;i2++) {
		 		for(i1=0;i1<n1;i1++) {
		    			z[i2+i3*n2+i1*n2*n3]
						= temp[i1+i2*n1+i3*n1*n2];
				}
			}
		}
		free1float(temp);
	} else if (loopdsp == 2) {
		temp = ealloc1float(nz);
		for (iz=0;iz<nz;iz++) temp[iz] = z[iz];
		for (i3=0;i3<n3;i3++) {
			for (i2=0;i2<n2;i2++) {
				for(i1=0;i1<n1;i1++) {
					z[i1+i3*n1+i2*n1*n3]
						= temp[i1+i2*n1+i3*n1*n2];
				}
			}
		}
		free1float(temp);
	}

	verbose = 1;  getparint("verbose",&verbose);
	if (verbose) warn("bclip=%g wclip=%g",bclip,wclip);

	/* get scaled sampling intervals */
	d1s = 1.0;  getparfloat("d1s",&d1s);
	d2s = 1.0;  getparfloat("d2s",&d2s);
	d1s = fabs(d1s);  d1s *= d1;
	d2s = fabs(d2s);  d2s *= d2;

	/* get axes parameters */
	xbox = 1.0; getparfloat("xbox",&xbox);
	ybox = 1.5; getparfloat("ybox",&ybox);
	wbox = 6.0; getparfloat("wbox",&wbox);
	hbox = 8.0; getparfloat("hbox",&hbox);
	x1beg = x1min; getparfloat("x1beg",&x1beg);
	x1end = x1max; getparfloat("x1end",&x1end);
	d1num = 0.0; getparfloat("d1num",&d1num);
	f1num = x1min; getparfloat("f1num",&f1num);
	n1tic = 1; getparint("n1tic",&n1tic);
	getparstring("grid1",&grid1s);
	if (STREQ("dot",grid1s))
		grid1 = DOT;
	else if (STREQ("dash",grid1s))
		grid1 = DASH;
	else if (STREQ("solid",grid1s))
		grid1 = SOLID;
	else
		grid1 = NONE;
	getparstring("label1",&label1);
	x2beg = x2min; getparfloat("x2beg",&x2beg);
	x2end = x2max; getparfloat("x2end",&x2end);
	d2num = 0.0; getparfloat("d2num",&d2num);
	f2num = 0.0; getparfloat("f2num",&f2num);
	n2tic = 1; getparint("n2tic",&n2tic);
	getparstring("grid2",&grid2s);
	if (STREQ("dot",grid2s))
		grid2 = DOT;
	else if (STREQ("dash",grid2s))
		grid2 = DASH;
	else if (STREQ("solid",grid2s))
		grid2 = SOLID;
	else
		grid2 = NONE;
	getparstring("label2",&label2);
	getparstring("labelfont",&labelfont);
	labelsize = 18.0; getparfloat("labelsize",&labelsize);
	getparstring("title",&title);
	if (!getparstring("title2",&title2)) notitle2=1;
	getparstring("titlefont",&titlefont);
	titlesize = 24.0; getparfloat("titlesize",&titlesize);
	getparstring("style",&styles);
	if (STREQ("normal",styles))
		style = NORMAL;
	else
		style = SEISMIC;

	/* adjust x1beg and x1end to fall on sampled values */

	i1beg = NINT((x1beg-f1)/d1);
	i1beg = MAX(0,MIN(n1dsp-1,i1beg));
	x1beg = f1+i1beg*d1;
	i1end = NINT((x1end-f1)/d1);
	i1end = MAX(0,MIN(n1dsp-1,i1end));
	x1end = f1+i1end*d1;

	/* adjust x2beg and x2end to fall on sampled values */
	i2beg = NINT((x2beg-f2)/d2);
	i2beg = MAX(0,MIN(n2dsp-1,i2beg));
	x2beg = f2+i2beg*d2;
	i2end = NINT((x2end-f2)/d2);
	i2end = MAX(0,MIN(n2dsp-1,i2end));
	x2end = f2+i2end*d2;

	/* allocate space for image bytes */
	n1c = 1+abs(i1end-i1beg);
	n2c = 1+abs(i2end-i2beg);

	/* convert data to be imaged into unsigned characters */
	zscale = (wclip!=bclip)?255.0/(wclip-bclip):1.0e10;
	zoffset = -bclip*zscale;
	i1step = (i1end>i1beg)?1:-1;
	i2step = (i2end>i2beg)?1:-1;

	/* determine sampling after scaling */
	n1s = MAX(1,NINT(1+(n1c-1)*d1/d1s));
	d1s = (n1s>1)?d1*(n1c-1)/(n1s-1):d1;
	n2s = MAX(1,NINT(1+(n2c-1)*d2/d2s));
	d2s = (n2s>1)?d2*(n2c-1)/(n2s-1):d2;

	/* convert axes box parameters from inches to points */
	xbox *= 72.0;
	ybox *= 72.0;
	wbox *= 72.0;
	hbox *= 72.0;

	/* set bounding box */
	psAxesBBox3(
		xbox,ybox,wbox,hbox,
		labelfont,labelsize,
		titlefont,titlesize,
		style,bbox);
	boundingbox(bbox[0],bbox[1],bbox[2],bbox[3]);

	/* begin PostScript */
	beginps();

	/* loop over n3 */
	for(i3=0;i3<n3dsp;i3++) {
		cz = ealloc1(n1c*n2c,sizeof(char));
		czp = cz;
		for (i1c=0,i1=i1beg; i1c<n1c; i1c++,i1+=i1step) {
			for (i2c=0,i2=i2beg; i2c<n2c; i2c++,i2+=i2step) {
				zi = zoffset
					+z[i1+i2*n1dsp+i3*n1dsp*n2dsp]*zscale;
				if (zi<0.0) zi = 0.0;
				if (zi>255.0) zi = 255.0;
				*czp++ = (unsigned char)zi;
			}
		}

		/* if necessary, interpolate to scaled sampling intervals */
		if (n1s!=n1c || n2s!=n2c) {
			sz = ealloc1(n1s*n2s,sizeof(char));
			intl2b(n2c,d2,0.0,n1c,d1,0.0,cz,
				n2s,d2s,0.0,n1s,d1s,0.0,sz);
			free1(cz); 
		} else {
			sz = cz;
		}

		/* determine axes pads */
		p1beg = (x1end>x1beg)?-fabs(d1s)/2:fabs(d1s)/2;
		p1end = (x1end>x1beg)?fabs(d1s)/2:-fabs(d1s)/2;
		p2beg = (x2end>x2beg)?-fabs(d2s)/2:fabs(d2s)/2;
		p2end = (x2end>x2beg)?fabs(d2s)/2:-fabs(d2s)/2;

		newpage("1",i3+1);

		/* save graphics state */
		gsave();

		/* translate coordinate system by box offset */
		translate(xbox,ybox);

		/* determine image matrix */
		if (style==NORMAL) {
			matrix[0] = 0;  matrix[1] = n1s;  matrix[2] = n2s;
			matrix[3] = 0;  matrix[4] = 0;  matrix[5] = 0;
		} else {
			matrix[0] = n2s;  matrix[1] = 0;  matrix[2] = 0;
			matrix[3] = -n1s;  matrix[4] = 0;  matrix[5] = n1s;
		}

		scale(wbox,hbox);

		/* draw the image (before axes so grid lines are visible) */
		image(n2s,n1s,8,matrix,sz);

		/* restore graphics state */
		grestore();

		/* update title2 only if n3>1*/
		strcpy(title2s,title2);
		if ( notitle2 == 0 ) {
			char *nullchar="";	
			f3s = f3 + i3 * d3;
			sprintf(c80,"= %.4g %s",f3s,nullchar);
			strcat(title2s,c80);
		}

		/* draw axes and title */
		psAxesBox3(
			xbox,ybox,wbox,hbox,
			x1beg,x1end,p1beg,p1end,
			d1num,f1num,n1tic,grid1,label1,
			x2beg,x2end,p2beg,p2end,
			d2num,f2num,n2tic,grid2,label2,
			labelfont,labelsize,
			title,titlefont,titlesize,
			style, title2s); 

		showpage();
	}

	/* end PostScript */
	endps();

	return EXIT_SUCCESS;
}
Esempio n. 6
0
int
main (int argc, char **argv)
{
	int n1,n2,n3,nbpe,perm,i1,i2,i3,verbose;
	char *v=NULL;
	char *tempstem=NULL, **tempdirs=NULL;
	int ntempdirs;
	char tmpstem[] = "foo";
	char tmpdir[] = "/tmp";
	FILE *infp=stdin,*outfp=stdout;
	VND *handle;
	long N[3] ;
	long key[3] ;

	/* hook up getpar */
	initargs(argc,argv);
	requestdoc(1);

	/* get parameters */
	if (!getparint("n1",&n1)) err("must specify n1!");
	if (!getparint("n2",&n2)) err("must specify n2!");
	ntempdirs = countparval("scratchdir");
	if(ntempdirs > 0) {
		tempdirs = (char **) ealloc1(ntempdirs, sizeof(char *));
		getparstringarray("scratchdir",tempdirs);
	} else  {
		ntempdirs = 1;
		tempdirs = (char **) ealloc1(ntempdirs, sizeof(char *));
		strcpy(tempdirs[0],tmpdir);
	}
	if (!getparstring("scratchstem",&tempstem))
		tempstem = &tmpstem[0];
	if (!getparint("nbpe",&nbpe)) nbpe = sizeof(float);
	if (!getparint("perm",&perm)) perm = 231; /* trace to time slice */
	if (!getparint("n3",&n3)) {
		if (efseeko(infp,(off_t) 0,SEEK_END)==-1)
			err("input file size unknown; specify n3\n");
		n3 = (int) (eftello(infp)/(((off_t)n1)*((off_t)n2)*((off_t)nbpe)));
		efseeko(infp, (off_t) 0,SEEK_SET);
	}
	verbose = 0;  getparint("verbose",&verbose);
	if (verbose) warn("n1=%d  n2=%d n3=%d nbpe=%d perm=%d\n",n1,n2,n3,nbpe,perm);

	/* allocate space for a single vector in any dimension */
	v = ealloc1((n1+n2+n3),nbpe);

	/* allocate big matrix state */
	N[0] = n1; N[1] = n2; N[2] = n3;
	handle = VNDop(2,200000000,3,N,1,nbpe,tempstem,ntempdirs,tempdirs,1);

	/* put vectors along 1st dimension to big matrix */
	if (verbose) warn("Reading input file");
	for (i3=0; i3<n3; i3++) {
	for (i2=0; i2<n2; i2++) {
		if (fread(v,1,nbpe*n1,infp)!=nbpe*n1)
			err("Error reading input file:  i2=%d\n",i2);
		key[0] = 0; key[1] = i2; key[2] = i3;
		VNDrw('w',0,handle,0,key,0,v,0,1,n1,21,NULL);
	}
	}

	/* get vectors along 2nd dimension from big matrix */
	if (verbose) warn("Writing output file");
	switch(perm) {

	case 123:
		for (i3=0; i3<n3; i3++) {
		for (i2=0; i2<n2; i2++) {
			key[0] = 0; key[1] = i2; key[2] = i3;
			VNDrw('r',0,handle,0,key,0,v,0,1,n1,123,NULL);
		if (fwrite(v,1,nbpe*n1,outfp)!=nbpe*n1)
			err("Error writing output file:  i2=%d i3=%d",i2,i3);
		}
		}
		break;

	case 132:
		for (i2=0; i2<n2; i2++) {
		for (i3=0; i3<n3; i3++) {
			key[0] = 0; key[1] = i2; key[2] = i3;
			VNDrw('r',0,handle,0,key,0,v,0,1,n1,132,NULL);
		if (fwrite(v,1,nbpe*n1,outfp)!=nbpe*n1)
			err("Error writing output file:  i2=%d i3=%d",i2,i3);
		}
		}
		break;

	case 213:
		for (i3=0; i3<n3; i3++) {
		for (i1=0; i1<n1; i1++) {
			key[0] = i1; key[1] = 0; key[2] = i3;
			VNDrw('r',0,handle,1,key,0,v,0,1,n2,213,NULL);
		if (fwrite(v,1,nbpe*n2,outfp)!=nbpe*n2)
			err("Error writing output file:  i1=%d i3=%d",i1,i3);
		}
		}
		break;

	case 231:
		for (i1=0; i1<n1; i1++) {
		for (i3=0; i3<n3; i3++) {
			key[0] = i1; key[1] = 0; key[2] = i3;
			VNDrw('r',0,handle,1,key,0,v,0,1,n2,231,NULL);
		if (fwrite(v,1,nbpe*n2,outfp)!=nbpe*n2)
			err("Error writing output file:  i1=%d i3=%d",i1,i3);
		}
		}
		break;

	case 312:
		for (i2=0; i2<n2; i2++) {
		for (i1=0; i1<n1; i1++) {
			key[0] = i1; key[1] = i2; key[2] = 0;
			VNDrw('r',0,handle,2,key,0,v,0,1,n3,312,NULL);
		if (fwrite(v,1,nbpe*n3,outfp)!=nbpe*n3)
			err("Error writing output file:  i1=%d i2=%d",i1,i2);
		}
		}
		break;

	case 321:
		for (i1=0; i1<n1; i1++) {
		for (i2=0; i2<n2; i2++) {
			key[0] = i1; key[1] = i2; key[2] = 0;
			VNDrw('r',0,handle,2,key,0,v,0,1,n3,321,NULL);
		if (fwrite(v,1,nbpe*n3,outfp)!=nbpe*n3)
			err("Error writing output file:  i1=%d i2=%d",i1,i2);
		}
		}
		break;

	default:
		err("Unrecognized transpose permutation %d",perm);
	}

	/* free big matrix state */
	VNDcl(handle,1);
	if (verbose) warn("Transpose done!");

	return(CWP_Exit());
}
Esempio n. 7
0
int 
main (int argc, char **argv)
{
	int n1,n2,n1tic,n2tic,nfloats,bbox[4],
	  i1,i2,grid1,grid2,style,
	  n1c,n2c,n1s,n2s,i1beg,i1end,i2beg,i2end,i1c,i2c,
	  nz,iz,i1step,i2step,verbose,hls,bps,
	  legend,ugrid=SOLID,lstyle=VERTLEFT,lz,lbegsup=0,lendsup=0,ln=256,
	  lbbox[4], threecolor=0; /* BEREND, Schoenfelder */
        int lnice; /* c liner */
	float labelsize,titlesize,perc,clip,bperc,wperc,bclip,wclip,
		d1,f1,d2,f2,*z,*temp,zscale,zoffset,zi,
		xbox,ybox,width,height,
		x1beg,x1end,x2beg,x2end,
		x1min,x1max,x2min,x2max,
		d1num,f1num,d2num,f2num,
		p1beg,p1end,p2beg,p2end,matrix[6],colors[3][3], /* for 3 color mode */
		d1s,d2s,
	  lwidth,lheight,lx,ly,lbeg,lend,lmin=(float) FLT_MAX,lmax=(float) -FLT_MAX,
	  ldnum,lfnum,ld,lf=0,labmatrix[6]; /* BEREND, Schoenfelder */
	float axeswidth, ticwidth, gridwidth;
	unsigned char *cz,*czp,*sz,*data_legend=NULL;
	char *label1="",*label2="",*title="",*units="",
	  *legendfont="times_roman10",
	  *labelfont="Helvetica",*titlefont="Helvetica-Bold",
	  *styles="seismic",*grid1s="none",*grid2s="none",
	  *titlecolor="black",*axescolor="black",*gridcolor="black",
	  *lstyles="vertleft",*lgrids="none";
	FILE *infp=stdin;

	float **x1curve=NULL,**x2curve=NULL,*curvewidth=NULL;
	int i,j,curve=0,*npair=NULL,ncurvecolor=0,ncurvewidth=0,ncurvedash=0,*curvedash=NULL;
	char **curvecolor=NULL,**curvefile=NULL;
	FILE *curvefp=NULL;
	cwp_Bool is_curve = cwp_false;

	/* initialize getpar */
	initargs(argc,argv);
	requestdoc(1);

	/* get parameters describing 1st dimension sampling */
	if (!getparint("n1",&n1)) err("must specify n1!\n");
	d1 = 1.0;  getparfloat("d1",&d1);
	f1 = 0.0;  getparfloat("f1",&f1);
	x1min = (d1>0.0)?f1:f1+(n1-1)*d1;
	x1max = (d1<0.0)?f1:f1+(n1-1)*d1;

	/* get parameters describing 2nd dimension sampling */
	if (!getparint("n2",&n2)) {
		if (efseeko(infp,(off_t) 0,SEEK_END)!=0)
			err("must specify n2 if in a pipe!\n");
		nfloats = (int) (eftello(infp)/((off_t) sizeof(float)));
		efseeko(infp,(off_t) 0,SEEK_SET);
		n2 = nfloats/n1;
	}
	d2 = 1.0;  getparfloat("d2",&d2);
	f2 = 0.0;  getparfloat("f2",&f2);
	x2min = (d2>0.0)?f2:f2+(n2-1)*d2;
	x2max = (d2<0.0)?f2:f2+(n2-1)*d2;

	/* read color parameters */
	if (!getparint("threecolor",&threecolor)) threecolor=1;
	bps = 8;
	hls = 0;
	/* color[][0] is black, color[][2] is white in 2 color mode */
	colors[R][0] = colors[G][0] = colors[B][0] = 0.0;
	colors[R][1] = colors[G][1] = colors[B][1] = 0.5;
	colors[R][2] = colors[G][2] = colors[B][2] = 1.0;
	if (countparval("brgb") || countparval("wrgb")) {
		float brgb[3],grgb[3],wrgb[3];
		brgb[R] = brgb[G] = brgb[B] = 0.0;
		wrgb[R] = wrgb[G] = wrgb[B] = 1.0;
		getparfloat("brgb",&brgb[0]);
		getparfloat("wrgb",&wrgb[0]);
		grgb[R] = (brgb[R] + wrgb[R])/2.;
		grgb[G] = (brgb[G] + wrgb[G])/2.;
		grgb[B] = (brgb[B] + wrgb[B])/2.;
		if (threecolor==1)
		  getparfloat("grgb",&grgb[0]);
		brgb[R] = MAX(0.0,MIN(1.0,brgb[R]));
		grgb[R] = MAX(0.0,MIN(1.0,grgb[R]));
		wrgb[R] = MAX(0.0,MIN(1.0,wrgb[R]));
		brgb[G] = MAX(0.0,MIN(1.0,brgb[G]));
		grgb[G] = MAX(0.0,MIN(1.0,grgb[G]));
		wrgb[G] = MAX(0.0,MIN(1.0,wrgb[G]));
		brgb[B] = MAX(0.0,MIN(1.0,brgb[B]));
		grgb[B] = MAX(0.0,MIN(1.0,grgb[B]));
		wrgb[B] = MAX(0.0,MIN(1.0,wrgb[B]));
		colors[R][0] = brgb[R];	 colors[R][1] = grgb[R];  colors[R][2] = wrgb[R];
		colors[G][0] = brgb[G];	 colors[G][1] = grgb[G];  colors[G][2] = wrgb[G];
		colors[B][0] = brgb[B];	 colors[B][1] = grgb[B];  colors[B][2] = wrgb[B];
		if (!getparint("bps",&bps)) bps = 12;
		if (bps!=12 && bps!=24)
			err("bps must equal 12 or 24 for color plots!\n");
	} else if (countparval("bhls") || countparval("whls")) {
		float bhls[3],ghls[3],whls[3];
		hls = 1;
		bhls[H] = ghls[H] = whls[H] = 0.0;
		bhls[L] = 0.0;	ghls[L] = 0.5;	whls[L] = 1.0;
		bhls[S] = ghls[S] = whls[S] = 0.0;
		getparfloat("bhls",&bhls[0]);
		getparfloat("whls",&whls[0]);
		ghls[H] = (bhls[H] + whls[H])/2.;
		ghls[L] = (bhls[L] + whls[L])/2.;
		ghls[S] = (bhls[S] + whls[S])/2.;
		if (threecolor==1)
		  getparfloat("ghls",&ghls[0]);
		bhls[L] = MAX(0.0,MIN(1.0,bhls[L]));
		ghls[L] = MAX(0.0,MIN(1.0,ghls[L]));
		whls[L] = MAX(0.0,MIN(1.0,whls[L]));
		bhls[S] = MAX(0.0,MIN(1.0,bhls[S]));
		ghls[S] = MAX(0.0,MIN(1.0,ghls[S]));
		whls[S] = MAX(0.0,MIN(1.0,whls[S]));
		colors[H][0] = bhls[0];	 colors[H][1] = ghls[0];  colors[H][2] = whls[0];
		colors[L][0] = bhls[1];	 colors[L][1] = ghls[1];  colors[L][2] = whls[1];
		colors[S][0] = bhls[2];	 colors[S][1] = ghls[2];  colors[S][2] = whls[2];
		if (!getparint("bps",&bps)) bps = 12;
		if (bps!=12 && bps!=24)
			err("bps must equal 12 or 24 for color plots!\n");
	}

	/* get legend specs BEREND, Schoenfelder */
	legend = 0; getparint("legend", &legend); /* BEREND, Schoenfelder */
	getparstring("units", &units); /* BEREND, Schoenfelder */
	getparstring("legendfont", &legendfont);     /* BEREND, Schoenfelder */

	/* set up curve plotting */
	if ((curve=countparval("curve"))!=0) {
		curvefile=(char**)ealloc1(curve,sizeof(void*));
		getparstringarray("curve",curvefile);
		if ((x1curve=(float**)malloc(curve*sizeof(void*)))==NULL)
			err("Could not allocate x1curve pointers\n");
		if ((x2curve=(float**)malloc(curve*sizeof(void*)))==NULL)
			err("Could not allocate x2curve pointers\n");
		npair=ealloc1int(curve);
		getparint("npair",npair);
		is_curve = cwp_true;
	} else {
		npair=(int *)NULL;
		curvefile=(char **)NULL;
		x1curve=(float **)NULL;
		x2curve=(float **)NULL;
		is_curve = cwp_false;
	}
	if (is_curve) {
	 if ((ncurvecolor=countparval("curvecolor"))<curve) {
		curvecolor=(char**)ealloc1(curve,sizeof(void*));
		if (!getparstringarray("curvecolor",curvecolor)) {
			curvecolor[0]=(char *)cwp_strdup("black\0");
			ncurvecolor=1;
		}
		for (i=ncurvecolor; i<curve; i++)
			curvecolor[i]=(char *)cwp_strdup(curvecolor[ncurvecolor-1]);
	 } else if (ncurvecolor) {
		curvecolor=(char**)ealloc1(ncurvecolor,sizeof(void*));
		getparstringarray("curvecolor",curvecolor);
	 }
	 for (j=0; j<curve; j++) {
		curvefp=efopen(curvefile[j],"r");
		x1curve[j]=ealloc1float(npair[j]);
		x2curve[j]=ealloc1float(npair[j]);
		for (i=0; i<npair[j]; i++) {
			fscanf(curvefp,"%f",&x1curve[j][i]);
			fscanf(curvefp,"%f",&x2curve[j][i]);
		}
		efclose(curvefp);
	 }
	}

	/* read binary data to be plotted */
	nz = n1*n2;
	z = ealloc1float(nz);
	if (fread(z,sizeof(float),nz,infp)!=nz)
		err("error reading input file!\n");

	/* if necessary, determine clips from percentiles */
	if (getparfloat("clip",&clip)) {
		bclip = clip;
		wclip = -clip;
	}
	if ((!getparfloat("bclip",&bclip) || !getparfloat("wclip",&wclip)) &&
		!getparfloat("clip",&clip)) {
		perc = 100.0;  getparfloat("perc",&perc);
		temp = ealloc1float(nz);
		for (iz=0; iz<nz; iz++)
			temp[iz] = z[iz];
		if (!getparfloat("bclip",&bclip)) {
			bperc = perc;	getparfloat("bperc",&bperc);
			iz = (nz*bperc/100.0);
			if (iz<0) iz = 0;
			if (iz>nz-1) iz = nz-1;
			qkfind(iz,nz,temp);
			bclip = temp[iz];
		}
		if (!getparfloat("wclip",&wclip)) {
			wperc = 100.0-perc;  getparfloat("wperc",&wperc);
			iz = (nz*wperc/100.0);
			if (iz<0) iz = 0;
			if (iz>nz-1) iz = nz-1;
			qkfind(iz,nz,temp);
			wclip = temp[iz];
		}
		free1float(temp);
	}
	verbose = 1;  getparint("verbose",&verbose);
	if (verbose) warn("bclip=%g wclip=%g",bclip,wclip);

	/* get scaled sampling intervals */
	d1s = 1.0;  getparfloat("d1s",&d1s);
	d2s = 1.0;  getparfloat("d2s",&d2s);
	d1s = fabs(d1s);  d1s *= d1;
	d2s = fabs(d2s);  d2s *= d2;

	/* get axes parameters */
	xbox = 1.5; getparfloat("xbox",&xbox); /* if psimage is called by ximage, it */
	ybox = 1.5; getparfloat("ybox",&ybox); /* will xbox=1.166 and ybox=1.167 */
	width = 6.0; getparfloat("wbox",&width); getparfloat("width",&width);
	height = 8.0;getparfloat("hbox",&height);getparfloat("height",&height);
         /* begin c liner */
	lnice = 0;  getparint("lnice",&lnice); 
        if (lnice==1) {
            ybox = 2.2;
            /* lx=8 is set below, after getpar on lx ... c liner */
            width = 5.4;
            height = 7.2;
        }
         /* end c liner */
	x1beg = x1min; getparfloat("x1beg",&x1beg);
	x1end = x1max; getparfloat("x1end",&x1end);
	d1num = 0.0; getparfloat("d1num",&d1num);
	f1num = x1min; getparfloat("f1num",&f1num);
	n1tic = 1; getparint("n1tic",&n1tic);
	getparstring("grid1",&grid1s);
	if (STREQ("dot",grid1s))
		grid1 = DOT;
	else if (STREQ("dash",grid1s))
		grid1 = DASH;
	else if (STREQ("solid",grid1s))
		grid1 = SOLID;
	else
		grid1 = NONE;
	getparstring("label1",&label1);
	x2beg = x2min; getparfloat("x2beg",&x2beg);
	x2end = x2max; getparfloat("x2end",&x2end);
	d2num = 0.0; getparfloat("d2num",&d2num);
	f2num = 0.0; getparfloat("f2num",&f2num);
	n2tic = 1; getparint("n2tic",&n2tic);
	getparstring("grid2",&grid2s);
	if (STREQ("dot",grid2s))
		grid2 = DOT;
	else if (STREQ("dash",grid2s))
		grid2 = DASH;
	else if (STREQ("solid",grid2s))
		grid2 = SOLID;
	else
		grid2 = NONE;
	getparstring("label2",&label2);
	getparstring("labelfont",&labelfont);
	labelsize = 18.0; getparfloat("labelsize",&labelsize);
	getparstring("title",&title);
	getparstring("titlefont",&titlefont);
	titlesize = 24.0; getparfloat("titlesize",&titlesize);
	getparstring("titlecolor",&titlecolor);
	getparstring("axescolor",&axescolor);
	getparstring("gridcolor",&gridcolor);

	/* axes and tic width */
        if(!getparfloat("axeswidth",&axeswidth)) axeswidth=1;
        if (!getparfloat("ticwidth",&ticwidth)) ticwidth=axeswidth;
        if(!getparfloat("gridwidth",&gridwidth)) gridwidth =axeswidth;

	if (is_curve) {
	 if ((ncurvewidth=countparval("curvewidth"))<curve) {
		curvewidth=ealloc1float(curve);
		if (!getparfloat("curvewidth",curvewidth)) {
			curvewidth[0]=axeswidth;
			ncurvewidth=1;
		}
		for (i=ncurvewidth; i<curve; i++)
			curvewidth[i]=curvewidth[ncurvewidth-1];
	 } else {
		curvewidth=ealloc1float(ncurvewidth);
		getparfloat("curvewidth",curvewidth);
	 }
	 if ((ncurvedash=countparval("curvedash"))<curve) {
		curvedash=ealloc1int(curve);
		if (!getparint("curvedash",curvedash)) {
		        curvedash[0]=0;
			ncurvedash=1;
		}
		for (i=ncurvedash; i<curve; i++)
			curvedash[i]=curvedash[ncurvedash-1];
	 } else {
		curvedash=ealloc1int(ncurvedash);
		getparint("curvedash",curvedash);
	 }
	}

	getparstring("style",&styles);

	if (STREQ("normal",styles))
		style = NORMAL;
	else
		style = SEISMIC;

	/* Get or calc legend parameters */
	/* Legend min and max: Calc from data read in */
	if (legend) {
	  for (lz=0;lz<nz;lz++) {
	    lmin=FMIN(lmin,z[lz]);
	    lmax=FMAX(lmax,z[lz]);
	  }
	  if (verbose==2) warn("lmin=%g lmax=%g",lmin,lmax);
	}

	if (legend) {
	  lbeg = lmin; if (getparfloat("lbeg",&lbeg)) lbegsup=1;
	  lend = lmax; if (getparfloat("lend",&lend)) lendsup=1;


	  /* Change wclip,bclip to be inside legend range */
	  wclip = FMAX(lbeg,wclip); /* [wclip,bclip] has to be in [lbeg,lend] */
	  bclip = FMIN(lend,bclip);
	  if (lbegsup!=1) { /* Add white and black areas to show possible clipping */ 
	    float rangeperc=(bclip-wclip)/20.;
	    lbeg=wclip-rangeperc;
	  }
	  if (lendsup!=1) {
	    float rangeperc=(bclip-wclip)/20.;
	    lend=bclip+rangeperc;
	  }
	  
	  lfnum = lmin; getparfloat("lfnum",&lfnum);
	
	  getparstring("lstyle",&lstyles);
	  if (STREQ("vertright",lstyles))
	    lstyle = VERTRIGHT;
	  else if (STREQ("horibottom",lstyles))
	    lstyle = HORIBOTTOM;

	  /* legend dimensions (BEREND), Schoenfelder */
	  lwidth = 0.1 ;lheight = height/2;
	  if (lstyle==HORIBOTTOM) {
	    lwidth=width/1.2 ;lheight = 0.24;
	  }
	  getparfloat("lwidth",&lwidth);
	  getparfloat("lheight",&lheight);
	  
	  lx=.8;ly = ybox+(height-lheight)/2;
	  if (lstyle==VERTRIGHT) {
	    lx=xbox+width+0.1;
	  } else if (lstyle==HORIBOTTOM) {
	    lx=xbox+(width-lwidth)/2.0;ly = 1.0;
	  }
	  getparfloat("lx",&lx);
          if (lnice==1) lx = 8;   /* c liner */
	  getparfloat("ly",&ly);
	  
	  getparstring("lgrid",&lgrids);
	  if (STREQ("dot",lgrids))
	    ugrid = DOT;
	  else if (STREQ("dash",lgrids))
	    ugrid = DASH;
	  else if (STREQ("solid",lgrids))
	    ugrid = SOLID;
	  else
	    ugrid = NONE;
	}

	/* adjust x1beg and x1end to fall on sampled values */
	/* This will not allow to display an area greater than the data supplied */
	i1beg = NINT((x1beg-f1)/d1);
	i1beg = MAX(0,MIN(n1-1,i1beg));
	x1beg = f1+i1beg*d1;
	i1end = NINT((x1end-f1)/d1);
	i1end = MAX(0,MIN(n1-1,i1end));
	x1end = f1+i1end*d1;

	/* adjust x2beg and x2end to fall on sampled values */
	i2beg = NINT((x2beg-f2)/d2);
	i2beg = MAX(0,MIN(n2-1,i2beg));
	x2beg = f2+i2beg*d2;
	i2end = NINT((x2end-f2)/d2);
	i2end = MAX(0,MIN(n2-1,i2end));
	x2end = f2+i2end*d2;

	if (legend) {
	  /* Make legend color values */
	  int lll=0,lcount,perc5=13,ilbeg,ilend; /* color scale */
	  if (lbegsup!=1) {
	    ln+=perc5; /* white area */
	  }
	  if (lendsup!=1) {
	    ln+=perc5; /* black area */
	  }
	  data_legend = ealloc1(ln,sizeof(char));
	  if (lbegsup!=1) {
	    for (lll=0;lll<perc5;lll++) data_legend[lll]=(char) 255; /* white area */
	  }
	  for (lcount=255;lcount>=0;lcount--,lll++) data_legend[lll]=(char) lcount;
	  if (lendsup!=1) {
	    for (;lll<ln;lll++) data_legend[lll]=(char) 0; /* black area */
	  }
	  lf=lbeg;ld=(lend-lbeg)/(ln-1);
	  if (!(getparfloat("ldnum",&ldnum)))	ldnum=0.0;

	  /* adjust lbeg and lend to fall on sampled values */
	  ilbeg = NINT((lbeg-lf)/ld);
	  ilbeg = MAX(0,MIN(ln-1,ilbeg));
	  lbeg = lf+ilbeg*ld;
	  ilend = NINT((lend-lf)/ld);
	  ilend = MAX(0,MIN(ln-1,ilend));
	  lend = lf+ilend*ld;
	}
	/* allocate space for image bytes */
	n1c = 1+abs(i1end-i1beg);
	n2c = 1+abs(i2end-i2beg);
	cz = ealloc1(n1c*n2c,sizeof(char));

	/* convert data to be imaged into unsigned characters */
	zscale = (wclip!=bclip)?255.0/(wclip-bclip):1.0e10;
	zoffset = -bclip*zscale;
	i1step = (i1end>i1beg)?1:-1;
	i2step = (i2end>i2beg)?1:-1;
	czp = cz;
	for (i1c=0,i1=i1beg; i1c<n1c; i1c++,i1+=i1step) {
		for (i2c=0,i2=i2beg; i2c<n2c; i2c++,i2+=i2step) {
			zi = zoffset+z[i1+i2*n1]*zscale;
			if (zi<0.0) zi = 0.0;
			if (zi>255.0) zi = 255.0;
			*czp++ = (unsigned char)zi;
		}
	}
	free1float(z);

	/* determine sampling after scaling */
	n1s = MAX(1,NINT(1+(n1c-1)*d1/d1s));
	d1s = (n1s>1)?d1*(n1c-1)/(n1s-1):d1;
	n2s = MAX(1,NINT(1+(n2c-1)*d2/d2s));
	d2s = (n2s>1)?d2*(n2c-1)/(n2s-1):d2;

	/* if necessary, interpolate to scaled sampling intervals */
	if (n1s!=n1c || n2s!=n2c) {
		sz = ealloc1(n1s*n2s,sizeof(char));
		intl2b(n2c,d2,0.0,n1c,d1,0.0,cz,n2s,d2s,0.0,n1s,d1s,0.0,sz); /* Interpol array */
		free1(cz);
	} else {
		sz = cz;
	}

	/* determine axes pads */
	p1beg = (x1end>x1beg)?-fabs(d1s)/2:fabs(d1s)/2;
	p1end = (x1end>x1beg)?fabs(d1s)/2:-fabs(d1s)/2;
	p2beg = (x2end>x2beg)?-fabs(d2s)/2:fabs(d2s)/2;
	p2end = (x2end>x2beg)?fabs(d2s)/2:-fabs(d2s)/2;

	/* convert axes box parameters from inches to points */
	xbox *= 72.0;
	ybox *= 72.0;
	width *= 72.0;
	height *= 72.0;
	if (legend) {
	  lx *= 72.0; /* Schoenfelder */
	  ly *= 72.0; /* Schoenfelder */
	  lwidth *= 72.0; /* Schoenfelder */
	  lheight *= 72.0; /* Schoenfelder */
	}

	/* set bounding box */
	psAxesBBox(
		   xbox,ybox,width,height,
		   labelfont,labelsize,
		   titlefont,titlesize,
		   style,bbox);
	if (legend) {
	  psLegendBBox( /* Space for legend Schoenfelder */
			lx,ly,lwidth,lheight,
			labelfont,labelsize,
			lstyle,lbbox);
	  /* Include space for legend Schoenfelder */
	  bbox[0]=MIN(bbox[0],lbbox[0]);
	  bbox[1]=MIN(bbox[1],lbbox[1]);
	  bbox[2]=MAX(bbox[2],lbbox[2]);
	  bbox[3]=MAX(bbox[3],lbbox[3]);
	}
	boundingbox(bbox[0],bbox[1],bbox[2],bbox[3]);
	/* begin PostScript */
	begineps();

	/* save graphics state */
	gsave();

	/* translate coordinate system by box offset */
	translate(xbox,ybox);

	/* determine image matrix */
	if (style==NORMAL) {
		matrix[0] = 0;	matrix[1] = n1s;  matrix[2] = n2s;
		matrix[3] = 0;	matrix[4] = 0;	matrix[5] = 0;
	} else {
		matrix[0] = n2s;  matrix[1] = 0;  matrix[2] = 0;
		matrix[3] = -n1s;  matrix[4] = 0;  matrix[5] = n1s;
	}

	scale(width,height);

	/* draw the image (before axes so grid lines are visible) */
	drawimage(hls,colors,n2s,n1s,bps,matrix,sz);
	/***************************/
	/* main image has been drawn, restore graphics state */
	grestore();

	/* *********************************/
	/* draw the colorbar (before axes so grid lines are visible) Schoenfelder*/
	if (legend) {
	  gsave();
	  translate(lx,ly);
	  scale(lwidth,lheight);
	  if ((lstyle==VERTLEFT) || (lstyle==VERTRIGHT)) {
	    labmatrix[0] = 1;	 labmatrix[1] = 0;  labmatrix[2] = 0;
	    labmatrix[3] = ln; labmatrix[4] = 0;  labmatrix[5] = 0;
	    drawimage(hls,colors,1,ln,bps,labmatrix,data_legend);
	  } else {
	    labmatrix[0] = -1;	 labmatrix[1] = 0;  labmatrix[2] = 0;
	    labmatrix[3] = ln; labmatrix[4] = 0;  labmatrix[5] = 0;
	    rotate(-90);
	    drawimage(hls,colors,1,ln,bps,labmatrix,data_legend);
	    rotate(90);
	  }
	  
	  grestore();
	}

	/* draw curve */
	for (i=0; i<curve; i++) {
		gsave();
		psDrawCurve(
			xbox,ybox,width,height,
			x1beg,x1end,p1beg,p1end, 
			x2beg,x2end,p2beg,p2end,
			x1curve[i],x2curve[i],npair[i],
			curvecolor[i],curvewidth[i],curvedash[i],style);
		grestore();
	}


	gsave();
	/* draw axes and title */
	psAxesBox(
		  xbox,ybox,width,height,
		  x1beg,x1end,p1beg,p1end,
		  d1num,f1num,n1tic,grid1,label1,
		  x2beg,x2end,p2beg,p2end,
		  d2num,f2num,n2tic,grid2,label2,
		  labelfont,labelsize,
		  title,titlefont,titlesize,
		  titlecolor,axescolor,gridcolor,
		  ticwidth,axeswidth,gridwidth,
		  style);
	/* restore graphics state */
	grestore();

	/* draw axes and title for legend Schoenfelder*/
	if (legend) {
	  float lpbeg,lpend;
	  int lntic=1;
	  gsave();
	  lpbeg = 0.0; /*(lend>lbeg)?-fabs(d1s)/2:fabs(d1s)/2;*/
	  lpend = 0.0; /*(lend>lbeg)?fabs(d1s)/2:-fabs(d1s)/2;*/
	  
	  psLegendBox(
		    lx,ly,lwidth,lheight,
		    lbeg,lend,lpbeg,lpend,
		    ldnum,lf,lntic,ugrid,units,
		    labelfont,labelsize,
		    axescolor,gridcolor,
		    lstyle);
	  grestore();
	}

	/* end PostScript */
	showpage();
	endeps();

	if (curve) {
		free1int(npair);
		for (i=0; i<curve; i++) {
			free1float(x1curve[i]);
			free1float(x2curve[i]);
		}
		free1float(curvewidth);
		free1int(curvedash);
		free((void**)x1curve);
		free((void**)x2curve);
		free((void**)curvefile);
		free((void**)curvecolor);
	}

	return 0;
}