Skip to content

Commit f8144ac

Browse files
committed
Rework jpd generation for better file checking, naming
1 parent 1d68af2 commit f8144ac

File tree

4 files changed

+169
-82
lines changed

4 files changed

+169
-82
lines changed

deploy/runtime/ui/Marine Wave Resource Time Series.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,9 +1069,9 @@
10691069
"\t\t\t}",
10701070
"\t\t\trescanlibrary('wave');",
10711071
"\t\t\tvalue('wave_resource_filename', file);\r",
1072-
"\t\t\tf_name = value( 'wave_resource_filename' );\r",
1073-
"\t\t\tf_name = slash_back_to_forward( f_name );\r",
1074-
"\t\t\tupdate_library_folders( path_only( f_name ) );\r",
1072+
"\t\t\t//f_name = value( 'wave_resource_filename' );\r",
1073+
"\t\t\t//f_name = slash_back_to_forward( f_name );\r",
1074+
"\t\t\t//update_library_folders( path_only( f_name ) );\r",
10751075
"\t\t\t//value( 'location', file_to_key( f_name ) );",
10761076
"\t\t\t//value('name', name);\r",
10771077
"\t\t\t//value('name', value('location'));",

src/invoke.cpp

Lines changed: 164 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -4429,7 +4429,7 @@ void fcall_makejpdfile(lk::invoke_t& cxt)
44294429
UICallbackContext& cc = *(UICallbackContext*)cxt.user_data();
44304430
Library* reloaded = 0;
44314431
wxString file(cxt.arg(0).as_string().Lower());
4432-
WaveResourceTSData_makeJPD(file, true);
4432+
WaveResourceTSData_makeJPD(file, 0, true);
44334433
wxString wave_resource_db = SamApp::GetUserLocalDataDir() + "/WaveResourceData.csv";
44344434
ScanWaveResourceData(wave_resource_db, true);
44354435
reloaded = Library::Load(wave_resource_db);
@@ -4463,60 +4463,46 @@ void fcall_make_jpd_multiyear(lk::invoke_t& cxt)
44634463
ssc_number_t* period_arr;
44644464
ssc_number_t* height_bin;
44654465
ssc_number_t* period_bin;
4466+
ssc_number_t* resource_matrix;
4467+
ssc_number_t* resource_matrix_total;
4468+
util::matrix_t<double> resource_matrix_jpd;
4469+
int matrix_rows = 0;
4470+
int matrix_cols = 0;
44664471
ssc_number_t* year_arr;
44674472
ssc_number_t* month_arr;
44684473
ssc_number_t* day_arr;
44694474
ssc_number_t* hour_arr;
44704475
ssc_number_t* min_arr;
44714476
const char* str;
4472-
wxCSVData csv;
4473-
csv(0, 0) = "Source";
4474-
csv(0, 1) = "Location ID";
4475-
csv(0, 2) = "Jurisdiction";
4476-
csv(1, 2) = "Federal";
4477-
csv(0, 3) = "Latitude";
4478-
csv(0, 4) = "Longitude";
4479-
csv(0, 5) = "Time Zone";
4480-
csv(1, 5) = "0";
4481-
csv(0, 6) = "Local Time Zone";
4482-
csv(0, 7) = "Distance to Shore";
4483-
csv(0, 8) = "Directionality Coefficient";
4484-
csv(1, 8) = "-";
4485-
csv(0, 9) = "Energy Period";
4486-
csv(1, 9) = "s";
4487-
csv(0, 10) = "Maximum Energy Direction";
4488-
csv(1, 10) = "deg";
4489-
csv(0, 11) = "Mean Absolute Period";
4490-
csv(1, 11) = "s";
4491-
csv(0, 12) = "Mean Wave Direction";
4492-
csv(1, 12) = "deg";
4493-
csv(0, 13) = "Mean Zero - Crossing Period";
4494-
csv(1, 13) = "s";
4495-
csv(0, 14) = "Omni - Directional Wave Power";
4496-
csv(1, 14) = "W/m";
4497-
csv(0, 15) = "Peak Period";
4498-
csv(1, 15) = "s";
4499-
csv(0, 16) = "Significant Wave Height";
4500-
csv(1, 16) = "m";
4501-
csv(0, 17) = "Spectral Width";
4502-
csv(1, 17) = "-";
4503-
csv(0, 18) = "Water Depth";
4504-
csv(0, 19) = "Version";
4505-
csv(1, 19) = "v1.0.0";
4506-
4507-
csv(2, 0) = "Year";
4508-
csv(2, 1) = "Month";
4509-
csv(2, 2) = "Day";
4510-
csv(2, 3) = "Hour";
4511-
csv(2, 4) = "Minute";
4512-
csv(2, 5) = "Significant Wave Height";
4513-
csv(2, 6) = "Energy Period";
45144477

