def createInsertSensorXml(obs):
    featureXml = utils.XML_FEATURE.format(obs[0].lower(), utils.featureId(obs[0]), obs[0], utils.STATIONS[obs[0]][1])
    timePositionXml = utils.XML_TIME_POSITION.format(obs[1].strftime("%Y-%m-%dT23:59:59.000+00:00"))
    procedureXref = utils.procedureXref("gsod")

    observations = [
        ("http://vocab.example.com/phenomenon/TemperatureMean", "°C"),
        ("http://vocab.example.com/phenomenon/TemperatureMax", "°C"),
        ("http://vocab.example.com/phenomenon/TemperatureMin", "°C"),
        ("http://vocab.example.com/phenomenon/WindspeedMean", "kn"),
        ("http://vocab.example.com/phenomenon/WindspeedMax", "kn"),
        ("http://vocab.example.com/phenomenon/WindspeedGust", "kn"),
        ("http://vocab.example.com/phenomenon/Precipitation", "mm"),
    ]

    result = []
    for i in range(2, len(obs)):
        if obs[i] not in [9999.9, 999.9, 99.99]:
            xmlObsProp = utils.XML_OBSERVED_PROPERTY.format(observations[i - 2][0])
            xmlResult = utils.XML_RESULT.format(observations[i - 2][1], obs[i])
            content = timePositionXml + procedureXref + xmlObsProp + featureXml + xmlResult
            insertObsXml = utils.XML_INSERT_OBS.format(utils.offeringId("gsod"), content)
            log.debug(insertObsXml)
            result.append(insertObsXml)
    return result
def createXml(dt, value, station, pheno):
    log.info("Create XML for: {0}, {1}, {2}, {3}".format(dt, value, station, pheno))
    featureXml = utils.XML_FEATURE.format(station.lower(), utils.featureId(station), station, utils.STATIONS[station][1])
    timePositionXml = utils.XML_TIME_POSITION.format(dt.strftime("%Y-%m-%dT00:00:00.000+00:00"))
    procedureXref = utils.procedureXref("histalp")

    xmlObsProp = utils.XML_OBSERVED_PROPERTY.format(PHENOMENONS[pheno][0])
    xmlResult = utils.XML_RESULT.format(PHENOMENONS[pheno][1], value)
    content = timePositionXml + procedureXref + xmlObsProp + featureXml + xmlResult
    insertObsXml = utils.XML_INSERT_OBS.format(utils.offeringId("histalp"), content)
    log.debug(insertObsXml)
    return insertObsXml
        featureXml = utils.XML_FEATURE.format(obs.station_id.lower(), utils.featureId(obs.station_id), obs.station_id,
                                              utils.STATIONS[obs.station_id][1])
        timePositionXml = utils.XML_TIME_POSITION.format(obs.time.strftime("%Y-%m-%dT%H:%M:00.000+00:00"))
        procedureXref = utils.procedureXref("metar")

        # loop over all interesting observations and push them into the SOS
        for observation in observations:
            if (observation[2]):
                xmlObsProp = utils.XML_OBSERVED_PROPERTY.format(observation[0])
                if (observation[1] == ""):
                    xmlResult = utils.XML_RESULT_TEXT.format(observation[1])
                else:
                    xmlResult = utils.XML_RESULT.format(observation[1], observation[2])

                content = timePositionXml + procedureXref + xmlObsProp + featureXml + xmlResult
                insertObsXml = utils.XML_INSERT_OBS.format(utils.offeringId("metar"), content)

                log.debug(insertObsXml)

                try:
                    request = urllib.request.Request(utils.POX_URL, insertObsXml.encode("UTF-8"), utils.POST_HEADER)
                    resp_handle = urllib.request.urlopen(request)
                    response = resp_handle.read().decode(resp_handle.headers.get_content_charset())
                    if "ows:Exception" not in response:
                        log.info("Observation inserted: {}".format(observation[0]))
                    elif "Observation with same values already contained in database" in response:
                        log.warn("Observation already in SOS: {}".format(observation[0]))
                    else:
                        log.info(response)
                    resp_handle.close()
                except Exception as e: