Esempio n. 1
0
def getStationsFromTimeserieTypeElemCodes(timeserietypeID, elem_codes, output='list', has_all_elems_now=True):
    """Gets all stations with a given timeserie type and element codes.

    :param timeserietypeID      [string]
    :param elem_codes           [string] or [list of string]
    :param output               [string]
    :param has_all_elems_now    [bool] Station must have all elements still active
    :return stationList         [list of dictionary elements] or None if file is requested output.

    Output options:
        'list':         returns a list of dictionary elements.
        'xml':          returns NULL but saves a .xml file to the working folder.
        'txt':          returns NULL but saves a .txt file to the working folder.

    timeseries_type options:
        0   dayly averages
        1   monthly averages
        2   hour average

    Denne finner alle stasjoner emd langbølget stråling
    http://eklima.met.no/met/MetService?invoke=getStationsFromTimeserieTypeElemCodes&timeserietypeID=2&elem_codes=QLI&username=

    <item xsi:type="ns3:no_met_metdata_StationProperties">
        <amsl xsi:type="xsd:int">10</amsl>
        <department xsi:type="xsd:string">AKERSHUS</department>
        <fromDay xsi:type="xsd:int">31</fromDay>
        <fromMonth xsi:type="xsd:int">12</fromMonth>
        <fromYear xsi:type="xsd:int">1953</fromYear>
        <latDec xsi:type="xsd:double">59.8927</latDec>
        <latLonFmt xsi:type="xsd:string">decimal_degrees</latLonFmt>
        <lonDec xsi:type="xsd:double">10.6158</lonDec>
        <municipalityNo xsi:type="xsd:int">219</municipalityNo>
        <name xsi:type="xsd:string">FORNEBU</name>
        <stnr xsi:type="xsd:int">19400</stnr>
        <toDay xsi:type="xsd:int">30</toDay>
        <toMonth xsi:type="xsd:int">11</toMonth>
        <toYear xsi:type="xsd:int">1998</toYear>
        <utm_e xsi:type="xsd:int">254787</utm_e>
        <utm_n xsi:type="xsd:int">6647583</utm_n>
        <utm_zone xsi:type="xsd:int">33</utm_zone>
        <wmoNo xsi:type="xsd:int">1488</wmoNo>
    </item>

    """

    if isinstance(elem_codes, t.ListType):
        elem_codes_string = ','.join(elem_codes)
    else:
        elem_codes_string = elem_codes
        elem_codes = [elem_codes]

    url = "http://eklima.met.no/metdata/MetDataService?invoke=getStationsFromTimeserieTypeElemCodes&timeserietypeID={0}&elem_codes={1}&username="******".xml", wsKlimaRequest.text.encode('utf-8'))
        return None

    # Take the request and make an element tree to be iterated
    # append all stations to a list
    root = etree.fromstring(wsKlimaRequest.content)
    for station in root.iter('item'):
        amsl = station.find('amsl').text
        department = fe.remove_norwegian_letters(station.find('department').text)
        fromDay = station.find('fromDay').text
        fromMonth = station.find('fromMonth').text
        fromYear = station.find('fromYear').text
        latDec = station.find('latDec').text
        lonDec = station.find('lonDec').text
        municipalityNo = fe.remove_norwegian_letters(station.find('municipalityNo').text)
        name = fe.remove_norwegian_letters(station.find('name').text)
        stnr = station.find('stnr').text
        toDay = station.find('toDay').text
        toMonth = station.find('toMonth').text
        toYear = station.find('toYear').text
        utm_e = station.find('utm_e').text
        utm_n = station.find('utm_n').text
        utm_zone = station.find('utm_zone').text

        from_date = dt.date(year=int(fromYear), month=int(fromMonth), day=int(fromDay))
        to_date = None
        if toYear is not '0':
            to_date = dt.date(year=int(toYear), month=int(toMonth), day=int(toDay))

        station_list.append({
                'amsl': int(amsl),
                'department': fe.add_norwegian_letters(department),
                'latDec': float(latDec),
                'lonDec': float(lonDec),
                'municipalityNo': int(municipalityNo),
                'name': fe.add_norwegian_letters(name),
                'stnr': int(stnr),
                'utm_e': int(utm_e),
                'utm_n': int(utm_n),
                'utm_zone': utm_zone,
                'from_date': from_date,
                'to_date': to_date})

    # look up all stations and find if elements are active and if all are present on the station
    if has_all_elems_now is True:
        for station in station_list:
            if station['to_date'] is None:                          # station is operational today
                use_station = True
                station_elems = getElementsFromTimeserieTypeStation(station['stnr'], timeserietypeID, output='list')
                station_elems_list = []
                for elem in station_elems:
                    if elem['toDate'] is None:                      # list elements being observed today
                        station_elems_list.append(elem['elemCode'])
                for elem in elem_codes:
                    if elem not in station_elems_list:              # requested elements are on the station
                        use_station = False
                if use_station is True:
                    final_station_list.append(station)
    else:
        final_station_list = station_list

    if output == 'list':
        return final_station_list
    elif output == 'txt':
        mfd.write_dictionary(filename, '.txt', final_station_list, tabulated=False)
        return None
