-
Notifications
You must be signed in to change notification settings - Fork 17
/
brs_xdr.c
174 lines (128 loc) · 4.92 KB
/
brs_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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* ------- file: -------------------------- brs_xdr.c ---------------
Version: rh2.0
Author: Han Uitenbroek (huitenbroek@nso.edu)
Last modified: Thu Jan 3 14:35:46 2008 --
-------------------------- ----------RH-- */
/* --- Routines to write and read background record structure to file.
XDR (external data representation) version. -- -------------- */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "rh.h"
#include "atom.h"
#include "atmos.h"
#include "spectrum.h"
#include "error.h"
#include "xdr.h"
#define BRS_DOT_OUT "brs.out"
/* --- Function prototypes -- -------------- */
/* --- Global variables -- -------------- */
extern Atmosphere atmos;
extern Spectrum spectrum;
extern char messageStr[];
/* ------- begin -------------------------- writeBRS.c -------------- */
void writeBRS(void)
{
const char routineName[] = "writeBRS";
FILE *fp_out;
XDR xdrs;
if ((fp_out = fopen(BRS_DOT_OUT, "w")) == NULL) {
sprintf(messageStr, "Unable to open output file %s", BRS_DOT_OUT);
Error(ERROR_LEVEL_1, routineName, messageStr);
return;
}
xdrstdio_create(&xdrs, fp_out, XDR_ENCODE);
if (!xdr_BRS(&xdrs)) {
sprintf(messageStr, "Unable to write to output file %s", BRS_DOT_OUT);
Error(ERROR_LEVEL_1, routineName, messageStr);
}
xdr_destroy(&xdrs);
fclose(fp_out);
}
/* ------- end ---------------------------- writeBRS.c -------------- */
/* ------- begin -------------------------- readBRS.c --------------- */
void readBRS(void)
{
const char routineName[] = "readBRS";
FILE *fp_in;
XDR xdrs;
if ((fp_in = fopen(BRS_DOT_OUT, "r")) == NULL) {
sprintf(messageStr, "Unable to open input file %s", BRS_DOT_OUT);
Error(ERROR_LEVEL_2, routineName, messageStr);
}
xdrstdio_create(&xdrs, fp_in, XDR_DECODE);
if (!xdr_BRS(&xdrs)) {
sprintf(messageStr, "Unable to read from input file %s", BRS_DOT_OUT);
Error(ERROR_LEVEL_2, routineName, messageStr);
}
xdr_destroy(&xdrs);
fclose(fp_in);
}
/* ------- end ---------------------------- readBRS.c --------------- */
/* ------- begin -------------------------- xdr_BRS.c --------------- */
bool_t xdr_BRS(XDR *xdrs)
{
const char routineName[] = "xdr_BRS";
register int nspect;
char *atmosID;
bool_t result = TRUE, *hasline, *ispolarized;
int Nsp;
long Nspace, Nrecno;
hasline =
(bool_t *) malloc(spectrum.Nspect * sizeof(bool_t));
ispolarized =
(bool_t *) malloc(spectrum.Nspect * sizeof(bool_t));
if (atmos.moving || atmos.Stokes)
Nrecno = 2 * spectrum.Nspect * atmos.Nrays;
else
Nrecno = spectrum.Nspect;
if (xdrs->x_op == XDR_ENCODE) {
atmosID = atmos.ID;
result &= xdr_counted_string(xdrs, &atmosID);
result &= xdr_long(xdrs, &atmos.Nspace);
result &= xdr_int(xdrs, &spectrum.Nspect);
/* --- Flags for presence of background line -- -------------- */
for (nspect = 0; nspect < spectrum.Nspect; nspect++)
hasline[nspect] = atmos.backgrflags[nspect].hasline;
result &= xdr_vector(xdrs, (char *) hasline, spectrum.Nspect,
sizeof(bool_t), (xdrproc_t) xdr_bool);
/* --- Flags for presence of polarized background -- ------------ */
for (nspect = 0; nspect < spectrum.Nspect; nspect++)
ispolarized[nspect] = atmos.backgrflags[nspect].ispolarized;
result &= xdr_vector(xdrs, (char *) ispolarized, spectrum.Nspect,
sizeof(bool_t), (xdrproc_t) xdr_bool);
} else {
atmos.backgrflags = (flags *) malloc(spectrum.Nspect * sizeof(flags));
atmos.backgrrecno = (long *) malloc(Nrecno * sizeof(long));
result &= xdr_counted_string(xdrs, &atmosID);
if (!strstr(atmosID, atmos.ID)) {
sprintf(messageStr,
"Input was derived from different atmosphere (%s) than current",
atmosID);
Error(WARNING, routineName, messageStr);
}
free(atmosID);
result &= xdr_long(xdrs, &Nspace);
result &= xdr_int(xdrs, &Nsp);
if (Nspace != atmos.Nspace || Nsp != spectrum.Nspect) {
free(hasline);
return FALSE;
}
/* --- Flags for presence of background line -- -------------- */
result &= xdr_vector(xdrs, (char *) hasline, spectrum.Nspect,
sizeof(bool_t), (xdrproc_t) xdr_bool);
for (nspect = 0; nspect < spectrum.Nspect; nspect++)
atmos.backgrflags[nspect].hasline = hasline[nspect];
/* --- Flags for presence of polarized background -- ------------ */
result &= xdr_vector(xdrs, (char *) ispolarized, spectrum.Nspect,
sizeof(bool_t), (xdrproc_t) xdr_bool);
for (nspect = 0; nspect < spectrum.Nspect; nspect++)
atmos.backgrflags[nspect].ispolarized = ispolarized[nspect];
}
result &= xdr_vector(xdrs, (char *) atmos.backgrrecno, Nrecno,
sizeof(int), (xdrproc_t) xdr_int);
free(hasline);
free(ispolarized);
return result;
}
/* ------- end ---------------------------- xdr_BRS.c --------------- */