{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c2603451",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import netCDF4 as nc\n",
    "import datetime as datetime\n",
    "from matplotlib import dates\n",
    "import os\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d9e130fc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/Users/cbrun/Documents/PROJETS/GC2014/DATA_GC2015\n"
     ]
    }
   ],
   "source": [
    "from pathlib import Path\n",
    "print(Path.cwd())\n",
    "Path = Path.cwd()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "ce292ab7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                 TIMESTAMP RECORD batt_volt_Min PTemp_Avg TAir_1_Avg  \\\n",
      "2      2015-04-07 13:30:00      0         14.59     5.334      0.144   \n",
      "3      2015-04-07 13:31:00      1         14.56     5.225      0.067   \n",
      "4      2015-04-07 13:32:00      2         14.58     5.063          0   \n",
      "5      2015-04-07 13:33:00      3         14.59      4.98      0.047   \n",
      "6      2015-04-07 13:34:00      4         14.58     4.959      0.066   \n",
      "...                    ...    ...           ...       ...        ...   \n",
      "21329  2015-04-22 09:11:00  21327         14.65     14.21      11.02   \n",
      "21330  2015-04-22 09:12:00  21328         14.66     14.31      11.13   \n",
      "21331  2015-04-22 09:13:00  21329         14.61      14.4      11.13   \n",
      "21332  2015-04-22 09:14:00  21330         14.66     14.55      10.96   \n",
      "21333  2015-04-22 09:15:00  21331         14.56     14.68      10.65   \n",
      "\n",
      "      HRAir_1_Avg TAir_2_Avg HRAir_2_Avg TAir_3_Avg HRAir_3_Avg  ...  \\\n",
      "2           74.63      0.126       74.27      0.305       75.71  ...   \n",
      "3           74.67      0.095       74.19      0.186       75.91  ...   \n",
      "4           74.98      0.067       74.34      0.125       76.08  ...   \n",
      "5           75.07      0.069       74.11      0.182       76.33  ...   \n",
      "6              75      0.089       74.26      0.177       76.22  ...   \n",
      "...           ...        ...         ...        ...         ...  ...   \n",
      "21329       52.55      11.31        51.4      11.51       53.52  ...   \n",
      "21330       49.73      11.48       48.56      11.62        50.7  ...   \n",
      "21331       51.47      11.33       51.29      11.56       51.91  ...   \n",
      "21332       53.03      11.09        52.2      11.59       53.48  ...   \n",
      "21333       54.53      10.73       54.23       11.3       55.36  ...   \n",
      "\n",
      "      long_dn_cor_Avg Rs_net_Avg Rl_net_Avg Albedo_Avg Rn_Avg EnetSw_Avg  \\\n",
      "2                   0          0          0          0      0       1641   \n",
      "3                   0          0          0          0      0       1882   \n",
      "4                   0          0          0          0      0       1597   \n",
      "5                   0          0          0          0      0       2329   \n",
      "6                   0          0          0          0      0       2681   \n",
      "...               ...        ...        ...        ...    ...        ...   \n",
      "21329               0          0          0          0      0       1925   \n",
      "21330               0          0          0          0      0       2235   \n",
      "21331               0          0          0          0      0       2004   \n",
      "21332               0          0          0          0      0       1297   \n",
      "21333               0          0          0          0      0       1121   \n",
      "\n",
      "      EnetLw_Avg Enet_Avg CGR4_TK_Avg CGR4_CO_Avg  \n",
      "2          -2186   -544.8    279.5029    319.0917  \n",
      "3          -2485   -602.7    279.2775    319.9019  \n",
      "4          -2141   -543.8    278.9731    320.3253  \n",
      "5          -3044   -715.5    278.7687    319.3202  \n",
      "6          -3478     -797    278.5817    318.4019  \n",
      "...          ...      ...         ...         ...  \n",
      "21329      -2734     -809    288.8086    363.7031  \n",
      "21330      -3130     -895    288.8118    363.7216  \n",
      "21331      -2851     -847    288.8188    365.6833  \n",
      "21332      -1983   -685.4    288.8909    367.3604  \n",
      "21333      -1749   -628.2    288.9464    368.3051  \n",
      "\n",
      "[21332 rows x 37 columns]\n",
      "2       2015-04-07 13:30:00\n",
      "3       2015-04-07 13:31:00\n",
      "4       2015-04-07 13:32:00\n",
      "5       2015-04-07 13:33:00\n",
      "6       2015-04-07 13:34:00\n",
      "                ...        \n",
      "21329   2015-04-22 09:11:00\n",
      "21330   2015-04-22 09:12:00\n",
      "21331   2015-04-22 09:13:00\n",
      "21332   2015-04-22 09:14:00\n",
      "21333   2015-04-22 09:15:00\n",
      "Name: TIMESTAMP, Length: 21332, dtype: datetime64[ns]\n",
      "2        0.144\n",
      "3        0.067\n",
      "4            0\n",
      "5        0.047\n",
      "6        0.066\n",
      "         ...  \n",
      "21329    11.02\n",
      "21330    11.13\n",
      "21331    11.13\n",
      "21332    10.96\n",
      "21333    10.65\n",
      "Name: TAir_1_Avg, Length: 21332, dtype: object\n",
      "2        279.5029\n",
      "3        279.2775\n",
      "4        278.9731\n",
      "5        278.7687\n",
      "6        278.5817\n",
      "           ...   \n",
      "21329    288.8086\n",
      "21330    288.8118\n",
      "21331    288.8188\n",
      "21332    288.8909\n",
      "21333    288.9464\n",
      "Name: CGR4_TK_Avg, Length: 21332, dtype: object\n",
      "2        -2186\n",
      "3        -2485\n",
      "4        -2141\n",
      "5        -3044\n",
      "6        -3478\n",
      "         ...  \n",
      "21329    -2734\n",
      "21330    -3130\n",
      "21331    -2851\n",
      "21332    -1983\n",
      "21333    -1749\n",
      "Name: EnetLw_Avg, Length: 21332, dtype: object\n",
      "2        1641\n",
      "3        1882\n",
      "4        1597\n",
      "5        2329\n",
      "6        2681\n",
      "         ... \n",
      "21329    1925\n",
      "21330    2235\n",
      "21331    2004\n",
      "21332    1297\n",
      "21333    1121\n",
      "Name: EnetSw_Avg, Length: 21332, dtype: object\n",
      "2        319.0917\n",
      "3        319.9019\n",
      "4        320.3253\n",
      "5        319.3202\n",
      "6        318.4019\n",
      "           ...   \n",
      "21329    363.7031\n",
      "21330    363.7216\n",
      "21331    365.6833\n",
      "21332    367.3604\n",
      "21333    368.3051\n",
      "Name: CGR4_CO_Avg, Length: 21332, dtype: object\n"
     ]
    }
   ],
   "source": [
    "#DATA GC fev 2023\n",
    "file_path = 'Data_meteo.dat'\n",
    "#file_path = '../28NOV/TOA5_4585.kacosonic_mto.dat'\n",
    "data_meteo = pd.read_csv(file_path, header=1, low_memory=False)[2::]\n",
    "print(data_meteo)\n",
    "N=len(data_meteo)\n",
    "#time=data_meteo['TIMESTAMP']\n",
    "time_meteo = pd.to_datetime(data_meteo['TIMESTAMP'])\n",
    "#pression = data_meteo['BP_hPa_Avg'].astype(float) #\n",
    "temperature1 = data_meteo['TAir_1_Avg']#.astype(float) #\n",
    "temperature2 = data_meteo['TAir_2_Avg']#.astype(float) #\n",
    "temperature3 = data_meteo['TAir_3_Avg']#.astype(float) #\n",
    "humidite1 = data_meteo['HRAir_1_Avg']#.astype(float) #\n",
    "humidite2 = data_meteo['HRAir_2_Avg']#.astype(float) #\n",
    "humidite3 = data_meteo['HRAir_3_Avg']#.astype(float) #\n",
    "vitesse1_norm = data_meteo['Windsonic_1_Vit_Avg']#.astype(float) #\n",
    "vitesse1_dir = data_meteo['Windsonic_1_Dir_Avg']#.astype(float) #\n",
    "vitesse2_norm = data_meteo['Windsonic_2_Vit_Avg']#.astype(float) #\n",
    "vitesse2_dir = data_meteo['Windsonic_2_Dir_Avg']#.astype(float) #\n",
    "vitesse3_norm = data_meteo['WMT700_WS_Avg']#.astype(float) #\n",
    "vitesse3_dir = data_meteo['WMT700_WD_Avg']#.astype(float) #\n",
    "vitesse3_x = data_meteo['WMT700_X_Avg']#.astype(float)\n",
    "vitesse3_y = data_meteo['WMT700_Y_Avg']#.astype(float)\n",
    "#\n",
    "##Kipp & Zonen CGR4 Pyrgeometer#\n",
    "temperature_pt100 = data_meteo['CGR4_TK_Avg']#.astype(float)\n",
    "rayonnement_up_LW = data_meteo['CGR4_CO_Avg']#.astype(float)\n",
    "#\n",
    "##CNR1\n",
    "#temperature_cnr1 = data_meteo['CNR1_T_C_Avg']#.astype(float)\n",
    "#rayonnement_up_LW = data_meteo['long_up_cor_Avg']#.astype(float)\n",
    "#rayonnement_down_LW = data_meteo['long_dn_Avg']#.astype(float)\n",
    "#rayonnement_up_SW = data_meteo['short_up_Avg']#.astype(float)\n",
    "#rayonnement_down_SW = data_meteo['short_dn_Avg']#.astype(float)\n",
    "#\n",
    "## CNR2 radiometer SW and LW net\n",
    "# ATTENTION coeff sensibilité à verifier!!\n",
    "rayonnement_LW = data_meteo['EnetLw_Avg']#.astype(float)\n",
    "rayonnement_SW = data_meteo['EnetSw_Avg']#.astype(float)\n",
    "rayonnement_net = data_meteo['Enet_Avg']#.astype(float)\n",
    "print(time_meteo)\n",
    "#print(time)\n",
    "print(temperature1)\n",
    "print(temperature_pt100)\n",
    "print(rayonnement_LW)\n",
    "print(rayonnement_SW)\n",
    "print(rayonnement_up_LW)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "eeeb271c",
   "metadata": {},
   "outputs": [],
   "source": [
    "ds = nc.Dataset('meteo_GC_avril2015.nc', 'w', format='NETCDF4')\n",
    "\n",
    "ds.createDimension('t', N)\n",
    "\n",
    "ti= ds.createVariable('time', 'f8', ('t',))\n",
    "T1 = ds.createVariable('air temperature 1', 'f8', ('t',))\n",
    "rh1 = ds.createVariable('air relative humidity 1', 'f8', ('t',))\n",
    "T2 = ds.createVariable('air temperature 2', 'f8', ('t',))\n",
    "rh2 = ds.createVariable('air relative humidity 2', 'f8', ('t',))\n",
    "T3 = ds.createVariable('air temperature 3', 'f8', ('t',))\n",
    "rh3 = ds.createVariable('air relative humidity 3', 'f8', ('t',))\n",
    "V1  = ds.createVariable('velocity norm 1', 'f8', ('t',))\n",
    "angle1  = ds.createVariable('velocity direction 1', 'f8', ('t',))\n",
    "V2  = ds.createVariable('velocity norm 2', 'f8', ('t',))\n",
    "angle2  = ds.createVariable('velocity direction 2', 'f8', ('t',))\n",
    "V3  = ds.createVariable('velocity norm 3', 'f8', ('t',))\n",
    "angle3  = ds.createVariable('velocity direction 3', 'f8', ('t',))\n",
    "ux3  = ds.createVariable('velocity 3 x', 'f8', ('t',))\n",
    "uy3  = ds.createVariable('velocity 3 y', 'f8', ('t',))\n",
    "Tp  = ds.createVariable('pt100 temperature', 'f8', ('t',))\n",
    "Lwup  = ds.createVariable('LW UP surface radiation CGR4', 'f8', ('t',))\n",
    "Rn  = ds.createVariable('net surface radiation CNR2', 'f8', ('t',))\n",
    "Lw  = ds.createVariable('LW net surface radiation CNR2', 'f8', ('t',))\n",
    "Sw  = ds.createVariable('SW net surface radiation CNR2', 'f8', ('t',))\n",
    "\n",
    "ti[:] = time_meteo\n",
    "T1[:] = temperature1\n",
    "rh1[:] = humidite1\n",
    "T2[:] = temperature2\n",
    "rh2[:] = humidite2\n",
    "T3[:] = temperature3\n",
    "rh3[:] = humidite3\n",
    "V1[:] = vitesse1_norm\n",
    "angle2[:] = vitesse1_dir\n",
    "V2[:] = vitesse2_norm\n",
    "angle3[:] = vitesse2_dir\n",
    "V3[:] = vitesse3_norm\n",
    "angle1[:] = vitesse3_dir\n",
    "ux3[:] = vitesse3_x\n",
    "uy3[:] = vitesse3_y\n",
    "Tp[:] = temperature_pt100#-273.15\n",
    "Lwup[:] = rayonnement_up_LW\n",
    "Rn[:] = rayonnement_net\n",
    "Lw[:] = rayonnement_LW\n",
    "Sw[:] = rayonnement_SW\n",
    "\n",
    "ds.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.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
