Skip to content

Commit

Permalink
#2601 Support the climo filename from the configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Howard Soh committed Jun 26, 2024
1 parent 5556c79 commit b263eac
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 170 deletions.
176 changes: 72 additions & 104 deletions src/libcode/vx_seeps/seeps.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,9 @@ using namespace netCDF;

bool standalone_debug_seeps = false;

static SeepsClimo *seeps_climo = 0;
static SeepsClimo *seeps_climo = nullptr;
static std::map<int,SeepsClimoGrid *> seeps_climo_grid_map_00;

static const char *def_seeps_filename =
"MET_BASE/climo/seeps/PPT24_seepsweights.nc";
static const char *def_seeps_grid_filename =
"MET_BASE/climo/seeps/PPT24_seepsweights_grid.nc";

static const char *var_name_sid = "sid";
static const char *var_name_lat = "lat";
static const char *var_name_lon = "lon";
Expand All @@ -55,20 +50,20 @@ double weighted_average(double, double, double, double);

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

SeepsClimo *get_seeps_climo() {
if (! seeps_climo) seeps_climo = new SeepsClimo();
SeepsClimo *get_seeps_climo(ConcatString seeps_point_climo_name) {
if (! seeps_climo) seeps_climo = new SeepsClimo(seeps_point_climo_name);
return seeps_climo;
}

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

void release_seeps_climo() {
if (seeps_climo) { delete seeps_climo; seeps_climo = 0; }
if (seeps_climo) { delete seeps_climo; seeps_climo = nullptr; }
}

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

SeepsClimoGrid *get_seeps_climo_grid(int month, int hour) {
SeepsClimoGrid *get_seeps_climo_grid(int month, ConcatString seeps_grid_climo_name, int hour) {
bool not_found = true;
SeepsClimoGrid *seeps_climo_grid = nullptr;
for (map<int,SeepsClimoGrid *>::iterator it=seeps_climo_grid_map_00.begin();
Expand All @@ -81,7 +76,7 @@ SeepsClimoGrid *get_seeps_climo_grid(int month, int hour) {
}

if (not_found) {
seeps_climo_grid = new SeepsClimoGrid(month, hour);
seeps_climo_grid = new SeepsClimoGrid(month, hour, seeps_grid_climo_name);
seeps_climo_grid_map_00[month] = seeps_climo_grid;
}
return seeps_climo_grid;
Expand Down Expand Up @@ -174,9 +169,11 @@ SeepsAggScore & SeepsAggScore::operator+=(const SeepsAggScore &c) {

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

SeepsClimoBase::SeepsClimoBase(ConcatString seeps_climo_name) : climo_file_name{seeps_climo_name} {

SeepsClimoBase::SeepsClimoBase() {
clear();
seeps_ready = false;

}

////////////////////////////////////////////////////////////////////////
Expand All @@ -188,13 +185,56 @@ SeepsClimoBase::~SeepsClimoBase() {
////////////////////////////////////////////////////////////////////////

void SeepsClimoBase::clear() {
seeps_ready = false;
filtered_count = 0;
seeps_p1_thresh.clear();
}

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

ConcatString SeepsClimoBase::get_climo_filename() {
ConcatString log_seeps_filename;
ConcatString seeps_filename;
const char *method_name = "SeepsClimoBase::get_climo_filename() -> ";

// Use the environment variable, if set.
ConcatString env_climo_name = get_env_climo_name();
bool use_env = get_env(env_climo_name.c_str(), seeps_filename);
if(!use_env) {
seeps_filename = climo_file_name.nonempty() ? climo_file_name : get_def_climo_name();
}
seeps_filename = replace_path(seeps_filename);

seeps_ready = file_exists(seeps_filename.c_str());
if (seeps_ready) {
mlog << Debug(7) << method_name << "SEEPS climo name=\""
<< seeps_filename.c_str() << "\"\n";
}
else {
ConcatString message = "";
ConcatString message2 = "";
if (use_env) {
message.add("from the environment variable ");
message.add(env_climo_name);
message2.add("Correct the environment variable");
}
else {
message.add(climo_file_name.nonempty()
? "from the configuration" : "from the default");
message2.add("Correct the configuration");
}
mlog << Warning << "\n" << method_name
<< "The SEEPS climo name \"" << seeps_filename.c_str()
<< "\" " << message << " does not exist!\n"
<< message2 << " to set its location "
<< "or disable output for SEEPS and SEEPS_MPR.\n\n";
exit(1);
}

return seeps_filename;
}

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

void SeepsClimoBase::set_p1_thresh(const SingleThresh &p1_thresh) {
seeps_p1_thresh = p1_thresh;
}
Expand All @@ -203,19 +243,12 @@ void SeepsClimoBase::set_p1_thresh(const SingleThresh &p1_thresh) {
////////////////////////////////////////////////////////////////////////


SeepsClimo::SeepsClimo() {
SeepsClimo::SeepsClimo(ConcatString seeps_climo_name) : SeepsClimoBase{seeps_climo_name} {

clear();
ConcatString seeps_name = get_climo_filename();
if (file_exists(seeps_name.c_str())) read_seeps_scores(seeps_name);

ConcatString seeps_name = get_seeps_climo_filename();
seeps_ready = file_exists(seeps_name.c_str());
if (seeps_ready) read_seeps_scores(seeps_name);
else {
mlog << Error << "\nSeepsClimo::SeepsClimo() -> "
<< "The SEEPS point climo data \"" << seeps_name << "\" is missing!\n"
<< "Set the " << MET_ENV_SEEPS_POINT_CLIMO_NAME
<< " environment variable to define its location "
<< "or disable output for SEEPS and SEEPS_MPR.\n\n";
exit(1);
}
}

////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -287,7 +320,7 @@ SeepsRecord *SeepsClimo::get_record(int sid, int month, int hour) {
SeepsRecord *record = nullptr;
const char *method_name = "SeepsClimo::get_record() -> ";

if (seeps_ready) {
if (is_seeps_ready()) {
SeepsClimoRecord *climo_record = nullptr;
map<int,SeepsClimoRecord *>::iterator it;
if (hour < 6 || hour >= 18) {
Expand All @@ -300,7 +333,7 @@ SeepsRecord *SeepsClimo::get_record(int sid, int month, int hour) {
}
if (nullptr != climo_record) {
double p1 = climo_record->p1[month-1];
if (seeps_p1_thresh.check(p1)) {
if (check_seeps_p1_thresh(p1)) {
record = new SeepsRecord;
record->sid = climo_record->sid;
record->lat = climo_record->lat;
Expand All @@ -316,7 +349,7 @@ SeepsRecord *SeepsClimo::get_record(int sid, int month, int hour) {
}
}
else if (!is_eq(p1, bad_data_double)) {
filtered_count++;
increase_filtered_count();
mlog << Debug(7) << method_name << " filtered by threshold p1="
<< climo_record->p1[month-1] <<"\n";
}
Expand All @@ -336,35 +369,6 @@ SeepsRecord *SeepsClimo::get_record(int sid, int month, int hour) {

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

ConcatString SeepsClimo::get_seeps_climo_filename() {
ConcatString seeps_filename;
const char *method_name = "SeepsClimo::get_seeps_climo_filename() -> ";

// Use the environment variable, if set.
bool use_env = get_env(MET_ENV_SEEPS_POINT_CLIMO_NAME, seeps_filename);
if(use_env) seeps_filename = replace_path(seeps_filename);
else seeps_filename = replace_path(def_seeps_filename);

if (seeps_ready = file_exists(seeps_filename.c_str())) {
mlog << Debug(7) << method_name << "SEEPS point climo name=\""
<< seeps_filename.c_str() << "\"\n";
}
else {
ConcatString message = "";
if (use_env) {
message.add("from the env. name ");
message.add(MET_ENV_SEEPS_POINT_CLIMO_NAME);
}
mlog << Warning << "\n" << method_name
<< "The SEEPS point climo name \"" << seeps_filename.c_str()
<< "\"" << message << " does not exist!\n\n";
}

return seeps_filename;
}

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

double SeepsClimo::get_score(int sid, double p_fcst, double p_obs,
int month, int hour) {
double score = bad_data_double;
Expand Down Expand Up @@ -484,6 +488,8 @@ void SeepsClimo::read_seeps_scores(ConcatString filename) {
double matrix_12_buf[SEEPS_MONTH*SEEPS_MATRIX_SIZE];
NcFile *nc_file = open_ncfile(filename.c_str());

clear();

// dimensions: month = 12 ; nstn = 5293 ; nmatrix = 9 ;
get_dim(nc_file, dim_name_nstn, nstn, true);
mlog << Debug(6) << method_name << "dimensions nstn = " << nstn << "\n";
Expand Down Expand Up @@ -642,7 +648,7 @@ void SeepsClimo::read_seeps_scores(ConcatString filename) {
}
catch(int i_err) {

seeps_ready = false;
set_seeps_ready(false);
mlog << Error << "\n" << method_name
<< "encountered an error on reading " << filename << ". ecception_code=" << i_err << "\n\n";

Expand All @@ -656,23 +662,16 @@ void SeepsClimo::read_seeps_scores(ConcatString filename) {
////////////////////////////////////////////////////////////////////////


SeepsClimoGrid::SeepsClimoGrid(int month, int hour) : month{month}, hour{hour}
SeepsClimoGrid::SeepsClimoGrid(int month, int hour, ConcatString seeps_climo_name)
: month{month}, hour{hour}, SeepsClimoBase{seeps_climo_name}
{

clear();
p1_buf = p2_buf = t1_buf = t2_buf = nullptr;
s12_buf = s13_buf = s21_buf = s23_buf = s31_buf = s32_buf = nullptr;

ConcatString seeps_name = get_seeps_climo_filename();
seeps_ready = file_exists(seeps_name.c_str());
if (seeps_ready) read_seeps_scores(seeps_name);
else {
mlog << Error << "\nSeepsClimoGrid::SeepsClimoGrid -> "
<< "The SEEPS grid climo data \"" << seeps_name << "\" is missing!\n"
<< "Set the " << MET_ENV_SEEPS_GRID_CLIMO_NAME
<< " environment variable to define its location "
<< "or disable output for SEEPS.\n\n";
exit(1);
}
ConcatString seeps_name = get_climo_filename();
if (file_exists(seeps_name.c_str())) read_seeps_scores(seeps_name);

}

Expand Down Expand Up @@ -708,7 +707,7 @@ SeepsScore *SeepsClimoGrid::get_record(int ix, int iy,
int offset = iy * nx + ix;
double p1 = p1_buf[offset];

if (seeps_p1_thresh.check(p1)) {
if (check_seeps_p1_thresh(p1)) {
// Determine location in contingency table
int ic = (p_obs>t1_buf[offset])+(p_obs>t2_buf[offset]);
int jc = (p_fcst>t1_buf[offset])+(p_fcst>t2_buf[offset]);
Expand All @@ -725,7 +724,7 @@ SeepsScore *SeepsClimoGrid::get_record(int ix, int iy,
seeps_record->score = score;
}
else if (~is_eq(p1, bad_data_double)) {
filtered_count++;
increase_filtered_count();
mlog << Debug(7) << method_name << " filtered by threshold p1=" << p1_buf[offset] <<"\n";
}
}
Expand Down Expand Up @@ -781,37 +780,6 @@ double SeepsClimoGrid::get_score(int ix, int iy, double p_fcst, double p_obs) {

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

ConcatString SeepsClimoGrid::get_seeps_climo_filename() {
ConcatString seeps_filename;
const char *method_name = "SeepsClimoGrid::get_seeps_climo_filename() -> ";

// Use the environment variable, if set.
bool use_env = get_env(MET_ENV_SEEPS_GRID_CLIMO_NAME, seeps_filename);
if(use_env) {
seeps_filename = replace_path(seeps_filename);
}
else seeps_filename = replace_path(def_seeps_grid_filename);

if (seeps_ready = file_exists(seeps_filename.c_str())) {
mlog << Debug(7) << method_name << "SEEPS grid climo name=\""
<< seeps_filename.c_str() << "\"\n";
}
else {
ConcatString message = "";
if (use_env) {
message.add("from the env. name ");
message.add(MET_ENV_SEEPS_GRID_CLIMO_NAME);
}
mlog << Warning << "\n" << method_name
<< "The SEEPS grid climo name \"" << seeps_filename.c_str()
<< "\"" << message << " does not exist!\n\n";
}

return seeps_filename;
}

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

void SeepsClimoGrid::read_seeps_scores(ConcatString filename) {
clock_t clock_time = clock();
const char *method_name = "SeepsClimoGrid::read_seeps_scores() -> ";
Expand Down Expand Up @@ -922,7 +890,7 @@ void SeepsClimoGrid::read_seeps_scores(ConcatString filename) {
}
catch(...) {

seeps_ready = false;
set_seeps_ready(false);
mlog << Error << "\n" << method_name
<< "encountered an error on reading " << filename << ".\n\n";

Expand Down
Loading

0 comments on commit b263eac

Please sign in to comment.