Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

48-smc_read_input #55

Merged
merged 4 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,7 @@
"xema"
],
"python.pythonPath": "~/anaconda3/envs/pymica/bin/python",
"python.testing.unittestArgs": [
"-v",
"-s",
"./test",
"-p",
"*_test.py"
],
"python.testing.pytestEnabled": false,
"python.testing.nosetestsEnabled": false,
"python.testing.unittestEnabled": true,

"python.linting.flake8Enabled": true,
"python.linting.pycodestyleEnabled": false,
"python.linting.pylintPath": "~/anaconda3/envs/pymica_test/bin/python",
Expand Down
66 changes: 66 additions & 0 deletions pymica/io/importers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""Subroutine for reading data from SMC API
"""

import json
import datetime
import urllib.request


from datetime import timedelta, datetime

def import_api_smc(variable: str, url_meta: str,
date: datetime, url: str, station_list: list) -> list:
"""

Args:
variables (str): String containing the name of the variables.
url_meta (str): String which contains the url for download the metadata
date (datetime): Is the time for which we obtained the data
url (str): String which contains the url for download the metadata
station_list (list): List with the codes of stations to be extracted.
If its None returns all the available stations.

Raises:
ValueError: Check if the variables are available.

Returns:
dict: dictionary containing the data ready to be interpolated.
"""

str_variables = {'2t':'32', '2r':'33', 'ws':'30', 'wdir':'31', 'ws':'46',
'wdir':'47', 'ws':'48', 'wdir':'49'}

# Check if the varible is in the possibles
if not set(map(str, [variable])) <= set(str_variables.keys()):
jrmiro marked this conversation as resolved.
Show resolved Hide resolved
raise ValueError('Les variables disponibles són ' +
str(list(str_variables.keys())))

# Mirem que station_list realment sigui una llista i ens assegurem
if not isinstance(station_list,list):
station_list = [None]

json_var=urllib.request.urlopen(url_meta).read()
metadata=json.loads(json_var.decode("utf-8"))


# The LOOP begins
data = []
# We add 5 minutes arbitrarely
json_data = urllib.request.urlopen(
url + "/" + str_variables[variable] + "?din=" +
str(date.strftime("%Y-%m-%dT%H:%MZ")) + "&dfi=" +
str(date.strftime("%Y-%m-%dT%H:%MZ"))).read()
var_data = json.loads(json_data)

for reg in var_data:
if station_list == [None] or (reg['codi'] in station_list):
for lec in reg['variables'][0]['lectures']:
for k in range(len(metadata)):
if metadata[k]['codi']==reg['codi']:
data.append({'id': reg['codi'], 'altitude': metadata[k]['altitud'],
'lat': metadata[k]['coordenades']['latitud'],
'lon': metadata[k]['coordenades']['longitud'],
'value': lec['valor']})

return data

9 changes: 0 additions & 9 deletions pymica/pymica.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,6 @@ def __input_data__(self, data_dict):
if not {'id','lat','lon','value'} < set(elements.keys()):
raise KeyError('id, lat, lon, value keys must be included in the imput data')

if 'id' not in elements.keys():
raise KeyError('id must be included in the data file')
if 'lat' not in elements.keys():
raise KeyError('lat must be included in the data file')
if 'lon' not in elements.keys():
raise KeyError('lon must be included in the data file')
if 'value' not in elements.keys():
raise KeyError('value must be included in the data file')

if self.methodology in ['id3d', 'mlr+id3d']:
for elements in data_dict:
if 'altitude' not in elements.keys():
Expand Down