def check(self, value): # we MUST return an bool or raise OptionConversionException # if we can't get one from "value" if type(value) == BooleanType: return value # only accept 0 and 1 as valid booleans... # cause a lot of problems in OptionChecker accept the same as python truth tables assume if type(value) in (IntType, LongType, FloatType): if value == 1: return True if value == 0: return False if type(value) == StringType: value = value.strip().lower() if value in self._truthTable: return value in self._trueValues raise OptionConversionException("couldn't convert '%s' to bool." % value) # any other type, raise exception raise OptionConversionException("couldn't convert %s to bool." % str(type(value)))
def check (self, value): try: tmp = self._checker.check (value) except OptionConversionException: raise OptionConversionException ("'%s' isn't a valid option." % str (value)) else: # inclusive if (tmp >= self._min) and (tmp <= self._max): return tmp else: raise OptionConversionException ("'%s' it's outside valid limits (%f <= x <= %f." % (str (value), self._min, self._max))
def sky_transparency(self, unit_out=units.pct): """ Returns, in percent, the sky transparency :param unit_out: """ if unit_out not in self.__accepted_transparency_unit__: raise OptionConversionException("Invalid transparency unit %s." % unit_out) return WSValue(datetime.datetime.utcnow(), np.random.rand() * 100, unit_out)
def check (self, value): # we MUST return an int or raise OptionConversionException # if we can't get one from "value" # simple case if type (value) in (IntType, LongType, FloatType, BooleanType): return int (value) if type (value) == StringType: # try to convert to int (use float first and then cast (loosely) try: tmp = float (value) tmp = int(tmp) return tmp except ValueError: # couldn't convert, nothing to do raise OptionConversionException ("couldn't convert '%s' to int value." % value) raise OptionConversionException ("couldn't convert '%s' to int." % str (type (value)))
def check(self, value, state=None): if not isinstance(value, Coord): try: return Coord.fromState(value, state) except ValueError: pass # any other type is ignored raise OptionConversionException('invalid coord value %s.' % value)
def check (self, value): # we MUST return an float or raise OptionConversionException # if we can't get one from "value" # simple case if type (value) in (FloatType, IntType, LongType, BooleanType): return float (value) if type (value) == StringType: # try to convert to int try: tmp = float (value) return tmp except ValueError: # couldn't convert, nothing to do raise OptionConversionException ("couldn't convert '%s' to float value." % value) raise OptionConversionException ("couldn't convert %s to float." % str (type (value)))
def rain_rate(self, unit_out=units.imperial.inch / units.hour): """ For testing purposes, it never rains. :param unit: :return: """ if unit_out not in self.__accepted_precipitation_unit__: raise OptionConversionException("Invalid precipitation unit %s." % unit_out) return WSValue(datetime.datetime.utcnow(), 0, unit_out)
def flux(self, unit=units.count): if unit not in self.__accepted_flux_units__: raise OptionConversionException("Invalid flux unit %s." % unit) if self._check(): return SeeingValue( self.obs_time(), self._convert_units(self._flux, units.count, unit), unit) else: return False
def seeing(self, unit=units.arcsec): if unit not in self.__accepted_seeing_units__: raise OptionConversionException("Invalid seeing unit %s." % unit) if self._check(): return SeeingValue( self.obs_time(), self._convert_units(self._seeing, units.arcsec, unit), unit) else: return False
def check (self, value): if type(value) == EnumValue: if value in self.enumtype: return value if type(value) == StringType: ret = [v for v in self.enumtype if str(v).upper() == value.upper()] if ret: return ret[0] raise OptionConversionException ('invalid enum value %s. not a %s enum.' % (value, str(self.enumtype)))
def airmass(self, unit=units.dimensionless_unscaled): if unit not in self.__accepted_airmass_units__: raise OptionConversionException("Invalid airmass unit %s." % unit) if self._check(): return SeeingValue( self.obs_time(), self._convert_units(self._airmass, units.dimensionless_unscaled, unit), unit) else: return False
def sky_transparency(self, unit_out=units.pct): if self._results is None: return False if unit_out not in self.__accepted_transparency_unit__: raise OptionConversionException( "Invalid sky transparency unit %s." % unit_out) return WSValue( self._results['utctime'], self._convert_units(self._results['sky_transparency']['Value'], units.pct, unit_out), unit_out)
def pressure(self, unit_out=units.Pa): if self._results is None: return False if unit_out not in self.__accepted_pressures_unit__: raise OptionConversionException("Invalid pressure unit %s." % unit_out) return WSValue( self._results['utctime'], self._convert_units(self._results['pressure']['Value'], units.cds.mmHg, unit_out), unit_out)
def humidity(self, unit_out=units.pct): if unit_out not in self.__accepted_humidity_units__: raise OptionConversionException("Invalid humidity unit %s." % unit_out) if self._check(): return WSValue( self.obs_time(), self._convert_units(self._humidity, units.pct, unit_out), unit_out) else: return False
def wind_direction(self, unit_out=units.degree): if self._results is None: return False if unit_out not in self.__accepted_direction_unit__: raise OptionConversionException("Invalid speed unit %s." % unit_out) return WSValue( self._results['utctime'], self._convert_units(self._results['wind_direction']['Value'], units.deg, unit_out), unit_out)
def wind_speed(self, unit_out=units.meter / units.second): if self._results is None: return False if unit_out not in self.__accepted_speed_units__: raise OptionConversionException("Invalid speed unit %s." % unit_out) return WSValue( self._results['utctime'], self._convert_units(self._results['wind_speed']['Value'], (units.m / units.s), unit_out), unit_out)
def temperature(self, unit_out=units.Celsius): if self._results is None: return False if unit_out not in self.__accepted_temperature_units__: raise OptionConversionException("Invalid temperature unit %s." % unit_out) return WSValue( self._results['utctime'], self._convert_units(self._results['temperature']['Value'], units.Celsius, unit_out), unit_out)
def humidity(self, unit_out=units.pct): if self._results is None: return False if unit_out not in self.__accepted_humidity_units__: raise OptionConversionException("Invalid humidity unit %s." % unit_out) return WSValue( self._results['utctime'], self._convert_units(self._results['humidity']['Value'], units.pct, unit_out), unit_out)
def temperature(self, unit_out=units.Celsius): if unit_out not in self.__accepted_temperature_units__: raise OptionConversionException("Invalid temperature unit %s." % unit_out) if self._check(): return WSValue( self.obs_time(), self._convert_units(self._temperature, units.Celsius, unit_out), unit_out) else: return False
def sky_transparency(self, unit_out=units.pct): """ Returns, in percent, the sky transparency :param unit_out: """ if unit_out not in self.__accepted_transparency_unit__: raise OptionConversionException("Invalid transparency unit %s." % unit_out) if self._results is not None: return WSValue(self._results['last_update'], self._results['transparency'], unit_out) else: return False
def pressure(self, unit_out=units.Pa): """ Pressure at 1.5 atm :param unit: :return: """ pressure_reference = 1140. # MM_HG if unit_out not in self.__accepted_pressures_unit__: raise OptionConversionException("Invalid pressure unit %s." % unit_out) pressure = self._convert_units(pressure_reference, units.cds.mmHg, unit_out) return WSValue(datetime.datetime.utcnow(), pressure, unit_out)
def check (self, value): for option in self._options: try: tmp = option["checker"].check (value) if tmp == option["value"]: return option["value"] else: continue except OptionConversionException: continue raise OptionConversionException ("'%s' isn't a valid option." % str (value))
def dew_point(self, unit_out=units.Celsius): """ :param unit_out: :return: """ if self._results is None: return False if unit_out not in self.__accepted_temperature_units__: raise OptionConversionException("Invalid dew point unit %s." % unit_out) return WSValue( self._results['utctime'], self._convert_units(self._results['dew_point']['Value'], units.Celsius, unit_out), unit_out)
def wind_speed(self, unit_out=units.meter / units.second): """ Returns the wind speed in the chosen unit (Default: Meters per second). :param unit: Unit in which the instrument should return the wind speed. :return: the wind speed. """ reference_speed = 10 # M_PER_S if unit_out not in self.__accepted_speed_units__: raise OptionConversionException("Invalid speed unit %s." % unit_out) speed = self._convert_units(reference_speed, units.meter / units.second, unit_out) return WSValue(datetime.datetime.utcnow(), speed, unit_out)
def humidity(self, unit_out=units.pct): """ Returns the 100% relative humidity in the range between 20% and 100%. :param unit: Unit in which the instrument should return the humidity. :return: the humidity. """ current_time = datetime.datetime.utcnow() if unit_out not in self.__accepted_humidity_units__: raise OptionConversionException("Invalid humidity unit %s." % unit_out) humidity = 40 * math.cos(self._hourinradians(current_time.hour)) + 60. humidity = self._convert_units(humidity, units.pct, unit_out) return WSValue(current_time, humidity, unit_out)
def wind_direction(self, unit_out=units.degree): """ Returns the wind direction in the chosen unit in the range between 0 to 360 degrees. :param unit: Unit in which the instrument should return the angle. :return: the angle. """ if unit_out not in self.__accepted_direction_unit__: raise OptionConversionException("Invalid direction unit %s." % unit_out) hour = datetime.datetime.utcnow().hour reference_direction = 180 * math.sin(self._hourinradians(hour)) + 180 direction = self._convert_units(reference_direction, units.degree, unit_out) return WSValue(datetime.datetime.utcnow(), direction, unit_out)
def dew_point(self, unit_out=units.Celsius): """ Some simulations ran on 'http://www.cactus2000.de/uk/unit/masshum.shtml' suggests that the dew point at 1.5mm Hg and low temperatures are very low, around -20 C. Here I'm using -10 C. :param unit: Unit in which the instrument should return the temperature. :return: the angle. """ if unit_out not in self.__accepted_temperature_units__: raise OptionConversionException("Invalid temperature unit %s." % unit_out) temperature = self._convert_units( 10, units.Celsius, unit_out, equivalencies=units.equivalencies.temperature()) return WSValue(datetime.datetime.utcnow(), temperature, unit_out)
def temperature(self, unit_out=units.Celsius): """ Returns the temperature in the chosen unit in the range between -10 C and +40 C. :param unit: Unit in which the instrument should return the temperature. :return: the temperature. """ current_time = datetime.datetime.utcnow() if unit_out not in self.__accepted_temperature_units__: raise OptionConversionException("Invalid temperature unit %s." % unit_out) temperature = 25 * math.sin(self._hourinradians(current_time.hour) - math.pi / 2.) + 15. temperature = self._convert_units( temperature, units.Celsius, unit_out, equivalencies=units.equivalencies.temperature()) return WSValue(current_time, temperature, unit_out)