{ "cells": [ { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import xarray as xr\n", "\n", "# Specify the path to your NetCDF file\n", "file_path = 'transect_r20240117.nc'\n", "file_path = r'C:\\Users\\fuentesm\\Marilu\\Deltares\\Projects\\Westerchelde\\Data\\S2A_MSI_2015_07_16_10_50_24_merged_westerschelde_L2W.nc'\n", "#file_path ='http://opendap.deltares.nl/thredds/dodsC/opendap/rijkswaterstaat/jarkus/profiles/transect.nc' \n", "\n", "# Open the NetCDF file with xarray\n", "ds = xr.open_dataset(file_path)\n", "\n", "# Display the dataset information\n", "# print(ds)\n" ] }, { "cell_type": "code", "execution_count": 267, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C:/Users/fuentesm/Marilu/Deltares/Projects/Westerchelde/Data/S2A_MSI_2015_07_16_10_50_24_merged_westerschelde_L2W.nc\n", "C:/Users/fuentesm/Marilu/Deltares/Projects/Westerchelde/Data/S2A_MSI_2023_08_23_10_56_47_merged_westerschelde_L2W.nc\n" ] } ], "source": [ "import os\n", "import xarray as xr\n", "from datetime import datetime\n", "import matplotlib.pyplot as plt\n", "from datetime import datetime\n", "import pandas as pd\n", "import altair as alt\n", "import numpy as np\n", "\n", "folder_path = 'C:/Users/fuentesm/Marilu/Deltares/Projects/Westerchelde/Data/'\n", "folder_path_stations = 'C:/Users/fuentesm/Marilu/Deltares/Projects/Westerchelde/Data/Stations.xlsx' \n", "\n", "# List all files in the folder\n", "files_in_folder = os.listdir(folder_path)\n", "\n", "# Filter the list to include only files ending with \"ic.nc\"\n", "filtered_files = [file for file in files_in_folder if file.endswith(\"L2W.nc\")]\n", "\n", "filtered_files.sort()\n", "\n", "datasets_list = []\n", "# Print the list of matching files\n", "for file in filtered_files:\n", " path = os.path.join(folder_path , file)\n", " print(path)\n", " dataset = xr.open_dataset(path)\n", " time_series = [datetime.fromisoformat(dataset.attrs.get(\"isodate\"))] # Replace with your time series data\n", " ##new_dataset = xr.concat([dataset[\"chl_re_gons\"]], dim=xr.DataArray(time_series, coords={\"time\": time_series}, dims=[\"time\"]))\n", " new_dataset = xr.concat([dataset], dim=xr.DataArray(time_series, coords={\"time\": time_series}, dims=[\"time\"]))\n", " datasets_list.append(new_dataset)\n", "\n", "merged_data = xr.concat(datasets_list, dim='time')\n" ] }, { "cell_type": "code", "execution_count": 268, "metadata": {}, "outputs": [], "source": [ "def get_window(x_coord, y_coord, stationID):\n", " time_series = merged_data.sel(x=x_coord, y=y_coord, method='nearest')\n", " y_coord_exact = time_series['y'].values.tolist()\n", " x_coord_exact = time_series['x'].values.tolist()\n", " y_index = np.where(merged_data['y'] == y_coord_exact)[0].tolist()[0]\n", " x_index = np.where(merged_data['x'] == x_coord_exact)[0].tolist()[0]\n", " central_lon = x_index\n", " central_lat = y_index\n", "\n", " lat_start, lat_end = central_lat - 1, central_lat + 2\n", " lon_start, lon_end = central_lon - 1, central_lon + 2\n", "\n", " window_values = merged_data.isel(y=slice(lat_start, lat_end), x=slice(lon_start, lon_end))\n", " window_values = window_values.expand_dims(station=[stationID])\n", "\n", " return window_values\n", "\n", "# stationID = 'S1'\n", "# station = 'SCHAARVODDL\tSchaar van Ouden Doel'\n", "# lon = 4.250932603\n", "# lat = 51.35118367\n", "# x_coord = 5.8711167e+05 #5.208e+05 \n", "# y_coord = 5.68962179e+06\n", "\n", "# stationID = 'S2'\n", "# station = 'VLISSGNLDK\tVlissingen Nolledijk'\n", "# lon = 3.553066,\t\n", "# lat = 51.450453\n", "# x_coord = 5.3843131e+05 \n", "# y_coord = 5.70006397e+06 \n", "\n", "df = pd.read_excel(folder_path_stations)\n", "\n", "stations_list = []\n", "for index, row in df.iterrows():\n", " # print(row['stationID'])\n", " station_window = get_window(row['x_coord'], row['y_coord'], row['stationID'])\n", " stations_list.append(station_window)\n", "\n", "merged_stations = xr.concat(stations_list, dim='station')\n", "# station1 = get_window(x_coord, y_coord, stationID)" ] }, { "cell_type": "code", "execution_count": 269, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
<xarray.Dataset>\n", "Dimensions: (x: 6, y: 6, time: 2, station: 2)\n", "Coordinates:\n", " * x (x) float64 5.384e+05 5.384e+05 ... 5.871e+05 5.872e+05\n", " * y (y) float64 5.69e+06 5.69e+06 ... 5.7e+06 5.7e+06\n", " * time (time) object 1437043824456000000 1692788207527148000\n", " * station (station) object 'S1' 'S2'\n", "Data variables:\n", " transverse_mercator (station, time) float64 9.969e+36 ... 9.969e+36\n", " lon (station, time, y, x) float32 nan nan nan ... nan nan\n", " lat (station, time, y, x) float32 nan nan nan ... nan nan\n", " l2_flags (station, time, y, x) float64 nan nan nan ... nan nan\n", " chl_re_gons (station, time, y, x) float32 nan nan nan ... nan nan\n", "Attributes: (12/381)\n", " generated_by: ACOLITE\n", " generated_on: 2024-01-18 10:07:11 CET\n", " contact: Quinten Vanhellemont\n", " product_type: NetCDF\n", " metadata_profile: beam\n", " metadata_version: 0.5\n", " ... ...\n", " luts: ['ACOLITE-LUT-202110-MOD1', 'A...\n", " luts_pressures: [ 500 750 1013 1100]\n", " slicing: False\n", " scene_download: False\n", " runid: 20240118_100708\n", " inputfile: ['/eodc/private/deltares/INPUT...
<xarray.Dataset>\n", "Dimensions: (station: 1, time: 2, x: 3, y: 3)\n", "Coordinates:\n", " * station (station) object '1'\n", " * x (x) float64 5.871e+05 5.871e+05 5.872e+05\n", " * y (y) float64 5.69e+06 5.69e+06 5.69e+06\n", " * time (time) object 1437043824456000000 1692788207527148000\n", "Data variables:\n", " transverse_mercator (station, time) float64 9.969e+36 9.969e+36\n", " lon (station, time, y, x) float32 4.25 4.251 ... 4.252\n", " lat (station, time, y, x) float32 51.35 51.35 ... 51.35\n", " l2_flags (station, time, y, x) int32 1 1 1 1 1 1 ... 0 0 0 0 0 0\n", " chl_re_gons (station, time, y, x) float32 nan nan ... 8.628 9.062\n", "Attributes: (12/381)\n", " generated_by: ACOLITE\n", " generated_on: 2024-01-18 10:07:11 CET\n", " contact: Quinten Vanhellemont\n", " product_type: NetCDF\n", " metadata_profile: beam\n", " metadata_version: 0.5\n", " ... ...\n", " luts: ['ACOLITE-LUT-202110-MOD1', 'A...\n", " luts_pressures: [ 500 750 1013 1100]\n", " slicing: False\n", " scene_download: False\n", " runid: 20240118_100708\n", " inputfile: ['/eodc/private/deltares/INPUT...