{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Preparing climate file for CIM" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " Script prepared by Dr. Dasaraden Mauree \n", " Contact: EPFL \n", " dasaraden.mauree@epfl.ch / dasaraden.mauree@gmail.com \n", " \n", " use this script to read the standard Meteonorm file\n", " and replace some values from file obtained from \n", " Vahid Nik file." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "#importing necessary packages\n", "import pandas as pd\n", "import os as os\n", "import re" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def swap_columns(df, c1, c2):\n", " df['temp'] = df[c1]\n", " df[c1] = df[c2]\n", " df[c2] = df['temp']\n", " df.drop(columns=['temp'], inplace=True)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def new_col(df,df1):\n", " header_met=df.columns\n", " df1.columns=header_met\n", " \n", " df1[['dm', 'm', 'h', 'G_Dh', 'G_Bn', 'DD', 'RH', 'N']]= df1[['dm', 'm', 'h', 'G_Dh', 'G_Bn', 'DD', 'RH', 'N']].astype(int)\n", " df1['h']=df1['h']+1\n", " df1[['Ta', 'FF', 'RR']]=df1[['Ta', 'FF', 'RR']].round(1)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "def remove(string): \n", " pattern = re.compile(r'\\s+') \n", " return re.sub(pattern, ',', string) " ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "extreme=\"data\\Vahid_files\"\n", "path=\"data\\Meteonorm_files\"\n", "out=\"data\\Merged_files\"\n", "\n", "filelist = os.listdir(path)\n", "header_vf=['YY','m','dm','h','G_Dh','G_Bn','Ta','Ts','DD','FF','RH','HOR','N']\n", "\n", "for i in filelist:\n", " city=i.split('.')[0]\n", " #print(\"Reading \"+city)\n", " meteonorm=pd.read_csv(path+\"/\"+i, header=\"infer\", sep=\"\\s+\", encoding = \"ISO-8859-1\", skiprows=3)\n", " ecy=pd.read_csv(extreme+\"/\"+city+\"_ECY_multiple_13Scs_2070_2099\", header=None, names=header_vf, sep=\"\\s+\", encoding = \"ISO-8859-1\")\n", " ewy=pd.read_csv(extreme+\"/\"+city+\"_EWY_multiple_13Scs_2070_2099\", header=None, names=header_vf, sep=\"\\s+\", encoding = \"ISO-8859-1\")\n", " #print(\"Done reading \"+city)\n", " \n", " ecy=ecy.drop(['YY','Ts'], axis=1)\n", " ewy=ewy.drop(['YY','Ts'], axis=1)\n", " ecy['HOR']=meteonorm['RR']\n", " ewy['HOR']=meteonorm['RR']\n", " swap_columns(ecy,'DD','FF')\n", " swap_columns(ewy,'DD','FF')\n", " swap_columns(ecy,'m','dm')\n", " swap_columns(ewy,'m','dm')\n", " \n", " new_col(meteonorm,ecy)\n", " new_col(meteonorm,ewy)\n", " \n", " # open file in read mode \n", " fn = open(path+\"/\"+i, 'r') \n", "\n", " # open other file in write mode \n", " fn1 = open(out+\"/\"+city+'_ECY.cli', 'w')\n", " fn2 = open(out+\"/\"+city+'_EWY.cli', 'w') \n", "\n", " # read the content of the file line by line \n", " cont = fn.readlines() \n", " for i in range(0, 3): \n", " cont[i]=(cont[i].replace(',','.'))\n", " if(i==1):\n", " cont[i]=remove(cont[i])\n", " cont[i]=cont[i][1:-1]\n", " fn1.write(cont[i])\n", " fn2.write(cont[i])\n", " fn1.write('\\n')\n", " fn2.write('\\n')\n", "\n", " # close the file \n", " fn.close() \n", " fn1.close()\n", " fn2.close()\n", " \n", " #Selecting columns and saving in file for Citysim\n", " ecy.to_csv(out+\"/\"+city+'_ECY.cli', sep=' ', mode='a', index=False)\n", " ewy.to_csv(out+\"/\"+city+'_EWY.cli', sep=' ', mode='a', index=False)" ] }, { "cell_type": "code", "execution_count": 146, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 52.517 13.3889 43 1 \n", "\n", "52.517,13.3889,43,1\n" ] } ], "source": [ "# open file in read mode \n", "fn = open(path+\"/Berlin.cli\", 'r') \n", "\n", " # open other file in write mode \n", "fn1 = open(\"test.cli\", 'w')\n", "\n", " # read the content of the file line by line \n", "cont = fn.readlines() \n", "for i in range(0, 3):\n", " cont[i]=(cont[i].replace(',','.'))\n", " if(i==1):\n", " print(cont[i])\n", " cont[i]=remove(cont[i])\n", " cont[i]=cont[i][1:-1]\n", " print(cont[i])\n", " fn1.write(cont[i])\n", "fn1.write('\\n')\n", "\n", " # close the file \n", "fn.close() \n", "fn1.close()\n", " \n", " #Selecting columns and saving in file for Citysim\n", "ecy.to_csv(\"test.cli\", sep=' ', mode='a', index=False)" ] } ], "metadata": { "anaconda-cloud": {}, "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": 2 }