Commit ac6ed6a7 authored by Renzo Marini's avatar Renzo Marini
Browse files

modos agregados

parent 1549df81
......@@ -27,7 +27,7 @@ class GribParser:
self.model = pickle.load(handle)
if alt and not self.model["metadata"]["alt"]:
self.create_non_cumulative_values()
self.dump()
self.dump_model()
else:
self.model = {}
self.model["data"] = {}
......@@ -48,7 +48,7 @@ class GribParser:
self.load_metadata(grb, alt)
if alt:
self.create_non_cumulative_values()
self.dump()
self.dump_model()
self.define_grid()
# Define el conjunto de puntos que son considerados como válidos dentro del grid.
......@@ -63,8 +63,8 @@ class GribParser:
grib_info = pygrib.fromstring(self.model["metadata"]["additional_info"])
self.i_step = grib_info["iDirectionIncrementInDegrees"]
self.j_step = grib_info["jDirectionIncrementInDegrees"]
i_range = range(min(borders[0], borders[2]), max(borders[0], borders[2]) + 1, int(self.i_step))
j_range = range(min(borders[1], borders[3]), max(borders[1], borders[3]) + 1, int(self.j_step))
i_range = np.arange(min(borders[0], borders[2]), max(borders[0], borders[2]) + 1, self.i_step)
j_range = np.arange(min(borders[1], borders[3]), max(borders[1], borders[3]) + 1, self.j_step)
self.grid = [[i, j] for i in i_range for j in j_range]
def load_step_range(self, step_range, initial_time):
......@@ -102,7 +102,7 @@ class GribParser:
self.model["metadata"]["alt"] = alt
self.model["metadata"]["additional_info"] = grb.tostring()
def dump(self):
def dump_model(self):
with open(self.pickle_path, 'wb') as handle:
pickle.dump(dict(self.model), handle, protocol=pickle.HIGHEST_PROTOCOL)
......@@ -129,7 +129,7 @@ class GribParser:
return False
return True
def dump_percentile(self, n, point, alt):
def dump_csv(self, n, point, alt, mode):
param_data = self.model["data"]
initial_time = self.model["metadata"]["initial_time"]
lat_rel = int(point[0] - self.model["metadata"]["firstLat"])
......@@ -156,7 +156,10 @@ class GribParser:
data = data["normal"]
for step in step_range:
if step in data:
row.append(np.percentile(data[step][:, int(lat_rel), int(lon_rel)], n))
if mode == "normal":
row.append(data[step][0, int(lat_rel), int(lon_rel)])
elif mode == "percentile":
row.append(np.percentile(data[step][:, int(lat_rel), int(lon_rel)], n))
else:
row.append(not_found_field)
else:
......@@ -167,7 +170,12 @@ class GribParser:
os.makedirs('CSV')
if alt:
parameter = parameter + "[alt]"
csv_path = "CSV/" + center + "_" + parameter + "_per" + str(n) + "_" \
if mode == "normal":
csv_path = "CSV/" + center + "_" + parameter + "_" \
+ initial_time + "hs_" + "lat" + str(point[0]) + "_lon" + str(point[1]) + ".csv"
elif mode == "percentile":
csv_path = "CSV/" + center + "_" + parameter + "_per" + str(n) + "_" \
+ initial_time + "hs_" + "lat" + str(point[0]) + "_lon" + str(point[1]) + ".csv"
first = [""] + formatted_step_range
if os.path.isfile(csv_path):
......@@ -197,14 +205,19 @@ if __name__ == "__main__":
group.add_argument('--area', type=int, nargs=4,
help='Ingrese la latitud y longitud del primer y último punto del área')
group.add_argument('--all', dest='all', action='store_true')
parser.add_argument('--per', type=int, required=True,
parser.add_argument('--per', type=int, required=False,
help='Ingrese el percentil')
args = parser.parse_args()
grib_parser = GribParser(args.path, args.alt)
if args.per is None:
mode = "normal"
else:
mode = "percentile"
if args.point:
if grib_parser.valid_input(args.point):
grib_parser.dump_percentile(args.per, args.point, args.alt)
grib_parser.dump_csv(args.per, args.point, args.alt, mode)
else:
if args.area:
area = args.area
......@@ -215,4 +228,4 @@ if __name__ == "__main__":
if grib_parser.valid_input(area[:2]) and grib_parser.valid_input(area[2:]):
grib_parser.define_grid(area)
for point in grib_parser.grid:
grib_parser.dump_percentile(args.per, point, args.alt)
grib_parser.dump_csv(args.per, args.point, args.alt, mode)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment