/* ------------------------------------------------------------------------- These functions write out gms compatible BINARY dataset files. The main function shows an example of how to use these subroutines. For questions on format, see the gms file formats document, OR, contact tech support at tech2@ems-i.com ------------------------------------------------------------------------- */ #include #include #include #include #define gms_OBJECTCARD 100 #define gms_SIZEFLOATCARD 110 #define gms_SIZEFLAGCARD 120 #define gms_BEGINSCALARCARD 130 #define gms_BEGINVECTORCARD 140 #define gms_NUMDATACARD 170 #define gms_NUMCELLSCARD 180 #define gms_NAMECARD 190 #define gms_VECTORTYPECARD 150 #define gms_TIMESTEPCARD 200 #define gms_ENDCARD 210 unsigned char InitializeGMSDatasetFile (FILE *fp, char *name, int numdata, int numcells, unsigned char scalar); unsigned char WriteGMSScalarTimeStep (FILE *fp, int numdata, int numcells, float timestep, unsigned char *statflags, float *data); unsigned char WriteGMSVectorTimeStep (FILE *fp, int numdata, int numcells, float timestep, unsigned char *statflags, float *x, float *y, float *z); unsigned char EndGMSDatasetFile (FILE *fp); /* ------------------------------------------------------------------------- TEST MAIN FUNCTION to read test.dat into gms, you must create a 3dgrid with 30 cells ------------------------------------------------------------------------- */ void main (void) /* test functions */ { int i, numdata=30, numcells=30; unsigned char statusflags[30]; float data[30], x[30], y[30], z[30]; char name[40]; FILE *fp; fp = fopen("test.dat", "wb+"); strcpy(name, "functiontest"); for (i=0; i 1 success 0 failure AUTHOR: jig 6/2/1999 ----------------------------------------------------------------------------- */ unsigned char InitializeGMSDatasetFile (FILE *fp, char *name, int numdata, int numcells, unsigned char scalar) { unsigned int objectcard = gms_OBJECTCARD, sizefloatcard = gms_SIZEFLOATCARD, sizeflagcard = gms_SIZEFLAGCARD, beginscalarcard = gms_BEGINSCALARCARD, beginvectorcard = gms_BEGINVECTORCARD, numdatacard = gms_NUMDATACARD, numcellscard = gms_NUMCELLSCARD, namecard = gms_NAMECARD, vectortypecard = gms_VECTORTYPECARD; unsigned int sizefloat, sizeflag, objecttype, version, sizeuint, vectortype, uinumdata, uinumcells; if (!fp) return 0; sizefloat = sizeof(float); sizeflag = sizeof(unsigned char); sizeuint = sizeof(unsigned int); objecttype = 7; /* 3D grid */ version = 3000; /* GMS binary data set file format version */ vectortype = 1; /* Vectors will be applied to cells (not gridnodes) */ uinumdata = (unsigned int)numdata; uinumcells = (unsigned int)numcells; if (fwrite(&version, sizeuint, 1, fp) != 1) return 0; if (fwrite(&objectcard, sizeuint, 1, fp) != 1) return 0; if (fwrite(&objecttype, sizeuint, 1, fp) != 1) return 0; if (fwrite(&sizefloatcard, sizeuint, 1, fp) != 1) return 0; if (fwrite(&sizefloat, sizeuint, 1, fp) != 1) return 0; if (fwrite(&sizeflagcard, sizeuint, 1, fp) != 1) return 0; if (fwrite(&sizeflag, sizeuint, 1, fp) != 1) return 0; if (scalar) { if (fwrite(&beginscalarcard, sizeuint, 1, fp) != 1) return 0; } else { if (fwrite(&beginvectorcard, sizeuint, 1, fp) != 1) return 0; if (fwrite(&vectortypecard, sizeuint, 1, fp) != 1) return 0; if (fwrite(&vectortype, sizeuint, 1, fp) != 1) return 0; } if (fwrite(&numdatacard, sizeuint, 1, fp) != 1) return 0; if (fwrite(&uinumdata, sizeuint, 1, fp) != 1) return 0; if (fwrite(&numcellscard, sizeuint, 1, fp) != 1) return 0; /* for cell-centered grids numdata = numcells */ if (fwrite(&uinumcells, sizeuint, 1, fp) != 1) return 0; if (fwrite(&namecard, sizeuint, 1, fp) != 1) return 0; if (strlen(name)>39) name[39] = '\0'; if (fwrite(name, sizeof(char), 40, fp) != 40) return 0; return 1; } /* end of InitializeGMSDatasetFile*/ /* ----------------------------------------------------------------------------- NAME: WriteGMSScalarTimeStep PURPOSE: Writes a scalar time step to a GMS binary data set file PRE: File is valid and has been initialized int numdata is the number of values in the data set int numcells is num cells for activation float timestep is the time corresponding to the time step unsigned char *statflags is an array of stat flags. If no status flags are to be included then this is NULL. float *data is an array of scalar values. POST: One more time step has been written and file is ready for another scalar time step or time step end flag RETURN: unsigned char -> 1 success 0 failure AUTHOR: jig 6/2/1999 ----------------------------------------------------------------------------- */ unsigned char WriteGMSScalarTimeStep (FILE *fp, int numdata, int numcells, float timestep, unsigned char *statflags, float *data) { unsigned int timestepcard = gms_TIMESTEPCARD; unsigned int sizefloat, sizeflag; unsigned char activeflag; if (!fp || !data) return 0; sizefloat = sizeof(float); sizeflag = sizeof(unsigned char); if (fwrite(×tepcard, sizeof(unsigned int), 1, fp) != 1) return 0; if (statflags) activeflag = 1; else activeflag = 0; if (fwrite(&activeflag, sizeflag, 1, fp) != 1) return 0; if (fwrite(×tep, sizefloat, 1, fp) != 1) return 0; if (statflags) { if (fwrite(statflags, sizeflag, numcells, fp) != (unsigned)numcells) return 0; } if (fwrite(data, sizefloat, numdata, fp) != (unsigned)numdata) return 0; return 1; } /* end of WriteGMSScalarTimeStep*/ /* ----------------------------------------------------------------------------- NAME: WriteGMSVectorTimeStep PURPOSE: Writes a vector time step to a GMS binary data set file PRE: File is valid and has been initialized for vector data int numdata is the number of values in the data set int numcells is num cells for activation float timestep is the time corresponding to the time step unsigned char *statflags is an array of stat flags. If no status flags are to be included then this is NULL. float *x,*y,*z are arrays of vector values. POST: One more time step has been written and file is ready for another vector time step or time step end flag RETURN: unsigned char -> 1 success 0 failure AUTHOR: jig 6/2/1999 ----------------------------------------------------------------------------- */ unsigned char WriteGMSVectorTimeStep (FILE *fp, int numdata, int numcells, float timestep, unsigned char *statflags, float *x, float *y, float *z) { unsigned int timestepcard = gms_TIMESTEPCARD; unsigned int sizefloat, sizeflag; unsigned char activeflag; int i; if (!fp || !x || !y || !z) return 0; sizefloat = sizeof(float); sizeflag = sizeof(unsigned char); if (fwrite(×tepcard, sizeof(unsigned int), 1, fp) != 1) return 0; if (statflags) activeflag = 1; else activeflag = 0; if (fwrite(&activeflag, sizeflag, 1, fp) != 1) return 0; if (fwrite(×tep, sizefloat, 1, fp) != 1) return 0; if (statflags) { if (fwrite(statflags, sizeflag, numcells, fp) != (unsigned)numcells) return 0; } for (i=0; i 1 success 0 failure AUTHOR: jig 6/2/1999 ----------------------------------------------------------------------------- */ unsigned char EndGMSDatasetFile (FILE *fp) { unsigned int endcard = gms_ENDCARD; /* signifies end of a dataset */ if (!fp) return 0; if (fwrite(&endcard, sizeof(unsigned int), 1, fp) != 1) return 0; return 1; } /* end of EndGMSDatasetFile*/