{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1db3b94c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import xarray as xr\n",
    "import os\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8927fa60",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                           time    lwp\n",
      "0 2011-02-03 15:51:00.000018775 -0.005\n",
      "1 2011-02-03 15:51:02.000007927 -0.005\n",
      "2 2011-02-03 15:51:03.999997079 -0.005\n",
      "3 2011-02-03 15:51:05.999986231 -0.005\n",
      "4 2011-02-03 15:51:06.999980807 -0.005\n"
     ]
    }
   ],
   "source": [
    "# Define the folder path\n",
    "folder_path = r\"C:\\Users\\magda\\Master_Thesis\\transfer_2989619_files_897465f4\\magda_lwp\"\n",
    "\n",
    "# Initialize an empty list to store dataframes\n",
    "dataframes = []\n",
    "\n",
    "# Loop through all files in the folder\n",
    "for file_name in os.listdir(folder_path):\n",
    "    if file_name.endswith(\".nc\"):  # Ensure we're only processing NetCDF files\n",
    "        file_path = os.path.join(folder_path, file_name)\n",
    "        dataset = xr.open_dataset(file_path)\n",
    "        \n",
    "        # Convert time to a pandas datetime format\n",
    "        time_values = pd.to_datetime(dataset[\"time\"].values)\n",
    "        \n",
    "        # Extract LWP values\n",
    "        lwp_values = dataset[\"lwp\"].values\n",
    "        \n",
    "        # Create a DataFrame\n",
    "        df = pd.DataFrame({\"time\": time_values, \"lwp\": lwp_values})\n",
    "        dataframes.append(df)\n",
    "        \n",
    "        # Close dataset\n",
    "        dataset.close()\n",
    "\n",
    "# Concatenate all dataframes into one\n",
    "final_df = pd.concat(dataframes, ignore_index=True)\n",
    "\n",
    "print(final_df.head())\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e89119df",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                                  time       lwp\n",
      "0        2011-02-03 15:51:00.000018775 -0.005000\n",
      "1        2011-02-03 15:51:02.000007927 -0.005000\n",
      "2        2011-02-03 15:51:03.999997079 -0.005000\n",
      "3        2011-02-03 15:51:05.999986231 -0.005000\n",
      "4        2011-02-03 15:51:06.999980807 -0.005000\n",
      "...                                ...       ...\n",
      "22723211 2012-12-30 22:59:52.000016570  0.023211\n",
      "22723212 2012-12-30 22:59:53.000011146  0.025613\n",
      "22723213 2012-12-30 22:59:55.000000298  0.026112\n",
      "22723214 2012-12-30 22:59:56.999989449  0.033320\n",
      "22723215 2012-12-30 22:59:57.999984025  0.032742\n",
      "\n",
      "[22723216 rows x 2 columns]\n"
     ]
    }
   ],
   "source": [
    "print(final_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "9af05fd8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<xarray.Dataset>\n",
      "Dimensions:          (time: 4464, day_in_time_interval: 31, nv: 2)\n",
      "Coordinates:\n",
      "  * time             (time) datetime64[ns] 2000-05-01 ... 2000-05-31T23:50:00...\n",
      "Dimensions without coordinates: day_in_time_interval, nv\n",
      "Data variables:\n",
      "    iso_dataset      |S1 ...\n",
      "    product          |S1 ...\n",
      "    station_details  |S1 ...\n",
      "    date             (time) int32 ...\n",
      "    valid_dates      (day_in_time_interval) int8 ...\n",
      "    time_bnds        (time, nv) datetime64[ns] ...\n",
      "    SWD              (time) float32 ...\n",
      "    SWU              (time) float32 ...\n",
      "    LWD              (time) float32 ...\n",
      "    LWU              (time) float32 ...\n",
      "Attributes:\n",
      "    title:                    CESAR tower meteorological profiles validated\n",
      "    institution:              Royal Netherlands Meteorological Institute (KNMI)\n",
      "    comment:                  \n",
      "    Conventions:              CF-1.4\n",
      "    location:                 CESAR observatory, the Netherlands\n",
      "    file_creation_date_time:  20110126 12:02:34 (UTC)\n",
      "    history:                  Continuous observations are performed and archi...\n",
      "    references:               cesar_surface_radiation_lb1_t10_v1.0.pdf @ http...\n",
      "\n",
      "Variables in the dataset: ['iso_dataset', 'product', 'station_details', 'time', 'date', 'valid_dates', 'time_bnds', 'SWD', 'SWU', 'LWD', 'LWU']\n",
      "\n",
      "Variable: iso_dataset\n",
      "b''\n",
      "\n",
      "Variable: product\n",
      "b''\n",
      "\n",
      "Variable: station_details\n",
      "b''\n",
      "\n",
      "Variable: time\n",
      "['2000-05-01T00:00:00.000000000' '2000-05-01T00:10:00.000017881'\n",
      " '2000-05-01T00:20:00.000035762' ... '2000-05-31T23:30:00.219726562'\n",
      " '2000-05-31T23:40:00.073242187' '2000-05-31T23:50:00.146484375']\n",
      "\n",
      "Variable: date\n",
      "[20000501 20000501 20000501 ... 20000531 20000531 20000531]\n",
      "\n",
      "Variable: valid_dates\n",
      "[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]\n",
      "\n",
      "Variable: time_bnds\n",
      "[['2000-05-01T00:00:00.000000000' '2000-05-01T00:10:00.000017881']\n",
      " ['2000-05-01T00:10:00.000017881' '2000-05-01T00:20:00.000035762']\n",
      " ['2000-05-01T00:20:00.000035762' '2000-05-01T00:30:00.000000000']\n",
      " ...\n",
      " ['2000-05-31T23:30:00.219726562' '2000-05-31T23:40:00.073242187']\n",
      " ['2000-05-31T23:40:00.073242187' '2000-05-31T23:50:00.146484375']\n",
      " ['2000-05-31T23:50:00.146484375' '2000-06-01T00:00:00.219726562']]\n",
      "\n",
      "Variable: SWD\n",
      "[0. 0. 0. ... 0. 0. 0.]\n",
      "\n",
      "Variable: SWU\n",
      "[0. 0. 0. ... 0. 0. 0.]\n",
      "\n",
      "Variable: LWD\n",
      "[nan nan nan ... nan nan nan]\n",
      "\n",
      "Variable: LWU\n",
      "[nan nan nan ... nan nan nan]\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# Define the file path\n",
    "file_path = r\"C:\\Users\\magda\\Master_Thesis\\transfer_2989619_files_897465f4\\magda_sw\\net\\labdata\\stephan\\Observations\\NCDF_Cabauw\\Surface_Radiation\\cesar_surface_radiation_lb1_t10_v1.0_200005.nc\"\n",
    "\n",
    "# Check if the file exists\n",
    "if not os.path.exists(file_path):\n",
    "    print(f\"File not found: {file_path}\")\n",
    "else:\n",
    "    # Open the NetCDF file\n",
    "    dataset = xr.open_dataset(file_path)\n",
    "    \n",
    "    # Display dataset metadata\n",
    "    print(dataset)\n",
    "    \n",
    "    # Display variable names\n",
    "    print(\"\\nVariables in the dataset:\", list(dataset.variables))\n",
    "    \n",
    "    # Display first few records of each variable\n",
    "    for var in dataset.variables:\n",
    "        print(f\"\\nVariable: {var}\")\n",
    "        print(dataset[var].values)  # Print values directly\n",
    "    \n",
    "    # Close the dataset\n",
    "    dataset.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
