-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadInWeather.py
More file actions
63 lines (45 loc) · 1.65 KB
/
readInWeather.py
File metadata and controls
63 lines (45 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
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
53
54
55
56
57
58
59
60
61
62
63
import netCDF4 as nc4
import pylab as pl
import math
import pandas as pd
import datetime
year = -9999
root_grp = None
def getMeteoValue(meteoVar,time, latitude, longitude):
global year
global root_grp
meteoVariables = []
currentYear = pd.Timestamp(time).year
#currentYear = pd.to_datetime(time).dt.year
#print(currentYear)
if (currentYear != year):
year = currentYear
print 'data/d3_'+str(year)+'.nc'
root_grp = nc4.Dataset('data/d3_'+str(year)+'.nc')
meteoVariable = root_grp.variables[meteoVar]
#print meteoVariable.dimensions
latVar = root_grp.variables['latitude']
lonVar = root_grp.variables['longitude']
totalLats = len(latVar)
totalLons = len(lonVar)
maxLat = max(latVar)
maxLon = max(lonVar)
minLat = min(latVar)
minLon = min(lonVar)
cellSizeLat = (max(latVar) - min (latVar))/totalLats
cellSizeLat = latVar[1] - latVar[0]
cellSizeLon = lonVar[1] - lonVar[0]
#(max(lonVar) - min(lonVar)) / totalLons
cellTileLat = math.ceil((-minLat + latitude)/cellSizeLat)
cellTileLon = math.ceil((-minLon + longitude)/cellSizeLon)
time_var = root_grp.variables['time']
#dtime = nc4.num2date(time_var[:], time_var.units)
#prova = time_var.sel(time = time)
test = nc4.date2index(pd.Timestamp(time), time_var, select= "nearest")
#print("hello")
#print precipitation[test,cellTileLat,cellTileLon]
return meteoVariable[test, cellTileLat, cellTileLon]
#print(len(temp))
#print(temp[0])
#hs = pd.Series(root_grp.variables['area'].dimensions['lat'])
#getMeteoValue(datetime.datetime.strptime("2015-01-04 10:00", "%Y-%m-%d %H:%M"),52,6)