-
Notifications
You must be signed in to change notification settings - Fork 17
/
readb_xdr.c
98 lines (71 loc) · 2.91 KB
/
readb_xdr.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* ------- file: -------------------------- readb_xdr.c -------------
Version: rh2.0
Author: Han Uitenbroek ([email protected])
Last modified: Thu Jan 19 16:51:09 2012 --
-------------------------- ----------RH-- */
/* --- Reads the atmospheric magnetic field.
XDR (external data representation) version. -- -------------- */
/* --- Read the absolute value of the magnetic field, and the angles
gamma (the angle of the field with the normal direction to the
atmosphere) and chi (the angle of the field with the positive
x-axis, measured counter-clockwise).
Note: Magnetic field strength should be given in Tesla (1 T = 10^4 Gauss)
Angles should be given in radians.
-- -------------- */
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "rh.h"
#include "atom.h"
#include "atmos.h"
#include "error.h"
#include "inputs.h"
#include "xdr.h"
/* --- Function prototypes -- -------------- */
/* --- Global variables -- -------------- */
extern InputData input;
extern char messageStr[];
/* ------- begin -------------------------- readB.c ----------------- */
bool_t readB(Atmosphere *atmos)
{
const char routineName[] = "readB";
bool_t result = TRUE;
size_t recordsize;
off_t offset;
FILE *fp_stokes;
XDR xdrs;
if (!strcmp(input.Stokes_input, "none")) return FALSE;
if ((fp_stokes = fopen(input.Stokes_input, "r")) == NULL) {
sprintf(messageStr, "Unable to open inputfile %s", input.Stokes_input);
Error(ERROR_LEVEL_2, routineName, messageStr);
}
atmos->B = (double *) malloc(atmos->Nspace * sizeof(double));
atmos->gamma_B = (double *) malloc(atmos->Nspace * sizeof(double));
atmos->chi_B = (double *) malloc(atmos->Nspace * sizeof(double));
if (input.xdr_endian) {
xdrstdio_create(&xdrs, fp_stokes, XDR_DECODE);
result &= xdr_vector(&xdrs, (char *) atmos->B, atmos->Nspace,
sizeof(double), (xdrproc_t) xdr_double);
result &= xdr_vector(&xdrs, (char *) atmos->gamma_B, atmos->Nspace,
sizeof(double), (xdrproc_t) xdr_double);
result &= xdr_vector(&xdrs, (char *) atmos->chi_B, atmos->Nspace,
sizeof(double), (xdrproc_t) xdr_double);
xdr_destroy(&xdrs);
} else {
recordsize = atmos->Nspace;
result &= (fread(atmos->B, sizeof(double), recordsize,
fp_stokes) == recordsize);
result &= (fread(atmos->gamma_B, sizeof(double), recordsize,
fp_stokes) == recordsize);
result &= (fread(atmos->chi_B, sizeof(double), recordsize,
fp_stokes) == recordsize);
}
if (!result) {
sprintf(messageStr, "Unable to read from input file %s",
input.Stokes_input);
Error(ERROR_LEVEL_1, routineName, messageStr);
}
fclose(fp_stokes);
return result;
}
/* ------- end ---------------------------- readB.c ----------------- */