Ejemplo n.º 1
0
    def select_daily_weather_api(self, url, start_date, end_date):
        xml_data = urllib.urlopen(url).read()
        xml_data = xml.etree.ElementTree.fromstring(xml_data)
        ns = {'wx': DTN_DOC}
        xml_data = xml_data.find('wx:locationResponseList', ns)
        xml_data = xml_data.find('wx:locationResponse', ns)
        rainfall = xml_data.find('wx:precipitationAmountList', ns).find('wx:values', ns).text
        et = xml_data.find('wx:evapotranspirationList', ns).find('wx:values', ns).text
        max_temperature = xml_data.find('wx:maxTemperatureList', ns).find('wx:values', ns).text
        min_temperature = xml_data.find('wx:minTemperatureList', ns).find('wx:values', ns).text

        # solar_radiation = xml_data.find('wx:solarRadiationList', ns).find('wx:values', ns).text
        # max_relative_humidity = xml_data.find('wx:maxRelativeHumidityList', ns).text
        # min_relative_humidity = xml_data.find('wx:minRelativeHumidityList', ns).text
        # avg_wind_speed = xml_data.find('wx:avgWindSpeedList', ns).find('wx:values', ns).text

        rainfall = rainfall.split(' ')
        et = et.split(' ')
        max_temperature = max_temperature.split(' ')
        min_temperature = min_temperature.split(' ')
        # solar_radiation = solar_radiation.split(' ')
        # max_relative_humidity = max_relative_humidity.split(' ')
        # min_relative_humidity = min_relative_humidity.split(' ')
        # avg_wind_speed = avg_wind_speed.split(' ')

        rainfall = [uom.Uom(float(val), uom.Inch) for val in rainfall]
        et = [uom.Uom(float(val), uom.Inch) for val in et]
        max_temperature = [uom.Uom(float(val), uom.Fahrenheit) for val in max_temperature]
        min_temperature = [uom.Uom(float(val), uom.Fahrenheit) for val in min_temperature]
        # solar_radiation = [uom.Uom(float(val), uom.Watt / uom.Meter ** 2) for val in solar_radiation]
        # max_relative_humidity = [uom.Uom(float(val), uom.NoUomx100) for val in max_relative_humidity]
        # min_relative_humidity = [uom.Uom(float(val), uom.NoUomx100) for val in min_relative_humidity]
        # avg_wind_speed = [uom.Uom(float(val), uom.Mile / uom.Hour) for val in avg_wind_speed]

        dtod_weather = {}
        if end_date == start_date:
            end_date = start_date + timedelta(days=1)
        for index, date in enumerate(date_ext.datetime_range(start_date, end_date)):
            date = date.replace(hour=12)

            dto_weather = DtoFieldWeather(account_device_id=self.account_device_id)
            dto_weather.created = date
            dto_weather.max_temperature = max_temperature[index]
            dto_weather.min_temperature = min_temperature[index]
            dto_weather.rainfall = rainfall[index]
            dto_weather.evapotranspiration = et[index]
            # dto_weather.solar_radiation = solar_radiation[index]
            # dto_weather.max_relative_humidity = max_relative_humidity[index]
            # dto_weather.min_relative_humidity = min_relative_humidity[index]
            # dto_weather.wind_speed = avg_wind_speed[index]
            dtod_weather[date] = dto_weather
        return dtod_weather
Ejemplo n.º 2
0
 def convert_type(self, attr, value):
     try:
         if value is None or isinstance(value, Null):
             return value
         if isinstance(self._attr_type[attr],
                       DtoUom) and not isinstance(value, uom.Uom):
             return uom.Uom(value, self._attr_type[attr].base_uom)
         if isinstance(self._attr_type[attr], DtoInteger):
             return int(value)
         if isinstance(self._attr_type[attr], DtoNumeric):
             return float(value)
         if isinstance(self._attr_type[attr], DtoText):
             if not isinstance(value, unicode) and not isinstance(
                     value, str):
                 raise DtoValueError()
         if isinstance(self._attr_type[attr], DtoBytea):
             return psycopg2.Binary(value)
         if isinstance(self._attr_type[attr], DtoBoolean):
             return bool(value)
         if isinstance(self._attr_type[attr], DtoTimestamp):
             if not isinstance(value, datetime):
                 raise DtoValueError()
         if isinstance(self._attr_type[attr], DtoDate):
             if not isinstance(value, date):
                 raise DtoValueError()
         if isinstance(self._attr_type[attr], DtoTimeDelta):
             if not isinstance(value, timedelta):
                 raise DtoValueError()
         return value
     except:
         dto_error = DtoError()
         dto_error.type_error = attr
         raise DtoValueError(dto_error)
Ejemplo n.º 3
0
def calculate_radius(polygon):
    """
        calculate radius of field circle polygon
        :param polygon: shapely.geometry.Polygon
        :return: uom object
    """
    radius = math.sqrt(calculate_areas(polygon).value / math.pi)
    return uom.Uom(radius, uom.Meter)
Ejemplo n.º 4
0
def calculate_areas(polygon):
    """
    calculate area of polygon
    :param polygon: shapely.geometry.Polygon
    :return: uom object
    """
    project = ft.partial(
        pj.transform, pj.Proj(init='epsg:4326'),
        pj.Proj('+proj=eck4 +lat_0=' + str(polygon.centroid.y) + ' +lon_0=' +
                str(polygon.centroid.x)))
    field_projected = transform(project, polygon)
    # convert from square meters to acres
    return uom.Uom(field_projected.area, uom.SquareMeter)