Esempio n. 2
0
def getStationsFromTimeserieTypeElemCodes(timeserietypeID, elem_codes, output="list", has_all_elems_now=True):
    """Gets all stations with a given timeserie type and element codes.

    :param timeserietypeID      [string]
    :param elem_codes           [string] or [list of string]
    :param output               [string]
    :param has_all_elems_now    [bool] Station must have all elements still active
    :return stationList         [list of dictionary elements] or None if file is requested output.

    Output options:
        'list':         returns a list of dictionary elements.
        'xml':          returns NULL but saves a .xml file to the working folder.
        'txt':          returns NULL but saves a .txt file to the working folder.

    timeseries_type options:
        0   dayly averages
        1   monthly averages
        2   hour average

    Denne finner alle stasjoner emd langbølget stråling
    http://eklima.met.no/met/MetService?invoke=getStationsFromTimeserieTypeElemCodes&timeserietypeID=2&elem_codes=QLI&username=

    <item xsi:type="ns3:no_met_metdata_StationProperties">
        <amsl xsi:type="xsd:int">10</amsl>
        <department xsi:type="xsd:string">AKERSHUS</department>
        <fromDay xsi:type="xsd:int">31</fromDay>
        <fromMonth xsi:type="xsd:int">12</fromMonth>
        <fromYear xsi:type="xsd:int">1953</fromYear>
        <latDec xsi:type="xsd:double">59.8927</latDec>
        <latLonFmt xsi:type="xsd:string">decimal_degrees</latLonFmt>
        <lonDec xsi:type="xsd:double">10.6158</lonDec>
        <municipalityNo xsi:type="xsd:int">219</municipalityNo>
        <name xsi:type="xsd:string">FORNEBU</name>
        <stnr xsi:type="xsd:int">19400</stnr>
        <toDay xsi:type="xsd:int">30</toDay>
        <toMonth xsi:type="xsd:int">11</toMonth>
        <toYear xsi:type="xsd:int">1998</toYear>
        <utm_e xsi:type="xsd:int">254787</utm_e>
        <utm_n xsi:type="xsd:int">6647583</utm_n>
        <utm_zone xsi:type="xsd:int">33</utm_zone>
        <wmoNo xsi:type="xsd:int">1488</wmoNo>
    </item>

    """

    if isinstance(elem_codes, t.ListType):
        elem_codes_string = ",".join(elem_codes)
    else:
        elem_codes_string = elem_codes
        elem_codes = [elem_codes]

    url = "http://eklima.met.no/metdata/MetDataService?invoke=getStationsFromTimeserieTypeElemCodes&timeserietypeID={0}&elem_codes={1}&username="******"getWSklima.py -> getStationsFromTimeserieTypeElemCodes: Requesting url {0}".format(url)
    wsKlimaRequest = re.get(url)

    station_list = []
    final_station_list = []
    filename = "{0}Stations with {1} of {2}".format(env.data_path, timeserietypeID, elem_codes_string)

    # Check which output option is requested
    if output == "xml":  # save the xml received on the eklima request
        mfd.write_large_string(filename, ".xml", wsKlimaRequest.text.encode("utf-8"))
        return None

    # Take the request and make an element tree to be iterated
    # append all stations to a list
    root = etree.fromstring(wsKlimaRequest.content)
    for station in root.iter("item"):
        amsl = station.find("amsl").text
        department = fe.remove_norwegian_letters(station.find("department").text)
        fromDay = station.find("fromDay").text
        fromMonth = station.find("fromMonth").text
        fromYear = station.find("fromYear").text
        latDec = station.find("latDec").text
        lonDec = station.find("lonDec").text
        municipalityNo = fe.remove_norwegian_letters(station.find("municipalityNo").text)
        name = fe.remove_norwegian_letters(station.find("name").text)
        stnr = station.find("stnr").text
        toDay = station.find("toDay").text
        toMonth = station.find("toMonth").text
        toYear = station.find("toYear").text
        utm_e = station.find("utm_e").text
        utm_n = station.find("utm_n").text
        utm_zone = station.find("utm_zone").text

        from_date = dt.date(year=int(fromYear), month=int(fromMonth), day=int(fromDay))
        to_date = None
        if toYear is not "0":
            to_date = dt.date(year=int(toYear), month=int(toMonth), day=int(toDay))

        station_list.append(
            {
                "amsl": int(amsl),
                "department": fe.add_norwegian_letters(department),
                "latDec": float(latDec),
                "lonDec": float(lonDec),
                "municipalityNo": int(municipalityNo),
                "name": fe.add_norwegian_letters(name),
                "stnr": int(stnr),
                "utm_e": int(utm_e),
                "utm_n": int(utm_n),
                "utm_zone": utm_zone,
                "from_date": from_date,
                "to_date": to_date,
            }
        )

    # look up all stations and find if elements are active and if all are present on the station
    if has_all_elems_now is True:
        for station in station_list:
            if station["to_date"] is None:  # station is operational today
                use_station = True
                station_elems = getElementsFromTimeserieTypeStation(station["stnr"], timeserietypeID, output="list")
                station_elems_list = []
                for elem in station_elems:
                    if elem["toDate"] is None:  # list elements being observed today
                        station_elems_list.append(elem["elemCode"])
                for elem in elem_codes:
                    if elem not in station_elems_list:  # requested elements are on the station
                        use_station = False
                if use_station is True:
                    final_station_list.append(station)
    else:
        final_station_list = station_list

    if output == "list":
        return final_station_list
    elif output == "txt":
        mfd.write_dictionary(filename, ".txt", final_station_list, tabulated=False)
        return None
Esempio n. 3
0
def getElementsFromTimeserieTypeStation(stationID, timeserietypeID, output='list'):
    """Gets availabe weather elements on a given met station.

    :param stationID:       The station number.
    :param timeserietypeID: The timeserietypeID to be requested. Eg 0 is daily avarage.
    :param output:          3 types of output are available.
    :return:                A list of dictionary elements or makes files that are saved to the data directory.

    Output options:
        'list':         returns a list of dictionary elements.
        'xml':          returns NULL but saves a .xml file to the working folder.
        'txt':          returns NULL but saves a .txt file to the working folder. The separation value is tab.

    Example of xml that is iterated:

        <item xsi:type="ns3:no_met_metdata_ElementProperties">
            <description xsi:type="xsd:string">Nedbør for en løpende måned (30 døgn)</description>
            <elemCode xsi:type="xsd:string">RR_720</elemCode>
            <elemGroup xsi:type="xsd:string">Nedbør</elemGroup>
            <elemNo xsi:type="xsd:int">-1</elemNo>
            <fromdate xsi:type="xsd:dateTime">1957-01-30T00:00:00.000Z</fromdate>
            <language xsi:type="xsd:string">no</language>
            <name xsi:type="xsd:string">Nedbør 720 timer (siste 30 døgn)</name>
            <todate xsi:type="xsd:dateTime" xsi:nil="true"/>
            <unit xsi:type="xsd:string">mm</unit>
        </item>

    NOTE: if you only wish to read data for a given station check out
    http://eklima.met.no/eklimapub/servlet/ReportInfo?action=parameterinfo&tab=T_ELEM_DIURNAL&s=19710&la=no&co=NO

    NOTE2: Asker has id 19710 and Blindern 18700
    """

    print 'getWSklima: Requesting getElementsFromTimeserieTypeStation on {0} for {1}'\
        .format(stationID, timeserietypeID)

    url = "http://eklima.met.no/metdata/MetDataService?invoke=getElementsFromTimeserieTypeStation&timeserietypeID={1}&stnr={0}"\
        .format(stationID, timeserietypeID)
    wsKlimaRequest = re.get(url)

    stationElementList = []
    filename = '{0}Elements on {1}_{2}'.format(env.data_path, stationID, timeserietypeID)

    if output == 'xml':
        mfd.write_large_string(filename, '.xml', wsKlimaRequest.text.encode('utf-8'))
        return None

    # Take the request and make an element tree to be iterated
    root = etree.fromstring(wsKlimaRequest.content)
    for element in root.iter('item'):
        elemGroup = element.find('elemGroup').text.encode('utf-8')
        elemCode = element.find('elemCode').text.encode('utf-8')
        name = element.find('name').text.encode('utf-8')
        description = element.find('description').text.encode('utf-8')
        unit = element.find('unit').text.encode('utf-8')

        fromdate = element.find('fromdate').text
        fromdate = dt.datetime.strptime(fromdate[0:10], "%Y-%m-%d").date()
        todate = element.find('todate').text
        if todate is not None:
            todate = dt.datetime.strptime(todate[0:10], "%Y-%m-%d").date()

        stationElementList.append({'elemGroup': elemGroup,
                                   'elemCode': elemCode,
                                   'name': name,
                                   'description': description,
                                   'unit': unit,
                                   'fromdate': fromdate,
                                   'toDate': todate})

    if output == 'list':
        return stationElementList
    if output == 'txt':
        mfd.write_dictionary(filename, '.txt', stationElementList, tabulated=False)
        return None
