-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_shadow_format.c
82 lines (73 loc) · 2.03 KB
/
example_shadow_format.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//
//
// Simple C program to write a SHADOW binary file.
//
// Notes:
// reclen is the size of bytes of the array to be written
// as in FORTRAN, it is written before and after each record.
//
// the file is filled with values that correspond to the column
// number starting from zero(to be changed by the user)
//
// Authors: M. Sanchez del Rio and D. Karkoulis
//
// Date: 2011, November 17
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
double *ray=NULL;
double ray_vec[18];
int i,j,ierr;
int npoint ;
int reclen;
int ncol=18 ;
int iflag=1 ;
npoint=11000;
printf(" Number of rays npoint: %d\n", npoint);
printf(" Number of cols ncol: %d\n", ncol);
printf(" iflag: %d\n", iflag);
// allocate ray
ray = ( double* ) realloc ( ray, 18*npoint*sizeof(double) );
memset(ray,0,sizeof(double)*18*npoint);
// fill array with column number
for(i=0;i<npoint;i++){
for(j=0;j<ncol;j++){
ray[i + npoint * j]=j;
}
}
// open file
FILE *fp = fopen("testio.00", "w");
// write header
reclen = 3*sizeof(int);
fwrite(&reclen, sizeof(int), 1, fp);
fwrite(&ncol, sizeof(int), 1, fp);
fwrite(&npoint, sizeof(int), 1, fp);
fwrite(&iflag, sizeof(int), 1, fp);
fwrite(&reclen, sizeof(int), 1, fp);
reclen = ncol * sizeof(double);
// you can close the file if you want (to be reopened in append
// mode).
//fclose(fp);
// loop over rays
for (i = 0; i < npoint; ++i) {
// reopen file if closed before
//fp=fopen("testio.00","a");
fwrite(&reclen, sizeof(int), 1, fp);
//cp from matrix to vector
for (j = 0; j < 18; j++) {
ray_vec[j] = ray[i + npoint * j];
}
fwrite(ray_vec, sizeof(double), 18, fp);
fwrite(&reclen, sizeof(int), 1, fp);
// close file (if wanted)
//fclose(fp);
}
// close and exit
fclose(fp);
free(ray);
printf(" File written to disk: testio.00 \n");
return EXIT_SUCCESS;
}