Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import numpy as np
- import rasterio
- import matplotlib.pyplot as plt
- import time
- from time import sleep
- from meeo_sso_cli import login, getToken
- import os
- import threading
- from functools import partial
- import random
- dic_years = ["2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010"]
- dic_m = ["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"]
- dic_months = ["00","01","02","03","04","05","06","07","08","09","10","11","12"]
- dic_weeks = ["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51","52"]
- collection = 'ERA-Interim_temp2m_4326_05'
- username = 'premia'
- password = '4gr1cult'
- year_ini = 2000#request.inputs['year_ini'][0].data
- year_fin = 2002#request.inputs['year_fin'][0].data
- month_ini = 1#request.inputs['month_ini'][0].data
- month_fin = 4#request.inputs['month_fin'][0].data
- #username = str(request.inputs['user'][0].data) #'figuera'
- #password = str(request.inputs['pass'][0].data) #'110586ra'
- #aggr = str(request.inputs['aggr'][0].data)
- aggr = 'm'
- class myThread (threading.Thread):
- def __init__(self, access_token,time_t):
- threading.Thread.__init__(self)
- self.access_token = access_token
- self.time_t = time_t
- self.result = None
- def run(self):
- print ("Starting " + self.name)
- for i in range(0,len(self.time_t)):
- download_file_paral(self.access_token, self.time_t[i])
- print ("Exiting " + self.name)
- def download_file_paral(self):
- #print(access_token)
- #print(time_t)
- #for i in range(0,len(time_t)):
- #print(str(time_t))
- st_mean = []
- #fname = '/home/meeo/wps/processes/result/'+str(random.randint(0,9999))+'.tif'
- fname = str(random.randint(0,9999))+'.tif'
- url = 'https://eodataservice.org/dar05?service=WCS&Request=GetCoverage&version=2.0.0&subset=unix('+str(self.time_t)+')&format=image/tiff&CoverageId=ERA-Interim_temp2m_4326_05'
- params = {'geometry': 'geometry=POLYGON ((11.930252963058445 45.055792724312049,11.986114233195178 45.100873749334674,12.019434990820596 45.075393169974056,12.035115347350205 45.038152323216238,12.005714678857188 45.003851543307718,11.965533765250065 44.989151209061205,11.921432762510539 44.995031342759809,11.97533398808107 45.027372078102132,11.930252963058445 45.055792724312049))',
- 'format':'image/tiff',
- 'CoverageId':'ERA-Interim_temp2m_4326_05',
- 'token': self.access_token}
- #execute query
- result = requests.post(url, params=params)
- #print(result)
- with open(fname, 'wb') as f:
- #print(fname)
- f.write(result.content)
- #read data and compute mean value
- src = rasterio.open(fname)
- array = src.read()
- print(np.mean(array))
- st_mean.append(np.mean(array))
- #print(np.mean(array))
- return st_mean
- def get_result( self ):
- return self.result
- def monthly_compute(year_ini, year_fin, month_ini, month_fin, dic_months, collection, access_token):
- times = []
- start = time.time()
- #compute time steps
- #fname = "/home/meeo/wps/processes/result/image.tif"
- st_mean = np.zeros((year_fin-year_ini+1)*(month_fin-month_ini+1)) #60
- #x = np.zeros(len(dic_days)*delta_month)
- x = []
- #assume aggregation is every hour for one daydic_hours_e
- for y in range(year_ini, year_fin+1):
- for m in range(month_ini,month_fin+1):
- if m%2 == 0 and m != 2: #that means pair months
- time_t = str(y)+'-'+dic_months[m]+'-01T00:00:00,'+str(y)+'-'+dic_months[m]+'-30T23:59:59'
- elif m == 2:
- time_t = str(y)+'-'+dic_months[m]+'-01T00:00:00,'+str(y)+'-'+dic_months[m]+'-28T23:59:59'
- else:
- time_t = str(y)+'-'+dic_months[m]+'-01T00:00:00,'+str(y)+'-'+dic_months[m]+'-31T23:59:59'
- x.append(str(y) + '-' +dic_m[m-1] )
- times.append(time_t)
- return x, times
- def weekly_compute(year_ini, year_fin, month_ini, month_fin, dic_months, collection, access_token):
- times = []
- start = time.time()
- #compute time steps
- dic_days_s = ["01","07","14","21"]
- dic_days_31 = ["08","15","22","31"]
- dic_days_30 = ["08","15","22","30"]
- dic_days_28 = ["08","15","22","28"]
- #fname = "/home/meeo/wps/processes/result/image.tif"
- st_mean = np.zeros((year_fin-year_ini+1)*(month_fin-month_ini+1)*len(dic_days_31)) #60
- #x = np.zeros(len(dic_days)*delta_month)
- x = []
- #assume aggregation is every hour for one daydic_hours_e
- for y in range(year_ini, year_fin+1):
- for m in range(month_ini,month_fin+1):
- for d in range(0,4):
- if m%2 == 0 and m != 2: #that means pair months
- time_t = str(y)+'-'+dic_months[m]+'-'+dic_days_s[d]+'T00:00:00,'+str(y)+'-'+dic_months[m]+'-'+dic_days_30[d]+'T23:59:59'
- elif m == 2:
- time_t = str(y)+'-'+dic_months[m]+'-'+dic_days_s[d]+'T00:00:00,'+str(y)+'-'+dic_months[m]+'-'+dic_days_28[d]+'T23:59:59'
- else:
- time_t = str(y)+'-'+dic_months[m]+'-'+dic_days_s[d]+'T00:00:00,'+str(y)+'-'+dic_months[m]+'-'+dic_days_30[d]+'T23:59:59'
- x.append(str(y) + '-' +dic_m[m-1] )
- times.append(time_t)
- return x, times
- def parallel_runs(x, time_t):
- start = time.time()
- print('Start processing in parallel ...')
- #cores = multiprocessing.cpu_count()
- #pool = multiprocessing.Pool(processes=cores) #processes max = n of cores
- #part = partial(download_file_paral, access_token)
- threadLock = threading.Lock()
- threads = []
- result_list = []
- thread1 = myThread(access_token,time_t[0:int(len(time_t)/2)])
- thread2 = myThread(access_token,time_t[int(len(time_t)/2):int(len(time_t))])
- thread1.start()
- thread2.start()
- threads.append(thread1)
- threads.append(thread2)
- #thread1.join()
- #thread2.join()
- for t in threads:
- t.join()
- for thread in threads:
- if thread.get_result() is not None:
- result_list.append( thread.get_result() )
- #result_list = pool.map(unwrap_self_f, zip([part]*len(part), time_t))
- #result_list = pool.map(part, time_t)
- os.system('rm *.tif')
- #print(result_list)
- print("elapsed time: ", time.time()-start, "seconds")
- return result_list
- # def download_file_paral(access_token,time_t):
- # print(access_token)
- # #print(time_t)
- # #for i in range(0,len(time_t)):
- # print(str(time_t))
- # st_mean = []
- # #fname = '/home/meeo/wps/processes/result/'+str(random.randint(0,9999))+'.tif'
- # fname = str(random.randint(0,9999))+'.tif'
- # url = 'https://eodataservice.org/dar05?service=WCS&Request=GetCoverage&version=2.0.0&subset=unix('+str(time_t)+')&format=image/tiff&CoverageId=ERA-Interim_temp2m_4326_05'
- # params = {'geometry': 'geometry=POLYGON ((11.930252963058445 45.055792724312049,11.986114233195178 45.100873749334674,12.019434990820596 45.075393169974056,12.035115347350205 45.038152323216238,12.005714678857188 45.003851543307718,11.965533765250065 44.989151209061205,11.921432762510539 44.995031342759809,11.97533398808107 45.027372078102132,11.930252963058445 45.055792724312049))',
- # 'format':'image/tiff',
- # 'CoverageId':'ERA-Interim_temp2m_4326_05',
- # 'token': access_token}
- #
- # #execute query
- # result = requests.post(url, params=params)
- # #print(result)
- # with open(fname, 'wb') as f:
- # #print(fname)
- # f.write(result.content)
- # #read data and compute mean value
- # src = rasterio.open(fname)
- # array = src.read()
- # print(np.mean(array))
- # st_mean.append(np.mean(array))
- # #print(np.mean(array))
- # return st_mean
- ##### MAIN #######
- access_token = login( username, password )[ 'access_token' ]
- if aggr == 'm':
- x,time_t = monthly_compute(year_ini,year_fin,month_ini,month_fin,dic_months,collection, access_token)
- else:
- x,time_t = weekly_compute(year_ini,year_fin,month_ini,month_fin,dic_months,collection, access_token)
- air_temp = parallel_runs(x,time_t)
- print(x)
- print(air_temp)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement