{
 "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"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c072e72d",
   "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": 3,
   "id": "d9e130fc",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "lDIR='DATA_RAW'\n",
    "file0='TOA5_TOB1_2095.kacosonic_'\n",
    "file1=\"csat_2015_04_11_0000\"\n",
    "file1=\"csat_2015_04_12_0000\"\n",
    "file1=\"csat_2015_04_13_0000\"\n",
    "file1=\"csat_2015_04_14_0000\"\n",
    "file1=\"csat_2015_04_15_0000\"\n",
    "file1=\"csat_2015_04_16_0000\"\n",
    "file1=\"csat_2015_04_17_0000\"\n",
    "file1=\"csat_2015_04_18_0000\"\n",
    "file1=\"csat_2015_04_19_0000\" # il y a des NAN non traités\n",
    "file1=\"csat_2015_04_20_0000\"\n",
    "file1=\"csat_2015_04_21_0000\"\n",
    "file1=\"csat_2015_04_22_0000\"\n",
    "file1=\"csat_2015_04_10_0000\"\n",
    "file1=\"csat_2015_04_10_0942\"\n",
    "file1=\"csat_2015_04_09_0000\"\n",
    "file1=\"csat_2015_04_08_0000\"\n",
    "file_path = file0+file1+\".dat\"\n",
    "Path=Path/lDIR/file_path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6697798e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                      TIMESTAMP    RECORD     Ux_1     Uy_1      Uz_1  \\\n",
      "2        2015-04-08 00:00:00.05  81783737  0.36525  0.20125  -0.46075   \n",
      "3         2015-04-08 00:00:00.1  81783738   0.3765    0.196  -0.47575   \n",
      "4        2015-04-08 00:00:00.15  81783739   0.3355  0.16775   -0.4895   \n",
      "5         2015-04-08 00:00:00.2  81783740  0.28575  0.16825  -0.51225   \n",
      "6        2015-04-08 00:00:00.25  81783741  0.34125  0.12825  -0.56475   \n",
      "...                         ...       ...      ...      ...       ...   \n",
      "1727997   2015-04-08 23:59:59.8  83511732   0.5945  0.26375  -1.30825   \n",
      "1727998  2015-04-08 23:59:59.85  83511733  0.75425    0.256  -1.18475   \n",
      "1727999   2015-04-08 23:59:59.9  83511734  0.54025  0.23725    -1.058   \n",
      "1728000  2015-04-08 23:59:59.95  83511735   0.5295  0.19875   -1.0685   \n",
      "1728001     2015-04-09 00:00:00  83511736  0.54275  0.37025   -1.0555   \n",
      "\n",
      "          T_sonic_1 diag_csat_1       Ux_2     Uy_2        Uz_2   T_sonic_2  \\\n",
      "2         0.1286926          41      0.082  0.26325    -0.36075  -0.3939209   \n",
      "3         0.1286926          42    0.23825   0.4375    -0.35375  -0.4120178   \n",
      "4         0.1236877          43     0.1825  0.28875    -0.38025  -0.3889465   \n",
      "5        0.07754517          44     0.2805   0.3155    -0.37575   -0.374176   \n",
      "6         0.1204224          45    0.36175   0.3855      -0.342  -0.3494263   \n",
      "...             ...         ...        ...      ...         ...         ...   \n",
      "1727997    7.542389          36      0.998    0.023     -0.9715    7.214844   \n",
      "1727998    7.525635          37       1.04  0.03325  -0.9235001    7.169769   \n",
      "1727999    7.455475          38     0.7655  0.10575     -0.9495    7.072876   \n",
      "1728000    7.427063          39    0.72325  0.13625  -0.9480001    7.076233   \n",
      "1728001    7.462158          40  0.8355001   0.2935     -1.0185    7.081238   \n",
      "\n",
      "        diag_csat_2       Ux_3        Uy_3        Uz_3   T_sonic_3  \\\n",
      "2                41     0.3145       0.452    -0.43925  -0.2307739   \n",
      "3                42     0.3105      0.6035     -0.4735  -0.2225342   \n",
      "4                43    0.31675   0.5732501     -0.4485  -0.2093506   \n",
      "5                44     0.3825       0.501     -0.3755  -0.2077026   \n",
      "6                45      0.406      0.4285    -0.36525  -0.2192078   \n",
      "...             ...        ...         ...         ...         ...   \n",
      "1727997          36  0.9867501    -0.01675  -0.9137501    7.664429   \n",
      "1727998          37    1.01175     -0.0385  -0.9365001    7.589172   \n",
      "1727999          38  0.9845001  0.09325001  -0.8700001     7.64267   \n",
      "1728000          39     0.8925     0.12725    -0.82025    7.517273   \n",
      "1728001          40    0.62475     0.32825     -0.9755    7.478882   \n",
      "\n",
      "        diag_csat_3  Vbatt  \n",
      "2                41  12.58  \n",
      "3                42  12.58  \n",
      "4                43  12.58  \n",
      "5                44  12.58  \n",
      "6                45  12.58  \n",
      "...             ...    ...  \n",
      "1727997          36  12.67  \n",
      "1727998          37  12.67  \n",
      "1727999          38  12.67  \n",
      "1728000          39  12.67  \n",
      "1728001          40  12.66  \n",
      "\n",
      "[1728000 rows x 18 columns]\n",
      "2         2015-04-08 00:00:00.050\n",
      "3         2015-04-08 00:00:00.100\n",
      "4         2015-04-08 00:00:00.150\n",
      "5         2015-04-08 00:00:00.200\n",
      "6         2015-04-08 00:00:00.250\n",
      "                    ...          \n",
      "1727997   2015-04-08 23:59:59.800\n",
      "1727998   2015-04-08 23:59:59.850\n",
      "1727999   2015-04-08 23:59:59.900\n",
      "1728000   2015-04-08 23:59:59.950\n",
      "1728001   2015-04-09 00:00:00.000\n",
      "Name: TIMESTAMP, Length: 1728000, dtype: datetime64[ns]\n"
     ]
    }
   ],
   "source": [
    "data_sonic = pd.read_csv(Path, header=1,low_memory=False)[2::]\n",
    "print(data_sonic)\n",
    "csat_u1 = data_sonic['Ux_1'].astype(float) \n",
    "csat_v1 = data_sonic['Uy_1'].astype(float) \n",
    "csat_w1 = data_sonic['Uz_1'].astype(float) \n",
    "csat_t1 = data_sonic['T_sonic_1'].astype(float) \n",
    "csat_u2 = data_sonic['Ux_2'].astype(float) \n",
    "csat_v2 = data_sonic['Uy_2'].astype(float) \n",
    "csat_w2 = data_sonic['Uz_2'].astype(float) \n",
    "csat_t2 = data_sonic['T_sonic_2'].astype(float) \n",
    "csat_u3 = data_sonic['Ux_3'].astype(float) \n",
    "csat_v3 = data_sonic['Uy_3'].astype(float) \n",
    "csat_w3 = data_sonic['Uz_3'].astype(float) \n",
    "csat_t3 = data_sonic['T_sonic_3'].astype(float) \n",
    "# files 11-22 avril\n",
    "#csat_y = data_sonic['YEAR']\n",
    "#csat_m = data_sonic['MONTH']\n",
    "#csat_d = data_sonic['DAY']\n",
    "#csat_h = data_sonic['HOUR']\n",
    "#csat_mi = data_sonic['MINUTE']\n",
    "#csat_s = data_sonic['SECOND']\n",
    "#time= pd.DataFrame({'year': csat_y, 'month': csat_m , \n",
    "#                  'day': csat_d, 'hour': csat_h, \n",
    "#                  'minute': csat_mi, 'second': csat_s} )\n",
    "#print(time)\n",
    "#time_sonic = pd.to_datetime(time, unit='s')\n",
    "##\n",
    "# files 10 avril\n",
    "time_sonic = pd.to_datetime(data_sonic['TIMESTAMP'])\n",
    "N=len(data_sonic)\n",
    "print(time_sonic)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "fe82628a",
   "metadata": {},
   "outputs": [],
   "source": [
    "#rotation du repere CSAT\n",
    "file_path = file1+\".nc\"\n",
    "ds = nc.Dataset(file_path, 'w', format='NETCDF4')\n",
    "\n",
    "# taille complete 24h\n",
    "#ds.createDimension('t', 1728000)\n",
    "# taill plus courte fin de campagne\n",
    "#ds.createDimension('t', 671249)\n",
    "ds.createDimension('t', N)\n",
    "\n",
    "\n",
    "ti = ds.createVariable('time', 'f8', ('t'))\n",
    "u1 = ds.createVariable('u_csat1', 'f8', ('t'))\n",
    "v1 = ds.createVariable('v_csat1', 'f8', ('t'))\n",
    "w1  = ds.createVariable('w_csat1', 'f8', ('t'))\n",
    "temp1  = ds.createVariable('temp_csat1', 'f8', ('t'))\n",
    "u2 = ds.createVariable('u_csat2', 'f8', ('t'))\n",
    "v2 = ds.createVariable('v_csat2', 'f8', ('t'))\n",
    "w2  = ds.createVariable('w_csat2', 'f8', ('t'))\n",
    "temp2  = ds.createVariable('temp_csat2', 'f8', ('t'))\n",
    "u3 = ds.createVariable('u_csat3', 'f8', ('t'))\n",
    "v3 = ds.createVariable('v_csat3', 'f8', ('t'))\n",
    "w3  = ds.createVariable('w_csat3', 'f8', ('t'))\n",
    "temp3  = ds.createVariable('temp_csat3', 'f8', ('t'))\n",
    "\n",
    "#rotation du repere CSAT x downslope, z normal to the slope, y direct/xz\n",
    "ti[:] = time_sonic\n",
    "u1[:] = csat_u1\n",
    "v1[:] = csat_w1\n",
    "w1[:] = -csat_v1\n",
    "#intercalibration CSAT2 /CSAT1 & CSAT3 => +0.5K A. Philibert M2 2015 p31\n",
    "temp1[:] = csat_t1\n",
    "u2[:] = csat_u2\n",
    "v2[:] = csat_w2\n",
    "w2[:] = -csat_v2\n",
    "temp2[:] = csat_t2+0.5\n",
    "u3[:] = csat_u3\n",
    "v3[:] = csat_w3\n",
    "w3[:] = -csat_v3\n",
    "temp3[:] = csat_t3\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
}
