def upload_signals_to_amphora(irradiation_amphora_id, signals): #upload data to amphora data website configuration = Configuration() auth_api = amphora_client.AuthenticationApi( amphora_client.ApiClient(configuration)) token_request = amphora_client.TokenRequest(username=os.getenv('username'), password=os.getenv('password')) try: # Gets a token res = auth_api.authentication_request_token( token_request=token_request) configuration.api_key["Authorization"] = "Bearer " + res # create an instance of the Users API, now with Bearer token users_api = amphora_client.UsersApi( amphora_client.ApiClient(configuration)) me = users_api.users_read_self() print(me) except ApiException as e: print("Exception when calling AuthenticationAPI: %s\n" % e) amphora_api = amphora_client.AmphoraeApi( amphora_client.ApiClient(configuration)) try: print(signals) amphora_api.amphorae_signals_upload_signal_batch( irradiation_amphora_id, request_body=signals) except ApiException as e: print("Exception when calling AmphoraeApi: %s\n" % e)
def upload_signals_to_amphora(site_id, amphora_id, state_service): if state_service == 'NSW': var_string = variable_string() var_dict = variable_codes() NSW_params = query_params()[state_service] signals = query_ts_values(site_id, var_string, var_dict, NSW_params) elif state_service == 'VIC': var_string = vic_variable_string() var_dict = vic_variable_codes() VIC_params = query_params()[state_service] signals = query_ts_values(site_id, var_string, var_dict, VIC_params) else: print( "error in upload_signals_to_amphora: state_service must be 'NSW' or 'VIC'" ) return if signals == None: print("No signals received") return print(signals) # LOAD configuration = Configuration() # Create an instance of the Authentication class auth_api = amphora_client.AuthenticationApi( amphora_client.ApiClient(configuration)) token_request = amphora_client.TokenRequest(username=username, password=password) try: print("Logging In") token = auth_api.authentication_request_token( token_request=token_request) configuration.api_key["Authorization"] = "Bearer " + str(token) print("Logged in") client = amphora_client.ApiClient(configuration) amphora_api = amphora_client.AmphoraeApi(client) amphora = amphora_api.amphorae_read(amphora_id) print(f'Uploading signals to {amphora.name} {amphora.id}') print(signals) amphora_api.amphorae_signals_upload_signal_batch( amphora.id, request_body=signals) # this sends the data to Amphora Data print(f'Sent {len(signals)} signals') except ApiException as e: print("Exception: %s\n" % e) raise e
def establish_connection(): ''' create token and return it status: working with v0.3.0! ''' configuration = Configuration() configuration.host = 'https://beta.amphoradata.com' # create instance of API class, creates an unauthenticated client auth_api = amphora_client.AuthenticationApi( amphora_client.ApiClient(configuration)) # if function throws ValueError 'username'/'password' must not be none, then the environment # variables are not set, c.f. setup-EForecast.sh and readme.MD section Quickstart token = auth_api.authentication_request_token( token_request=amphora_client.TokenRequest(username=useful['username'], password=useful['password'])) return token
def create_or_update_amphorae(amphora_map, location_info): # LOAD configuration = Configuration() # Create an instance of the Authentication class auth_api = amphora_client.AuthenticationApi( amphora_client.ApiClient(configuration)) token_request = amphora_client.TokenRequest(username=os.getenv('username'), password=os.getenv('password')) new_map = dict() try: print("Logging in") token = auth_api.authentication_request_token( token_request=token_request) configuration.api_key["Authorization"] = "Bearer " + str(token) print("Logged in") client = amphora_client.ApiClient(configuration) amphora_api = amphora_client.AmphoraeApi(client) #keys are postcodes for key in amphora_map: id = amphora_map[key] if (id == None): # we have to create an Amphora ghiloc = location_info[key] locname = ghiloc['name'] print(f'Creating new Amphora for location {locname}') # create the details of the Amphora name = 'Solar Irradiance GHI: ' + ghiloc[ 'name'] + ' (' + ghiloc['state'] + ')' desc = 'Calculated data, from ' + ghiloc['name'] + \ '. PostCode: ' + str(key) + '\r\n' + \ 'Properties include: \r\n- temperature (degrees C)\r\n' + \ '- Solar Zenith Angle\r\n- Global Horizontal Irradiance (W/m^2)\r\n' + \ '- Cloud Cover (%)\r\nTemperature and cloud cover signals supplied by Weatherzone' labels = 'Weather,forecast,solar,timeseries' ts_cs_id = 'Weatherzone_Forecast' dto = amphora_client.CreateAmphora( name=name, description=desc, price=0, lat=ghiloc['lat'], lon=ghiloc['long'], labels=labels, terms_and_conditions_id=ts_cs_id) res = amphora_api.amphorae_create(create_amphora=dto) # now create the signals print("Creating Signals") for s in signals(): amphora_api.amphorae_signals_create_signal(res.id, signal=s) new_map[key] = res.id else: a = amphora_api.amphorae_read(id) print(f'Using existing amphora: {a.name}') new_map[key] = id existing_signals = amphora_api.amphorae_signals_get_signals(id) if (len(existing_signals) > 0): print('Signals exist already') else: print('Adding signals') for s in signals(): amphora_api.amphorae_signals_create_signal(id, signal=s) except ApiException as e: print("Error Create or update amphorae: %s\n" % e) raise e return new_map
import amphora_client from amphora_client.rest import ApiException from amphora_client.configuration import Configuration import os from src.MLA import * EYCI_id = "1bd74490-ccf6-43d1-99f1-ef53f60c293c" ESTLI_id = "35f32a02-5abf-417b-a8ec-40dc7ac3ff7d" EYCI_dict, ESTLI_dict = get_EYCI_ESTLI_data() #upload data to amphora data website configuration = Configuration() auth_api = amphora_client.AuthenticationApi( amphora_client.ApiClient(configuration)) token_request = amphora_client.TokenRequest(username=os.getenv('username'), password=os.getenv('password')) try: # Gets a token res = auth_api.authentication_request_token(token_request=token_request) configuration.api_key["Authorization"] = "Bearer " + res # create an instance of the Users API, now with Bearer token users_api = amphora_client.UsersApi( amphora_client.ApiClient(configuration)) me = users_api.users_read_self() except ApiException as e: print("Exception when calling AuthenticationAPI: %s\n" % e) amphora_api = amphora_client.AmphoraeApi( amphora_client.ApiClient(configuration))
def create_or_update_amphorae(amphora_map, location_info): # LOAD configuration = Configuration() # Create an instance of the Authentication class auth_api = amphora_client.AuthenticationApi( amphora_client.ApiClient(configuration)) token_request = amphora_client.TokenRequest(username=username, password=password) new_map = dict() try: print("Logging in") token = auth_api.authentication_request_token( token_request=token_request) configuration.api_key["Authorization"] = "Bearer " + str(token) print("Logged in") client = amphora_client.ApiClient(configuration) amphora_api = amphora_client.AmphoraeApi(client) for key in amphora_map: id = amphora_map[key] if (id == None): # we have to create an Amphora waterloc = location_info[key] locname = waterloc['name'] print(f'Creating new Amphora for location {locname}') # create the details of the Amphora name = 'Water Information: ' + waterloc[ 'name'] + ' (' + waterloc['state'] + ')' desc = 'WaterNSW data, from ' + waterloc[ 'name'] + '. WaterNSW site id: ' + key labels = 'Water,actuals,timeseries' ts_cs_id = 'Creative_Commons_4p0' #TODO: add terms_and_conditions_id when it's on the website #terms_and_conditions_id = '' dto = amphora_client.CreateAmphora( name=name, description=desc, labels=labels, price=0, lat=waterloc['lat'], lon=waterloc['long'], terms_and_conditions_id=ts_cs_id) res = amphora_api.amphorae_create(create_amphora=dto) # now create the signals print("Creating Signals") for s in signals(): amphora_api.amphorae_signals_create_signal(res.id, signal=s) new_map[key] = res.id else: a = amphora_api.amphorae_read(id) print(f'Using existing amphora: {a.name}') new_map[key] = id existing_signals = amphora_api.amphorae_signals_get_signals(id) if (len(existing_signals) > 0): print('Signals exist already') else: print('Adding signals') for s in signals(): amphora_api.amphorae_signals_create_signal(id, signal=s) except ApiException as e: print("Error Create or update amphorae: %s\n" % e) raise e return new_map
import amphora_client as a10a from amphora_client.configuration import Configuration from amphora_extensions.file_uploader import FileUploader # Import non-Amphora librarys from array import array import os import numpy as np import time from datetime import datetime, timedelta # Login to amphoradata.com configuration = Configuration() configuration.host = "https://app.amphoradata.com" auth_api = a10a.AuthenticationApi(a10a.ApiClient(configuration)) token_request = a10a.TokenRequest(username=os.getenv('username'), password=os.getenv('password')) res = auth_api.authentication_request_token(token_request=token_request) configuration.api_key["Authorization"] = "Bearer " + res amphora_api = a10a.AmphoraeApi(a10a.ApiClient(configuration)) # Define model function def time_product(date_time): time_hour = date_time.hour time_minute = date_time.minute time_second = date_time.second time_prod = time_hour * time_minute * time_second return time_prod
def get_temps_cloud(id, start_date, end_date, required_data): configuration = Configuration() configuration.host = "https://beta.amphoradata.com" # Create an instance of the auth API class auth_api = a10a.AuthenticationApi(a10a.ApiClient(configuration)) token_request = a10a.TokenRequest(username=os.getenv('username'), password=os.getenv('password')) try: # Gets a token t1_start = time.perf_counter() res = auth_api.authentication_request_token( token_request=token_request) t1_stop = time.perf_counter() print("Elapsed time:", t1_stop - t1_start) # print performance indicator configuration.api_key["Authorization"] = "Bearer " + res amphora_api = a10a.AmphoraeApi(a10a.ApiClient(configuration)) print(f'Getting signals for: {amphora_api.amphorae_read(id).name}') signals = amphora_api.amphorae_signals_get_signals(id) properties = list((s._property for s in signals)) print(properties) ts_api = a10a.TimeSeriesApi(a10a.ApiClient( configuration)) # the API for interacting with time series # Create a DateTimeRange to describe over what period we want data. time_range = a10a.DateTimeRange(_from=start_date, to=end_date) # Create a variable object for getting temperature data temperatureVariable = a10a.NumericVariable( kind="numeric", value=a10a.Tsx(tsx="$event.temperature"), aggregation=a10a.Tsx("avg($value)")) get_series = a10a.GetSeries( [id], search_span=time_range, inline_variables={"temperature": temperatureVariable}) time_series_data = ts_api.time_series_query_time_series( a10a.QueryRequest(get_series=get_series)) print( f'Got {len(time_series_data.timestamps)} datapoints and {len(time_series_data.properties)} properties' ) # access the data in time_series_data.properties print("-----------") temp_values = next(value for value in time_series_data.properties if value.name == 'temperature') # Create a variable object for getting cloudCover data cloudCoverVariable = a10a.NumericVariable( kind="numeric", value=a10a.Tsx(tsx="$event.cloudCover"), aggregation=a10a.Tsx("avg($value)")) get_series = a10a.GetSeries( [id], search_span=time_range, inline_variables={"cloudCover": cloudCoverVariable}) time_series_data = ts_api.time_series_query_time_series( a10a.QueryRequest(get_series=get_series)) print( f'Got {len(time_series_data.timestamps)} datapoints and {len(time_series_data.properties)} properties' ) # access the data in time_series_data.properties print("-----------") cloudCover_values = next(value for value in time_series_data.properties if value.name == 'cloudCover') wtVar = a10a.NumericVariable(kind="numeric", value=a10a.Tsx(tsx="$event.wt"), aggregation=a10a.Tsx("avg($value)")) get_series = a10a.GetSeries([id], search_span=time_range, inline_variables={"wt": wtVar}) time_series_data = ts_api.time_series_query_time_series( a10a.QueryRequest(get_series=get_series)) wt_values = next(value for value in time_series_data.properties if value.name == 'wt') #create dictionary of signals, with timestamp: [temp, cloud_cover] for each entry if both required temp_cloud_dict = {} wt_dict = {} for i in range(len(temp_values.values)): #update the dictionary with the value for the most recent write_time #and override wt_dict entries if so if time_series_data.timestamps[i] not in temp_cloud_dict.keys() or \ wt_values.values[i] > wt_dict[time_series_data.timestamps[i]]: if required_data == 'both': temp_cloud_dict[time_series_data.timestamps[i]] = [ temp_values.values[i], cloudCover_values.values[i] ] elif required_data == 'cloud_cover': temp_cloud_dict[time_series_data.timestamps[ i]] = cloudCover_values.values[i] elif required_data == 'temperature': temp_cloud_dict[ time_series_data.timestamps[i]] = temp_values.values[i] else: raise ValueError( "required_data must be 'both', 'temperature' or 'cloud_cover'" ) wt_dict[time_series_data.timestamps[i]] = wt_values.values[i] except ApiException as e: print("Exception when calling API: %s\n" % e) return temp_cloud_dict