Purves, Murray committed Oct 25, 2018 1 2 3 4 5 6 7 8 9 ``````/* * Example utility to convert a vertical profile of meteorological * data to the ARL format */ #include #include #include `````` Purves, Murray committed Oct 25, 2018 10 11 ``````#include "radixcommand/commandline.hh" #include "radixcore/stringfunctions.hh" `````` Purves, Murray committed Oct 26, 2018 12 ``````#include "radixmath/util.hh" `````` Purves, Murray committed Oct 25, 2018 13 `````` `````` Purves, Murray committed Oct 25, 2018 14 ``````#include "radixio/arldatastream.hh" `````` Purves, Murray committed Oct 25, 2018 15 16 17 ``````#include "radixio/csvfile.hh" using namespace radix; `````` Purves, Murray committed Oct 25, 2018 18 `````` `````` Purves, Murray committed Oct 26, 2018 19 20 21 22 23 24 25 26 27 28 29 30 31 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 ``````/** * @brief interpolateValues Interpolate/extrapolate missing values from a data * vector Basic capability for now - linear interpolation, extrapolation assumes * constant from ends. * @param pressureValues Pressure vector for meteorology - requires all values * to be present. Used for determining 'interpolation bounds' (there's * definitely a better phrase for this) * @param valuesToInterpolate */ bool interpolateValues(const std::vector &pressureValues, std::vector &valuesToInterpolate) { float missingValue = -9999.f; // Ensure pressure and other vector are same length for interpolation if (pressureValues.size() != valuesToInterpolate.size()) { std::cout << "Error! Pressure vector is not same size (" << pressureValues.size() << ") as vector to interpolate (" << valuesToInterpolate.size() << ") - can't use to interpolate" << std::endl; return false; } // Ensure we have no missing values in the pressure dataset for (float pressure : pressureValues) { if (pressure == -9999.f) { std::cout << "Error! Pressure vector contains " << missingValue << " values - can't use to interpolate" << std::endl; return false; } } // Write out initial values std::cout << "Interpolating " << valuesToInterpolate.size() << " values; initial:" << std::endl << " "; for (float f : valuesToInterpolate) { std::cout << f << " "; } std::cout << std::endl; // Loop through vector first to find first/last non-missing indices size_t firstIndex = valuesToInterpolate.size(), lastIndex = 0; bool foundFirst = false; for (size_t i = 0; i < valuesToInterpolate.size(); ++i) { if (valuesToInterpolate[i] != missingValue) { if (!foundFirst) { firstIndex = i; foundFirst = true; } lastIndex = i; } } // Fill in values before first and after last indices std::cout << " Found first (" << firstIndex << ") and last (" << lastIndex << ") non-missing indices; filling in values before & after..." << std::endl; for (size_t i = 0; i < firstIndex; ++i) { valuesToInterpolate[i] = valuesToInterpolate[firstIndex]; } for (size_t i = lastIndex + 1; i < valuesToInterpolate.size(); ++i) { valuesToInterpolate[i] = valuesToInterpolate[lastIndex]; } // Fill in missing values in central parts of vector std::cout << " Filling in missing data in central part of vector..." << std::endl; for (size_t i = firstIndex + 1; i < lastIndex; ++i) { // Search for a missing value size_t lastGood = i - 1; if (valuesToInterpolate[i] == missingValue) { // Get the next good value while (valuesToInterpolate[i] == missingValue) { i++; } size_t nextGood = i; // Interpolate between the two good values for (size_t j = lastGood + 1; j < nextGood; ++j) { valuesToInterpolate[j] = valuesToInterpolate[lastGood] + ((valuesToInterpolate[nextGood] - valuesToInterpolate[lastGood]) * ((pressureValues[j] - pressureValues[lastGood]) / (pressureValues[nextGood] - pressureValues[lastGood]))); } } } // Write out final values std::cout << "Interpolation complete; final:" << std::endl << " "; for (float f : valuesToInterpolate) { std::cout << f << " "; } std::cout << std::endl; return true; } `````` Purves, Murray committed Oct 25, 2018 130 131 ``````int main(int argc, char **argv) { `````` Purves, Murray committed Oct 25, 2018 132 133 134 135 136 137 138 139 140 141 142 `````` std::cout << "************************" << std::endl; std::cout << "***** radixsnd2arl *****" << std::endl; std::cout << "************************" << std::endl; // Set up command line options CommandLine commandLine(argc, argv); commandLine.addOption("i", "Input csv file containing met data", true); commandLine.addOption("clat", "Centre latitude of output ARL file (degrees)", true); commandLine.addOption("clon", "Centre longitude of output ARL file (degrees)", true); `````` Purves, Murray committed Oct 30, 2018 143 144 `````` commandLine.addOption("e", "Extent of output ARL file (km) [500]", false); commandLine.addOption("r", "Resolution of output ARL file (km) [10]", false); `````` Purves, Murray committed Oct 25, 2018 145 `````` commandLine.addOption("t", "Time of data start (YYYYMMDDHH)", true); `````` Purves, Murray committed Oct 30, 2018 146 147 `````` commandLine.addOption("n", "Number of one hour timesteps to output [1]", false); `````` Purves, Murray committed Oct 25, 2018 148 149 150 151 152 `````` commandLine.addOption("o", "Output ARL file", false); // Ensure required options present std::vector commandErrors; if (!commandLine.validate(commandErrors)) `````` Purves, Murray committed Oct 25, 2018 153 `````` { `````` Purves, Murray committed Oct 25, 2018 154 155 156 157 158 159 160 161 `````` std::cout << "Error in arguments..." << std::endl; for (std::string error : commandErrors) { std::cout << "\t" << error << std::endl; } std::cout << std::endl; commandLine.printParsedLine(std::cout); `````` Purves, Murray committed Oct 25, 2018 162 163 `````` return -1; } `````` Purves, Murray committed Oct 25, 2018 164 165 166 167 168 `````` // Get command line options std::string inputCsvPath = commandLine.get("i"); std::string outputArlPath = commandLine.get("o", inputCsvPath + ".bin"); `````` Purves, Murray committed Oct 30, 2018 169 170 `````` float extent = commandLine.get("e", 500.0); float resolution = commandLine.get("r", 10.0); `````` Purves, Murray committed Oct 25, 2018 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 `````` float centreLat = commandLine.get("clat"); float centreLon = commandLine.get("clon"); int numberTimesteps = commandLine.get("n", 1); std::string startTime = commandLine.get("t"); // Get the grid size int numberGridCells = (int)(extent / resolution); if (!numberGridCells % 2 == 1) { numberGridCells++; } // Parse the start time // TODO: Check string is the right length int year = from_string(startTime.substr(0, 4), 1951); int month = from_string(startTime.substr(4, 2), 11); int day = from_string(startTime.substr(6, 2), 19); int hour = from_string(startTime.substr(8, 2), 17); std::cout << "Creating ARL-formatted met file with parameters:" << std::endl; `````` Purves, Murray committed Oct 30, 2018 189 `````` std::cout << " " << extent << " by " << extent << " km grid centred on (" `````` Purves, Murray committed Oct 25, 2018 190 191 192 193 194 195 196 197 198 199 200 201 `````` << centreLat << "," << centreLon << ") with resolution " << resolution << " (" << numberGridCells << " cells in each direction)" << std::endl; std::cout << " " << numberTimesteps << " timesteps (of 1 hour each) starting from " << year << "-" << month << "-" << day << " at " << hour << "00" << std::endl << std::endl; // If we have correct options, parse input file std::cout << "Reading input csv file: " << inputCsvPath << std::endl; // Header strings to denote fields `````` Purves, Murray committed Oct 26, 2018 202 `````` std::string pressureString = "pres", tempString = "temp", relHumString = "rh", `````` 203 204 `````` dewPtString = "tdew", wDirString = "wdir", wSpdString = "wspd", heightString = "zhgt"; `````` Purves, Murray committed Oct 26, 2018 205 `````` int pressureIndex = -1, tempIndex = -1, relHumIndex = -1, wDirIndex = -1, `````` 206 `````` wSpdIndex = -1, heightIndex = -1; `````` Purves, Murray committed Oct 25, 2018 207 208 209 210 211 212 213 214 215 216 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 `````` // Open and read the csv CSVFile inputCsv(inputCsvPath); std::vector> inputData; bool inputReadSuccess = inputCsv.data(inputData); if (!inputReadSuccess) { std::cout << "Failed to read input csv!" << std::endl; std::cout << "Please ensure the file exists and has the appropriate permissions." << std::endl; return -2; } // Ensure there is data in here if (inputData.size() < 5) { std::cout << "Not enough data in input csv!" << std::endl; std::cout << "There are only " << inputData.size() << " lines in this file - at least 5 are required (4 headers + data)" << std::endl; return -3; } // Work out which fields are which data points - should be 2nd line (1) std::cout << "Finding data fields..." << std::endl; for (int entry = 0; entry < inputData[1].size(); ++entry) { if (inputData[1][entry] == pressureString) { std::cout << " Found pressure at index " << entry << std::endl; pressureIndex = entry; } else if (inputData[1][entry] == tempString) { std::cout << " Found temperature at index " << entry << std::endl; tempIndex = entry; } else if (inputData[1][entry] == relHumString) { std::cout << " Found relative humidity at index " << entry << std::endl; relHumIndex = entry; } else if (inputData[1][entry] == wDirString) { std::cout << " Found wind direction at index " << entry << std::endl; wDirIndex = entry; } else if (inputData[1][entry] == wSpdString) { std::cout << " Found wind speed at index " << entry << std::endl; wSpdIndex = entry; } `````` 260 261 262 263 264 `````` else if (inputData[1][entry] == heightString) { std::cout << " Found height at index " << entry << std::endl; heightIndex = entry; } `````` Purves, Murray committed Oct 25, 2018 265 266 `````` } // If we are missing one, we can't continue `````` Purves, Murray committed Oct 26, 2018 267 268 `````` if (pressureIndex == -1 || tempIndex == -1 || relHumIndex == -1 || wDirIndex == -1 || wSpdIndex == -1) `````` Purves, Murray committed Oct 25, 2018 269 `````` { `````` Purves, Murray committed Oct 26, 2018 270 `````` std::cout << "Missing data fields in input!" << std::endl; `````` Purves, Murray committed Oct 25, 2018 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 `````` if (pressureIndex == -1) { std::cout << " Missing pressure field (denoted by " << pressureString << ")"; } if (tempIndex == -1) { std::cout << " Missing temperature field (denoted by " << tempString << ")"; } if (relHumIndex == -1) { std::cout << " Missing relative humidity field (denoted by " << relHumString << ")"; } if (wDirIndex == -1) { std::cout << " Missing wind direction field (denoted by " << wDirString << ")"; } if (wSpdIndex == -1) { std::cout << " Missing wind speed field (denoted by " << wSpdString << ")"; } `````` 296 297 298 299 `````` if (heightIndex == -1) { std::cout << " Missing height field (denoted by " << heightString << ")"; } `````` Purves, Murray committed Oct 25, 2018 300 301 302 303 304 `````` std::cout << "Please ensure these fields are present and rerun."; return -4; } // Read the data `````` Purves, Murray committed Oct 26, 2018 305 `````` std::vector inputPressures, inputTemps, inputRelHums, inputWDirs, `````` 306 `````` inputWSpds, inputHeights; `````` Purves, Murray committed Oct 25, 2018 307 308 309 310 311 312 313 314 315 316 317 `````` std::cout << "Data fields found - reading data..." << std::endl; float missingValue = -9999.f; for (int row = 4; row < inputData.size(); ++row) { if (inputData[row].size() < inputData[1].size()) { std::cout << " Warning: this row (" << row + 1 << ") has less entries than the field names row" << std::endl; ; } `````` Purves, Murray committed Oct 26, 2018 318 `````` bool foundPressure = false, foundTemp = false, foundRelHum = false, `````` 319 `````` foundWDir = false, foundWSpd = false, foundHeight = false; `````` Purves, Murray committed Oct 25, 2018 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 `````` for (int entry = 0; entry < inputData[row].size(); ++entry) { float thisValue = from_string(inputData[row][entry], missingValue); if (entry == pressureIndex) { foundPressure = true; inputPressures.push_back(thisValue); } else if (entry == tempIndex) { foundTemp = true; inputTemps.push_back(thisValue); } else if (entry == relHumIndex) { foundRelHum = true; inputRelHums.push_back(thisValue); } else if (entry == wSpdIndex) { foundWSpd = true; inputWSpds.push_back(thisValue); } else if (entry == wDirIndex) { foundWDir = true; inputWDirs.push_back(thisValue); } `````` 349 350 351 352 353 `````` else if (entry == heightIndex) { foundHeight = true; inputHeights.push_back(thisValue); } `````` Purves, Murray committed Oct 25, 2018 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 `````` } // Add missing data if any of the above weren't found if (!foundPressure) { std::cout << " Warning: couldn't find pressure in row " << row << std::endl; inputPressures.push_back(missingValue); } if (!foundTemp) { std::cout << " Warning: couldn't find temperature in row " << row << std::endl; inputTemps.push_back(missingValue); } if (!foundRelHum) { std::cout << " Warning: couldn't find relative humidity in row " << row << std::endl; inputRelHums.push_back(missingValue); } if (!foundWSpd) { std::cout << " Warning: couldn't find wind speed in row " << row << std::endl; inputWSpds.push_back(missingValue); } if (!foundWDir) { std::cout << " Warning: couldn't find wind direction in row " << row << std::endl; inputWDirs.push_back(missingValue); } `````` 386 387 388 389 390 391 `````` if (!foundHeight) { std::cout << " Warning: couldn't find height in row " << row << std::endl; inputHeights.push_back(missingValue); } `````` Purves, Murray committed Oct 25, 2018 392 393 394 395 `````` } std::cout << "Data read complete: " << inputPressures.size() << " entries read." << std::endl; `````` Purves, Murray committed Oct 26, 2018 396 397 398 399 400 401 402 403 404 405 `````` // Interpolate the data to remove missing values std::cout << "Interpolating values to remove missing data..." << std::endl; std::cout << "Temperature:" << std::endl; interpolateValues(inputPressures, inputTemps); std::cout << "Relative humidity:" << std::endl; interpolateValues(inputPressures, inputRelHums); std::cout << "Wind speed:" << std::endl; interpolateValues(inputPressures, inputWSpds); std::cout << "Wind direction:" << std::endl; interpolateValues(inputPressures, inputWDirs); `````` 406 407 `````` std::cout << "Height:" << std::endl; interpolateValues(inputPressures, inputHeights); `````` Purves, Murray committed Oct 26, 2018 408 409 `````` std::cout << "Interpolation complete." << std::endl; `````` Purves, Murray committed Oct 25, 2018 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 `````` // Convert the data into ARL format std::cout << "Converting data to ARL format..." << std::endl; ARLDataStream outputStream(outputArlPath, std::ios::out); for (int timestep = 0; timestep < numberTimesteps; ++timestep) { // Write index header section std::cout << " Writing index headers for timestep " << timestep << "..." << std::endl; ARLRecordHeader thisRecordHeader; thisRecordHeader.year = year; thisRecordHeader.month = month; thisRecordHeader.day = day; thisRecordHeader.hour = hour + timestep; thisRecordHeader.ic = 0; thisRecordHeader.il = 0; thisRecordHeader.cgrid = "99"; thisRecordHeader.kvar = "INDX"; thisRecordHeader.nexp = 0; thisRecordHeader.prec = 0.f; thisRecordHeader.var1 = 0.f; ARLIndexHeader thisIndexHeader; `````` Purves, Murray committed Oct 29, 2018 431 432 433 434 435 436 437 `````` thisIndexHeader.model_id = "NFDB"; thisIndexHeader.icx = 0; thisIndexHeader.mn = 0; thisIndexHeader.pole_lat = centreLat; thisIndexHeader.pole_lon = centreLon; thisIndexHeader.ref_lat = centreLat; thisIndexHeader.ref_lon = centreLon; `````` Purves, Murray committed Oct 30, 2018 438 `````` thisIndexHeader.size = resolution; `````` Purves, Murray committed Oct 29, 2018 439 440 441 442 443 444 445 446 447 448 449 450 451 452 `````` thisIndexHeader.orient = 0.f; thisIndexHeader.tang_lat = centreLat; thisIndexHeader.sync_xp = (numberGridCells + 1) / 2; thisIndexHeader.sync_yp = (numberGridCells + 1) / 2; thisIndexHeader.sync_lat = centreLat; thisIndexHeader.sync_lon = centreLon; thisIndexHeader.dummy = 0.f; thisIndexHeader.nx = numberGridCells; thisIndexHeader.ny = numberGridCells; thisIndexHeader.nz = inputPressures.size(); thisIndexHeader.z_flag = 2; thisIndexHeader.lenh = numberGridCells * numberGridCells; thisIndexHeader.levels = inputPressures; thisIndexHeader.nVarsAtLevels = std::vector(inputPressures.size(), 5); `````` Purves, Murray committed Oct 29, 2018 453 454 455 456 457 458 459 460 461 462 463 464 465 466 `````` std::vector surfaceVarNames = {"PRSS", "TEMP", "RELH", "UWND", "VWND"}; std::vector varNames = {"HGTS", "TEMP", "RELH", "UWND", "VWND"}; thisIndexHeader.varNames.push_back(surfaceVarNames); for (size_t level = 1; level < inputPressures.size(); ++level) { thisIndexHeader.varNames.push_back(varNames); } std::vector checkSums = std::vector(5, 0); for (size_t level = 0; level < inputPressures.size(); ++level) { thisIndexHeader.checkSums.push_back(checkSums); } `````` Purves, Murray committed Oct 29, 2018 467 `````` `````` Purves, Murray committed Oct 25, 2018 468 469 470 471 472 473 474 475 476 477 478 `````` // Write the headers outputStream.write_record_header(thisRecordHeader); outputStream.write_index_header(thisRecordHeader, thisIndexHeader); std::cout << " Headers written" << std::endl; // Write meteorological variables for each level for (int level = 0; level < inputPressures.size(); ++level) { std::cout << " Writing meteorological data for timestep " << timestep << ", level " << level << "..." << std::endl; `````` 479 480 481 `````` thisRecordHeader.il = level; // Write pressure/height variables `````` Purves, Murray committed Oct 25, 2018 482 `````` { `````` Purves, Murray committed Oct 29, 2018 483 484 485 `````` if (level == 0) { thisRecordHeader.kvar = "PRSS"; `````` 486 487 488 489 490 491 492 493 494 `````` thisRecordHeader.var1 = inputPressures[level]; std::vector> thisData( numberGridCells, std::vector(numberGridCells, inputPressures[level])); std::cout << " pressure..."; outputStream.write_record_header(thisRecordHeader); outputStream.write_record(thisRecordHeader, thisIndexHeader, thisData); std::cout << "written" << std::endl; `````` Purves, Murray committed Oct 29, 2018 495 496 497 `````` } else { `````` 498 499 500 501 502 503 504 505 506 507 `````` thisRecordHeader.kvar = "HGTS"; thisRecordHeader.var1 = inputHeights[level]; std::vector> thisData( numberGridCells, std::vector(numberGridCells, inputHeights[level])); std::cout << " height..."; outputStream.write_record_header(thisRecordHeader); outputStream.write_record(thisRecordHeader, thisIndexHeader, thisData); std::cout << "written" << std::endl; `````` Purves, Murray committed Oct 29, 2018 508 `````` } `````` Purves, Murray committed Oct 25, 2018 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 `````` } // Write temperature levels { thisRecordHeader.kvar = "TEMP"; thisRecordHeader.var1 = inputTemps[level]; std::vector> thisData( numberGridCells, std::vector(numberGridCells, inputTemps[level])); std::cout << " temperature..."; outputStream.write_record_header(thisRecordHeader); outputStream.write_record(thisRecordHeader, thisIndexHeader, thisData); std::cout << "written" << std::endl; } // Write relative humidity levels { thisRecordHeader.kvar = "RELH"; thisRecordHeader.var1 = inputRelHums[level]; std::vector> thisData( numberGridCells, std::vector(numberGridCells, inputRelHums[level])); std::cout << " relative humidity..."; outputStream.write_record_header(thisRecordHeader); outputStream.write_record(thisRecordHeader, thisIndexHeader, thisData); std::cout << "written" << std::endl; } // Write wind data // Need to calculate wind u and v components from direction/speed float thisWindU = `````` Purves, Murray committed Oct 29, 2018 540 541 `````` (inputWSpds[level] * 0.5144444444) * sin(toRadians(fmod((inputWDirs[level] + 180.0), 360.0))); `````` Purves, Murray committed Oct 25, 2018 542 `````` float thisWindV = `````` Purves, Murray committed Oct 29, 2018 543 544 `````` (inputWSpds[level] * 0.5144444444) * cos(toRadians(fmod((inputWDirs[level] + 180.0), 360.0))); `````` Purves, Murray committed Oct 26, 2018 545 `````` std::cout << " Initial wspd = " << inputWSpds[level] `````` Purves, Murray committed Oct 26, 2018 546 547 `````` << " knots, wdir: " << inputWDirs[level] << " degrees" << std::endl; `````` Purves, Murray committed Oct 26, 2018 548 `````` std::cout << " Converted wind components: u = " << thisWindU `````` Purves, Murray committed Oct 26, 2018 549 `````` << " m/s, v = " << thisWindV << "m/s" << std::endl; `````` Purves, Murray committed Oct 25, 2018 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 `````` { thisRecordHeader.kvar = "UWND"; thisRecordHeader.var1 = thisWindU; std::vector> thisData( numberGridCells, std::vector(numberGridCells, thisWindU)); std::cout << " wind (u component)..."; outputStream.write_record_header(thisRecordHeader); outputStream.write_record(thisRecordHeader, thisIndexHeader, thisData); std::cout << "written" << std::endl; } { thisRecordHeader.kvar = "VWND"; thisRecordHeader.var1 = thisWindV; std::vector> thisData( numberGridCells, std::vector(numberGridCells, thisWindV)); std::cout << " wind (v component)..."; outputStream.write_record_header(thisRecordHeader); outputStream.write_record(thisRecordHeader, thisIndexHeader, thisData); std::cout << "written" << std::endl; } } std::cout << " Written timestep " << timestep << std::endl; } std::cout << "File write complete!" << std::endl; `````` Purves, Murray committed Oct 25, 2018 576 577 `````` return 0; }``````