def get_ice_cover(LocationName, fromDate, toDate): """ Method returns a list of IceCover objects from regObs between fromDate to toDate. :param LocationName: [string/list] name as given in regObs in ObsLocation table :param fromDate: [string] The from date as 'YYYY-MM-DD' :param toDate: [string] The to date as 'YYYY-MM-DD' :return: http://api.nve.no/hydrology/regobs/v0.9.4/Odata.svc/IceCoverObsV?$filter= DtObsTime%20gt%20datetime%272013-11-01%27%20and%20 DtObsTime%20lt%20datetime%272014-06-01%27%20and%20 LocationName%20eq%20%27Hakkloa%20nord%20372%20moh%27%20and%20 LangKey%20eq%201 """ iceCoverList = [] if isinstance(LocationName, types.ListType): for l in LocationName: iceCoverList = iceCoverList + get_ice_cover(l, fromDate, toDate) else: view = 'IceCoverObsV' OdataLocationName = fe.change_unicode_to_utf8hex(LocationName) oDataQuery = "DtObsTime gt datetime'{0}' and " \ "DtObsTime lt datetime'{1}' and " \ "LocationName eq '{2}' and " \ "LangKey eq 1".format(fromDate, toDate, OdataLocationName) # get data for current view and dates url = "http://api.nve.no/hydrology/regobs/{0}/Odata.svc/{2}?$filter={1}&$format=json".decode('utf8').format(api_version, oDataQuery, view) data = requests.get(url).json() datalist = data['d']['results'] for ic in datalist: iceCoverDate = pz.normal_time_from_unix_time(int(ic['DtObsTime'][6:-2])) iceCoverName = ic['IceCoverName'] iceCoverBefore = ic['IceCoverBeforeName'] cover = ice.IceCover(iceCoverDate, iceCoverName, iceCoverBefore, LocationName) cover.set_regid(int(ic['RegID'])) iceCoverList.append(cover) return iceCoverList
def make_weather_element_list_from_url(url, stationID, elementID, methodReference): """Returns a list of weatherElements given the url to a request on Chartserver/ShowData.aspx. This is common code to the "get" methods. :param url [string]: URL for the datarequest to chartserver/ShowData :param stationID [string]: Station ID in hydra. Ex: stationID = '6.24.4' :param elementID [string]: Element ID in hydra. Ex: elementID = '17.1' :param methodReference [dictionary]: Reference added to the metadata. Ex: {'MethodName': 'Chartserver - getStationdata'} :return [list[WeatherElement]]: List of WeatherElement objects. """ print "getChartserverdata: Requesting {0}".format(url) request = requests.get(url) if request.status_code == 500: if "The length of the string exceeds the value set on the maxJsonLength property." in request.content: return "Request less data." else: return "HTTP 500 for unknown reason." datareq = request.json() seriesPoints = datareq[0][u'SeriesPoints'] legendText = datareq[0][u'LegendText'] weatherElementList = [] for sp in seriesPoints: value = sp[u'Value'] date = pz.normal_time_from_unix_time(int(sp[u'Key'][6:-2])) we = WeatherElement(stationID, date, elementID, value) we.Metadata.append(methodReference) we.Metadata.append({'URL': url}) we.Metadata.append({'LegendText': legendText}) weatherElementList.append(we) # Chartserver/ShowData.aspx returns half hour values and on request it returns [from, to]. That is, # it includes the to value so if the request is nested the data will contain doublets of values # om the "to dates". These are therefore taken out in this method. del weatherElementList[-1] return weatherElementList
def make_weather_element_list_from_url(url, stationID, elementID, methodReference): """Returns a list of weatherElements given the url to a request on Chartserver/ShowData.aspx. This is common code to the "get" methods. :param url [string]: URL for the datarequest to chartserver/ShowData :param stationID [string]: Station ID in hydra. Ex: stationID = '6.24.4' :param elementID [string]: Element ID in hydra. Ex: elementID = '17.1' :param methodReference [dictionary]: Reference added to the metadata. Ex: {'MethodName': 'Chartserver - getStationdata'} :return [list[WeatherElement]]: List of WeatherElement objects. """ print "getChartserverdata: Requesting {0}".format(url) request = requests.get(url) if request.status_code == 500: if "The length of the string exceeds the value set on the maxJsonLength property." in request.content: return "Request less data." else: return "HTTP 500 for unknown reason." datareq = request.json() seriesPoints = datareq[0][u'SeriesPoints'] legendText = datareq[0][u'LegendText'] weatherElementList = [] for sp in seriesPoints: value = sp[u'Value'] date = pz.normal_time_from_unix_time(int(sp[u'Key'][6:-2])) we = WeatherElement(stationID, date, elementID, value) we.Metadata.append(methodReference) we.Metadata.append({'URL':url}) we.Metadata.append({'LegendText':legendText}) weatherElementList.append(we) # Chartserver/ShowData.aspx returns half hour values and on request it returns [from, to]. That is, # it includes the to value so if the request is nested the data will contain doublets of values # om the "to dates". These are therefore taken out in this method. del weatherElementList[-1] return weatherElementList
def get_ice_thickness(LocationName, fromDate, toDate): ''' Method returns a list of ice thickness between two dates for a given location in regObs. :param LocationName: [string/list] name as given in regObs in ObsLocation table. Multiploe locations posible :param fromDate: [string] The from date as 'YYYY-MM-DD' :param toDate: [string] The to date as 'YYYY-MM-DD' :return: ''' ice_columns = [] if isinstance(LocationName, types.ListType): for l in LocationName: ice_columns = ice_columns + get_ice_thickness(l, fromDate, toDate) else: view = 'IceThicknessV' OdataLocationName = fe.change_unicode_to_utf8hex(LocationName) # Crazyshitencoding oDataQuery = "DtObsTime gt datetime'{0}' and " \ "DtObsTime lt datetime'{1}' and " \ "LocationName eq '{2}' and " \ "LangKey eq 1".format(fromDate, toDate, OdataLocationName) # get data for current view and dates url = "http://api.nve.no/hydrology/regobs/{0}/Odata.svc/{2}?$filter={1}&$format=json".decode('utf8').format(api_version, oDataQuery, view) data = requests.get(url).json() datalist = data['d']['results'] for ic in datalist: date = pz.normal_time_from_unix_time(int(ic['DtObsTime'][6:-2])) RegID = ic['RegID'] layers = get_ice_thickness_layers(RegID) if len(layers) == 0: layers = [ ice.IceLayer(float(ic['IceThicknessSum']), 'unknown') ] ice_column = ice.IceColumn(date, layers) ice_column.add_metadata('RegID', RegID) ice_column.add_metadata('LocatonName', LocationName) ice_column.add_layer_at_index(0, ice.IceLayer(ic['SlushSnow'], 'slush')) ice_column.add_layer_at_index(0, ice.IceLayer(ic['SnowDepth'], 'snow')) ice_column.merge_and_remove_excess_layers() ice_column.update_draft_thickness() ice_column.update_top_layer_is_slush() iha = ic['IceHeightAfter'] # if ice height after is not given I make an estimate so that I know where to put it in the plot if iha is None: ice_column.update_water_line() ice_column.add_metadata('IceHeightAfter', 'Modeled') iha = ice_column.draft_thickness - ice_column.water_line if ice_column.top_layer_is_slush: iha = iha + const.snow_pull_on_water ice_column.water_line = ice_column.draft_thickness - float(iha) if ice_column.top_layer_is_slush is True: ice_column.water_line -= ice_column.column[0].height ice_columns.append(ice_column) return ice_columns