-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalcArrivalTime.py
154 lines (131 loc) · 4.68 KB
/
calcArrivalTime.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from osgeo import gdal
from osgeo.gdalconst import *
import sys,os
import numpy as np
from netCDF4 import Dataset
fdmaxFile='OUT-EXTREMUM.nc'
fha='OUT-T-ETA.nc'
# call example:
# creaFiles.py [-s srtm.tif] -w 0.3 -lp 36.32 36.19 -dp 36.12 37.42 [-od outDem.grd] [-ol outLake.grd ] [-res newcellsize]
def CreateGeoTiff(FileName, Array, xsize, ysize, GeoT,nodata=99999.0):
try:
DataType = gdal.GDT_Float32
WKT= 'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Latitude",NORTH],AXIS["Longitude",EAST],AUTHORITY["EPSG","4326"]]'
driver = gdal.GetDriverByName('GTiff')
print('creating dataset')
# Set up the dataset
DataSet = driver.Create( FileName, xsize, ysize, 1, DataType )
# the '1' is for band 1.
print (GeoT)
DataSet.SetGeoTransform(GeoT)
DataSet.SetProjection( WKT )
# Write the array
print (xsize,ysize,np.shape(Array))
band=DataSet.GetRasterBand(1)
band.SetNoDataValue(nodata)
band.WriteArray( Array )
return FileName
except Exception as e:
print('error ',e)
import sys
args=sys.argv
dire=''
hmin=0.05
step=5
for j in range(len(args)):
arg=args[j]
if arg=='-d':
dire=args[j+1]
if arg=='-h':
hmin=float(args[j+1])
if arg=='-s':
step=int(args[j+1])
print( 'opening dataset:',dire+os.sep+fdmaxFile)
ncmax= Dataset(dire+os.sep+fdmaxFile, 'r')
print ('extracting lat/lon')
lons=ncmax.variables['LON'][:]
lats=ncmax.variables['LAT'][:]
fdmax=ncmax.variables['FD_MAX'][:]
print (len(lons))
indices=np.where((fdmax>hmin) & (fdmax<1000))
nvalues=np.shape(indices)[1]
print ('nvalues>'+format(hmin)+'=',nvalues,'\n')
print ('opening dataset:',fha)
ncha= Dataset(dire+os.sep+fha, 'r')
ti=ncha.variables['TIME'][:]
#ha=ncha.variables['HA'][:]
ha0=ncha.variables['HA'][0]
iy0=indices[1]
ix0=indices[2]
tarr=np.zeros((len(lats), len(lons)))
tarr.fill(-1)
fa=open(dire+os.sep+'tarrival.XYZ','w')
fa.write('lon,lat,tarr (h)\n')
for it in range(len(ti)):
ha=ncha.variables['HA'][it]
if int(it/100)*100==it:
print(it,round(ti[it]/3600.0,1))
for i in range(0,len(ix0),step):
ix=ix0[i]
iy=iy0[i]
hv=ha[iy,ix]-ha0[iy,ix]
if hv>0.05 and tarr[iy,ix]==-1:
tarr[iy,ix]=ti[it]/3600.0
fa=open(dire+os.sep+'tarrival.txt','w')
fa.write('lon,lat,tarr (h)\n')
for i in range(0,len(ix0),step):
ix=ix0[i]
iy=iy0[i]
fa.write(format(lons[ix])+','+format(lats[iy])+','+format(tarr[iy,ix])+'\n')
fa.close()
print(len(lats),len(lons))
#fa=open(dire+os.sep+'tarrival.XYZ','w')
#fa.write('lon,lat,tarr (#h#)\n')
#for ix in range(6013):
# for iy in range(6121):
# fa.write(format(lons[ix])+','+format(lats[iy])+','+format(tarr[iy,ix])+'\n')
#fa.close()
# for ix in range(0,len(lons),5):
# if int(ix/250)*250==ix:
# print ix,len(lons)
# for iy in range(0,len(lats),5):
# if ha[0,iy,ix]==ha[len(ti)-1,iy,ix]:
# tarr[iy,ix]=-1
# else:
# for it in range(len(ti)):
# hv=ha[it,iy,ix]-ha[0,iy,ix]
# if hv>0.2:
# tarr[iy,ix]=ti[it]/3600.0
# fa=open('tarrival.XYZ','a')
# fa.write(format(lons[ix])+','+format(lats[iy])+','+format(tarr[iy,ix])+'\n')
# fa.close()
# break
# # for i in range(nvalues):
# arrivalTime=-1
# for it in range(len(ti)):
# hv=ha[it,iy[i],ix[i]]-ha[0,iy[i],ix[i]]
# #print it,hv,hv>0.2
# if hv>0.2:
# #print 'found'
# arrivalTime=ti[it]/3600.
# break
# #print 'Arrival time=',arrivalTime
# tarr[iy[i],ix[i]]=arrivalTime
# fa=open('tarrival.XYZ','a')
# fa.write(format(lons[ix[i]])+','+format(lats[iy[i]])+','+format(arrivalTime)+'\n')
# fa.close
# if (int(i/10000)*10000==i):
# print i,arrivalTime,format(i/nvalues*100.)+'%'
print ('preparing gTiff')
xsize=len(lons)
ysize=len(lats)
dlon=lons[1]-lons[0]
dlat=lats[1]-lats[0]
if lons[0]>180:
GeoT=[lons[0]-360,dlon,0,lats[0],0, dlat]
else:
GeoT=[lons[0],dlon,0,lats[0],0, dlat]
try:
print (CreateGeoTiff(dire+os.sep+'tarrHours.tif',tarr,xsize,ysize,GeoT,99999.0) )
except Exception as e:
print ('error in procedure',e)