Skip to content

Instantly share code, notes, and snippets.

@bubbobne
Last active September 1, 2020 17:05
Show Gist options
  • Save bubbobne/442eb9ebfa56955550b83416ddc7746b to your computer and use it in GitHub Desktop.
Save bubbobne/442eb9ebfa56955550b83416ddc7746b to your computer and use it in GitHub Desktop.
dati_idrometro
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Download data\n",
"\n",
"Data are available at [UFFICIO DIGHE](https://www.floods.it/public/DatiStoriciDown.php?Argomento=Trento%20Ponte%20S%20Lorenzo&Corpo=fiume%20Adige) whith [CC BY 4.0](http://creativecommons.org/licenses/by/4.0/) license and with attribuition to \"Dati della rete di monitoraggio dell'Ufficio Dighe – Servizio Prevenzione rischi della Provincia Autonoma di Trento\""
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
"years = range(1994,2020)\n",
"data_dir = './data'\n",
"file_str = data_dir + '/portate_san_lorenzo_{}.zip'\n",
"url = 'https://www.floods.it/public/DatiStoriciBis/DSdownloadBis.php?Arg=Trento%20Ponte%20S%20Lorenzo&Type=Portate&Anno={}'\n",
"\n",
"import urllib.request\n",
"import os\n",
"import zipfile\n",
"\n",
"if not os.path.exists(data_dir):\n",
" os.makedirs(data_dir)\n",
"\n",
"for year in years:\n",
" f = file_str.format(year)\n",
" urllib.request.urlretrieve(url.format(year), f)\n",
" with zipfile.ZipFile(f, \"r\") as zip_ref:\n",
" zip_ref.extractall(data_dir)\n",
" \n",
"\n",
"\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2001\n",
"(55, 2)\n",
"2014\n",
"(31, 2)\n",
"1997\n",
"(202, 2)\n"
]
}
],
"source": [
"import pandas as pd\n",
"import re\n",
"\n",
"\n",
"y= '2000'\n",
"for root, dirs, files in os.walk(data_dir):\n",
" for file in files:\n",
" if \".csv\" in file:\n",
" df = pd.read_csv(root+'/'+file, skiprows=6)\n",
" df.columns = ['data', 'portata']\n",
" y = re.findall(r'\\d+', root)[0]\n",
" df = df[(df.portata > 750) & (df.data > y+'-06-01') & (df.data < y+'-08-31') ]\n",
" if df.shape[0] > 0:\n",
" print(y)\n",
" print(df.shape)\n",
"\n",
" #append the file name to the list\n",
"\t\t#print(os.path.join(root,file))\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
#!/usr/bin/env python
# coding: utf-8
# ## Download data
#
# Data are available at [UFFICIO DIGHE](https://www.floods.it/public/DatiStoriciDown.php?Argomento=Trento%20Ponte%20S%20Lorenzo&Corpo=fiume%20Adige) whith [CC BY 4.0](http://creativecommons.org/licenses/by/4.0/) license and with attribuition to "Dati della rete di monitoraggio dell'Ufficio Dighe – Servizio Prevenzione rischi della Provincia Autonoma di Trento"
# In[36]:
years = range(1994,2020)
data_dir = './data'
file_str = data_dir + '/portate_san_lorenzo_{}.zip'
url = 'https://www.floods.it/public/DatiStoriciBis/DSdownloadBis.php?Arg=Trento%20Ponte%20S%20Lorenzo&Type=Portate&Anno={}'
import urllib.request
import os
import zipfile
if not os.path.exists(data_dir):
os.makedirs(data_dir)
for year in years:
f = file_str.format(year)
urllib.request.urlretrieve(url.format(year), f)
with zipfile.ZipFile(f, "r") as zip_ref:
zip_ref.extractall(data_dir)
# In[37]:
import pandas as pd
import re
y= '2000'
for root, dirs, files in os.walk(data_dir):
for file in files:
if ".csv" in file:
df = pd.read_csv(root+'/'+file, skiprows=6)
df.columns = ['data', 'portata']
y = re.findall(r'\d+', root)[0]
df = df[(df.portata > 750) & (df.data > y+'-06-01') & (df.data < y+'-08-31') ]
if df.shape[0] > 0:
print(y)
print(df.shape)
#append the file name to the list
#print(os.path.join(root,file))
# In[ ]:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment