/* Write xdr data */ #include #include #include #include #include /* file handle and xdr stream */ FILE *file; XDR xdrs; #define MAXFILES 100 short nfiles=0; struct xdrfile { char *flnm; int nframes; }; static struct xdrfile files[MAXFILES]; /*--------------------------------------------------------------------*/ bool_t xdr_counted_string(char **p) { int input; short length; XDR *xdrloc; xdrloc = &xdrs; input = (xdrloc->x_op == XDR_DECODE); if (!input) length = strlen(*p); if (!xdr_short(xdrloc, &length)) return(FALSE); if (input) { *p = malloc((unsigned) (length + 1)); *p[length] = '\0'; } return(length ? xdr_string(xdrloc, p, length) : TRUE); } /*--------------------------------------------------------------------*/ /* Main C function to write an xdr file -- this is the only C function called from fortran as follows: istat = wrxdrc(flnm(1:lenstr(flnm)),hdr1,hdr2,hdr3,hdr4, + f,nx,ny,xx,yy,xlabel,ylabel,mtime,iclose) */ int WRXDRC(_fcd flnm, _fcd hdr1, _fcd hdr2, _fcd hdr3, _fcd hdr4, float *f2d, short *nx, short *ny, float *xx, float *yy, _fcd xlab, _fcd ylab, short *mtime, short *iclose) { int slen,istat,n,clen; u_int fsize = sizeof(float); u_int maxsize; char *cflnm, *cstr; short frsize,iframe,ifile; /* Open file and create xdr stream: */ slen = _fcdlen(flnm); cflnm = malloc(slen+1); strncpy(cflnm, _fcdtocp(flnm), slen); strcpy(cflnm+slen,"\0"); if ((file = fopen(cflnm,"a"))==NULL) { printf("wrxdrc: cannot open file %s\n",cflnm); exit(1); } xdrstdio_create(&xdrs, file, XDR_ENCODE); ifile = -1; for (n=0; n < MAXFILES; n++) { if (strcmp(files[n].flnm,cflnm)==0) { ifile = n; } } if (ifile == -1) { /* new file */ nfiles++; if (nfiles > MAXFILES) { printf(">>> WRXDR: too many xdr files = %d (increase MAXFILES)\n", MAXFILES); exit(1); } ifile = nfiles; files[ifile].nframes = 0; files[ifile].flnm = cflnm; printf("WRXDR: Opened file %s to receive xdr data (file #%d).\n", cflnm,ifile); } /* If iclose flag is set, write number of frames to end of file, destroy stream, close file, and return: */ if (*iclose > 0) { iframe = files[ifile].nframes; (void) xdr_short(&xdrs,&iframe); (void) fclose(file); xdr_destroy(&xdrs); printf("WRXDR: Closed xdr file %s (nframes=%d)\n", cflnm,iframe); return(1); } /* Write dummy integer for later use in frame sizing: */ frsize = 0; (void) xdr_short(&xdrs, &frsize); /* Convert fortran header strings and write each to the file: */ slen = _fcdlen(hdr1); cstr = malloc(slen+1); strncpy(cstr, _fcdtocp(hdr1), slen); strcpy(cstr+slen, "\0"); (void) xdr_counted_string(&cstr); slen = _fcdlen(hdr2); cstr = malloc(slen+1); strncpy(cstr, _fcdtocp(hdr2), slen); strcpy(cstr+slen, "\0"); (void) xdr_counted_string(&cstr); slen = _fcdlen(hdr3); cstr = malloc(slen+1); strncpy(cstr, _fcdtocp(hdr3), slen); strcpy(cstr+slen, "\0"); (void) xdr_counted_string(&cstr); slen = _fcdlen(hdr4); cstr = malloc(slen+1); strncpy(cstr, _fcdtocp(hdr4), slen); strcpy(cstr+slen, "\0"); (void) xdr_counted_string(&cstr); /* Write nx: */ (void) xdr_short(&xdrs, nx); /* Write xlab: */ slen = _fcdlen(xlab); cstr = malloc(slen+1); strncpy(cstr, _fcdtocp(xlab), slen); strcpy(cstr+slen, "\0"); (void) xdr_counted_string(&cstr); /* Write xx(nx) */ for (n=0; n < *nx; n++) { (void) xdr_float(&xdrs,(xx+n)); } /* Write ny: */ (void) xdr_short(&xdrs, ny); /* Write ylab: */ slen = _fcdlen(ylab); cstr = malloc(slen+1); strncpy(cstr, _fcdtocp(ylab), slen); strcpy(cstr+slen, "\0"); (void) xdr_counted_string(&cstr); /* Write yy(ny) */ for (n=0; n < *ny; n++) { (void) xdr_float(&xdrs,(yy+n)); } /* Write model time (day,hr,min) (this added 4/8/97) */ for (n=0; n < 3; n++) { (void) xdr_short(&xdrs, (mtime+n)); } /* Write data f2d(nx,ny) (may be 1d or 2d): */ if (*nx > 1 && *ny > 1) for (n=0; n < *nx * *ny; n++) { (void) xdr_float(&xdrs,(f2d+n)); } else if (*nx == 1 && *ny > 1) for (n=0; n < *ny; n++) { (void) xdr_float(&xdrs,(f2d+n)); } else if (*ny == 1 && *nx > 1) for (n=0; n < *nx; n++) { (void) xdr_float(&xdrs,(f2d+n)); } /* Report to stdout and close file: */ files[ifile].nframes++; printf("WRXDR: Wrote frame %d to file %s.\n", files[ifile].nframes,cflnm); (void) fclose(file); xdr_destroy(&xdrs); }