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

Import script `write_date_range.py`

This will be useful to create `n_dates_1` and `n_dates_14`. It should
be faster for this purpose than using the more general script `filter.py`.
parent 86cf7b84
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python3
"""This script reads data in a date range from an SHPC and writes it
to a new SHPC. We assume that all the dates in the date range are in
the same slice of the input SHPC. A single slice is written in the
output SHPC.
"""
import shapefile
from os import path
import util_eddies
import os
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("input_SHPC")
parser.add_argument("output_SHPC")
parser.add_argument("date_start", type = int)
parser.add_argument("-n", "--n_dates", type = int, default = 1)
args = parser.parse_args()
SHPC = util_eddies.SHPC_class(args.input_SHPC, "Anticyclones")
slice_dir = path.join(args.output_SHPC, "Slice_0")
date_stop = args.date_start + args.n_dates
i_slice = SHPC.get_slice(args.date_start)
os.makedirs(slice_dir)
for orientation in ["Anticyclones", "Cyclones"]:
orient_dir = path.join(slice_dir, orientation)
os.mkdir(orient_dir)
extremum = SHPC.get_reader(i_slice, "extremum", orientation)
outermost_cont = SHPC.get_reader(i_slice, "outermost_contour", orientation)
max_speed_cont = SHPC.get_reader(i_slice, "max_speed_contour", orientation)
fname = path.join(orient_dir, "ishape_last.txt")
with shapefile.Writer(path.join(orient_dir, "extremum")) as extremum_filt, \
shapefile.Writer(path.join(orient_dir, "outermost_contour")) \
as outermost_cont_filt, \
shapefile.Writer(path.join(orient_dir, "max_speed_contour")) \
as max_speed_cont_filt, open(fname, "w") as ishape_last:
extremum_filt.fields = extremum.fields[1:]
outermost_cont_filt.fields = outermost_cont.fields[1:]
max_speed_cont_filt.fields = max_speed_cont.fields[1:]
for my_date in range(args.date_start, date_stop):
eddy_index = 0
for ishape in SHPC.ishape_range(my_date, i_slice, orientation):
eddy_index += 1
for sr, w in zip([extremum.shapeRecord(ishape),
outermost_cont.shapeRecord(ishape),
max_speed_cont.shapeRecord(ishape)],
[extremum_filt, outermost_cont_filt,
max_speed_cont_filt]):
r = sr.record
r["eddy_index"] = eddy_index
w.record(*r)
w.shape(sr.shape)
ishape_last.write(f"{len(extremum_filt) - 1}\n")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment