Commit 65143245 authored by renzo's avatar renzo

mover data de descarga_diaria

parent 319259bd
# import requests
#
# shots_url = 'http://www.hidro.gob.ar/Api/v1/AlturasHorarias/ValoresGrafico/BSAS/2020-04-14T12:19'
#
# # request the URL and parse the JSON
# response = requests.get(shots_url)
# response.raise_for_status() # raise exception if invalid response
# shots = response.json()
# 1
import os
import subprocess
import shutil
import re
from shutil import copy
import metadata
def get_signature(script_name):
script_path = metadata.matlab_scripts + "/" + script_name + ".m"
regex = r"function.*=.*" + script_name + ".*[(](.*)[)]"
with open(script_path, 'r') as f:
for line in f:
match = re.match(regex, line)
if match:
signature = match.group(1)
break
return [x.strip() for x in signature.split(",")]
def create_matlab_call(script_name, source, optional_args):
# Get Matlab script signature
signature = get_signature(script_name)
# Get Matlab call arguments that match signature
args = ""
for param in signature:
if param in optional_args and optional_args[param]:
args += " " + optional_args[param]
elif hasattr(metadata, source) and param in getattr(metadata, source):
args += " " + getattr(metadata, source)[param]
elif hasattr(metadata, param):
args += " " + getattr(metadata, param)
else:
raise RuntimeError("El script de Matlab " + script_name + " recibe los parámetros " + str(signature)
+ ", pero " + param + " no se ha proporcionado al llamar al script, ni se encuentra"
"en el archivo de configuración del proyecto")
return "matlab -nodisplay -nosplash -r \"try, " + script_name + args \
+ ", catch me, fprintf('%s / %s\\n',me.identifier,me.message), exit(1), end, exit(0)\""
def copy_mats(paths, dir, filenames, patterns):
found = False
for name in filenames:
name_parts = name.split(".mat")
if len(name_parts) > 1:
name_without_date = "".join([s for s in name_parts[0] if not s.isdigit()])
for template in patterns:
substring_parts = name_without_date.split(template)
if len(substring_parts) > 1 and not substring_parts[-1]: # es un .mat del source i
found = True
copy(dir + "/" + name, paths[source])
return found
root = "/home/renzo/Desktop/automatizacion-de-bajadas/tmp"
templates = [["fn", "estaciones"], ["noaa"], ["Argentina"], ["COP"], ["MVD", "LPa", "IdF"]]
sources = ["hycom", "noaa", "shn", "copernicus", "anp"]
paths = {}
for source in sources:
paths[source] = getattr(metadata, source)["output_path"]
for path, dirnames, filenames in os.walk(root):
source_counter = 0
for i, source in enumerate(sources):
found = copy_mats(paths, path, filenames, templates[i])
if not found:
script_name = getattr(metadata, source)["process"][0]
script_path = metadata.matlab_scripts + "/" + script_name + ".m"
copy(script_path, path)
if source == "noaa":
optional_args = {"day": "today"}
else:
optional_args = {}
call = create_matlab_call(script_name, source, optional_args)
os.chdir(path)
subprocess.run(call, shell=True)
found = copy_mats(paths, path, filenames, templates[i])
if found:
source_counter += 1
if source_counter == 5:
shutil.rmtree(path)
\ No newline at end of file
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