Debian Codegraph @

src/SPERR/utilities/raw_tools/float2nc.c

raw
1 /*
2 This program reads in a binary raw file, and output it in NetCDF format.
3 The NetCDF file could be visualized in VisIt.
4 */
5 #include <netcdf.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8
9 /* We are writing 2D data, an NX * NY grid. */
10 #define NDIMS 2
11 #define NX 128
12 #define NY 128
13
14 /* Handle errors by printing an error message and exiting with a non-zero
15 * status. */
16 #define ERRCODE 2
17 #define ERR(e) \
18 { \
19 free(data_buf); \
20 fprintf(stderr, "Error: %s\n", nc_strerror(e)); \
21 exit(ERRCODE); \
22 }
23
24 int main(int argc, char** argv)
25 {
26 if (argc != 3) {
27 fprintf(stderr, "Usage: ./a.out input_filename output_filename.");
28 return 1;
29 }
30
31 const long num_of_bytes = sizeof(float) * NX * NY;
32
33 /* Check if the file to read is good */
34 FILE* f = fopen(argv[1], "r");
35 if (f == NULL) {
36 fprintf(stderr, "Error! Cannot open input file: %s\n", argv[1]);
37 return 1;
38 }
39 fseek(f, 0, SEEK_END);
40 if (ftell(f) < num_of_bytes) {
41 fprintf(stderr, "Error! Input file size error: %s\n", argv[1]);
42 fprintf(stderr, " Expecting %ld bytes, got %ld bytes.\n", num_of_bytes, ftell(f));
43 fclose(f);
44 return 1;
45 }
46 fseek(f, 0, SEEK_SET);
47
48 /* Allocate space for the input data */
49 float* data_buf = (float*)malloc(num_of_bytes);
50 if (fread(data_buf, sizeof(float), NX * NY, f) != NX * NY) {
51 fprintf(stderr, "Error! Input file read error: %s\n", argv[1]);
52 free(data_buf);
53 fclose(f);
54 return 1;
55 }
56 fclose(f);
57
58 /* When we create netCDF variables and dimensions, we get back an ID for each
59 * one. */
60 int ncid, x_dimid, y_dimid, varid, retval;
61 int dimids[NDIMS];
62
63 /* Always check the return code of every netCDF function call. In
64 * this example program, any retval which is not equal to NC_NOERR
65 * (0) will cause the program to print an error message and exit
66 * with a non-zero return code. */
67
68 /* Create the file. The NC_CLOBBER parameter tells netCDF to
69 * overwrite this file, if it already exists.*/
70 if ((retval = nc_create(argv[2], NC_CLOBBER, &ncid)))
71 ERR(retval);
72
73 /* Define the dimensions. NetCDF will hand back an ID for each. */
74 if ((retval = nc_def_dim(ncid, "x", NX, &x_dimid)))
75 ERR(retval);
76 if ((retval = nc_def_dim(ncid, "y", NY, &y_dimid)))
77 ERR(retval);
78
79 /* The dimids array is used to pass the IDs of the dimensions of the variable.
80 */
81 dimids[0] = x_dimid;
82 dimids[1] = y_dimid;
83
84 /* Define the variable. The type of the variable in this case is NC_FLOAT
85 * (4-byte float). */
86 if ((retval = nc_def_var(ncid, "data", NC_FLOAT, NDIMS, dimids, &varid)))
87 ERR(retval);
88
89 /* End define mode. This tells netCDF we are done defining metadata. */
90 if ((retval = nc_enddef(ncid)))
91 ERR(retval);
92
93 /* Write the data to the file. In this case we write all the data in one
94 * operation. */
95 if ((retval = nc_put_var_float(ncid, varid, data_buf)))
96 ERR(retval);
97
98 /* Close the file. This frees up any internal netCDF resources
99 * associated with the file, and flushes any buffers. */
100 if ((retval = nc_close(ncid)))
101 ERR(retval);
102
103 free(data_buf);
104
105 printf("*** SUCCESS writing NetCDF file %s! ***\n", argv[2]);
106 return 0;
107 }