Skip to content

Commit

Permalink
feature 2216 ioda2nc station_id (#2259)
Browse files Browse the repository at this point in the history
Co-authored-by: Howard Soh <hsoh@seneca.rap.ucar.edu>
Co-authored-by: John Halley Gotway <johnhg@ucar.edu>
  • Loading branch information
3 people committed Sep 15, 2022
1 parent 3d8ce9f commit 1d95e91
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
30 changes: 23 additions & 7 deletions src/basic/vx_log/str_wrappers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ using namespace std;
#include "str_wrappers.h"


////////////////////////////////////////////////////////////////////////

bool is_whitespaces(char cur_char) {
return (' ' == cur_char || '\t' == cur_char || '\n' == cur_char || '\r' == cur_char);
}

////////////////////////////////////////////////////////////////////////

int m_strlen(const char *str) {
Expand Down Expand Up @@ -109,7 +115,22 @@ void m_strncpy(char *to_str, const char *from_str, const int buf_len,

////////////////////////////////////////////////////////////////////////

void m_rstrip(char *str_buf, int buf_len) {
bool m_replace_char(char *str_buf, char from_ch, char to_ch, bool all_instances) {
bool replaced = false;
int str_len = m_strlen(str_buf);
for(int idx=0; idx<str_len; idx++) {
if (from_ch == str_buf[idx]) {
replaced = true;
str_buf[idx] = to_ch;
if (!all_instances) break;
}
}
return replaced;
}

////////////////////////////////////////////////////////////////////////

void m_rstrip(char *str_buf, int buf_len, bool find_white_ch) {
// Make sure it's NULL terminated
if (buf_len >= 0) str_buf[buf_len] = '\0';
// Change the trailing blank space to a null
Expand All @@ -119,13 +140,8 @@ void m_rstrip(char *str_buf, int buf_len) {
str_buf[idx] = '\0';
if((idx > 0) && !is_whitespaces(str_buf[idx-1])) break;
}
else if (!find_white_ch) break;
}
}

////////////////////////////////////////////////////////////////////////

bool is_whitespaces(char cur_char) {
return (' ' == cur_char || '\t' == cur_char || '\n' == cur_char || '\r' == cur_char);
}

////////////////////////////////////////////////////////////////////////
5 changes: 4 additions & 1 deletion src/basic/vx_log/str_wrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ extern void m_strncpy(char *to_str, const char *from_str, const int buf_len,
const char *method_name, const char *extra_msg=(char *)0,
bool truncate=false);

extern void m_rstrip(char *str_buf, const int buf_len=-1);
extern void m_rstrip(char *str_buf, const int buf_len=-1, bool find_white_ch=true);

extern bool m_replace_char(char *str_buf, char from_ch, char to_ch,
bool all_instances=true);

extern bool is_whitespaces(char cur_char);

Expand Down
5 changes: 5 additions & 0 deletions src/basic/vx_util/observation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ Observation::Observation(const string &header_type, const string &station_id,
_height(height_m),
_value(value)
{

string::iterator it = _stationId.begin();
while (' ' == *it) it++; // skip leading spaces
std::replace(it, _stationId.end(), ' ', '_');

}


Expand Down
3 changes: 2 additions & 1 deletion src/tools/other/ioda2nc/ioda2nc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,8 @@ void process_ioda_file(int i_pb) {
if(has_station_id) {
char tmp_sid[nstring+1];
m_strncpy(tmp_sid, hdr_station_ids+(i_read*nstring), nstring, method_name_s, "tmp_sid");
m_rstrip(tmp_sid, nstring);
m_rstrip(tmp_sid, nstring, false);
m_replace_char(tmp_sid, ' ', '_');
hdr_sid = tmp_sid;
}
else hdr_sid.clear();
Expand Down

0 comments on commit 1d95e91

Please sign in to comment.