/*
convert6432.c
Program to modify the header in grd type 1 GMT files
If these files are generated in Linux, the size of the header is 892;
if they are created in Cygwin or by the Microsoft C binaries, the size
is 896.
This program modifies the header, thus converting files from one
implementation to the other.
>From Linux to Cygwin, it inserts 4 bytes in the header
>From Cygwin to Linux, it removes 4 bytes in the header
The original file is not modified; a new converted file is created.
convert6432 ishift
ishift = 4 to add 4 bytes (linux to cygwin)
ishift = -4 to remove 4 bytes (cygwin to linux)
Marta, August 3, 2003
mghidella@dna.gov.ar
*/
#include
#include
#include
#include
#include
#include
main (argc,argv)
int argc;
char **argv;
{
char *infile, *outfile, *bufch, chaux[4];
FILE *fpin,*fpout;
/* Note that NMAX can be larger; it has to be greater than or equal to the
grid size in bytes */
int num,ie,imem,NMAX=4000000, iread,iw,i;
int nx,ny,node_offset,ishift,npad=0,*pnx;
float *grid;
if (argc != 4) {
fprintf(stderr,"Needed argumets:\n");
fprintf(stderr,"1) Input grd file (grd format =1)\n");
fprintf(stderr,"2) Output grd file (grd format =1)\n");
fprintf(stderr,"3) Number of bytes to shift in the header:\n");
fprintf(stderr," 4 (adds 4 bytes, linux to cygwin conversion)\n");
fprintf(stderr," -4 (removes 4 bytes, cygwin to linux conversion)\n");
exit(-1);
}
infile=argv[1]; outfile=argv[2];
ishift=atoi(argv[3]);
printf("infile: %s; outfile: %s; shift= %d\n",infile,outfile,ishift);
if (ishift != 4 && ishift != -4) {
fprintf(stderr,"third argument has to be either 4 or -4;\n exits ...\n");
exit(-1);
}
if(fpin=fopen(infile,"rb"))
fprintf(stderr, "opened file: %s \n", infile);
else {fprintf(stderr, "error opening: %s \n", infile);exit(-1);}
if(fpout=fopen(outfile,"wb"))
fprintf(stderr, "opened file: %s \n", outfile);
else {fprintf(stderr, "error opening: %s \n", outfile);exit(-1);}
/* Reads the first 4 bytes in the file to check that it is not a netcdf file */
fread(chaux,1,4,fpin);
if (chaux[0]==67 && chaux[1]==68 && chaux[2]==70 && chaux[3]==1) {
fprintf(stderr, "seems to be a netcdf file: chaux= %s \nexits...\n",chaux);
exit(-1);
}
/* gets nx from auxiliary char */
pnx = (int *) chaux;
nx=*pnx;
/* Reads ny and node_offset */
fread(&ny,4,1,fpin);
fread(&node_offset,4,1,fpin);
/* writes ny, ny and node_offset in the output file */
fwrite(&nx,4,1,fpout);
fwrite(&ny,4,1,fpout);
fwrite(&node_offset,4,1,fpout);
printf("nx= %d; ny= %d, node_offset=%d\n",nx,ny,node_offset);
/* num is the numbr of grid elements */
num=nx*ny;
/* only reads in this case */
if(ishift == -4) {fread(&npad,4,1,fpin);
}
/* only writes in this case */
if(ishift == 4) {fwrite(&npad,4,1,fpout);
}
printf("num= %d,num; npad= %d\n",num,npad);
/* calculates number of bytes needed for storage */
imem=sizeof(float)*num;
/* checks memory */
if(imem > NMAX) {
fprintf(stderr,"needed memory greater than expected ...");
exit(-1);
}
else
fprintf(stderr,"imem= %d\n",imem);
/* allocates memory for the remainder of the header */
bufch=(char *) malloc(880);
/* allocates grid memory */
grid=(float *) malloc(imem);
/* reads and writes the rest of the header */
iread=fread(bufch,1,880,fpin);
iw=fwrite(bufch,1,880,fpout);
printf("iread= %d iw= %d\n",iread,iw);
/* reads the whole grid */
iread=fread(grid,4,num,fpin);
/* checks for errors */
if( iread != num ) {
fprintf(stderr,"conversion failed; exits ...\n");
exit(-1);
}
/* writes the entire grid */
iw=fwrite(grid,4,num,fpout);
printf("iread= %d iw= %d\n",iread,iw);
free(grid);
free(bufch);
fclose(fpin);
fclose(fpout);
}