{
 "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": "ede82261",
   "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_GC2015'\n",
    "lDIR='DATA_RAW'\n",
    "file0='TOA5_TOB1_6021.kacosonic_'\n",
    "file1=\"gill_2015_04_11_0000\"\n",
    "file1=\"gill_2015_04_12_0000\"\n",
    "file1=\"gill_2015_04_13_0000\"\n",
    "file1=\"gill_2015_04_14_0000\"\n",
    "file1=\"gill_2015_04_15_0000\"\n",
    "file1=\"gill_2015_04_16_0000\"\n",
    "file1=\"gill_2015_04_17_0000\"\n",
    "file1=\"gill_2015_04_18_0000\" #pb data infinie vers 5h00\n",
    "file1=\"gill_2015_04_19_0000\"\n",
    "file1=\"gill_2015_04_20_0000\" #pb data infinie vers 4h00\n",
    "file1=\"gill_2015_04_21_0000\"\n",
    "file1=\"gill_2015_04_22_0000\"\n",
    "file1=\"gill_2015_04_10_0850\"\n",
    "file1=\"gill_2015_04_10_0933\"\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 Gill_Ux Gill_Uy Gill_Uz Gill_TSonic  \\\n",
      "2         2015-04-10 09:33:52.7  4916935   -0.04  -0.727   0.418        5.67   \n",
      "3         2015-04-10 09:33:52.9  4916936  -0.145  -0.799   0.316        5.67   \n",
      "4        2015-04-10 09:33:52.95  4916937  -0.211  -0.821   0.293        5.65   \n",
      "5           2015-04-10 09:33:53  4916938  -0.113  -0.851   0.248        5.72   \n",
      "6        2015-04-10 09:33:53.05  4916939  -0.173   -0.81   0.242        5.67   \n",
      "...                         ...      ...     ...     ...     ...         ...   \n",
      "1039341   2015-04-10 23:59:59.8  5956274   0.407  -0.944  -0.144           7   \n",
      "1039342  2015-04-10 23:59:59.85  5956275   0.328  -0.649   -0.12        6.88   \n",
      "1039343   2015-04-10 23:59:59.9  5956276   0.433  -0.578  -0.121         6.9   \n",
      "1039344  2015-04-10 23:59:59.95  5956277   0.568  -0.686    -0.1        6.93   \n",
      "1039345     2015-04-11 00:00:00  5956278   0.699  -0.656  -0.012        6.92   \n",
      "\n",
      "         Vbatt  \n",
      "2        12.99  \n",
      "3        12.99  \n",
      "4           13  \n",
      "5           13  \n",
      "6           13  \n",
      "...        ...  \n",
      "1039341  12.45  \n",
      "1039342  12.51  \n",
      "1039343  12.51  \n",
      "1039344  12.51  \n",
      "1039345  12.51  \n",
      "\n",
      "[1039344 rows x 7 columns]\n",
      "2         2015-04-10 09:33:52.700\n",
      "3         2015-04-10 09:33:52.900\n",
      "4         2015-04-10 09:33:52.950\n",
      "5         2015-04-10 09:33:53.000\n",
      "6         2015-04-10 09:33:53.050\n",
      "                    ...          \n",
      "1039341   2015-04-10 23:59:59.800\n",
      "1039342   2015-04-10 23:59:59.850\n",
      "1039343   2015-04-10 23:59:59.900\n",
      "1039344   2015-04-10 23:59:59.950\n",
      "1039345   2015-04-11 00:00:00.000\n",
      "Name: TIMESTAMP, Length: 1039344, dtype: datetime64[ns]\n"
     ]
    }
   ],
   "source": [
    "data_sonic = pd.read_csv(Path, header=1,low_memory=False)[2::]\n",
    "print(data_sonic)\n",
    "gill_u1 = data_sonic['Gill_Ux'].astype(float) \n",
    "gill_v1 = data_sonic['Gill_Uy'].astype(float) \n",
    "gill_w1 = data_sonic['Gill_Uz'].astype(float) \n",
    "gill_t1 = data_sonic['Gill_TSonic'].astype(float) \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 14/04/2015 NAN?\n",
    "#ds.createDimension('t', 1727998)\n",
    "# taill plus courte 14/04/2015 NAN?\n",
    "#ds.createDimension('t', 656301)\n",
    "ds.createDimension('t', N)\n",
    "\n",
    "ti = ds.createVariable('time', 'f8', ('t'))\n",
    "u1 = ds.createVariable('Gill_Ux', 'f8', ('t'))\n",
    "v1 = ds.createVariable('Gill_Uy', 'f8', ('t'))\n",
    "w1  = ds.createVariable('Gill_Uz', 'f8', ('t'))\n",
    "temp1  = ds.createVariable('Gill_TSonic', 'f8', ('t'))\n",
    "\n",
    "#no rotation du repere GILL x downslope, z normal to the slope, y direct/xz\n",
    "ti[:] = time_sonic\n",
    "u1[:] = gill_u1\n",
    "v1[:] = gill_v1\n",
    "w1[:] = gill_w1\n",
    "temp1[:] = gill_t1\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
}
