{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom datetime import datetime, timedelta\nfrom os import listdir\nimport matplotlib.pyplot as plt\nfrom shapely.geometry import Point\nimport geopandas as gpd\nfrom geopandas import GeoDataFrame\nfrom tqdm import tqdm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def extract_pm(filepath):\n    with open(filepath, 'r') as f:\n        lines = [line.strip() for line in f.readlines()[10:]]\n    \n    datetimes = []\n    pms = []\n    for line in lines:\n        year,month,day,hour,pm = line.split()[:5]\n        dt = datetime(int(year),int(month),int(day),int(hour)) + timedelta(hours=9)\n        datetimes.append(dt)\n        pms.append(float(pm))\n        \n    df = pd.DataFrame()\n    df['datetime'] = datetimes\n    df['pm'] = pms\n    df.set_index('datetime', inplace=True)\n    \n    return df\n\ndef extract_hotspot():\n    root_dir = '../input/2110446-data-science-and-data-engineering-2021/DS_kaggle_edited/Fire hotspot/'\n    dfs = []\n    for filename  in listdir(root_dir):\n        df = pd.read_csv(root_dir+filename)\n        df['datetime'] = pd.to_datetime(df['acq_date'] + df['acq_time'].apply(lambda x: '00000'+str(x)).apply(lambda x: ' '+x[-4:-2]+':'+x[-2:]+':00'))\n        df['datetime'] = df['datetime'] + timedelta(hours=7)\n        dfs.append((df,filename))\n    return dfs\n\ndef plot_geo(df):\n    geometry = [Point(xy) for xy in zip(df['longitude'], df['latitude'])]\n    gdf = GeoDataFrame(geometry=geometry)   \n\n    #this is a simple map that goes with geopandas\n    world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))\n    ax = world.plot(figsize=(18, 9))\n    gdf.plot(ax=ax, marker='o', color='red', markersize=15)\n    BBox = (0.571, 25.523, 93.032, 111.226)\n    ax.set_xlim(BBox[2], BBox[3])\n    ax.set_ylim(BBox[0], BBox[1])\n    \n    return ax\n\ndef visualize_hotspot_geo(dfs):\n    for df,filename in dfs:\n        print(filename)\n        plot_geo(df)\n        plt.show()\n            ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfs = extract_hotspot()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"visualize_hotspot_geo(dfs)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp_bkk = pd.read_csv('../input/2110446-data-science-and-data-engineering-2021/DS_kaggle_edited/Bangkok/Train/3H_temperature_Bangkok.csv', parse_dates=['datetime'], index_col='datetime')\ntemp_bkk.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp_bkk[['Temp(C)']].plot()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wind_bkk = pd.read_csv('../input/2110446-data-science-and-data-engineering-2021/DS_kaggle_edited/Bangkok/Train/3H_wind_Bangkok.csv')\nwind_bkk.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wind_bkk[['WindDir','Wind Speed(km/h)']].plot()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bkk_pm = extract_pm('../input/2110446-data-science-and-data-engineering-2021/DS_kaggle_edited/Bangkok/Train/Bangkok.txt')\nbkk_pm.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bkk_pm.plot()","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}