# -*- coding: utf-8 -*- """ Created on Mon Feb 3 12:54:11 2020 @original author: Saniya Khan @adopted code: Valentin Heimhuber This code uses a coords.csv file with a list of lat lon locations to extract SILO gridded climatology data at each location and writes it into a csv file SILO gridded data is 0.05° × 0.05° or 50x50km More information about the interpolated griddes SILO datasets can be found here: https://www.longpaddock.qld.gov.au/silo/faq/#faq5 Available variables are: """ from __future__ import unicode_literals import urllib.request import urllib.parse import pandas as pd from io import StringIO from itertools import repeat api_url = 'https://www.longpaddock.qld.gov.au/cgi-bin/silo' path_to_coordscsv_file = 'J:/Project/wrl2018064 Fisheries RAP/04_Working/05_Modelling/RMA/HEMIP/Global_Data/Climatology/SILO/' #replace by actual geocodes #geocode=pd.DataFrame([['YANKALILLA','-35.494262', '138.362596'], # ['PORT WAKEFIELD','-34.185349', '138.155379']],columns=['Brigade','latitude','longitude']) list_of_weather=[] weather=pd.DataFrame() def getGeocode(): geocode= pd.read_csv(path_to_coordscsv_file + "coords.csv") print(geocode.columns) geocode.set_index('Brigade') #print("dfdfdfD") #print(geocode) return geocode; def buildUrl(lat,long): params = { 'format': 'standard', 'lat': str(lat), 'lon': str(long), 'start': '20090101', 'finish': '20181231', 'username': 'sk3862@drexel.edu', 'password': 'silo' } url = api_url + '/DataDrillDataset.php?' + urllib.parse.urlencode(params) return url def sendRequest(url): with urllib.request.urlopen(url) as remote: data = remote.read() s=str(data,'utf-8') data_formatted = StringIO(s) df=pd.read_csv(data_formatted) return df def getData(lat,long) : return weather #lat long index geocode=getGeocode() for i in range(len(geocode)): print(i) brigade=[geocode.loc[i,'Brigade']] url=buildUrl(geocode.loc[i,'latitude'], geocode.loc[i,'longitude']) #url for lat long df=sendRequest(url) #ping the australian websiten if i==0: headr=df.iloc[31,:] headr.reset_index(inplace=True, drop=True) headr.replace('\s+', ',',regex=True,inplace=True) #separate out the header headr[0]=headr[0]+",Brigade"+",Latitude"+",Longitude"; list_of_weather.append(headr) df=df[32:(len(df)-1)]#cleaning remove header, indexes df.replace('\s+', ',',regex=True,inplace=True) #make csv space delimited to comma delimited df=df.iloc[:,-1]#cleaning df.name=brigade[0] formatted_data=df+","+brigade[0]+","+str(geocode.loc[i,'latitude'])+","+str(geocode.loc[i,'longitude']) list_of_weather.append(formatted_data)#combine for different locations #weather=pd.concat(list_of_weather) #weather = [getData(x, y) for x, y in zip(geocode['latittude'], geocode['longitude'])] #reformat into a nice dataframe with colnames and save that as CSV col_names=["Date (yyyymmdd)","Day","T.Max(oC)","Smx","T.Min(oC)","Smn","Rain (mm)","Srn","Evap(mm)","Sev", "Radn(MJ/m2)","Ssl" ,"VP (hPA)","Svp","RHmaxT(%)" ,"RHminT(%)" ,"Date2(ddmmyyyy)","Brigade","Latitude","Longitude"] df = pd.DataFrame(weather.str.split(',',n=19).tolist(), columns = col_names) df = df.iloc[1:] print(df.head()) df.to_csv(path_to_coordscsv_file + 'SILO_weather_data' + brigade[0] + '.csv')