def get_kdv(x_kdv): ''' Imports a x_kdv view from regObs and returns a dictionary with <key, value> = <ID, Name> An x_kdv is requested from the regObs api if a pickle file newer than a week exists. :param x_kdv: [string] x_kdv view :return dict: {} x_kdv as a dictionary Ex of use: aval_cause_kdv = get_kdv('AvalCauseKDV') Ex of url for returning values for IceCoverKDV in norwegian: http://api.nve.no/hydrology/regobs/v0.9.4/OData.svc/ForecastRegionKDV?$filter=Langkey%20eq%201%20&$format=json ''' kdv_file = '{0}{1}.pickle'.format(kdv_elements_folder, x_kdv) dict = {} if os.path.exists(kdv_file): ### Should be useful to test if the file is old and if so make a new one # max_file_age = 3 # file_date = time.ctime(os.path.getctime(kdv_file)) # date_limit = datetime.datetime.now() - datetime.timedelta(days=max_file_age) ### #print("Getting KDV from pickle:{0}".format(kdv_file)) dict = mp.unpickle_anything(kdv_file) else: url = 'http://api.nve.no/hydrology/regobs/{0}/OData.svc/{1}?$filter=Langkey%20eq%201%20&$format=json'\ .format(api_version, x_kdv) print("Getting KDV from URL:{0}".format(url)) kdv = requests.get(url).json() for a in kdv['d']['results']: try: if 'AvalCauseKDV' in url and a['ID'] > 9 and a['ID'] < 26: # this table gets special treatment dict[a["ID"]] = fe.remove_norwegian_letters(a["Description"]) else: dict[a["ID"]] = fe.remove_norwegian_letters(a["Name"]) except (RuntimeError, TypeError, NameError): pass mp.pickle_anything(dict, kdv_file) return dict
def get_tid_from_name(x_kdv, name): ''' Gets a xTID for a given xName from a xKDV element in regObs. In other words, it gets the ID for a given name. :param x_kdv: :param name: :return tid: ''' x_kdv = get_kdv(x_kdv) tid = -1 for xTID, xName in x_kdv.iteritems(): if xName == fe.remove_norwegian_letters(name): tid = xTID return tid
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×erietypeID=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×erietypeID={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
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×erietypeID=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×erietypeID={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