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

Convert the mat file to v6 from inside Python

Call the Matlab script from the Python script. We cannot do this with
subprocess because Matlab requires a terminal. With subprocess, the
Matlab script complains: "inappropriate ioctl for device". Note that
we cannot test that the Matlab script worked normally because it exit
with code 0 regardless: the exit instruction returns code 0.

The idea here is that we do not want to store the converted v6 mat files.
parent d3f2dfea
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python3 #!/usr/bin/env python3
"""Converts adt_YYYY-MM-DD_v6.mat files to a triplet of """Converts adt_YYYY-MM-DD.mat files to a triplet of shapefiles. The
shapefiles. The mat files should be in v6 format. The data in each mat files are assumed to be in v7.3 (HDF5) format and will be
input file is the set of detected instantaneous eddies at a given converted to v6 format before being read. The data in each input
date. file is the set of detected instantaneous eddies at a given date.
""" """
...@@ -14,6 +14,8 @@ import scipy.io as sio ...@@ -14,6 +14,8 @@ import scipy.io as sio
import datetime import datetime
import f90nml import f90nml
import sys import sys
import pty
import os
def define_fields(writers): def define_fields(writers):
"""writers is a dictionary of shapefile.Writer objects.""" """writers is a dictionary of shapefile.Writer objects."""
...@@ -91,7 +93,7 @@ def write(writers, cell, cyclone): ...@@ -91,7 +93,7 @@ def write(writers, cell, cyclone):
adt_file = sys.argv[1] adt_file = sys.argv[1]
dirname, basename = path.split(adt_file) dirname, basename = path.split(adt_file)
my_date = datetime.datetime.strptime(basename, "adt_%Y-%m-%d_v6.mat").date() my_date = datetime.datetime.strptime(basename, "adt_%Y-%m-%d.mat").date()
if len(sys.argv) == 3: if len(sys.argv) == 3:
final_date = datetime.datetime.strptime(sys.argv[2], "%Y-%m-%d").date() final_date = datetime.datetime.strptime(sys.argv[2], "%Y-%m-%d").date()
...@@ -122,8 +124,11 @@ for f in factories: ...@@ -122,8 +124,11 @@ for f in factories:
file = path.join(f["SHP_coll"], "ishape_last.txt") file = path.join(f["SHP_coll"], "ishape_last.txt")
f["writers"]["ishape_last"] = open(file, "w") f["writers"]["ishape_last"] = open(file, "w")
while True: while True:
matlab_data = sio.loadmat(adt_file, squeeze_me = True, os.symlink(adt_file, "adt.mat")
pty.spawn(["matlab", "-nojvm", "-r", "inst_eddies"])
matlab_data = sio.loadmat("adt_v6.mat", squeeze_me = True,
variable_names = ("date_num", "X", "Y", variable_names = ("date_num", "X", "Y",
"Anticyclonic_Cell", "Anticyclonic_Cell",
"Cyclonic_Cell", "ADT")) "Cyclonic_Cell", "ADT"))
...@@ -136,9 +141,10 @@ while True: ...@@ -136,9 +141,10 @@ while True:
for f in factories: for f in factories:
write(f["writers"], matlab_data[f["cell_name"]], f["cyclone"]) write(f["writers"], matlab_data[f["cell_name"]], f["cyclone"])
os.remove("adt.mat")
my_date += datetime.timedelta(1) my_date += datetime.timedelta(1)
if my_date > final_date: break if my_date > final_date: break
basename = my_date.strftime("adt_%Y-%m-%d_v6.mat") basename = my_date.strftime("adt_%Y-%m-%d.mat")
adt_file = path.join(dirname, basename) adt_file = path.join(dirname, basename)
nml = f90nml.Namelist() nml = f90nml.Namelist()
...@@ -149,3 +155,5 @@ nml["grid_nml"] = dict(corner_deg = [matlab_data["X"][0], matlab_data["Y"][0]], ...@@ -149,3 +155,5 @@ nml["grid_nml"] = dict(corner_deg = [matlab_data["X"][0], matlab_data["Y"][0]],
for f in factories: for f in factories:
nml.write(path.join(f["SHP_coll"], "grid_nml.txt"), force = True) nml.write(path.join(f["SHP_coll"], "grid_nml.txt"), force = True)
for k, v in f["writers"].items(): v.close() for k, v in f["writers"].items(): v.close()
os.remove("adt_v6.mat")
File deleted
...@@ -1097,8 +1097,10 @@ ...@@ -1097,8 +1097,10 @@
"command": "command":
[ [
"$src_dir/Convert_Matlab/inst_eddies_v6.py", "$src_dir/Convert_Matlab/inst_eddies_v6.py",
"$src_dir/Tests/Input/Eurec4A_OA/adt_2019-12-15_v6.mat" "/data/lmd-oce/EUREC4A_OA/DATA_SAT_NewExport_Atalante/TOEddies/Eddies/adt_2019-12-15.mat",
] "2019-12-16"
],
"required": ["$src_dir/Convert_Matlab/inst_eddies.m"]
}, },
{ {
"title": "Plot_eddy_contours", "title": "Plot_eddy_contours",
......
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