Skip to content
Snippets Groups Projects
Commit d858fb72 authored by Lionel GUEZ's avatar Lionel GUEZ
Browse files

Create class SHPC

The motivation is to manage an SHPC directory without opening first
all the files inside. The class allows to open files dynamically and
remember already opened files.
parent ce5e320b
No related branches found
No related tags found
No related merge requests found
......@@ -4,6 +4,82 @@ from os import path
import numpy as np
import sys
class SHPC_class:
def __init__(self, SHPC_dir, def_orient):
"""def_orient, for default orientation, is used to define d_init and
n_dates. Since we are going to open files, we might as
well ask the user which orientation will be useful.
"""
self.dir = SHPC_dir
self.def_orient = def_orient
fname = path.join(SHPC_dir, "n_slices.txt")
with open(fname) as f: self.n_slices = int(f.read())
self._readers = [{"Anticyclones": {}, "Cyclones": {}}
for i in range(self.n_slices)]
for i_slice, reader in enumerate(self._readers):
my_shapefile = path.join(SHPC_dir, f"Slice_{i_slice}", def_orient,
"extremum")
reader[def_orient]["extremum"] = shapefile.Reader(my_shapefile)
self.d_init = [reader[def_orient]["extremum"].record(0).date
for reader in self._readers]
self._n_dates = np.zeros(self.n_slices, dtype = int)
self._ishape_last = [{"Anticyclones": None, "Cyclones": None}
for i in range(self.n_slices)]
self._n_shapes = [{"Anticyclones": 0, "Cyclones": 0}
for i in range(self.n_slices)]
def get_n_dates(self, i_slice):
if self._n_dates[i_slice] == 0:
# Find out the number of dates in the slice:
if self._ishape_last[i_slice]["Anticyclones"] is not None:
self._n_dates[i_slice] \
= self._ishape_last[i_slice]["Anticyclones"].size
elif self._ishape_last[i_slice]["Cyclones"] is not None:
self._n_dates[i_slice] \
= self._ishape_last[i_slice]["Cyclones"].size
else:
self._n_dates[i_slice] = self.get_ishape_last(i_slice).size
return self._n_dates[i_slice]
def get_ishape_last(self, i_slice, orientation = None):
if orientation is None: orientation = self.def_orient
if self._ishape_last[i_slice][orientation] is None:
fname = path.join(self.dir, f"Slice_{i_slice}", orientation,
"ishape_last.txt")
print("Reading", fname, "...")
self._ishape_last[i_slice][orientation] \
= np.loadtxt(fname, dtype = int, ndmin = 1)
return self._ishape_last[i_slice][orientation]
def get_reader(self, i_slice, layer, orientation = None):
if orientation is None: orientation = self.def_orient
if layer not in self._readers[i_slice][orientation]:
my_shapefile = path.join(self.dir, f"Slice_{i_slice}", orientation,
layer)
print("Opening", my_shapefile, "...")
self._readers[i_slice][orientation][layer] \
= shapefile.Reader(my_shapefile)
return self._readers[i_slice][orientation][layer]
def get_n_shapes(self, i_slice, orientation = None):
if orientation is None: orientation = self.def_orient
if self._n_shapes[i_slice][orientation] == 0:
reader = self.get_reader(i_slice, layer = "extremum",
orientation = orientation)
self._n_shapes[i_slice][orientation] = len(reader)
return self._n_shapes[i_slice][orientation]
def comp_ishape(handler, date, eddy_index):
"""Compute the location in the shapefiles. handler should be a
dictionary returned by open_shpc.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment