/** * @param string $duneFeature The dune feature against which to compare the max TWL. Accepts "toe' or 'crest' * as inputs. * * @return bool True if the Max TWL exceeds the height of the goven feature. * @throws InvalidOperationException If a time series has not been loaded into the dune object. * @throws MyInvalidArgumentException If the $duneFeature string contains an invalid value. */ public final function maxTwlExceeds($duneFeature) { if (is_null($this->timeSeries)) { throw new InvalidOperationException('Total water level data has not been loaded. Maximum level cannot be determined.'); } $maxTwlData = $this->timeSeries->maxTwl(); switch ($duneFeature) { case 'toe': if ($maxTwlData['height'] > $this->toeHeight) { return TRUE; } else { return FALSE; } break; case 'crest': if ($maxTwlData['height'] > $this->crestHeight) { return TRUE; } else { return FALSE; } break; default: throw new MyInvalidArgumentException('INPUT: target. An invalid input was detected for the target. Target may be either "toe" or "crest"'); break; } }
/** * Parses a TWL file for data. * * @param string $site * @param string $fileDateTime The file date time in the format YYYYMMDDtHHMMSS * @param resource $fileHandle Te file handle to the file containing the data to be parsed. * @param bool $errorOnBadData If true then bad parsed data will result in an exception. If false the data * will be skipped. * * @throws InvalidOperationException * @throws MyInvalidArgumentException */ protected static final function importTwlFile($site, $fileDateTime, $fileHandle, $errorOnBadData = TRUE) { if (is_null(self::$databaseCredentials)) { throw new InvalidOperationException('Database credentials must be set at the class level to allow this action to take place.'); } /** @var TwlTimeSeries $currentTimeSeries */ $currentTimeSeries = NULL; $whitespacePattern = '/\\s+/'; $twlFile = new TwlFile($site, $fileDateTime); $line = 0; print $twlFile->getSiteName() . '<br>'; print $twlFile->getFileDateTime() . '<br>'; while (!feof($fileHandle)) { $line++; $twlFileLine = fgets($fileHandle); $twlFileLine = trim($twlFileLine); $twlFileLineData = preg_split($whitespacePattern, $twlFileLine); if ($twlFileLineData[0] == NULL || substr($twlFileLineData[0], 0, 1) == '%') { continue; // Skip the line } if (count($twlFileLineData) != 16) { if ($errorOnBadData) { throw new MyInvalidArgumentException('Import failed. Line ' . $line . ' does not contain sufficient data or is improperly formatted.'); } else { continue; // Skip the line. } } try { $position = new LatLng($twlFileLineData[2], $twlFileLineData[1]); $dataPoint = new TwlDataPoint($twlFileLineData[0], $position, $twlFileLineData[3], $twlFileLineData[4], $twlFileLineData[6], $twlFileLineData[8], $twlFileLineData[7], $twlFileLineData[9], $twlFileLineData[11], $twlFileLineData[10], $twlFileLineData[12], $twlFileLineData[13], $twlFileLineData[14], $twlFileLineData[15]); if (is_null($currentTimeSeries) || !$currentTimeSeries->getPosition()->isEqualTo($position)) { if (isset($currentTimeSeries)) { $currentTimeSeries->sortDataPoints(); } $currentTimeSeries = new TwlTimeSeries($position, $twlFileLineData[5]); $twlFile->addTimeSeries($currentTimeSeries); } $currentTimeSeries->addDataPoint($dataPoint); } catch (Exception $e) { if ($errorOnBadData) { throw new MyInvalidArgumentException('Import failed. Line ' . $line . ' contains invalid data.', $e); } else { continue; } } } $DBH = call_user_func_array('DatabaseHelper::getInstance', self::$databaseCredentials); $error = FALSE; $DBH->beginTransaction(); $fileId = $twlFile->saveFile(); if ($fileId) { TwlTimeSeries::setDatabaseCredentials(self::$databaseCredentials); TwlDataPoint::setDatabaseCredentials(self::$databaseCredentials); foreach ($twlFile->timeSeries as $individualTimeSeries) { if ($timeSeriesId = $individualTimeSeries->saveTimeSeries()) { foreach ($individualTimeSeries as $individualDataPoint) { if (!$individualDataPoint->saveDataPoint($fileId, $timeSeriesId)) { print 'twlDataPoint could not be saved to DB'; $error = TRUE; break 2; } } } else { print $timeSeriesId; print 'twlTimeSeries could not be saved to DB<br>'; $error = TRUE; break; } } $siteLastImportQuery = <<<MYSQL UPDATE sites SET last_import = :importTime WHERE site_id = :selectedSiteId MYSQL; $tempDateTime = new DateTime($fileDateTime, new DateTimeZone('UTC')); $siteLastImportParams = array('importTime' => $tempDateTime->format('Y-m-d H:i:s'), 'selectedSiteId' => Site::getIdByName($site)); DatabaseHelper::query($DBH, $siteLastImportQuery, $siteLastImportParams); } elseif ($fileId === '0') { print 'twlFile already exists in the DB.'; $error = TRUE; } else { print 'twlFile could not be saved to DB'; $error = TRUE; } if ($error) { $DBH->rollBack(); } else { $DBH->commit(); } }
<?php /** * Provides functionality to return a dune time series for graphing. * * Project: twlProject * File: loadDune.php * Created: 3/6/2016 5:19 PM * * @author Richard Snell <*****@*****.**> */ include_once '../includes/globalConfig.php'; Dune::setDatabaseCredentials($twlProjectCredentials); GeographicalFeature::setDatabaseCredentials($geonamesCredentials); TwlTimeSeries::setDatabaseCredentials($twlProjectCredentials); $DBH = call_user_func_array('DatabaseHelper::getInstance', $twlProjectCredentials); $timeSeriesId = filter_input(INPUT_GET, 'timeSeriesId', FILTER_VALIDATE_INT); $fileId = filter_input(INPUT_GET, 'fileId', FILTER_VALIDATE_INT); $duneId = filter_input(INPUT_GET, 'duneId', FILTER_VALIDATE_INT); if (empty($timeSeriesId) || empty($fileId) || empty($duneId)) { print 'empty'; exit; } $dune = Dune::loadById($duneId); $location = new GeographicalFeature($dune->getPosition()); $location->reverseGeocode(); $dune->setTimeSeries(TwlTimeSeries::loadTimeSeries($fileId, $timeSeriesId)); $returnData = $dune->exportDune(); $returnData['impEvt'] = $dune->duneImpactingEvents(); $returnData['dune']['location'] = $location->getCity(); print json_encode($returnData);