Sie sind auf Seite 1von 8

Point Query & Tiles over Kerla:

Manish Modani

API Keys: a0d69ec1942942c0969ec1942992c0f4

Point Query:

https://api.weather.com/v3/wx/forecast/daily/10day?geocode=10.8505,76.2711&format=json&unit
s=e&language=en-US&apiKey=a0d69ec1942942c0969ec1942992c0f4

Titles:

In this section, procedure to visualize CORE API Temperature Tiles over Kerla is discussed. Same
procedure can be applied to visualize other weather parameters.

https://www.maptiler.com/google-maps-coordinates-tile-bounds-projection/

Step 1: Find X, Y & Z using following webpage:

http://www.maptiler.org/google-maps-coordinates-tile-bounds-projection/

For Kerla It is: 91, 60, 7 (X,Y,Z)

Step 2: Find TS and FTS

(For Current condition only ts required, For forecast both ts and fts required).

Ts = Timeslice
fts=Forecasted time slice

Use following link to find TS and FTS.


https://api.weather.com/v3/TileServer/series/productSet/PPAcore?apiKey=yourApiKey

https://api.weather.com/v3/TileServer/series/productSet/PPAcore?apiKey=a0d69ec1942942c0969ec1
942992c0f4

Step 3: Tile Visualization

(a) Visualization of current Temperature:

Following URL should be used for Current Temperature visualization.

https://api.weather.com/v3/TileServer/tile/temp?ts=<ts>&xyz=<X:Y:Z>&apiKey=yourApiKey

In present case, the URL looks like:

e.g.

https://api.weather.com/v3/TileServer/tile/temp?ts=1580117700&xyz=91:60:7&apiKey=a0d69ec
1942942c0969ec1942992c0f4
Above link shows following

(b) Visualization of Temperature Forecast

Following URL should be used to visualize the Temperature Forecast:

https://api.weather.com/v3/TileServer/tile/tempFcst?ts=<ts>&fts=<fts>&xyz=<X:Y:Z>&apiKey
=yourApiKey

In present case, the link looks like:

https://api.weather.com/v3/TileServer/tile/tempFcst?ts=1580117400&fts=1580142600&xyz=91:6
0:7&apiKey=a0d69ec1942942c0969ec1942992c0f4

Above link gives following.

(PS: Forecast layer will have many forecast files (upto 10 days). One has to choose ts &
respective fts correctly).
#### Downloads TWC tiler binary data and creates VRT for each tile.

import requests,json,csv,shutil,os,math,subprocess
import multiprocessing as mp
from osgeo import gdal
from os.path import expanduser #to enable setting home directory
cross platform
import glob

#Runs in Python 2.7.10 on a MacBook (Linux OS)


#Not tested in Python 3.x.

#OS directory setup


home = expanduser("~") #assigns home directory, not tested in
Windows

################## Configs ##############


APIkey=' a0d69ec1942942c0969ec1942992c0f4' #Substitute your key
here.

#Here are some example product sets.


#You can find more variables here:
https://docs.google.com/document/d/17J1X71GlnvUphj4hWPDoKd79jUQsXnbd
xFcbR_OkTaM/edit#

# 4 Python variables need setting for each data product. Copy the
ones for the product you want to collect to the bottom of this list.

