Skip to content

Commit

Permalink
bug fixes that showed up in running the unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Albo committed Aug 3, 2022
1 parent 0b1b673 commit 4c33bd8
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 70 deletions.
60 changes: 44 additions & 16 deletions src/tools/other/mode_time_domain/mtd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -210,18 +210,16 @@ MM_Engine engine;
// storage for valid times
//

unixtime * valid_times_fcst = 0;
valid_times_fcst = new unixtime [fcst_filenames.n()];
unixtime * valid_times_obs = 0;
valid_times_obs = new unixtime [obs_filenames.n()];
vector<unixtime> valid_times_fcst;
vector<unixtime> valid_times_obs;

//
// read the data files
//

mtd_read_data(config, *(config.fcst_info), fcst_filenames, fcst_raw, valid_times_fcst);
valid_times_fcst = mtd_read_data(config, *(config.fcst_info), fcst_filenames, fcst_raw);

mtd_read_data(config, *(config.obs_info), obs_filenames, obs_raw, valid_times_obs);
valid_times_obs = mtd_read_data(config, *(config.obs_info), obs_filenames, obs_raw);

if ( fcst_raw.nt() != obs_raw.nt() ) {

Expand Down Expand Up @@ -446,6 +444,13 @@ for (j=0; j<(fcst_obj.n_objects()); ++j) {

mask_2d = fcst_obj.const_t_mask(t, j + 1); // 1-based

if (t < 0 || t >= (int)valid_times_fcst.size()) {
mlog << Error
<< "\n " << program_name << ": index " << t << " out of forecast valid times range 0 to " << valid_times_fcst.size()-1
<< "\n\n";
exit ( 1 );
}

fcst_raw.get_data_plane(t, raw_2d);

att_2 = calc_2d_single_atts(mask_2d, raw_2d, j + 1, config.inten_perc_value);
Expand Down Expand Up @@ -480,6 +485,13 @@ for (j=0; j<(obs_obj.n_objects()); ++j) {

mask_2d = obs_obj.const_t_mask(t, j + 1); // 1-based

if (t < 0 || t >= (int)valid_times_obs.size()) {
mlog << Error
<< "\n " << program_name << ": index " << t << " out of obs valid times range 0 to " << valid_times_obs.size()-1
<< "\n\n";
exit ( 1 );
}

obs_raw.get_data_plane(t, raw_2d);

att_2 = calc_2d_single_atts(mask_2d, raw_2d, j + 1, config.inten_perc_value);
Expand Down Expand Up @@ -695,7 +707,14 @@ if ( have_pairs ) {

// cout << "j = " << j << ", vol = " << mask_2d.object_volume(0) << '\n';

fcst_raw.get_data_plane(t, raw_2d);
if (t < 0 || t >= (int)valid_times_fcst.size()) {
mlog << Error
<< "\n " << program_name << ": index " << t << " out of forecast valid times range 0 to " << valid_times_fcst.size()-1
<< "\n\n";
exit ( 1 );
}

fcst_raw.get_data_plane(t, raw_2d);

att_2 = calc_2d_single_atts(mask_2d, raw_2d, j + 1, config.inten_perc_value);

Expand Down Expand Up @@ -736,7 +755,14 @@ if ( have_pairs ) {
// mask_2d = mask.const_t_mask(t, j + 1); // 1-based
mask_2d = mask.const_t_mask(t, 1); // 1-based

obs_raw.get_data_plane(t, raw_2d);
if (t < 0 || t >= (int)valid_times_obs.size()) {
mlog << Error
<< "\n " << program_name << ": index " << t << " out of obs valid times range 0 to " << valid_times_obs.size()-1
<< "\n\n";
exit ( 1 );
}

obs_raw.get_data_plane(t, raw_2d);

att_2 = calc_2d_single_atts(mask_2d, raw_2d, j + 1, config.inten_perc_value);

Expand Down Expand Up @@ -882,9 +908,6 @@ mlog << Debug(2)

do_mtd_nc_output(config.nc_info, engine, fcst_raw, obs_raw, fcst_obj, obs_obj, config, path.c_str());

if ( valid_times_fcst ) { delete [] valid_times_fcst; valid_times_fcst = 0; }
if ( valid_times_obs ) { delete [] valid_times_obs; valid_times_obs = 0; }

//
// done
//
Expand Down Expand Up @@ -1066,14 +1089,13 @@ ConcatString path;
// storage for valid times
//

unixtime * valid_times = 0;
valid_times = new unixtime [single_filenames.n()];
vector<unixtime> valid_times;

//
// read the data files
//

mtd_read_data(config, *(config.fcst_info), single_filenames, raw, valid_times);
valid_times = mtd_read_data(config, *(config.fcst_info), single_filenames, raw);

//
// copy forecast name/units/level to observation
Expand Down Expand Up @@ -1177,7 +1199,15 @@ for (j=0; j<(obj.n_objects()); ++j) {

mask_2d = obj.const_t_mask(t, j + 1); // 1-based

if (t < 0 || t >= (int)valid_times.size()) {
mlog << Error
<< "\n " << program_name << ": index " << t << " out of valid times range 0 to " << valid_times.size()-1
<< "\n\n";
exit ( 1 );
}

raw.get_data_plane(t, raw_2d);
//raw_2d.set_valid(valid_times[t]);

att_2 = calc_2d_single_atts(mask_2d, raw_2d, j + 1, config.inten_perc_value);

Expand Down Expand Up @@ -1243,8 +1273,6 @@ mlog << Debug(2)
do_mtd_nc_output(config.nc_info, raw, obj, config, path.c_str());


if ( valid_times ) { delete [] valid_times; valid_times = 0; }

//
// done
//
Expand Down
35 changes: 34 additions & 1 deletion src/tools/other/mode_time_domain/mtd_file_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ Nx = Ny = Nt = 0;

StartValidTime = (unixtime) 0;

ActualValidTimes.clear();

DeltaT = 0;

Filename.clear();
Expand Down Expand Up @@ -292,7 +294,6 @@ return;

}


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


Expand All @@ -306,6 +307,19 @@ return;

}

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


void MtdFileBase::init_actual_valid_times(const vector<unixtime> &validTimes)

{

ActualValidTimes = validTimes;

return;

}


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

Expand Down Expand Up @@ -349,6 +363,25 @@ return ( StartValidTime + t*DeltaT );

}

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


unixtime MtdFileBase::actual_valid_time(int t) const

{

if ( (t < 0) || ( t >= (int)ActualValidTimes.size()) ) {

mlog << Error << "\n\n MtdFileBase::valid_time(int t) -> range check error\n\n";

exit ( 1 );

}

return ( ActualValidTimes[t] );

}


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

Expand Down
11 changes: 9 additions & 2 deletions src/tools/other/mode_time_domain/mtd_file_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,11 @@ class MtdFileBase {

int Nx, Ny, Nt;

unixtime StartValidTime;
unixtime StartValidTime; // useful for constant time increments

int DeltaT; // seconds
int DeltaT; // seconds, useful for constant time increments

vector<unixtime> ActualValidTimes; // useful for uneven time increments

IntArray Lead_Times;

Expand All @@ -113,8 +115,11 @@ class MtdFileBase {
void set_grid(const Grid &);

void set_start_valid_time (unixtime);

void set_delta_t (int); // seconds

void init_actual_valid_times(const vector<unixtime> &validTimes);

void set_lead_time(int index, int value);

void set_filetype(MtdFileType);
Expand Down Expand Up @@ -143,6 +148,8 @@ class MtdFileBase {

unixtime valid_time (int) const;

unixtime actual_valid_time (int) const;

int lead_time (int index) const;

//
Expand Down
66 changes: 34 additions & 32 deletions src/tools/other/mode_time_domain/mtd_read_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ using namespace std;
////////////////////////////////////////////////////////////////////////


void mtd_read_data(MtdConfigInfo & config, VarInfo & varinfo,
const StringArray & filenames, MtdFloatFile & raw,
unixtime valid_times[])
vector<unixtime> mtd_read_data(MtdConfigInfo & config, VarInfo & varinfo,
const StringArray & filenames, MtdFloatFile & raw)

{

Expand All @@ -45,7 +44,7 @@ int j, k;
Met2dDataFile * data_2d_file = 0;
Met2dDataFileFactory factory;
DataPlane plane;

vector<unixtime> valid_times;
//
// read the files
//
Expand Down Expand Up @@ -73,7 +72,7 @@ for (j=0; j<(filenames.n()); ++j) {

}

valid_times[j] = plane.valid();
valid_times.push_back(plane.valid());

if ( j == 0 ) {

Expand All @@ -95,56 +94,60 @@ for (j=0; j<(filenames.n()); ++j) {

varinfo.set_valid(valid_times[0]);

// store the valid times vector into the raw data for later use in do_2d_txt_output()

raw.init_actual_valid_times(valid_times);

//
// check the time intervals for consistency
// Store the time differences between succesive valid times in an array
// See if differences are constant or not, and if not see if all diffs are months
//

unixtime dt_start, dt, *dtArray;
int numDt = filenames.n() - 1;
dtArray = new unixtime [numDt];
unixtime dt_start;
vector<unixtime> dtVector;

dt_start = valid_times[1] - valid_times[0];
dtArray[0] = dt_start;
dtVector.push_back(dt_start);

for (j=2; j<(filenames.n()); ++j) {
dtArray[j - 1] = dt = valid_times[j] - valid_times[j - 1];
for (size_t k=2; k<valid_times.size(); ++k) {
dtVector.push_back(valid_times[k] - valid_times[k - 1]);
}

bool variableTimeIncs = false;
for (j=0; j<numDt; ++j) {
if (variableTimeIncs) {
for (size_t k=0; k<dtVector.size(); ++k) {
if (variableTimeIncs) {
break;
}
for (size_t k2=k+1; k2<dtVector.size(); ++k2) {
if ( dtVector[k] != dtVector[k2]) {
variableTimeIncs = true;
break;
}
for (k=j+1; k<numDt; ++k) {
if ( dtArray[j] != dtArray[k]) {
variableTimeIncs = true;
break;
}
}
}
}
}
if (variableTimeIncs) {
// compute the mode and use it as the actual delta, by storing it to dt_start
NumArray na;
for (j=0; j<numDt; ++j) {
na.add((double)dtArray[j]);
// compute the mode and use it as the actual delta, by storing it to dt_start
NumArray na;
for (size_t k=0; k<dtVector.size(); ++k) {
na.add((double)dtVector[k]);
}
dt_start = (unixtime)na.mode();

// test if the differences are all months (in seconds)
bool isMonths = true;
int secondsPerDay = 24*3600;
for (j=0; j<numDt; ++j) {
int days = dtArray[j]/secondsPerDay;
for (size_t k=0; k<dtVector.size(); ++k) {
int days = dtVector[k]/secondsPerDay;
if (days != 28 && days != 29 && days != 30 && days != 31) {
isMonths = false;
break;
}
}

if (isMonths) {
mlog << Debug(2) << "\n\n mtd_read_data() -> file time increments are months (not constant), use MODE of the increments, mode=" << dt_start << " seconds = " << dt_start/(24*3600) << " days\n\n";
mlog << Debug(1) << "File time increments are months (not constant), use MODE of the increments, mode=" << dt_start
<< " seconds = " << dt_start/(24*3600) << " days\n\n";
} else {
// compute some measures that might be used to exit with an error, for now just show them to the user and go on
double mean, var, svar;
Expand All @@ -153,12 +156,11 @@ if (variableTimeIncs) {
unixtime umean = (unixtime)mean;
unixtime uvar = (unixtime)var;
unixtime suvar = (unixtime)svar;
mlog << Warning << "\n\n mtd_read_data() -> file time increments are not constant, could be problematic\n";
mlog << Warning << " mtd_read_data() -> use MODE of the increments, mode=" << dt_start << "\n";
mlog << Warning << " mtd_read_data() -> time increment properties: mean=" << umean << " variance=" << uvar << " sqrt(var)=" << suvar << "\n\n";
mlog << Warning << "\n\n mtd_read_data() -> File time increments are not constant, could be problematic\n";
mlog << Warning << " mtd_read_data() -> Using MODE of the increments, mode=" << dt_start << "\n";
mlog << Warning << " mtd_read_data() -> Time increment properties: mean=" << umean << " variance=" << uvar << " sqrt(var)=" << suvar << "\n\n";
}
}
delete [] dtArray;

//
// load up the rest of the MtdFloatFile class members
Expand All @@ -178,7 +180,7 @@ raw.calc_data_minmax();
// done
//

return;
return valid_times;

}

Expand Down
8 changes: 5 additions & 3 deletions src/tools/other/mode_time_domain/mtd_read_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@

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

//
// returns the actual valid times
//

extern void mtd_read_data(MtdConfigInfo &, VarInfo &,
const StringArray & filenames, MtdFloatFile &,
unixtime valid_times[]);
extern vector<unixtime> mtd_read_data(MtdConfigInfo &, VarInfo &,
const StringArray & filenames, MtdFloatFile &);


////////////////////////////////////////////////////////////////////////
Expand Down
Loading

0 comments on commit 4c33bd8

Please sign in to comment.