-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatos10minutosImgCompleta.py
224 lines (184 loc) · 7.88 KB
/
datos10minutosImgCompleta.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# datos10minutosImgCompleta Versión 1.0 - Enero 2015
#
# Autores: Bellini Saibene Yanina;Banchero, Santiago
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
# Este script es parte del set de software desarrollado por INTA
# para el tratamiento de los datos de los radares meteorológicos.
import argparse
import pyodbc
import traceback
import sys, os
try:
from identify import Identify
except ImportError:
raise ImportError,"Se requiere el modulo Identify: https://github.com/INTA-Radar/ImgCompuestas/blob/master/identify.py"
parser = argparse.ArgumentParser(description="Calcula las variables del DataSet de entrenamiento y testeo para cada caso")
parser.add_argument("path_img",
help="Ubicación de los archivos raster a procesar")
parser.add_argument ("fecha", help="Fecha a procesar, formato: aaaammdd")
parser.add_argument("archivo", default="db", choices=["db", "txt"],help="Donde almacenar los datos")
#TODO: agregarle argumentos para seleccionar que variable y que elevación y/o horario se quiere procesar.
'''parser.add_argument("-dBZ", action='store_true', help="Genera las variables relacionadas con dBZ")
parser.add_argument("-ZDR", action='store_true', help="Genera la imágen relacionadas con ZDR")
parser.add_argument("-KDP", action='store_true', help="Genera las variables relacionadas con KDP")
parser.add_argument("-RhoHV", action='store_true', help="Genera la imágen relacionadas con RhoHV")
parser.add_argument("-PhiDP", action='store_true', help="Genera la imágen relacionadas con PhiDP")
parser.add_argument("-E", action='store_true', help="Genera la imágen relacionadas con Energía Cinética")
parser.add_argument("-EW", action='store_true', help="Genera la imágen relacionadas con Energía Cinética (funcion de peso)")'''
args = parser.parse_args()
#Realizar la conexion al sql server
if args.archivo=='db':
#TODO: Se debe manejar la conexión...solicitar los datos de alguna manera...o bien por parámetro o por teclado
#Ejemplo de conexión a SQL Server con Autenticación Integrada
#con = pyodbc.connect('Trusted_Connection=yes;DRIVER={SQL Server};SERVER=nombre del servidor;DATABASE=nombre de la base de datos')
#Ejemplo de conexión a SQL Server con Autenticación de SQL Server
con = pyodbc.connect('DRIVER={SQL Server};SERVER=nombre del servidor;DATABASE=ImplementacionGranizoRadar;UID=usuario;PWD=contraseña')
else:
#TODO: Código para crear los archivos de texto para almacenar el procesamiento.
file=args.fecha
#Obtiene el Id del Evento a partir de la fecha
anio=args.fecha[0:4]
mes=args.fecha[4:6]
dia=args.fecha[6:8]
# Creo los objetos Identify con las imagenes a procesar
#---- Bloque de procesamiento de los datos de imagenes cada 10 minutos ---------#
#recorro todos los puntos
print 'Iniciando recuperación de datos de los lotes de campo.'
dbz = [i for i in os.listdir(args.path_img) if i.startswith(args.fecha.replace('-','')) and i.find('dBZ') >0 and i.find('MAX') == -1 and i.find('MIN') == -1 and i.find('AVG') == -1 and i.find('TOT') == -1 and i.endswith('tif')]
zdr = [i for i in os.listdir(args.path_img) if i.startswith(args.fecha.replace('-','')) and i.find('ZDR') >0 and i.find('MAX') == -1 and i.find('MIN') == -1 and i.find('AVG') == -1 and i.find('TOT') == -1 and i.endswith('tif')]
rho = [i for i in os.listdir(args.path_img) if i.startswith(args.fecha.replace('-','')) and i.find('RhoHV') >0 and i.find('MAX') == -1 and i.find('MIN') == -1 and i.find('AVG') == -1 and i.find('TOT') == -1 and i.endswith('tif')]
#Este bloque es para recorrer el que tenga mayor cantidad y hacer un solo bucle.
cantidaddbz = len(dbz)
cantidadzdr = len(zdr)
cantidadrho = len(rho)
if cantidadrho > cantidaddbz :
if cantidadrho > cantidadzdr :
cantidad = cantidadrho
else:
cantidad = cantidadzdr
else:
if cantidaddbz > cantidadzdr:
cantidad = cantidaddbz
else:
cantidad = cantidadzdr
if cantidaddbz==1728: #Este valor sale de multiplicar 144 tomas de datos en un día * 12 elevaciones
volcompletodbz=1
else:
volcompletodbz=0
if cantidadrho==1728:
volcompletorho=1
else:
volcompletorho=0
if cantidadzdr==1728:
volcompletozdr=1
else:
volcompletozdr=0
print 'cantidad dbz:', len(dbz)
print 'cantidad zdr:', len(zdr)
print 'cantidad rho:', len(rho)
print 'cantidad:', cantidad
#raw_input()
#recorro todos los puntos
print 'Iniciando obtención de datos para la fecha determinada.'
# Bloque de obtención de los datos de cada imagen en cada elevacion y en cada horario
for i in xrange(cantidad):
print "Intentando procesar archivo ", i, " de ", cantidad, "."
#raw_input()
try:
img_tif_name=args.path_img+dbz[i]
horariodbz = dbz[i][8:12]
elevadbz = dbz[i][24:26]
print "Elevacion: ", elevadbz
#raw_input()
imgdbz = file(img_tif_name)
imgdbzd= Identify(img_tif_name)
except IOError:
imgdbz= None
print "No se encuetra el archivo: "+img_tif_name
except IndexError:
print "El archivo para esa elevación y toma no se encuentra."
imgdbz= None
try:
img_tif_name=args.path_img+zdr[i]
horariozdr = zdr[i][8:12]
elevazdr = zdr[i][24:26]
imgzdr = file(img_tif_name)
imgzdrd= Identify(img_tif_name)
except IOError:
imgzdr= None
print "No se encuetra el archivo: "+img_tif_name
except IndexError:
imgzdr= None
try:
img_tif_name=args.path_img+rho[i]
horariorho = rho[i][8:12]
elevarho = rho[i][26:28]
imgrho = file(img_tif_name)
imgrhod= Identify(img_tif_name)
except IOError:
imgrho= None
print "No se encuetra el archivo: "+img_tif_name
except IndexError:
imgrho= None
# Recorrido de una matriz
# De izquierda a derecha
# De arriba hacia abajo
for y in xrange(504): #Todas las imágenes tienen el mismo tamaño
for x in xrange(486): #Todas las imágenes tienen el mismo tamaño
print 'X:', x, 'Y:', y
if imgdbz==None:
valordbz=-99
elevadbz=-99
horariodbz=-99
else:
valordbz = imgdbzd.get_pixel_data(y,x)
print 'X:', x, 'Y:', y
if imgzdr==None:
valorzdr=-99
elevazdr=-99
horariozdr=-99
else:
valorzdr = imgzdrd.get_pixel_value(y,x)
print 'X:', x, 'Y:', y
if imgrho==None:
valorrho=-99
elevarho=-99
horariorho=-99
else:
valorrho = imgrhod.get_pixel_value(y,x)
#Obtenido los datos, almacenar en las tablas correspondientes
instr= 'INSERT INTO datosdBZ([Fecha],[x],[y],[Elevacion],[Horario],[dBZ],[VolCompleto]) VALUES (?,?,?,?,?,?,?)'
curins = con.cursor()
curins.execute(instr, (args.fecha,x,y, elevadbz ,horariodbz, valordbz, volcompletodbz))
curins.commit()
instr= 'INSERT INTO datosZDR([Fecha],[x],[y],[Elevacion],[Horario],[ZDR],[VolCompleto]) VALUES (?,?,?,?,?,?,?)'
curins = con.cursor()
curins.execute(instr, (args.fecha,x,y, elevazdr ,horariozdr, valorzdr, volcompletozdr))
curins.commit()
instr= 'INSERT INTO datosRhoHV([Fecha],[x],[y],[Elevacion],[Horario],[RhoHV],[VolCompleto]) VALUES (?,?,?,?,?,?,?)'
curins = con.cursor()
curins.execute(instr, (args.fecha,x,y, elevarho ,horariorho, valorrho, volcompletorho))
curins.commit()
# Cierro la conexión con la base de datos y los archivos
# Almacenar en archivos de texto
print "Almacenamiento finalizado"
con.close()