prod_name = "Temperatureheightaboveground"
prod_num=str(43)
rtQ=0 #set this variable to 0 for CoD data, which don't contain
forecast timestamp (rt) values.
vrttemplate=os.path.join(home,"Desktop\\TWC\\CSAG","template.zoom5.v
rt")

prod_name = "VAR01515FROM25011surface"
prod_num=str(7)
rtQ=0

prod_name = "Winddirectionfromwhichblowingheightaboveground"
prod_num=str(132)
rtQ=0 #set this variable so that we can collect from APIs when rt
isn't there.
lod=5
vrttemplate=os.path.join(home,"Desktop\\TWC\\CSAG","template.zoom5.v
rt")

prod_name = "Windspeedheightaboveground"
prod_num=str(33)
rtQ=0 #set this variable so that we can collect from APIs when rt
isn't there.
#Highest lod avail is 5...
lod=5
vrttemplate=os.path.join(home,"Desktop\\TWC\\CSAG","template.zoom5.v
rt")
prod_name = "Significantheightofcombinedwindwavesandswellmsl"
prod_num=str(395)
rtQ=1 #note the script only collects the first time-stamp. If you
want more timesteps, add another loop.
vrttemplate=os.path.join(home,"Desktop\\TWC\\CSAG","template.zoom3.v
rt")
#fname=os.path.join(home, "params","xylod.all.csv")

prod_name = "Temperatureheightaboveground"
prod_num=str(43)
rtQ=0 #set this variable to 0 for CoD data, which don't contain
forecast timestamp (rt) values.
vrttemplate=os.path.join(home,"Desktop\\TWC\\CSAG","template.zoom5.v
rt")

##################### REGION Definition #############

# data file for list of tiles


fname=os.path.join(home, "Desktop\\TWC\\CSAG","xylod.zoom5.EU.csv")
#list of tile locations and lod, eg 4,3,3
#this could be made by another script!
#Check that this and the vrt templates have UNIX encoding line
endings in Linux..!

#---------------------------Functions--------------#
########################PRODUCT INVENTORY
CALL################################
def InvCall(prod_num,prod_name,rtQ):
base1 = 'https://api.weather.com/v2/tiler/info?products='
base2 = prod_num + ":" + prod_name #product # and name
base3 = '&apiKey='+APIkey
url = base1 + base2 + base3
print ('Getting Inventory from', url)

APIcall = requests.get(url) #Our Temp API Call


if APIcall.status_code == 200: #if(APIcall.ok):
data = APIcall.json()
t =
data["layers"][prod_num][prod_name]["dimensions"][0]["t"][0]
if rtQ != 0:
rt =
data["layers"][prod_num][prod_name]["dimensions"][0]["rt"][0]
else:
rt = rtQ
return [t, rt]

########################## API CALLS


###################################
def APIcall(prod_num,prod_name,t,rt,x,y,z): # function for pulling
data
base1 = 'https://api.weather.com/v2/tiler/data?products='
base2 = prod_num + ":" + prod_name + "&t=" + str(t) + "&rt=" +
str(rt)

if rt==0:
base2 = prod_num + ":" + prod_name + "&t=" + t
base3 = "&lod="+z+"&x="+x+"&y="+y+"&apiKey="+APIkey
# base3 = "&lod=5&x=15&y=10&apiKey="+APIkey
# print "base3 is", base3
url = base1 + base2 + base3 # make API URL
print ("URL called: ", url)
STATICMAP_URL=url
path =os.path.join(home,'Desktop//TWC//CSAG',prod_name + "." + t
+ "."+x+"."+y+"."+z+".bin")

r = requests.get(STATICMAP_URL.format(bin), stream=True)
if r.status_code == 200:
with open(path, 'wb') as f:
r.raw.decode_content = True
shutil.copyfileobj(r.raw, f)
return (r.status_code)

######################## PROCESS DATA (MAKE VRTs)


#########################
def ProcessData(prod_num,prod_name,t,rt,x,y,z):
path =os.path.join(home,'Desktop\\TWC\\CSAG',prod_name + "." + t
+ "."+x+"."+y+"."+z+".bin")
bin = prod_name + "." + t + "."+x+"."+y+"."+z+".bin"
outVRT= os.path.join(home,'Desktop\\TWC\\CSAG',prod_name + "." +
t + "."+x+"."+y+"."+z+".vrt")
print (path)
print (bin)
print ("x is", x)
print ("y is", y)
print ("z is", z)

#convert the x and y to lat/long


lat_deg,lon_deg=num2deg(int(x),int(y),int(z))
print ("lat:" ,lat_deg)
print ("lon:" ,lon_deg)

#get the lower lat of the tile


lowerlat_deg,lon_deg=num2deg(int(x),int(y)+1,int(z))

print ("lat-lower:" ,lowerlat_deg)


pixelheight=(abs(lat_deg-lowerlat_deg)/256)
print ("pixelheight:" ,pixelheight)

#create a new VRT file for each binary downloaded.


# Read in the VRT template file
with open(vrttemplate, 'r') as file :
filedata = file.read()

# Replace the target string


filedata = filedata.replace('XXXXcoord', str(lon_deg))
filedata = filedata.replace('XXXYcoord', str(lat_deg))
filedata = filedata.replace('XXXbinary', bin)
filedata = filedata.replace('XXXPixelHeight', str(pixelheight))

# Write the file out again


with open(outVRT, 'w') as file:
file.write(filedata)

########################## Calculate lat/long of tiles


#######################
def num2deg(xtile, ytile, zoom):
#from
https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Tile_number
s_to_lon..2Flat.
n = 2.0 ** zoom
lon_deg = xtile / n * 360.0 - 180.0
lat_rad = math.atan(math.sinh(math.pi * (1 - 2 * ytile / n)))
lat_deg = math.degrees(lat_rad)
return (lat_deg, lon_deg)

######################## Create VRT for all tiles.


#########################
def makebigvrt(t):
prod_glob= prod_name + "." + str(t) + "*.vrt"
print ("prodglob is" ,prod_glob)
outputvrt= prod_name + "." + str(t) + ".vrt"
print ("tileset VRT is ", outputvrt)

vrtfiles=[]

os.chdir(os.path.join(home,'data'))
for name in glob.glob(os.path.join(prod_glob)):
vrtfiles.append(name)
print(vrtfiles)
b1 = gdal.BuildVRT(outputvrt, vrtfiles)

#######Start of main

values = InvCall(prod_num,prod_name,rtQ)
t= values[0]
rt=values[1]
print ("t is" , t)
print ("rt is" , rt)

tile_data = csv.reader(open(fname,'r'))

n_processes = 3
pool = mp.Pool(processes=n_processes)

for row in tile_data:


# print('Row #' + str(tile_data.line_num) + ' ' + str(row))

#for x in xrange(1, 5):


x = str(row[0])
y = str(row[1])
z = str(row[2])
respCode=APIcall(prod_num,prod_name,t,rt,x,y,z)
print ("data API call response code is: ",respCode)

#------attempt at multi-threading
#results = [pool.apply_async(TESTcall, args=(4,3,5)) for i in
range (0,4)]
#results = [pool.apply_async(APIcall,
args=(prod_num,prod_name,t,rt,x,y,z))]
#------attempt at multi-threading
if respCode == 200:
ProcessData(prod_num,prod_name,t,rt,x,y,z)
print ("DONE apicalls!")

#subprocess.call(['df', '-h'])
#other GDAL commands:
#gdal_translate -of XYZ
Windspeedheightaboveground.1528326300000.16.12.5.vrt
Windspeed.test.txt

#Linux command-line: gdalbuildvrt


Windspeedheightaboveground.1528238100000.vrt
Windspeedheightaboveground.1528238100000*.vrt

call = makebigvrt(t)

Das könnte Ihnen auch gefallen