Ejemplo n.º 5
0
 def add_attr(self,
              attr_name,
              attr_type=None,
              nullable=True,
              has_default=False,
              value=None,
              read_only=False):
     if attr_name in self.attributes:
         return
     self.attributes.append(attr_name)
     if isinstance(attr_type, DtoUom) and not isinstance(
             value, uom.Uom) and value is not None:
         value = uom.Uom(value, attr_type.base_uom)
     self.__dict__[attr_name] = value
     self._default_value[attr_name] = value
     self._attr_type[attr_name] = attr_type
     self._nullable[attr_name] = nullable
     self._has_default[attr_name] = has_default
     self._read_only[attr_name] = read_only
Ejemplo n.º 6
0
    def select_hourly_weather_api(self, url, start_date, end_date, latitude, longitude, elevation):

        xml_data = urllib.urlopen(url).read()
        xml_data = xml.etree.ElementTree.fromstring(xml_data)
        ns = {'wx': DTN_DOC}
        xml_data = xml_data.find('wx:locationResponseList', ns)
        xml_data = xml_data.find('wx:locationResponse', ns)
        rainfall = xml_data.find('wx:precipitationAmountList', ns).find('wx:values', ns).text
        temperature = xml_data.find('wx:temperatureList', ns).find('wx:values', ns).text
        solar_radiation = xml_data.find('wx:solarRadiationList', ns).find('wx:values', ns).text
        relative_humidity = xml_data.find('wx:relativeHumidityList', ns).text
        wind_speed = xml_data.find('wx:windSpeedList', ns).find('wx:values', ns).text
        weather_condition = xml_data.find('wx:weatherDescriptionList', ns).text
        weather_code = xml_data.find('wx:weatherCodeList', ns).text
        feels_like = xml_data.find('wx:feelsLikeList', ns).find('wx:values', ns).text
        precipitation_type = xml_data.find('wx:precipitationTypeList', ns).text
        wind_gust = xml_data.find('wx:windGustList', ns).find('wx:values', ns).text
        wind_direction = xml_data.find('wx:windDirectionList', ns).find('wx:values', ns).text

        rainfall = rainfall.split(' ')
        temperature = temperature.split(' ')
        solar_radiation = solar_radiation.split(' ')
        relative_humidity = relative_humidity.split(' ')
        wind_speed = wind_speed.split(' ')
        weather_condition = weather_condition.split(' ')
        weather_code = weather_code.split(' ')
        feels_like = feels_like.split(' ')
        precipitation_type = precipitation_type.split(' ')
        wind_gust = wind_gust.split(' ')
        wind_direction = wind_direction.split(' ')

        rainfall = [uom.Uom(float(val), uom.Inch) for val in rainfall]
        temperature = [uom.Uom(float(val), uom.Fahrenheit) for val in temperature]
        solar_radiation = [uom.Uom(float(val), uom.Watt / uom.Meter ** 2) for val in solar_radiation]
        relative_humidity = [uom.Uom(float(val), uom.NoUomx100) for val in relative_humidity]
        wind_speed = [uom.Uom(float(val), uom.Mile / uom.Hour) for val in wind_speed]
        temperature_feels_like = [uom.Uom(float(val), uom.Fahrenheit) for val in feels_like]
        wind_gust = [uom.Uom(float(val), uom.Mile / uom.Hour) for val in wind_gust]
        wind_direction = [uom.Uom(float(val), uom.NoUom) for val in wind_direction]

        try:
            chance_of_rain = xml_data.find('wx:probabilityOfPrecipitationList', ns).text
            chance_of_rain = chance_of_rain.split(' ')
            chance_of_rain = [uom.Uom(float(val), uom.NoUomx100) for val in chance_of_rain]
        except:
            chance_of_rain = [None] * len(rainfall)

        dtod_weather = {}
        if end_date == start_date:
            end_date = start_date + timedelta(hours=1)
        for index, date in enumerate(date_ext.datetime_range(start_date, end_date, timedelta(hours=1))):
            dto_weather = DtoFieldWeather(account_device_id=self.account_device_id)
            dto_weather.created = date
            dto_weather.temperature = temperature[index]
            dto_weather.rainfall = rainfall[index]
            dto_weather.solar_radiation = solar_radiation[index]
            dto_weather.relative_humidity = relative_humidity[index]
            dto_weather.wind_speed = wind_speed[index]
            dto_weather.wind_gust = wind_gust[index]
            dto_weather.wind_direction = wind_direction[index]
            dto_weather.condition = weather_condition[index].replace('-', ' ')
            dto_weather.code = weather_code[index]
            dto_weather.temperature_feels_like = temperature_feels_like[index]
            dto_weather.precipitation_probability = chance_of_rain[index]
            dto_weather.precipitation_type = precipitation_type[index]
            evapotranspiration = self.ref_et_hourly(
                date,
                date + timedelta(hours=1),
                dto_weather.temperature.convert(uom.Centigrade),
                dto_weather.wind_speed.convert(uom.Meter / uom.Second),
                dto_weather.solar_radiation.convert(uom.MegaJoule / uom.Meter ** 2 / uom.Hour),
                dto_weather.relative_humidity.convert(uom.NoUomx100),
                latitude,
                longitude,
                elevation)
            dto_weather.evapotranspiration = uom.Uom(evapotranspiration, uom.MilliMeter)
            dtod_weather[date] = dto_weather
        return dtod_weather