Esempio n. 4
0
def getElementsFromTimeserieTypeStation(stationID, timeserietypeID, output="list"):
    """Gets availabe weather elements on a given met station.

    :param stationID:       The station number.
    :param timeserietypeID: The timeserietypeID to be requested. Eg 0 is daily avarage.
    :param output:          3 types of output are available.
    :return:                A list of dictionary elements or makes files that are saved to the data directory.

    Output options:
        'list':         returns a list of dictionary elements.
        'xml':          returns NULL but saves a .xml file to the working folder.
        'txt':          returns NULL but saves a .txt file to the working folder. The separation value is tab.

    Example of xml that is iterated:

        <item xsi:type="ns3:no_met_metdata_ElementProperties">
            <description xsi:type="xsd:string">Nedbør for en løpende måned (30 døgn)</description>
            <elemCode xsi:type="xsd:string">RR_720</elemCode>
            <elemGroup xsi:type="xsd:string">Nedbør</elemGroup>
            <elemNo xsi:type="xsd:int">-1</elemNo>
            <fromdate xsi:type="xsd:dateTime">1957-01-30T00:00:00.000Z</fromdate>
            <language xsi:type="xsd:string">no</language>
            <name xsi:type="xsd:string">Nedbør 720 timer (siste 30 døgn)</name>
            <todate xsi:type="xsd:dateTime" xsi:nil="true"/>
            <unit xsi:type="xsd:string">mm</unit>
        </item>

    NOTE: if you only wish to read data for a given station check out
    http://eklima.met.no/eklimapub/servlet/ReportInfo?action=parameterinfo&tab=T_ELEM_DIURNAL&s=19710&la=no&co=NO
    http://eklima.met.no/eklimapub/servlet/ReportInfo?action=parameterinfo&tab=T_ELEM_OBS&s=15270&la=no&co=NO

    NOTE2: Asker has id 19710 and Blindern 18700
    """

    print "getWSklima: Requesting getElementsFromTimeserieTypeStation on {0} for {1}".format(stationID, timeserietypeID)

    url = "http://eklima.met.no/metdata/MetDataService?invoke=getElementsFromTimeserieTypeStation&timeserietypeID={1}&stnr={0}".format(
        stationID, timeserietypeID
    )
    wsKlimaRequest = re.get(url)

    stationElementList = []
    filename = "{0}Elements on {1}_{2}".format(env.data_path, stationID, timeserietypeID)

    if output == "xml":
        mfd.write_large_string(filename, ".xml", wsKlimaRequest.text.encode("utf-8"))
        return None

    # Take the request and make an element tree to be iterated
    root = etree.fromstring(wsKlimaRequest.content)
    for element in root.iter("item"):
        elemGroup = element.find("elemGroup").text.encode("utf-8")
        elemCode = element.find("elemCode").text.encode("utf-8")
        name = element.find("name").text.encode("utf-8")
        description = element.find("description").text.encode("utf-8")
        unit = element.find("unit").text.encode("utf-8")

        fromdate = element.find("fromdate").text
        fromdate = dt.datetime.strptime(fromdate[0:10], "%Y-%m-%d").date()
        todate = element.find("todate").text
        if todate is not None:
            todate = dt.datetime.strptime(todate[0:10], "%Y-%m-%d").date()

        stationElementList.append(
            {
                "elemGroup": elemGroup,
                "elemCode": elemCode,
                "name": name,
                "description": description,
                "unit": unit,
                "fromdate": fromdate,
                "toDate": todate,
            }
        )

    if output == "list":
        return stationElementList
    if output == "txt":
        mfd.write_dictionary(filename, ".txt", stationElementList, tabulated=False)
        return None