def generate_plot(): temp_num = rd_temp.dbsize( ) # Get the number of temperature dictionaries in database ''' Use the "random" module to randomly-generate a temp field to develop a time-series line plot of from the list: "GALT", "GMAXLT", and "GMINLT": ''' temp_field_list = [ "GALT (Celsius)", "GMAXLT (Celsius)", "GMINLT(Celsius)" ] temp_field = temp_field_list[random.randint(0, 2)] # Use matplolib to generate plot of time-series temperature data: time_array = numpy.array([]) # Initialize array to store time data temperature_array = numpy.array( []) # Initialize array to store temperature data # Use for loop to append values to array storing time and temperature field values: for i in range(0, temp_num): temp_key = i # Assign keyname to temperature time_array = numpy.append(time_array, i) temperature_array = numpy.append( temperature_array, float(rd_temp.hget(temp_key, temp_field))) # Use "Matplotlib" to generate a line plot of the time-series dataset: plt.plot(time_array, temperature_array) plt.title("Plot of " + temp_field + " vs. Time (Months) Starting from 01/01/1930 ") plt.xlabel("Time (Months)") plt.ylabel(temp_field) plt.savefig('/Time-Series-Line-Plot.png') # Open image and add to redis database containing job information: with open('/Time-Series-Line-Plot.png', 'rb') as f: img = f.read() rd_jobs.hset(f'job.{jid}', "image", img) # Set the image in redis database return_status = "Plot Generated Successfully" return return_status
def update_data_api(): temp_num = rd_temp.dbsize() # Get the number of temperature dictionaries in database # Query the UUID for date with historical temperature value(s) to be updated: UUID = request.args.get('UUID') # Get the temp_key from the database using UUID: for i in range(0, temp_num ): UUID_rd = rd_temp.hget(i, "UUID")# Get the UUID from database ''' If UUID returned from database equals UUID query value, define "temp_key" value and break ''' if (UUID_rd == UUID): # Assign keyname to temperature: temp_key = i break # Query the Global Average Land Temperature (GALT) in Celsius GALT_str = request.args.get('Global_Average_Land_Temp') # If "GALT_str" query value is defined update value in database: if (GALT_str != None): GALT = float(GALT_str) rd_temp.hset(temp_key, "GALT (Celsius)", GALT) # Query the Global Maximum Land Temperature (GMAXLT) in Celsius GMAXLT_str = request.args.get('Global_Maximum_Land_Temp') # If "GMAXLT_str" query value is defined update value in database: if (GMAXLT_str != None): GMAXLT = float(GMAXLT_str) rd_temp.hset(temp_key, "GMAXLT (Celsius)", GMAXLT) # Query the Global Minimum Land Temperature (GMINLT) in Celsius GMINLT_str = request.args.get('Global_Minimum_Land_Temp') # If "GMINLT_str" query value is defined update value in database: if (GMINLT_str!= None): GMINLT = float(GMINLT_str) rd_temp.hset(temp_key, "GMINLT(Celsius)", GMINLT) return_status = "Data Updated Successfully" return return_status
def delete_data_api(): temp_num = rd_temp.dbsize() # Get the number of temperature dictionaries in database # Query the date bounds for historical temperature value(s) to be deleted: date_low = request.args.get('date_lower_bound') date_up = request.args.get('date_upper_bound') ''' Extract date parameters from lower bound: ''' year_low = int(date_low[6:10]) month_low = int(date_low[0:2]) day_low = int(date_low[3:5]) ''' Extract date parameters from upper bound: ''' year_up = int(date_up[6:10]) month_up = int(date_up[0:2]) day_up = int(date_up[3:5]) temp_store = [] # Initialize list to store data outside range of deletion: # Use a for loop to return field values between start and end date: for i in range(0, temp_num): temp_key = i # Assign keyname to temperature ''' Extract the date parameters from the database: ''' date_db = rd_temp.hget(temp_key, "dt") year_db = int(date_db[6:10]) month_db = int(date_db[0:2]) day_db = int(date_db[3:5]) sto_cond = True # Initialize storage condition to true ''' Use while loop with nested if statements to check if date from database falls within range: ''' while (sto_cond == True): if (year_db < year_up and year_db > year_low): sto_cond = False break elif (year_db == year_up and month_db < month_up and month_db > month_low): sto_cond = False break elif (year_db == year_low and month_db > month_low and month_db < month_up): sto_cond = False break elif (year_db == year_up and month_db == month_up and day_db < day_up and day_db > day_low): sto_cond = False break elif (year_db == year_low and month_db == month_low and day_db > day_low and day_db < day_up): sto_cond = False break break ''' Append field values from database to data storage list if the condition "sto_cond" is true: ''' if (sto_cond == True): temp_store.append(rd_temp.hgetall(temp_key)) temp_store_num = len(temp_store) rd_temp.flushall() # Clear all elements from the redis database ''' Store new data structure in redis database: ''' for i in range(0,temp_store_num): temp_key = i # Assign keyname to temperature ''' Set data points that lie outside range of deletion to database: ''' rd_temp.hmset(temp_key, {"dt": temp_store[i]["dt"], "GALT (Celsius)": \ temp_store[i]["GALT (Celsius)"], "GMAXLT (Celsius)": \ temp_store[i]["GMAXLT (Celsius)"], "GMINLT(Celsius)": \ temp_store[i]["GMINLT(Celsius)"], "UUID": \ temp_store[i]["UUID"]}) return_status = "Temperature Data Deleted Successfully" return return_status
def retrieve_data_api(): temp_num = rd_temp.dbsize() # Get the number of temperature dictionaries in database # Query the dates for historical temperature value(s) to be created and stored: date_low = request.args.get('date_lower_bound') date_up = request.args.get('date_upper_bound') ''' Extract date parameters from lower bound: ''' year_low = int(date_low[6:10]) month_low = int(date_low[0:2]) day_low = int(date_low[3:5]) ''' Extract date parameters from upper bound: ''' year_up = int(date_up[6:10]) month_up = int(date_up[0:2]) day_up = int(date_up[3:5]) temp_output = [] # Initialize list to store output data # Use a for loop to return field values between start and end date: for i in range(0, temp_num): temp_key = i # Assign keyname to temperature ''' Extract the date parameters from the database: ''' date_db = rd_temp.hget(temp_key, "dt") year_db = int(date_db[6:10]) month_db = int(date_db[0:2]) day_db = int(date_db[3:5]) out_cond = True # Initialize output condition to true ''' Use while loop with nested if statements to check if date from database falls within range: ''' while (out_cond == True): if (year_db > year_up or year_db < year_low): out_cond = False break elif (year_db == year_up and month_db > month_up): out_cond = False break elif (year_db == year_low and month_db < month_low): out_cond = False break elif (year_db == year_up and month_db == month_up and day_db > day_up): out_cond = False break elif (year_db == year_low and month_db == month_low and day_db < day_low): out_cond = False break break ''' Append field values from database to output list if the condition "out_cond" is true: ''' if (out_cond == True): temp_output.append(rd_temp.hgetall(temp_key)) # Store data in json formatted dictionary: temp_dict = {"Earth Surface Temperature Data": temp_output} return temp_dict
def create_data_api(): temp_num = rd_temp.dbsize() # Get the number of temperature dictionaries in database # Query the data for historical temperature value(s) to be created and stored: date_in = request.args.get('date') # Query the Global Average Land Temperature (GALT) in Celsius GALT_str = request.args.get('Global_Average_Land_Temp') GALT = float(GALT_str) # Query the Global Maximum Land Temperature (GMAXLT) in Celsius GMAXLT_str = request.args.get('Global_Maximum_Land_Temp') GMAXLT = float(GMAXLT_str) # Query the Global Minimum Land Temperature (GMINLT) in Celsius GMINLT_str = request.args.get('Global_Minimum_Land_Temp') GMINLT = float(GMINLT_str) create_cond = True # Initialize data creation condition to true for i in range(0,temp_num): ''' Return date parameters for "temp_key" from database: ''' temp_key = i # Assign keyname to temperature date_db = rd_temp.hget(temp_key, "dt") # Return date for "temp_key" ''' Conditional to compare input date of creation to date in database: ''' if (date_in == date_db): create_cond = False break if (create_cond == True): temp_key = temp_num + 1 # Set value of date "date_in" in database: rd_temp.hset(temp_key, "dt", date_in) # Set value of GALT in database: rd_temp.hset(temp_key, "GALT (Celsius)", GALT) # Set value of GMAXLT in database: rd_temp.hset(temp_key, "GMAXLT (Celsius)", GMAXLT) # Set value of GMINLT in database: rd_temp.hset(temp_key, "GMINLT(Celsius)", GMINLT) # Invoke "uuid4()" method to assign random unique identifier to temperature UUID = str(uuid.uuid4()) rd_temp.hset(temp_key, "UUID", UUID) return_status = "Data Created and Stored Successfully" return return_status