33#include < charconv>
44#include < cstdio>
55#include < fstream>
6- #include < limits >
6+ #include < stdexcept >
77
88namespace {
99
@@ -89,11 +89,15 @@ constexpr const int sdata_start = 27;
8989 * 012345678901234567890123456789012345678901234567890123456789
9090 * 10 20 30 40 50
9191 */
92- int resolve_freq_cor_data_line (const char *line, char cmp, double *data) noexcept {
92+ int resolve_freq_cor_data_line (const char *line, char cmp,
93+ double *data) noexcept {
9394 /* get component (one char) */
9495 cmp = line[24 ];
9596 if ((cmp != ' X' && cmp != ' Y' ) && (cmp != ' Z' )) {
96- fprintf (stderr, " [ERROR] Failed resolving dpod/freq line [%s], invalid XYZ field! (traceback: %s)\n " , line, __func__);
97+ fprintf (stderr,
98+ " [ERROR] Failed resolving dpod/freq line [%s], invalid XYZ field! "
99+ " (traceback: %s)\n " ,
100+ line, __func__);
97101 return 1 ;
98102 }
99103 const char *str = line + 27 ;
@@ -108,22 +112,25 @@ int resolve_freq_cor_data_line(const char *line, char cmp, double *data) noexcep
108112}
109113} /* anonymous namespace */
110114
111- int dso::apply_dpod_freq_corr (
112- const char *fn,
113- const dso::datetime<dso::nanoseconds> &t,
114- const std::vector<dso::Sinex::SiteCoordinateResults> &sites_crd) noexcept {
115+ std::vector<dso::Sinex::SiteCoordinateResults> dso::get_dpod_freq_corr (
116+ const char *fn, const dso::datetime<dso::nanoseconds> &t,
117+ const std::vector<dso::Sinex::SiteCoordinateResults> &sites_crd) {
115118 /* open dpod freq file */
116119 std::ifstream fin (fn);
117120 if (!fin.is_open ()) {
118121 fprintf (stderr,
119122 " [ERROR] Failed opening dpod freq_corr file %s (traceback: %s)\n " ,
120123 fn, __func__);
121- return 1 ;
124+ throw std::runtime_error ( " [ERROR] Failed opening dpod freq_corr file \n " ) ;
122125 }
123126
124127 /* copy of input coordinates */
125128 std::vector<dso::Sinex::SiteCoordinateResults> scpy (sites_crd);
126129
130+ /* zero-out (cartesian) positions */
131+ for (auto it = scpy.begin (); it != scpy.end (); ++it)
132+ it->x = it->y = it->z = 0e0 ;
133+
127134 /* fractional day of year and phase at epoch */
128135 const auto ymd_ = t.as_ydoy ();
129136 const int idoy_ = ymd_.dy ().as_underlying_type ();
@@ -134,41 +141,46 @@ int dso::apply_dpod_freq_corr(
134141 char line[SZ];
135142 double cfreq = 0 ;
136143 double data[4 ];
137- char ccmp;
144+ char ccmp = ' Q ' ;
138145 double omega = 0e0 ;
139- int nfreq= 0 ;
146+ int nfreq = 0 ;
140147
141148 int error = 0 ;
142149 while (fin.getline (line, SZ) && (!error)) {
143150 /* skip lines starting with '#', else parse */
144151 if (line[0 ] != ' #' ) {
145152 /* first check site name */
146153 auto it = std::find_if (
147- scpy.cbegin (), scpy.cend (), [&](const dso::Sinex::SiteCoordinateResults &site) {
154+ scpy.begin (), scpy.end (),
155+ [&](const dso::Sinex::SiteCoordinateResults &site) {
148156 return ((!std::strncmp (site.msite .site_code (), line + 1 , 4 ) &&
149- !std::strncmp (site.msite .point_code (), line + 6 , 2 )) &&
150- (!std::strncmp (site.msite .domes (), line + 9 , 9 ) &&
151- !std::strncmp (site.msolnid , line + 18 , 4 )));
157+ !std::strncmp (site.msite .point_code (), line + 6 , 2 )) &&
158+ (!std::strncmp (site.msite .domes (), line + 9 , 9 ) &&
159+ !std::strncmp (site.msolnid , line + 18 , 4 )));
152160 });
153161 /* the station is in the list */
154162 if (it != scpy.cend ()) {
155163
156164 error += resolve_freq_cor_data_line (line, ccmp, data);
157165 if (!error) {
158- const double valmm = data[0 ] * std::cos (omega) + data[2 ] * std::sin (omega);
166+ const double valmm =
167+ data[0 ] * std::cos (omega) + data[2 ] * std::sin (omega);
159168 switch (ccmp) {
160- case ' X' :
161- it->x += valmm*1e-3 ;
162- break ;
163- case ' Y' :
164- it->y += valmm*1e-3 ;
165- break ;
166- case ' Z' :
167- it->z += valmm*1e-3 ;
168- break ;
169- default :
170- fprintf (stderr, " [ERROR] Invalid component in dpod_freq file %s; expected one of X, Y or Z; line is %s (traceback: %s)\n " , fn, line);
171- error += 1 ;
169+ case ' X' :
170+ it->x += valmm * 1e-3 ;
171+ break ;
172+ case ' Y' :
173+ it->y += valmm * 1e-3 ;
174+ break ;
175+ case ' Z' :
176+ it->z += valmm * 1e-3 ;
177+ break ;
178+ default :
179+ fprintf (stderr,
180+ " [ERROR] Invalid component in dpod_freq file %s; expected "
181+ " one of X, Y or Z; line is %s (traceback: %s)\n " ,
182+ fn, line, __func__);
183+ error += 1 ;
172184 }
173185 } /* no coefficients parsing error */
174186 } /* if station in list */
@@ -181,5 +193,45 @@ int dso::apply_dpod_freq_corr(
181193 } /* reading through file entries */
182194
183195 /* we should have read at least one list of frequencies */
184- return !((error==0 ) && (nfreq>=1 ));
196+ if (!((error == 0 ) && (nfreq >= 1 ))) {
197+ fprintf (stderr,
198+ " [ERROR] Failed applying parsing/applying frequency corrections "
199+ " from dpod file %s (traceback: %s)\n " ,
200+ fn, __func__);
201+ throw std::runtime_error (
202+ " [ERROR] Failed applying parsing/applying frequency corrections\n " );
203+ }
204+ return scpy;
205+ }
206+
207+ int dso::apply_dpod_freq_corr (
208+ const char *fn, const dso::datetime<dso::nanoseconds> &t,
209+ std::vector<dso::Sinex::SiteCoordinateResults> &sites_crd) noexcept {
210+ int error = 0 ;
211+ try {
212+ const auto cor = dso::get_dpod_freq_corr (fn, t, sites_crd);
213+ int idx = 0 ;
214+ for (auto it = sites_crd.begin (); it != sites_crd.end () && (!error); ++it) {
215+ /* we assume here 1-to-1 correspondance between sites_crd and cor */
216+ auto itc = cor.cbegin () + idx;
217+ if ((std::strcmp (itc->msite .site_code (), it->msite .site_code ()) ||
218+ std::strcmp (itc->msite .point_code (), it->msite .point_code ())) ||
219+ (std::strcmp (itc->msite .domes (), it->msite .domes ()) ||
220+ std::strcmp (itc->msolnid , it->msolnid ))) {
221+ error += 1 ;
222+ fprintf (stderr, " [ERROR] Corrupt(?) parsing of dpod_freq_cor file; expected correction for %s and found %s (traceback: %s)\n " , itc->msite .site_code (), it->msite .site_code (), __func__);
223+ }
224+ it->x += itc->x ;
225+ it->y += itc->y ;
226+ it->z += itc->z ;
227+ }
228+ } catch (std::exception &e) {
229+ error = 1 ;
230+ fprintf (stderr,
231+ " [ERROR] Failed applying dpod frequency corrections from file %s "
232+ " (traceback: %s)\n " ,
233+ fn, __func__);
234+ fprintf (stderr, " [ERROR] Caught exception %s" , e.what ());
235+ }
236+ return error;
185237}
0 commit comments