45154478

45164479
ssc_number_t val;
45174480
ssc_number_t first_year = 0;
45184481
int nrows;
45194482
int file_count = 0;
4483+
4484+
wxCSVData csv;
4485+
csv(0, 0) = "Name";
4486+
csv(0, 1) = "City";
4487+
csv(0, 2) = "State";
4488+
csv(0, 3) = "Country";
4489+
csv(0, 4) = "Latitude";
4490+
csv(0, 5) = "Longitude";
4491+
csv(0, 6) = "Nearby buoy number";
4492+
csv(0, 7) = "Average power flux";
4493+
csv(0, 8) = "Bathymetry";
4494+
csv(0, 9) = "Sea bed";
4495+
csv(0, 10) = "Time zone";
4496+
csv(1, 10) = "tz";
4497+
csv(0, 11) = "Data source";
4498+
csv(0, 12) = "Notes";
4499+
4500+
ssc_number_t year_min = 9999;
4501+
ssc_number_t year_max = 9999;
4502+
4503+
double num = 0;
4504+
4505+
45204506
while (has_more) {
45214507
// process file
45224508
wxString wf = folder + "/" + file;
@@ -4527,55 +4513,71 @@ void fcall_make_jpd_multiyear(lk::invoke_t& cxt)
45274513

45284514
if (const char* err = ssc_module_exec_simple_nothread("wave_file_reader", pdata))
45294515
{
4530-
//wxLogStatus("error scanning '" + wf + "'");
4516+
wxLogStatus("error scanning '" + wf + "'");
45314517
//cxt.error(err);
45324518
cxt.result().assign(err);
4533-
return;
4519+
continue;
45344520
}
45354521
else
45364522
{
45374523
if (file_count == 0) {
45384524
if (ssc_data_get_number(pdata, "location_id", &val))
4539-
csv(1, 1) = wxString::Format("%g", val);
4525+
csv(1, 0) = wxString::Format("%g", val);
45404526

4541-
if (ssc_data_get_number(pdata, "distance_to_shore_file", &val))
4542-
csv(1, 7) = wxString::Format("%g", val);
4527+
45434528

4544-
if (ssc_data_get_number(pdata, "water_depth_file", &val))
4545-
csv(1, 18) = wxString::Format("%g", val);
4529+
45464530

45474531
if (ssc_data_get_number(pdata, "lat", &val)) {
4548-
csv(1, 3) = wxString::Format("%g", val);
4532+
csv(1, 4) = wxString::Format("%g", val);
4533+
//csv(1, 0) = "lat" + wxString::Format("%g", val);
45494534
}
45504535

45514536
if (ssc_data_get_number(pdata, "lon", &val)) {
4552-
csv(1, 4) = wxString::Format("%g", val);
4537+
csv(1, 5) = wxString::Format("%g", val);
4538+
//csv(1, 0) += "_lon" + wxString::Format("%g", val);
4539+
}
4540+
4541+
if ((year_arr = ssc_data_get_array(pdata, "year", &nrows)) != 0)
4542+
{
4543+
year_min = year_arr[0];
4544+
year_max = year_arr[0];
45534545
}
4546+
4547+
45544548
/*
45554549
if ((year_arr = ssc_data_get_array(pdata, "year", &nrows)) != 0)
45564550
{
45574551
first_year = year_arr[0];
45584552
}
4559-
*/
4553+
*/
4554+
4555+
//Nearby Buoy
4556+
csv(1, 6) = "";
4557+
//Average power flux
4558+
csv(1, 7) = wxString::Format("%g", std::numeric_limits<double>::quiet_NaN());
4559+
//Bathymetry
4560+
csv(1, 8) = "";
4561+
//Sea Bed
4562+
csv(1, 9) = "";
45604563

45614564
if (ssc_data_get_number(pdata, "tz", &val))
4562-
csv(1, 6) = wxString::Format("%g", val);
4565+
csv(1, 10) = wxString::Format("%g", val);
45634566

45644567
if ((str = ssc_data_get_string(pdata, "data_source")) != 0)
4565-
csv(1, 0) = wxString(str);
4568+
csv(1, 11) = wxString(str);
4569+
45664570

4567-
if ((str = ssc_data_get_string(pdata, "notes")) != 0)
4568-
csv(1, 19) = wxString(str);
45694571
}
45704572

45714573
if ((year_arr = ssc_data_get_array(pdata, "year", &nrows)) != 0)
45724574
{
45734575
for (int i = 0; i < nrows; i++) {
4574-
csv(nrows * file_count + i + 3, 0) = wxString::Format("%g", year_arr[i]); //No year for typical wave year file
4576+
if (year_arr[0] > year_max) year_max = year_arr[0];
45754577
}
45764578
}
45774579

4578-
if ((month_arr = ssc_data_get_array(pdata, "month", &nrows)) != 0)
4580+
/*if ((month_arr = ssc_data_get_array(pdata, "month", &nrows)) != 0)
45794581
{
45804582
for (int i = 0; i < nrows; i++) {
45814583
csv(nrows * file_count + i + 3, 1) = wxString::Format("%g", month_arr[i]);
@@ -4614,30 +4616,115 @@ void fcall_make_jpd_multiyear(lk::invoke_t& cxt)
46144616
for (int i = 0; i < nrows; i++) {
46154617
csv(nrows * file_count + i + 3, 6) = wxString::Format("%g", period_arr[i]);
46164618
}
4619+
}*/
4620+
if ((resource_matrix = ssc_data_get_matrix(pdata, "wave_resource_matrix", &matrix_rows, &matrix_cols)) != 0)
4621+
{
4622+
if (file_count == 0) {
4623+
//resource_matrix_total = resource_matrix;
4624+
resource_matrix_jpd.resize(matrix_rows, matrix_cols);
4625+
for (int r = 0; r < matrix_rows; r++) {
4626+
for (int c = 0; c < matrix_cols; c++) {
4627+
resource_matrix_jpd.at(r, c) = resource_matrix[r * matrix_cols + c];
4628+
}
4629+
}
4630+
4631+
}
4632+
else {
4633+
for (int r = 0; r < matrix_rows ; r++) {
4634+
for (int c = 0; c < matrix_cols; c++) {
4635+
if (c != 0 && r != 0) { //Do not sum headers
4636+
//num = resource_matrix[i];
4637+
resource_matrix_jpd.at(r, c) += resource_matrix[r * matrix_cols + c]; //Sum
4638+
//num = resource_matrix_total[i];
4639+
}
4640+
}
4641+
}
4642+
}
46174643
}
46184644
}
4619-
ssc_data_free(pdata);
4645+
//ssc_data_free(pdata);
46204646

46214647
has_more = dir.GetNext(&file);
4622-
file_count++;
4623-
}
4624-
//csv(1, 0) += "_" + wxString::Format("%g", first_year + file_count - 1);
4625-
csv.WriteFile(final_file);
4626-
wxString err = WaveResourceTSData_makeJPD(final_file, true);
4627-
cxt.result().assign(err); //return name for library indexing
4628-
wxString wave_resource_db = SamApp::GetUserLocalDataDir() + "/WaveResourceData.csv";
4629-
ScanWaveResourceData(wave_resource_db, true);
4630-
//std::remove(final_file); //Remove multiyear time series file as it doesn't make sense to run in SAM
4631-
reloaded = Library::Load(wave_resource_db);
4632-
if (reloaded != 0)
4633-
{
4634-
if (&cc != NULL) {
4635-
std::vector<wxUIObject*> objs = cc.InputPage()->GetObjects();
4636-
for (size_t i = 0; i < objs.size(); i++)
4637-
if (LibraryCtrl* lc = objs[i]->GetNative<LibraryCtrl>())
4638-
lc->ReloadLibrary();
4648+
if (has_more) {
4649+
ssc_data_free(pdata); //if there are more files, delete the var table and start over, if not keep going
4650+
file_count++;
46394651
}
4652+
else {
4653+
file_count++; //Count the file that was just read before exiting
4654+
for (int r = 0; r < matrix_rows; r++)
4655+
{
4656+
4657+
for (int c = 0; c < matrix_cols; c++)
4658+
{
4659+
if (r != 0 && c != 0) {
4660+
resource_matrix_jpd.at(r, c) = resource_matrix_jpd.at(r, c) / file_count; //Normalize percentages
4661+
}
4662+
csv(2 + r, c) = wxString::Format("%g", resource_matrix_jpd.at(r, c));
4663+
if (r == 0 && c == 0) csv(2 + r, c) = "Hs/Te";
4664+
}
4665+
4666+
4667+
}
4668+
csv(1, 12) = wxString::Format("%g", year_min);
4669+
csv(1, 12) += "-" + wxString::Format("%g", year_max);
4670+
4671+
csv(1, 1) = "";
4672+
csv(1, 2) = "";
4673+
csv(1, 3) = "";
4674+
csv(1, 0) = wxString(final_file).substr(0, wxString(final_file).Find(".csv"));
4675+
4676+
4677+
csv.WriteFile(final_file);
4678+
ssc_data_free(pdata);
4679+
4680+
}
4681+
46404682
}
4683+
4684+
//csv(1, 0) += "_" + wxString::Format("%g", year_min);
4685+
//csv(1, 0) += "_" + wxString::Format("%g", year_max);
4686+
4687+
//csv(1, 1) = "";
4688+
//csv(1, 2) = "";
4689+
//csv(1, 3) = "";
4690+
4691+
////for (int i = 0; i < matrix_rows * matrix_cols; i++) {
4692+
//// if (i > matrix_cols && fmod(i, matrix_cols) != 0) //Do not sum headers
4693+
//// resource_matrix_total[i] = resource_matrix_total[i]/file_count; //Sum
4694+
////}
4695+
4696+
//for (int r = 0; r < matrix_rows; r++)
4697+
//{
4698+
4699+
// for (int c = 0; c < matrix_cols; c++)
4700+
// {
4701+
// if (r != 0 && c != 0) {
4702+
// resource_matrix_total[r * matrix_cols + c] = resource_matrix_total[r * matrix_cols + c] / file_count;
4703+
// }
4704+
// csv(2 + r, c) = wxString::Format("%g", (resource_matrix_total[r * matrix_cols + c]));
4705+
// if (r == 0 && c == 0) csv(2 + r, c) = "Hs/Te";
4706+
// }
4707+
4708+
//}
4709+
////csv(1, 0) += "_" + wxString::Format("%g", first_year + file_count - 1);
4710+
//ssc_data_free(pdata)
4711+
4712+
//csv.WriteFile(final_file);
4713+
//wxString err = WaveResourceTSData_makeJPD(final_file, resource_matrix_total, true);
4714+
//cxt.result().assign(err); //return name for library indexing
4715+
//wxString wave_resource_db = SamApp::GetUserLocalDataDir() + "/WaveResourceData.csv";
4716+
//ScanWaveResourceData(wave_resource_db, true);
4717+
////std::remove(final_file); //Remove multiyear time series file as it doesn't make sense to run in SAM
4718+
//reloaded = Library::Load(wave_resource_db);
4719+
//if (reloaded != 0)
4720+
//{
4721+
// if (&cc != NULL) {
4722+
// std::vector<wxUIObject*> objs = cc.InputPage()->GetObjects();
4723+
// for (size_t i = 0; i < objs.size(); i++)
4724+
// if (LibraryCtrl* lc = objs[i]->GetNative<LibraryCtrl>())
4725+
// lc->ReloadLibrary();
4726+
// }
4727+
//}
46414728
return;
46424729
}
46434730

src/library.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1744,7 +1744,7 @@ bool ScanTidalResourceData(const wxString& db_file, bool show_busy)
17441744
return csv.WriteFile(db_file);
17451745
}
17461746

1747-
wxString WaveResourceTSData_makeJPD(const wxString& ts_file, bool show_busy)
1747+
wxString WaveResourceTSData_makeJPD(const wxString& ts_file, ssc_number_t* resource_matrix, bool show_busy)
17481748
{
17491749
// TODO - update fields based on final file
17501750
wxBusyInfo* busy = 0;

src/library.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ bool ShowTidalResourceDataSettings();
107107
bool ScanWaveResourceData(const wxString &db_file, bool show_busy = false);
108108
bool ScanWaveResourceTSData(const wxString &db_file, bool show_busy = false);
109109
bool ScanTidalResourceData(const wxString& db_file, bool show_busy = false);
110-
wxString WaveResourceTSData_makeJPD(const wxString& db_file, bool show_busy = false);
110+
wxString WaveResourceTSData_makeJPD(const wxString& db_file, ssc_number_t* resource_matrix = 0, bool show_busy = false);
111111

112112
class LibraryCtrl;
113113
class wxTextCtrl;

0 commit comments

Comments
 (0)