Class: TimeseriesObjectiveFunction
- Inherits:
-
OpenStudio::Measure::ReportingMeasure
- Object
- OpenStudio::Measure::ReportingMeasure
- TimeseriesObjectiveFunction
- Defined in:
- lib/measures/TimeseriesObjectiveFunction/measure.rb
Overview
start the measure
Instance Method Summary collapse
-
#arguments(model = nil) ⇒ Object
define the arguments that the user will input.
-
#description ⇒ Object
human readable description.
-
#energyPlusOutputRequests(runner, user_arguments) ⇒ Object
adds output requests ahead of EnergyPlus simulation.
-
#modeler_description ⇒ Object
human readable description of modeling approach.
-
#name ⇒ Object
human readable name.
-
#outputs ⇒ Object
outputs for PAT.
-
#run(runner, user_arguments) ⇒ Object
define what happens when the measure is run.
-
#to_JSTime(os_time) ⇒ Object
Method to translate from OpenStudio’s time formatting to Javascript time formatting OpenStudio time 2009-May-14 00:10:00 Raw string Javascript time 2009/07/12 12:34:56.
Instance Method Details
#arguments(model = nil) ⇒ Object
define the arguments that the user will input
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/measures/TimeseriesObjectiveFunction/measure.rb', line 32 def arguments(model = nil) args = OpenStudio::Measure::OSArgumentVector.new # the name of the sql file csv_name = OpenStudio::Measure::OSArgument.makeStringArgument('csv_name', true) csv_name.setDisplayName('Path to CSV file for the metered data') csv_name.setDescription('Path to CSV file including file name.') csv_name.setDefaultValue('../../../lib/resources/mtr.csv') args << csv_name csv_time_header = OpenStudio::Measure::OSArgument.makeStringArgument('csv_time_header', true) csv_time_header.setDisplayName('CSV Time Header') csv_time_header.setDescription('CSV Time Header Value. Used to determine the timestamp column in the CSV file') csv_time_header.setDefaultValue('Date/Time') args << csv_time_header csv_var = OpenStudio::Measure::OSArgument.makeStringArgument('csv_var', true) csv_var.setDisplayName('CSV variable name') csv_var.setDescription('CSV variable name. Used to determine the variable column in the CSV file') csv_var.setDefaultValue('Whole Building:Facility Total Electric Demand Power [W](TimeStep)') args << csv_var convert_data_chs = OpenStudio::StringVector.new convert_data_chs << 'F to C' convert_data_chs << 'WH to J' convert_data_chs << 'CFM to m3/s' convert_data_chs << 'PSI to Pa' convert_data_chs << 'None' convert_data = OpenStudio::Measure::OSArgument.makeChoiceArgument('convert_data', convert_data_chs, true) convert_data.setDisplayName('Convert Units') convert_data.setDescription('Convert Units in Metered Data') convert_data.setDefaultValue('None') args << convert_data csv_var_dn = OpenStudio::Measure::OSArgument.makeStringArgument('csv_var_dn', true) csv_var_dn.setDisplayName('CSV variable display name') csv_var_dn.setDescription('CSV variable display name. Not yet Implemented') csv_var_dn.setDefaultValue('') args << csv_var_dn years = OpenStudio::Measure::OSArgument.makeBoolArgument('year', true) years.setDisplayName('Year in csv data timestamp') years.setDescription('Is the Year in the csv data timestamp => mm/dd/yyyy or mm/dd (true/false)') years.setDefaultValue(true) args << years seconds = OpenStudio::Measure::OSArgument.makeBoolArgument('seconds', true) seconds.setDisplayName('Seconds in csv data timestamp') seconds.setDescription('Is the Seconds in the csv data timestamp => hh:mm:ss or hh:mm (true/false)') seconds.setDefaultValue(true) args << seconds sql_key = OpenStudio::Measure::OSArgument.makeStringArgument('key_value', true) sql_key.setDisplayName('SQL key value. use no_key if there is no key, i.e. Electricity:Facility') sql_key.setDescription('SQL key value for the SQL query to find the variable in the SQL file') sql_key.setDefaultValue('no_key') args << sql_key sql_var = OpenStudio::Measure::OSArgument.makeStringArgument('timeseries_name', true) sql_var.setDisplayName('TimeSeries Name') sql_var.setDescription('TimeSeries Name for the SQL query to find the variable in the SQL file') sql_var.setDefaultValue('Facility Total Electric Demand Power') args << sql_var reportfreq_chs = OpenStudio::StringVector.new reportfreq_chs << 'Detailed' reportfreq_chs << 'Zone Timestep' reportfreq_chs << 'Hourly' reportfreq_chs << 'Daily' reportfreq_chs << 'Monthly' reportfreq_chs << 'RunPeriod' reporting_frequency = OpenStudio::Measure::OSArgument.makeChoiceArgument('reporting_frequency', reportfreq_chs, true) reporting_frequency.setDisplayName('Reporting Frequency') reporting_frequency.setDescription('Reporting Frequency for SQL Query') reporting_frequency.setDefaultValue('Zone Timestep') args << reporting_frequency environment_period = OpenStudio::Measure::OSArgument.makeStringArgument('environment_period', true) environment_period.setDisplayName('Environment Period') environment_period.setDescription('Environment Period for SQL query') environment_period.setDefaultValue('RUN PERIOD 1') args << environment_period norm = OpenStudio::Measure::OSArgument.makeDoubleArgument('norm', true) norm.setDisplayName('Norm of the difference of csv and sql') norm.setDescription('Norm of the difference of csv and sql. 1 is absolute value. 2 is euclidean distance. 3 is raw difference.') norm.setDefaultValue(1) args << norm scale = OpenStudio::Measure::OSArgument.makeDoubleArgument('scale', true) scale.setDisplayName('Scale factor to apply to the difference') scale.setDescription('Scale factor to apply to the difference (1 is no scale)') scale.setDefaultValue(1) args << scale find_avail = OpenStudio::Measure::OSArgument.makeBoolArgument('find_avail', true) find_avail.setDisplayName('Find Available data in the SQL file') find_avail.setDescription("Will RegisterInfo all the 'EnvPeriod', 'ReportingFrequencies', 'VariableNames', 'KeyValues' in the SQL file. Useful for debugging SQL issues.") find_avail.setDefaultValue(true) args << find_avail algorithm_download = OpenStudio::Measure::OSArgument.makeBoolArgument('algorithm_download', true) algorithm_download.setDisplayName('algorithm_download') algorithm_download.setDescription('Make JSON data available for algorithm_download (true/false)') algorithm_download.setDefaultValue(false) args << algorithm_download plot_flag = OpenStudio::Measure::OSArgument.makeBoolArgument('plot_flag', true) plot_flag.setDisplayName('plot_flag timeseries data') plot_flag.setDescription('Create plot of timeseries data (true/false)') plot_flag.setDefaultValue(true) args << plot_flag plot_name = OpenStudio::Measure::OSArgument.makeStringArgument('plot_name', true) plot_name.setDisplayName('Plot name') plot_name.setDescription('Name to include in reporting file name.') plot_name.setDefaultValue('plot_name') args << plot_name = OpenStudio::Measure::OSArgument.makeBoolArgument('verbose_messages', true) .setDisplayName('verbose_messages') .setDescription('verbose messages. Useful for debugging but MAJOR Performance Hit.') .setDefaultValue(false) args << = OpenStudio::Measure::OSArgument.makeBoolArgument('warning_messages', true) .setDisplayName('warning_messages') .setDescription('Warn on missing data.') .setDefaultValue(true) args << add_first_zero_for_plots = OpenStudio::Measure::OSArgument.makeBoolArgument('add_first_zero_for_plots', true) add_first_zero_for_plots.setDisplayName('add_first_zero_for_plots') add_first_zero_for_plots.setDescription('Add a point of zero value to the plot at the beginning of the runperiod.') add_first_zero_for_plots.setDefaultValue(false) args << add_first_zero_for_plots add_last_zero_for_plots = OpenStudio::Measure::OSArgument.makeBoolArgument('add_last_zero_for_plots', true) add_last_zero_for_plots.setDisplayName('add_last_zero_for_plots') add_last_zero_for_plots.setDescription('Add a point of zero value to the plot at the end of the runperiod.') add_last_zero_for_plots.setDefaultValue(false) args << add_last_zero_for_plots args end |
#description ⇒ Object
human readable description
22 23 24 |
# File 'lib/measures/TimeseriesObjectiveFunction/measure.rb', line 22 def description return 'Creates Objective Function from Timeseries Data' end |
#energyPlusOutputRequests(runner, user_arguments) ⇒ Object
adds output requests ahead of EnergyPlus simulation
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/measures/TimeseriesObjectiveFunction/measure.rb', line 179 def energyPlusOutputRequests(runner, user_arguments) super(runner, user_arguments) result = OpenStudio::IdfObjectVector.new # use the built-in error checking return result unless runner.validateUserArguments(arguments, user_arguments) # gather required values from user arguments key = runner.getStringArgumentValue('key_value', user_arguments) variable = runner.getStringArgumentValue('timeseries_name', user_arguments) timestep = runner.getStringArgumentValue('reporting_frequency', user_arguments) # add output request using values from user argument if key == 'no_key' # could also look for variable.include?(":Electricity") result << OpenStudio::IdfObject.load("Output:Meter,#{variable},#{timestep};").get else result << OpenStudio::IdfObject.load("Output:Variable,#{key},#{variable},#{timestep};").get end result end |
#modeler_description ⇒ Object
human readable description of modeling approach
27 28 29 |
# File 'lib/measures/TimeseriesObjectiveFunction/measure.rb', line 27 def modeler_description return "Creates Objective Function from Timeseries Data. The measure applies a Norm at each timestep between the difference of CSV metered data and SQL model data. A timeseries plot can also be created. Possible outputs are 'cvrmse', 'nmbe', 'simdata' = sum of the simulated data, 'csvdata' = sum of metered data, 'diff' = P Norm between the metered and simulated data if Norm is 1 or 2, else its just the Difference." end |
#name ⇒ Object
human readable name
17 18 19 |
# File 'lib/measures/TimeseriesObjectiveFunction/measure.rb', line 17 def name return 'TimeSeries Objective Function' end |
#outputs ⇒ Object
outputs for PAT
203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/measures/TimeseriesObjectiveFunction/measure.rb', line 203 def outputs result = OpenStudio::Measure::OSOutputVector.new # calibration metrics result << OpenStudio::Measure::OSOutput.makeDoubleOutput('diff') # kWh result << OpenStudio::Measure::OSOutput.makeDoubleOutput('simdata') # kWh result << OpenStudio::Measure::OSOutput.makeDoubleOutput('csvdata') # % result << OpenStudio::Measure::OSOutput.makeDoubleOutput('cvrmse') # % result << OpenStudio::Measure::OSOutput.makeDoubleOutput('nmbe') # kWh result end |
#run(runner, user_arguments) ⇒ Object
define what happens when the measure is run
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 |
# File 'lib/measures/TimeseriesObjectiveFunction/measure.rb', line 217 def run(runner, user_arguments) super(runner, user_arguments) # use the built-in error checking return false unless runner.validateUserArguments(arguments, user_arguments) # get the last model and sql file model = runner.lastOpenStudioModel if model.empty? runner.registerError('Cannot find last model.') return false end model = model.get sqlFile = runner.lastEnergyPlusSqlFile if sqlFile.empty? runner.registerError('Cannot find last sql file.') return false end sqlFile = sqlFile.get model.setSqlFile(sqlFile) # assign the user inputs to variables csv_name = runner.getStringArgumentValue('csv_name', user_arguments) csv_time_header = runner.getStringArgumentValue('csv_time_header', user_arguments) csv_var = runner.getStringArgumentValue('csv_var', user_arguments) csv_var_dn = runner.getStringArgumentValue('csv_var_dn', user_arguments) years = runner.getBoolArgumentValue('year', user_arguments) seconds = runner.getBoolArgumentValue('seconds', user_arguments) sql_key = runner.getStringArgumentValue('key_value', user_arguments) # PAT fails on empty string or even a space as string, even though runs fine in OS app, as hack adding "no_key" method which converts to empty string here sql_key = '' if sql_key.include?('no_key') sql_var = runner.getStringArgumentValue('timeseries_name', user_arguments) norm = runner.getDoubleArgumentValue('norm', user_arguments) scale = runner.getDoubleArgumentValue('scale', user_arguments) find_avail = runner.getBoolArgumentValue('find_avail', user_arguments) = runner.getBoolArgumentValue('verbose_messages', user_arguments) = runner.getBoolArgumentValue('warning_messages', user_arguments) algorithm_download = runner.getBoolArgumentValue('algorithm_download', user_arguments) plot_flag = runner.getBoolArgumentValue('plot_flag', user_arguments) plot_name = runner.getStringArgumentValue('plot_name', user_arguments) environment_period = runner.getStringArgumentValue('environment_period', user_arguments) reporting_frequency = runner.getStringArgumentValue('reporting_frequency', user_arguments) convert_data = runner.getStringArgumentValue('convert_data', user_arguments) last_zero = runner.getBoolArgumentValue('add_last_zero_for_plots', user_arguments) first_zero = runner.getBoolArgumentValue('add_first_zero_for_plots', user_arguments) # remove leading and trailing double quotes # windows users can shift + right click a file to copy as path, which has double quotes csv_name.gsub!('"', '') @name = plot_name # Method to translate from OpenStudio's time formatting # to Javascript time formatting # OpenStudio time # 2009-May-14 00:10:00 Raw string # Javascript time # 2009/07/12 12:34:56 def to_JSTime(os_time) js_time = os_time.to_s # Replace the '-' with '/' js_time = js_time.tr('-', '/') # Replace month abbreviations with numbers js_time = js_time.gsub('Jan', '01') js_time = js_time.gsub('Feb', '02') js_time = js_time.gsub('Mar', '03') js_time = js_time.gsub('Apr', '04') js_time = js_time.gsub('May', '05') js_time = js_time.gsub('Jun', '06') js_time = js_time.gsub('Jul', '07') js_time = js_time.gsub('Aug', '08') js_time = js_time.gsub('Sep', '09') js_time = js_time.gsub('Oct', '10') js_time = js_time.gsub('Nov', '11') js_time.gsub('Dec', '12') end # setup convert case convert_data when 'F to C' convert = 0.5556 when 'WH to J' convert = 3600 when 'CFM to m3/s' convert = 0.00047 when 'PSI to Pa' convert = 6894.76 else convert_data == 'None' convert = 1 end diff = 0.0 simdata = 0.0 csvdata = 0.0 ySum = 0.0 squaredError = 0.0 sumError = 0.0 n = 0 cvrmse = 0 nmbe = 0 # map = {'Whole Building:Facility Total Electric Demand Power [W](TimeStep)'=>['Whole Building','Facility Total Electric Demand Power'],'OCCUPIED_TZ:Zone Mean Air Temperature [C](TimeStep)'=>['OCCUPIED_TZ','Zone Mean Air Temperature']} map = { csv_var.to_s => { key: sql_key, var: sql_var, index: 0 } } cal = { 1 => 'January', 2 => 'February', 3 => 'March', 4 => 'April', 5 => 'May', 6 => 'June', 7 => 'July', 8 => 'August', 9 => 'September', 10 => 'October', 11 => 'November', 12 => 'December' } runner.registerInfo("csv_name: #{csv_name}") csv = CSV.read(csv_name, encoding: 'ISO-8859-1') # sql = OpenStudio::SqlFile.new(OpenStudio::Path.new('sim.sql')) sql = sqlFile # environment_period = sql.availableEnvPeriods[3] runner.registerInfo("environment_period: #{environment_period}") environment_periods = environment_period.split(',') runner.registerInfo("environment_periods: #{environment_periods}") runner.registerInfo("map: #{map}") runner.registerInfo('') if find_avail ts = sql.availableTimeSeries runner.registerInfo("available timeseries: #{ts}") runner.registerInfo('') envs = sql.availableEnvPeriods envs.each do |env_s| freqs = sql.availableReportingFrequencies(env_s) runner.registerInfo("available EnvPeriod: #{env_s}, available ReportingFrequencies: #{freqs}") freqs.each do |freq| vn = sql.availableVariableNames(env_s, freq.to_s) runner.registerInfo(" available ReportingFrequency: #{freq}, available variable names: #{vn}") vn.each do |v| kv = sql.availableKeyValues(env_s, freq.to_s, v) runner.registerInfo(" variable names: #{v}") runner.registerInfo(" available key value: #{kv}") end end end end runner.registerInfo("year: #{years}") runner.registerInfo("seconds: #{seconds}") if !years && seconds # mm/dd hh:mm:ss # check day time splits into two valid parts if !csv[1][0].split(' ')[0].nil? && !csv[1][0].split(' ')[1].nil? # check remaining splits are valid if !csv[1][0].split(' ')[0].split('/')[0].nil? && !csv[1][0].split(' ')[0].split('/')[1].nil? && !csv[1][0].split(' ')[1].split(':')[0].nil? && !csv[1][0].split(' ')[1].split(':')[1].nil? && !csv[1][0].split(' ')[1].split(':')[2].nil? runner.registerInfo("CSV Time format is correct: #{csv[1][0]} mm/dd hh:mm:ss") else runner.registerError("CSV Time format not correct: #{csv[1][0]}. Selected format is mm/dd hh:mm:ss") return false end else runner.registerError("CSV Time format not correct: #{csv[1][0]}. Does not split into 'day time'. Selected format is mm/dd hh:mm:ss") return false end elsif !years && !seconds # mm/dd hh:mm # check day time splits into two valid parts if !csv[1][0].split(' ')[0].nil? && !csv[1][0].split(' ')[1].nil? # check remaining splits are valid if !csv[1][0].split(' ')[0].split('/')[0].nil? && !csv[1][0].split(' ')[0].split('/')[1].nil? && !csv[1][0].split(' ')[1].split(':')[0].nil? && !csv[1][0].split(' ')[1].split(':')[1].nil? runner.registerInfo("CSV Time format is correct: #{csv[1][0]} mm/dd hh:mm") else runner.registerError("CSV Time format not correct: #{csv[1][0]}. Selected format is mm/dd hh:mm") return false end else runner.registerError("CSV Time format not correct: #{csv[1][0]}. Does not split into 'day time'. Selected format is mm/dd hh:mm") return false end elsif years && !seconds # mm/dd/yyyy hh:mm # check day time splits into two valid parts if !csv[1][0].split(' ')[0].nil? && !csv[1][0].split(' ')[1].nil? # check remaining splits are valid if !csv[1][0].split(' ')[0].split('/')[0].nil? && !csv[1][0].split(' ')[0].split('/')[1].nil? && !csv[1][0].split(' ')[0].split('/')[2].nil? && !csv[1][0].split(' ')[1].split(':')[0].nil? && !csv[1][0].split(' ')[1].split(':')[1].nil? runner.registerInfo("CSV Time format is correct: #{csv[1][0]} mm/dd/yyyy hh:mm") else runner.registerError("CSV Time format not correct: #{csv[1][0]}. Selected format is mm/dd/yyyy hh:mm") return false end else runner.registerError("CSV Time format not correct: #{csv[1][0]}. Does not split into 'day time'. Selected format is mm/dd/yyyy hh:mm") return false end elsif years && seconds # mm/dd/yyyy hh:mm:ss # check day time splits into two valid parts if !csv[1][0].split(' ')[0].nil? && !csv[1][0].split(' ')[1].nil? # check remaining splits are valid if !csv[1][0].split(' ')[0].split('/')[0].nil? && !csv[1][0].split(' ')[0].split('/')[1].nil? && !csv[1][0].split(' ')[0].split('/')[2].nil? && !csv[1][0].split(' ')[1].split(':')[0].nil? && !csv[1][0].split(' ')[1].split(':')[1].nil? && !csv[1][0].split(' ')[1].split(':')[2].nil? runner.registerInfo("CSV Time format is correct: #{csv[1][0]} mm/dd/yyyy hh:mm:ss") else runner.registerError("CSV Time format not correct: #{csv[1][0]}. Selected format is mm/dd/yyyy hh:mm:ss") return false end else runner.registerError("CSV Time format not correct: #{csv[1][0]}. Does not split into 'day time'. Selected format is mm/dd/yyyy hh:mm:ss") return false end end temp_sim = [] temp_mtr = [] temp_norm = [] runner.registerInfo('Begin timeseries parsing') # get timezone info tzs = model.getSite.timeZone.to_s runner.registerInfo("timezone = #{tzs}") tz = if tzs.to_i >= 0 # positive number if tzs.to_i < 10 # one digit "+0#{tzs.to_i}:00" else # two digit "+#{tzs.to_i}:00" end else # negative number if tzs.to_i * -1 < 10 # one digit "-0#{tzs.to_i * -1}:00" else # two digit "-#{tzs.to_i * -1}:00" end end runner.registerInfo("timezone = #{tz}") # export for plotting all_series = [] csv[0].each do |hdr| if hdr.to_s != csv_time_header.to_s unless map.key? hdr runner.registerInfo("CSV hdr #{hdr} is not in map: #{map}, skipping") if next end runner.registerInfo("hdr is: #{hdr}") runner.registerInfo("csv_var is: #{csv_var}") # next unless map.key? hdr key = map[hdr][:key] var = map[hdr][:var] diff_index = map[hdr][:index] runner.registerInfo("var: #{var}") runner.registerInfo("key: #{key}") # Store the timeseries data to hash for later # export to the HTML file series = {} series['name'] = "#{key} Simulated" series['type'] = var.to_s series['color'] = 'blue' data = [] series2 = {} series2['name'] = "#{key} Metered" series2['type'] = var.to_s series2['color'] = 'red' data2 = [] environment_periods.each do |environment_p| runner.registerInfo("sqlcall: #{environment_p},#{reporting_frequency},#{var},#{key}") if sql.timeSeries(environment_p, reporting_frequency, var, key).is_initialized ser = sql.timeSeries(environment_p, reporting_frequency, var, key).get else runner.registerWarning("sql.timeSeries not initialized environment_p: #{environment_p},reporting_frequency: #{reporting_frequency},var: #{var},key: #{key}.") next end date_times = ser.dateTimes first_date = date_times[0] last_date = date_times[-1] if date_times.size >= 2 delta = OpenStudio::Time.new((date_times[1] - date_times[0]).days, (date_times[1] - date_times[0]).hours, (date_times[1] - date_times[0]).minutes, (date_times[1] - date_times[0]).seconds) end runner.registerInfo("first_date: #{first_date}") if runner.registerInfo("last_date: #{last_date}") if # if series['units'] = ser.units series2['units'] = ser.units # end # add 0 for plotting if first_zero runner.registerInfo("adding first_zero: #{first_date - delta}") point = {} point['y'] = 0.0 point['time'] = to_JSTime(first_date - delta) data << point point2 = {} point2['y'] = 0.0 point2['time'] = to_JSTime(first_date - delta) data2 << point2 end csv.each_index do |row| next unless row > 0 if csv[row][0].nil? if runner.registerWarning("empty csv row number #{row}") end next end mon = csv[row][0].split(' ')[0].split('/')[0].to_i day = csv[row][0].split(' ')[0].split('/')[1].to_i year = csv[row][0].split(' ')[0].split('/')[2]&.to_i hou = csv[row][0].split(' ')[1].split(':')[0].to_i min = csv[row][0].split(' ')[1].split(':')[1].to_i sec = csv[row][0].split(' ')[1].split(':')[2]&.to_i if year.nil? dat = OpenStudio::Date.new(OpenStudio::MonthOfYear.new(cal[mon]), day) else dat = OpenStudio::Date.new(OpenStudio::MonthOfYear.new(cal[mon]), day, year) end tim = if sec.nil? OpenStudio::Time.new(0, hou, min, 0) else OpenStudio::Time.new(0, hou, min, sec) end dtm = OpenStudio::DateTime.new(dat, tim) unless dtm >= first_date && dtm <= last_date if runner.registerWarning("CSV DateTime #{dtm} is not in SQL Timeseries Dates") end next end if year.nil? etim = if sec.nil? Time.new(2009, mon, day, hou, min, 0, tz).to_i * 1000 else Time.new(2009, mon, day, hou, min, sec, tz).to_i * 1000 end else etim = if sec.nil? # HACK: since year is not in the sql file correctly # etim = Time.new(year, mon, day, hou, min, 0, tz).to_i * 1000 Time.new(2009, mon, day, hou, min, 0, tz).to_i * 1000 else # HACK: since year is not in the sql file correctly # etim = Time.new(year, mon, day, hou, min, sec, tz).to_i * 1000 Time.new(2009, mon, day, hou, min, sec, tz).to_i * 1000 end end runner.registerInfo("dtm: #{dtm}") if csv[row].each_index do |col| next unless col > 0 mtr = csv[row][col].to_s # try converting if convert == 0.5556 # this is a temperature if mtr != 'NAN' mtr = (mtr.to_f - 32) * convert else next # mtr = 0 end else if mtr != 'NAN' mtr = mtr.to_f * convert else next # mtr = 0 end end next unless csv[0][col] == hdr sim = ser.value(dtm) # store timeseries for plotting point = {} point['y'] = sim.round(6) point['time'] = to_JSTime(dtm) data << point point2 = {} point2['y'] = mtr.to_f.round(6) point2['time'] = to_JSTime(dtm) data2 << point2 dif = case norm when 1 scale.to_f * (mtr.to_f - sim.to_f).abs when 2 (scale.to_f * (mtr.to_f - sim.to_f))**2 else scale.to_f * (mtr.to_f - sim.to_f) end squaredError += (mtr.to_f - sim.to_f)**2 sumError += (mtr.to_f - sim.to_f) ySum += mtr.to_f n += 1 temp_sim << [etim, sim.to_f] temp_mtr << [etim, mtr.to_f] # temp_norm << [etim,dif.to_f] diff += dif.to_f simdata += sim.to_f csvdata += mtr.to_f runner.registerInfo("mtr value is #{mtr}") if runner.registerInfo("sim value is #{sim}") if runner.registerInfo("dif value is #{dif}") if runner.registerInfo("diff value is #{diff.inspect}") if end end # add 0 for plotting next unless last_zero runner.registerInfo("add last_zero: #{last_date + delta}") point = {} point['y'] = 0.0 point['time'] = to_JSTime(last_date + delta) data << point point2 = {} point2['y'] = 0.0 point2['time'] = to_JSTime(last_date + delta) data2 << point2 end series['displayname'] = plot_name series2['displayname'] = plot_name series['data'] = data series2['data'] = data2 all_series << series all_series << series2 yBar = [ySum / n, 1e-19].max cvrmse = 100.0 * Math.sqrt(squaredError / n) / yBar nmbe = 100.0 * (sumError / n) / yBar series['cvrmse'] = cvrmse.round(6) series['nmbe'] = nmbe.round(6) series2['cvrmse'] = cvrmse.round(6) series2['nmbe'] = nmbe.round(6) if algorithm_download require 'csv' CSV.open("timeseries_#{plot_name}.csv", 'wb') do |csv| csv << ['Simulation Time', 'Simulated Value', 'Metered time', 'Metered Value'] data.size.times do |i| csv << [data[i]['time'], data[i]['y'], data2[i]['time'], data2[i]['y']] end end end else runner.registerInfo("Found Time Header: #{csv_time_header}") end end # results = {"metadata" => {"tz" => tzs.to_i, "variables" => {"variable" => csv_var, "variable_display_name" => csv_var_dn}}, "data_mtr" => temp_mtr, "data_sim" => temp_sim, "data_diff" => temp_norm} # remove diff norm from results json results = { 'metadata' => { 'tz' => tzs.to_i, 'variables' => { 'variable' => csv_var, 'variable_display_name' => csv_var_dn } }, 'data_mtr' => temp_mtr, 'data_sim' => temp_sim } runner.registerInfo("Saving timeseries_#{csv_var}.json") FileUtils.mkdir_p(File.dirname("timeseries_#{csv_var}.json")) unless Dir.exist?(File.dirname("timeseries_#{csv_var}.json")) File.open("timeseries_#{csv_var}.json", 'wb') { |f| f << JSON.pretty_generate(results) } FileUtils.mkdir_p(File.dirname("allseries_#{csv_var}.json")) unless Dir.exist?(File.dirname("allseries_#{csv_var}.json")) File.open("allseries_#{csv_var}.json", 'wb') { |f| f << JSON.pretty_generate(all_series) } # check if analysis directory exists on server if algorithm_download && (File.basename(File.(File.join(Dir.pwd, '../../../'))).split('_')[0] == 'analysis') runner.registerInfo("Copying timeseries_#{csv_var}.json to downloads directory") directory_name = File.(File.join(Dir.pwd, '../../../downloads')) Dir.mkdir(directory_name) unless File.exist?(directory_name) FileUtils.cp("timeseries_#{csv_var}.json", directory_name) FileUtils.cp("allseries_#{csv_var}.json", directory_name) FileUtils.cp("timeseries_#{plot_name}.csv", directory_name) end diff = Math.sqrt(diff) if norm == 2 runner.registerInfo("results: #{results}") if runner.registerValue('diff', diff, '') runner.registerValue('simdata', simdata, '') runner.registerValue('csvdata', csvdata, '') runner.registerValue('cvrmse', cvrmse, '') runner.registerValue('nmbe', nmbe, '') if plot_flag runner.registerInfo('start plotting') all_series = all_series.to_json # read in template html_in_path = "#{File.dirname(__FILE__)}/resources/report.html.erb" html_in_path = if File.exist?(html_in_path) html_in_path else "#{File.dirname(__FILE__)}/report.html.erb" end html_in = '' File.open(html_in_path, 'r') do |file| html_in = file.read end # configure template with variable values renderer = ERB.new(html_in) html_out = renderer.result(binding) # write html file html_out_path = if plot_name.empty? "./report_#{csv_var}.html" else "./report_#{plot_name}.html" end File.open(html_out_path, 'w') do |file| file << html_out # make sure data is written to the disk one way or the other begin file.fsync rescue StandardError file.flush end end end sql.close true end |
#to_JSTime(os_time) ⇒ Object
Method to translate from OpenStudio’s time formatting to Javascript time formatting OpenStudio time 2009-May-14 00:10:00 Raw string Javascript time 2009/07/12 12:34:56
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/measures/TimeseriesObjectiveFunction/measure.rb', line 277 def to_JSTime(os_time) js_time = os_time.to_s # Replace the '-' with '/' js_time = js_time.tr('-', '/') # Replace month abbreviations with numbers js_time = js_time.gsub('Jan', '01') js_time = js_time.gsub('Feb', '02') js_time = js_time.gsub('Mar', '03') js_time = js_time.gsub('Apr', '04') js_time = js_time.gsub('May', '05') js_time = js_time.gsub('Jun', '06') js_time = js_time.gsub('Jul', '07') js_time = js_time.gsub('Aug', '08') js_time = js_time.gsub('Sep', '09') js_time = js_time.gsub('Oct', '10') js_time = js_time.gsub('Nov', '11') js_time.gsub('Dec', '12') end |