def getStartAndEndDateForClimateStation(climateStation, paths): """ Determine start and end date for the climate time-series associated with a climate station. Supports only point time-series. Does so by reading in the daily rain climate file. @param climateStation String representing path to climate station file @param paths rhessysworkflows.rhessys.RHESSysPaths @return Tuple of datetime.datetime representing start and end date of the climate time series; None is returned if the time-series lacks a start date @raise IOError if unable to read climate station file or daily rain time-series """ if not os.access(climateStation, os.R_OK): raise IOError("Unable to read climate station %s" % (climateStation, ), errno.EACCES) station = readParameterFile(climateStation) rainTimeseriesPath = "%s.rain" % (os.path.join( paths.RHESSYS_DIR, station['daily_climate_prefix']), ) if not os.access(rainTimeseriesPath, os.R_OK): raise IOError( "Unable to read daily rain time-series %s" % (rainTimeseriesPath, ), errno.EACCES) # Read daily rain timeseries to determine start and end date startDate = None endDate = None numDays = 0 startDateRe = re.compile( '^(?P<year>\d+)\s(?P<month>\d+)\s(?P<day>\d+)\s(?P<hour>\d+)\s*$') countDays = False f = open(rainTimeseriesPath, 'r') for line in f: if not countDays: line = line.strip() result = startDateRe.match(line) if result: startDate = datetime.datetime(year=int(result.group('year')), month=int(result.group('month')), day=int(result.group('day')), hour=int(result.group('hour'))) countDays = True continue # Count days numDays += 1 if startDate: timeDelta = datetime.timedelta(days=numDays) endDate = startDate + timeDelta return (startDate, endDate)
def getStartAndEndDateForClimateStation(climateStation, paths): """ Determine start and end date for the climate time-series associated with a climate station. Supports only point time-series. Does so by reading in the daily rain climate file. @param climateStation String representing path to climate station file @param paths rhessysworkflows.rhessys.RHESSysPaths @return Tuple of datetime.datetime representing start and end date of the climate time series; None is returned if the time-series lacks a start date @raise IOError if unable to read climate station file or daily rain time-series """ if not os.access(climateStation, os.R_OK): raise IOError("Unable to read climate station %s" % (climateStation,), errno.EACCES) station = readParameterFile(climateStation) rainTimeseriesPath = "%s.rain" % (os.path.join(paths.RHESSYS_DIR, station['daily_climate_prefix']), ) if not os.access(rainTimeseriesPath, os.R_OK): raise IOError("Unable to read daily rain time-series %s" % (rainTimeseriesPath,), errno.EACCES) # Read daily rain timeseries to determine start and end date startDate = None endDate = None numDays = 0 startDateRe = re.compile('^(?P<year>\d+)\s(?P<month>\d+)\s(?P<day>\d+)\s(?P<hour>\d+)\s*$') countDays = False f = open(rainTimeseriesPath, 'r') for line in f: if not countDays: line = line.strip() result = startDateRe.match(line) if result: startDate = datetime.datetime(year=int( result.group('year') ), month=int( result.group('month') ), day=int( result.group('day') ), hour=int( result.group('hour') ) ) countDays = True continue # Count days numDays += 1 if startDate: timeDelta = datetime.timedelta(days=numDays) endDate = startDate + timeDelta return (startDate, endDate)
defStrKey = "%s_defs" % (key,) if args.verbose: print("%s: %s" % (defStrKey, defStr) ) subs[defStrKey] = defStr # Second, climate stations if args.climateStation: # Use the base station specified on command line if args.verbose: print("Using single climate station: %s" % (args.climateStation,) ) climParamFilename = "%s.base" % (args.climateStation,) baseFile = os.path.join(paths._CLIM, climParamFilename) subs['climate_stations'] = baseFile subs['num_climate_stations'] = 1 climParams = readParameterFile( os.path.join(paths.RHESSYS_CLIM, climParamFilename) ) climateStationIDStr = "base_station_ID\tdvalue %s" % (climParams['base_station_id'],) subs['zone_base_station_ids'] = climateStationIDStr subs['zone_num_base_stations'] = 1 else: # Use base station raster map if args.verbose: print("Reading climate stations from raster: %s" % (grassMetadata['basestations_rast'],) ) # Get list of base station IDs from raster rasterIds = oset() pipe = grassLib.script.pipe_command('r.stats', flags='licn', input=grassMetadata['basestations_rast']) for line in pipe.stdout: values = line.strip().split() if values[1] != 'NULL': rasterIds.add( int(values[0]) ) # Get base station IDs from base station files