1use crate::{fmt_comment, fmt_doris, header::Header, prelude::FormattingError};
2
3use std::io::{BufWriter, Write};
4
5impl Header {
6 pub fn format<W: Write>(&self, w: &mut BufWriter<W>) -> Result<(), FormattingError> {
8 writeln!(
9 w,
10 "{}",
11 fmt_doris(
12 &format!(
13 "{:6}.{:02} O D",
14 self.version.major, self.version.minor
15 ),
16 "RINEX VERSION / TYPE"
17 )
18 )?;
19
20 writeln!(w, "{}", fmt_doris(&self.satellite, "SATELLITE NAME"))?;
21
22 if let Some(cospar) = &self.cospar {
23 writeln!(w, "{}", fmt_doris(&cospar.to_string(), "COSPAR"))?;
24 }
25
26 self.format_prog_runby(w)?;
27 self.format_observer_agency(w)?;
28
29 let mut string = format!("D {:10}", self.observables.len());
30
31 for observable in self.observables.iter() {
32 string.push_str(&format!(" {:x}", observable));
33 }
34
35 writeln!(w, "{}", fmt_doris(&string, "SYS / # / OBS TYPES"))?;
36
37 if let Some(epoch) = self.time_of_first_observation {
38 let (year, month, day, hours, mins, secs, nanos) = epoch.to_gregorian(epoch.time_scale);
39 writeln!(
40 w,
41 "{}",
42 fmt_doris(
43 &format!(
44 "{:6} {:5} {:5} {:5} {:5} {:4}.{} DOR",
45 year, month, day, hours, mins, secs, nanos
46 ),
47 "TIME OF FIRST OBS"
48 )
49 )?;
50 }
51
52 if let Some(epoch) = self.time_of_last_observation {
53 let (year, month, day, hours, mins, secs, nanos) = epoch.to_gregorian(epoch.time_scale);
54 writeln!(
55 w,
56 "{}",
57 fmt_doris(
58 &format!(
59 "{:6} {:5} {:5} {:5} {:5} {:4}.{} DOR",
60 year, month, day, hours, mins, secs, nanos
61 ),
62 "TIME OF LAST OBS"
63 )
64 )?;
65 }
66
67 writeln!(
68 w,
69 "{}",
70 fmt_doris(
71 &format!("{:6}", self.ground_stations.len()),
72 "# OF STATIONS"
73 )
74 )?;
75
76 for station in self.ground_stations.iter() {
77 writeln!(
78 w,
79 "{}",
80 fmt_doris(&format!("{:x}", station), "STATION REFERENCE")
81 )?;
82 }
83
84 writeln!(w, "{}", fmt_doris("", "END OF HEADER"))?;
85 Ok(())
86 }
87
88 fn format_prog_runby<W: Write>(&self, w: &mut BufWriter<W>) -> Result<(), FormattingError> {
90 let program = format!(
91 "doris-rs v{}",
92 Self::format_pkg_version(env!("CARGO_PKG_VERSION"))
93 );
94
95 let mut string = format!("{:<20}", program);
96
97 if let Some(runby) = &self.run_by {
98 let formatted = format!("{:<20}", runby);
99 string.push_str(&formatted);
100 } else {
101 string.push_str(" ");
102 };
103
104 if let Some(date) = &self.date {
105 string.push_str(date);
106 } else {
107 string.push_str(" ");
108 };
109
110 writeln!(w, "{}", fmt_doris(&string, "PGM / RUN BY / DATE"),)?;
112
113 Ok(())
114 }
115
116 fn format_observer_agency<W: Write>(
118 &self,
119 w: &mut BufWriter<W>,
120 ) -> Result<(), FormattingError> {
121 let mut string = if let Some(observer) = &self.observer {
122 format!("{:<20}", observer)
123 } else {
124 " ".to_string()
125 };
126
127 if let Some(agency) = &self.agency {
128 string.push_str(agency);
129 } else {
130 string.push_str(" ");
131 };
132
133 writeln!(w, "{}", fmt_doris(&string, "OBSERVER / AGENCY"),)?;
134
135 Ok(())
136 }
137
138 fn format_comments<W: Write>(&self, w: &mut BufWriter<W>) -> Result<(), FormattingError> {
140 for comment in self.comments.iter() {
141 writeln!(w, "{}", fmt_comment(comment))?;
142 }
143 Ok(())
144 }
145}