def pArchiveFile(filename): """ Move the file to the archive directory (if specified), inserting a timestamp in the name. :param str filename: Full path of the file to be archived. :return: `True` if the file is successfully moved, `False` otherwise. :rtype: bool """ path, ext = os.path.splitext(filename) path, base = os.path.split(path) archive_dir = pArchiveDir() ext = ext.lstrip('.') if archive_dir: if os.path.isdir(archive_dir): pass else: try: os.makedirs(archive_dir) except OSError: LOGGER.critcal("Cannot create %s", archive_dir) raise if pArchiveTimestamp(): archive_date = flModDate(filename, GLOBAL_DATEFMT) archive_file_name = pjoin(archive_dir, "%s.%s.%s" % (base, archive_date, ext)) else: archive_file_name = pjoin(archive_dir, "%s.%s" % (base, ext)) rc = pMoveFile(filename, archive_file_name) return rc
def testModDate(self): """Test flModDate function""" from time import localtime, strftime testdate = localtime(os.stat(self.filename).st_mtime) testdatestr = strftime('%Y-%m-%dT%H:%M:%S', testdate) self.assertEqual(testdatestr, files.flModDate(self.filename, '%Y-%m-%dT%H:%M:%S'))
def version(): """ Check version of TCRM code. This returns the full hash of the git commit, if git is available on the system. Otherwise, it returns the last modified date of this file. It's assumed that this would be the case if a user downloads the zip file from the git repository. """ vers = '' try: vers = git('log -1 --date=iso --pretty=format:"%ad %H"') except subprocess.CalledProcessError: log.info(("Unable to obtain version information " "- version string will be last modified date of code")) vers = flModDate(abspath(__file__), "%Y-%m-%d_%H:%M") return vers
def saveGustToFile(self, trackfile, result, filename): """ Save gusts to a file. """ lat, lon, speed, Vx, Vy, P = result trackfileDate = flModDate(trackfile) gatts = { 'title': 'TCRM hazard simulation - synthetic event wind field', 'tcrm_version': flProgramVersion(), 'python_version': sys.version, 'track_file': trackfile, 'track_file_date': trackfileDate, 'radial_profile': self.profileType, 'boundary_layer': self.windFieldType, 'beta': self.beta } # Add configuration settings to global attributes: for section in self.config.sections(): for option in self.config.options(section): key = "{0}_{1}".format(section, option) value = self.config.get(section, option) gatts[key] = value dimensions = { 0: { 'name': 'lat', 'values': lat, 'dtype': 'float64', 'atts': { 'long_name': 'Latitude', 'standard_name': 'latitude', 'units': 'degrees_north', 'axis': 'Y' } }, 1: { 'name': 'lon', 'values': lon, 'dtype': 'float64', 'atts': { 'long_name': 'Longitude', 'standard_name': 'longitude', 'units': 'degrees_east', 'axis': 'X' } } } variables = { 0: { 'name': 'vmax', 'dims': ('lat', 'lon'), 'values': speed, 'dtype': 'float32', 'atts': { 'long_name': 'Maximum 0.2-second gust wind speed', 'standard_name': 'wind_speed_of_gust', 'units': 'm/s', 'actual_range': (np.min(speed), np.max(speed)), 'valid_range': (0.0, 200.), 'cell_methods': ('time: maximum ', 'time: maximum (interval: 0.2 seconds)'), 'grid_mapping': 'crs' } }, 1: { 'name': 'ua', 'dims': ('lat', 'lon'), 'values': Vx, 'dtype': 'float32', 'atts': { 'long_name': 'Eastward component of maximum wind speed', 'standard_name': 'eastward_wind', 'units': 'm/s', 'actual_range': (np.min(Vx), np.max(Vx)), 'valid_range': (-200., 200.), 'grid_mapping': 'crs' } }, 2: { 'name': 'va', 'dims': ('lat', 'lon'), 'values': Vy, 'dtype': 'float32', 'atts': { 'long_name': 'Northward component of maximim wind speed', 'standard_name': 'northward_wind', 'units': 'm/s', 'actual_range': (np.min(Vy), np.max(Vy)), 'valid_range': (-200., 200.), 'grid_mapping': 'crs' } }, 3: { 'name': 'slp', 'dims': ('lat', 'lon'), 'values': P, 'dtype': 'float32', 'atts': { 'long_name': 'Minimum air pressure at sea level', 'standard_name': 'air_pressure_at_sea_level', 'units': 'Pa', 'actual_range': (np.min(P), np.max(P)), 'valid_range': (70000., 115000.), 'cell_methods': 'time: minimum', 'grid_mapping': 'crs' } }, 4: { 'name': 'crs', 'dims': (), 'values': None, 'dtype': 'i', 'atts': { 'grid_mapping_name': 'latitude_longitude', 'semi_major_axis': 6378137.0, 'inverse_flattening': 298.257222101, 'longitude_of_prime_meridian': 0.0 } } } nctools.ncSaveGrid(filename, dimensions, variables, gatts=gatts)
def _saveGustToFile(self, trackfile, result, filename): """ Save gusts to a file. """ lat, lon, speed, Vx, Vy, P = result trackfileDate = flModDate(trackfile) gatts = { 'title': 'TCRM hazard simulation - synthetic event wind field', 'tcrm_version': flProgramVersion(), 'python_version': sys.version, 'track_file': trackfile, 'track_file_date': trackfileDate, 'radial_profile': self.profileType, 'boundary_layer': self.windFieldType, 'beta': self.beta} # Add configuration settings to global attributes: for section in self.config.sections(): for option in self.config.options(section): key = "{0}_{1}".format(section, option) value = self.config.get(section, option) gatts[key] = value dimensions = { 0: { 'name': 'lat', 'values': lat, 'dtype': 'f', 'atts': { 'long_name': 'Latitude', 'standard_name': 'latitude', 'units': 'degrees_north', 'axis': 'Y' } }, 1: { 'name': 'lon', 'values': lon, 'dtype': 'f', 'atts': { 'long_name': 'Longitude', 'standard_name': 'longitude', 'units': 'degrees_east', 'axis': 'X' } } } variables = { 0: { 'name': 'vmax', 'dims': ('lat', 'lon'), 'values': speed, 'dtype': 'f', 'atts': { 'long_name': 'Maximum 3-second gust wind speed', 'standard_name': 'wind_speed_of_gust', 'units': 'm/s', 'actual_range': (np.min(speed), np.max(speed)), 'valid_range': (0.0, 200.), 'cell_methods': ('time: maximum ' 'time: maximum (interval: 3 seconds)'), 'grid_mapping': 'crs' } }, 1: { 'name': 'ua', 'dims': ('lat', 'lon'), 'values': Vx, 'dtype': 'f', 'atts': { 'long_name': 'Eastward component of maximum wind speed', 'standard_name': 'eastward_wind', 'units': 'm/s', 'actual_range': (np.min(Vx), np.max(Vx)), 'valid_range': (-200., 200.), 'grid_mapping': 'crs' } }, 2: { 'name': 'va', 'dims': ('lat', 'lon'), 'values': Vy, 'dtype': 'f', 'atts': { 'long_name': 'Northward component of maximim wind speed', 'standard_name': 'northward_wind', 'units': 'm/s', 'actual_range': (np.min(Vy), np.max(Vy)), 'valid_range': (-200., 200.), 'grid_mapping': 'crs' } }, 3: { 'name': 'slp', 'dims': ('lat', 'lon'), 'values': P, 'dtype': 'f', 'atts': { 'long_name': 'Minimum air pressure at sea level', 'standard_name': 'air_pressure_at_sea_level', 'units': 'Pa', 'actual_range': (np.min(P), np.max(P)), 'valid_range': (70000., 115000.), 'cell_methods': 'time: minimum', 'grid_mapping': 'crs' } }, 4: { 'name': 'crs', 'dims': (), 'values': None, 'dtype': 'i', 'atts': { 'grid_mapping_name': 'latitude_longitude', 'semi_major_axis': 6378137.0, 'inverse_flattening': 298.257222101, 'longitude_of_prime_meridian': 0.0 } } } nctools.ncSaveGrid(filename, dimensions, variables, gatts=gatts)