/* Define the data structure for the file header information */
typedef struct
{
int Identifier; // Will be set to 8675309 or 1010 for
// quick check of proper format
int NbrRays; // The number of rays in the file
char Description[100]; // A text description of the source
float SourceFlux; // The total flux in watts of this
// source
float RaySetFlux; // The flux in watts represented by
// this Ray Set
float Wavelength; // The wavelength in µm, 0 if
// a composite
float AzimuthBeg, AzimuthEnd; // Angular range for ray set
// (Degrees)
float PolarBeg, PolarEnd; // Angular range for ray set
// (Degrees)
long DimensionUnits; // METERS=0, INCHES=1, CM=2, FEET=3,
// MM=4
float LocX, LocY,LocZ; // Coordinate Translation of the source
float RotX,RotY,RotZ; // Source rotation (Radians)
float ScaleX, ScaleY, ScaleZ; // Scale factor to expand/
// contract source
float unused1, unused2, unused3, unused4;
int reserved1, reserved2, reserved3, reserved4;
} NSC_RAY_DATA_HEADER;
/* Define the data structure for the ray information */
typedef struct
{
float x, y, z;
float l, m, n;
float intensity;
} FLUX_ONLY;
typedef struct
{
float x, y, z;
float l, m, n;
float flux, wavelength;
} SPECTRAL_COLOR;
int main(void)
{
char filein[MAX_PATH_LENGTH];
char fileout[MAX_PATH_LENGTH];
char disp[MAX_PATH_LENGTH];
int i, file_ident, file_rays, file_format, file_type;
long file_dim;
float xr, yr, zr, lr, mr, nr, intr, wavr;
FILE *in, *out;
NSC_RAY_DATA_HEADER nscrdh;
FLUX_ONLY nscrdf;
SPECTRAL_COLOR nscrdc;
/* Determine the file names to be read from and written to */
printf("Enter the name of the binary file to be converted (include full path): ");
gets(filein);
printf("Enter the name for the ASCII text file (include full path): ");
gets(fileout);
/* Read necessary header data into local variables &
confirm file format */
fread(&nscrdh,1,sizeof(NSC_RAY_DATA_HEADER),in);
file_ident = nscrdh.Identifier;
file_rays = nscrdh.NbrRays;
file_dim = nscrdh.DimensionUnits;
file_format = nscrdh.ray_format_type;
file_type = nscrdh.flux_type;
if ((file_ident == 1010) || (file_ident == 8675309))
{
sprintf(disp, "Valid file identifier \n");
fputs(disp, out);
}
else
{
sprintf(disp, "Incorrect file identifier");
fputs(disp, out);
goto fast_exit;
}
if ((file_format == 0) || (file_format == 2))
{
if (file_format == 0)
{
if ((file_type == 0) || (file_type == 1))
{
goto data_write;
}
else
{
sprintf(disp, "Incorrect flux type identifier");
fputs(disp, out);
goto fast_exit;
}
}
else
{
if (file_type != 0)
{
sprintf(disp, "Incorrect flux type identifier");
fputs(disp, out);
goto, fast_exit;
}
}
}
else
{
sprintf(disp, "Incorrect file format identifier");
fputs(disp, out);
fputs("\n", out);
sprintf(disp, "File format identifier = %i", file_format);
fputs(disp, out);
goto fast_exit;
}
/* Write header, ray information into output file */
sprintf(disp, "%i %i \n", file_rays, file_dim, file_format, file_type);
fputs(disp, out);
if (file_format == 0)
{
for (i=0; i <= file_rays - 1; i++)
{
fread(&nscrdf,1,sizeof(FLUX_ONLY),in);
xr = nscrdf.x;
yr = nscrdf.y;
zr = nscrdf.z;
lr = nscrdf.l;
mr = nscrdf.m;
nr = nscrdf.n;
intr = nscrdf.flux;
sprintf(disp, "%f %f %f %f %f %f %f \n",
xr, yr, zr, lr, mr, nr, intr);
fputs(disp, out);
}
}
else
{
for (i=0; i <= file_rays - 1; i++)
{
fread(&nscrdc,1,sizeof(SPECTRAL_COLOR),in);
xr = nscrdc.x;
yr = nscrdc.y;
zr = nscrdc.z;
lr = nscrdc.l;
mr = nscrdc.m;
nr = nscrdc.n;
intr = nscrdc.flux;
wavr = nscrdc.wavelength;
sprintf(disp, "%f %f %f %f %f %f %f %f \n",
xr, yr, zr, lr, mr, nr, intr, wavr);
fputs(disp, out);
}
}