{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Exemple d'utilisation de gridmarthe [FR]\n", "\n", "Le package gridmarthe est conçu pour faciliter la lecture/l'écriture de grille au format Marthe.\n", "Ce notebook permet d'explorer quelques fonctionnalités de base du package et de montrer un exemple\n", "de traitement des grilles." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# import du package\n", "import gridmarthe as gm" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Fonctionnalités de base\n", "\n", "Les grilles contiennent certaines métadonnées, dont l'index du pas de temps\n", "mais pas les dates en elles-même. Aussi, il est utile de fournir à la fonction de lecture\n", "soit une série de date (à construire manuellement) soit un fichier pastp pour que les dates soient\n", "lues automatiquement.\n", "\n", "En complément, plusieurs arguments peuvent être utiles à la lecture (retrait des valeurs nulles, transformation des xy, ajout de l'indicateur de maillage ou des numéros de colonnes/lignes, etc.).\n", "\n", "Pour évaluer les options possibles, on peut afficher l'aide de la fonction." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function load_marthe_grid in module gridmarthe.gridmarthe:\n", "\n", "load_marthe_grid(filename, varname=None, dates=None, fpastp=None, nanval=None, dropna=False, xyfactor=1, keepligcol=False, add_id_grid=False, title=None, var_attrs={}, model_attrs={'resolution_units': 'm', 'projection': 'epsg:27572', 'domain': 'FR-France'}, verbose=True)\n", " LECSEM python wrapper\n", " Fortran modules from marthe src wrapped for python module\n", " \n", " memo : file.out read as seq => all layer then nested grid and all layers too. And so on for every timestep.\n", " coords with no value read as 1e+20\n", " zvar read as a single 1D array for every timestep\n", " \n", " Parameters\n", " ----------\n", " filename (str): A path to marthegrid file (*.permh, *.out, etc.)\n", " varname (str): variable to access in martgrid file, e.g `CHARGE` for groundwater head. See marthegrid file content.\n", " if None is passed (default), function will scan all varnames in filename and keep first only\n", " if 'all' is passed, function will scan all varnames in filename and keep all. All datavars are added to dataset, using recursive call to func\n", " if wrong variable name is passed, empty data will be returned.\n", " dates (sequence, Optionnal): Can be a pd.date_range, pd.Series, pd.DatetimeIndex, np.array or list of datetime/np.datetime objects.\n", " If no dates (or no fpastp) is provided, a fake sequence of dates from 1850 to 1900 will be used for xarray object\n", " fpastp (str, Optionnal) : A pastp file to read for dates\n", " nanval (float, Optionnal) : A code value for nan values. Default is 9999.\n", " dropna (bool, Optionnal) : Drop nan values (corresponding to nanval) in xarray object to return, default is False (keep nan values).\n", " xyfactor (int or float, Optionnal): factor to transform X and Y values. e.g.: 1000 to convert km XY to meters. Default is 1.\n", " keepligcol (bool, Optionnal): Add columns (col) and rows (lig) index (from 1 to n), Default is False.\n", " add_id_grid (bool, Optionnal): Add grid id (from 0 to n), useful for nested grids. 0 is main grid, Default is False\n", " title (str , Optionnal): Title for grid attributes. Default is None (not used)\n", " var_attrs (dict, Optionnal): Dictionnary of attributes to add to variable DataArray.\n", " model_attrs (dict, Optionnal): Dictionnary of attributes to add to Dataset.\n", " by default, gis attrs are added and can be modified\n", " 'resolution_units': 'm',\n", " 'projection' : 'epsg:27572',\n", " 'domain' : 'FR-France',\n", " verbose (bool, Optionnal): Print some information about execution in stdout. Default is False.\n", " \n", " Returns\n", " -------\n", " ds (xr.Dataset): a xarray.Dataset object containing values and attributes read from Marthe grid file.\n", "\n" ] } ], "source": [ "help(gm.load_marthe_grid)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# lecture d'une grille marthe et stockage dans un Dataset (librairie xarray)\n", "ds = gm.load_marthe_grid('./data/chasim_hallue.out', 'CHARGE', fpastp='./data/hallue.pastp', drop_nan=True) # si la valeur des nans n'est pas 9999 (ex. permh, NaNs = 0), on peut spécifier la valeur" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
<xarray.Dataset> Size: 784kB\n",
"Dimensions: (time: 205, zone: 927)\n",
"Coordinates:\n",
" * time (time) datetime64[ns] 2kB 1995-07-31 1995-08-01 ... 2012-07-01\n",
" * zone (zone) int64 7kB 255 256 257 258 259 ... 2722 2723 2724 2725 2726\n",
"Data variables:\n",
" charge (time, zone) float32 760kB 100.0 100.6 101.1 ... 27.0 26.0 26.35\n",
" x (zone) float32 4kB 617.8 618.2 618.8 619.2 ... 606.8 607.2 607.8\n",
" y (zone) float32 4kB 2.567e+03 2.567e+03 ... 2.543e+03 2.543e+03\n",
" dx (zone) float32 4kB 0.5 0.5 0.5 0.5 0.5 0.5 ... 0.5 0.5 0.5 0.5 0.5\n",
" dy (zone) float32 4kB 0.5 0.5 0.5 0.5 0.5 0.5 ... 0.5 0.5 0.5 0.5 0.5\n",
"Attributes: (12/16)\n",
" conventions: CF-1.10\n",
" title: Modélisation du bassin de la SOMME Nappe_Libre\n",
" marthe_grid_version: 9.0\n",
" original_dimensions: x,y,z [grids]: 53 54 1\n",
" lon_resolution: 0.5\n",
" lat_resolution: 0.5\n",
" ... ...\n",
" creation_date: Created on 2024-06-18T20:44:48Z UTC\n",
" institution: BRGM, French Geological Survey, Orléans, France\n",
" comment: Hydrogeological model created with MARTHE code (Thi...\n",
" resolution_units: m\n",
" projection: epsg:27572\n",
" domain: FR-France<xarray.Dataset> Size: 2MB\n",
"Dimensions: (y: 48, x: 44, time: 205)\n",
"Coordinates:\n",
" * y (y) float32 192B 2.543e+03 2.544e+03 ... 2.566e+03 2.567e+03\n",
" * x (x) float32 176B 599.2 599.8 600.2 600.8 ... 619.8 620.2 620.8\n",
" * time (time) datetime64[ns] 2kB 1995-07-31 1995-08-01 ... 2012-07-01\n",
"Data variables:\n",
" charge (time, y, x) float32 2MB nan nan nan nan ... 102.7 103.4 103.5\n",
" dx (y, x) float32 8kB nan nan nan nan nan nan ... 0.5 0.5 0.5 0.5 0.5\n",
" dy (y, x) float32 8kB nan nan nan nan nan nan ... 0.5 0.5 0.5 0.5 0.5\n",
"Attributes: (12/16)\n",
" conventions: CF-1.10\n",
" title: Modélisation du bassin de la SOMME Nappe_Libre\n",
" marthe_grid_version: 9.0\n",
" original_dimensions: x,y,z [grids]: 53 54 1\n",
" lon_resolution: 0.5\n",
" lat_resolution: 0.5\n",
" ... ...\n",
" creation_date: Created on 2024-06-18T20:44:48Z UTC\n",
" institution: BRGM, French Geological Survey, Orléans, France\n",
" comment: Hydrogeological model created with MARTHE code (Thi...\n",
" resolution_units: m\n",
" projection: epsg:27572\n",
" domain: FR-France<xarray.Dataset> Size: 2MB\n",
"Dimensions: (zone: 927, time: 205)\n",
"Coordinates:\n",
" * zone (zone) int64 7kB 255 256 257 258 259 ... 2722 2723 2724 2725 2726\n",
" * time (time) datetime64[ns] 2kB 1995-07-01 1995-08-01 ... 2012-07-01\n",
"Data variables:\n",
" spli (time, zone) float64 2MB 0.06557 0.06549 0.06554 ... 0.01162 0.6706\n",
"Attributes:\n",
" varname: CHARGE\n",
" units: m\n",
" missing_value: 9999.0\n",
" standard_name: water_table_level\n",
" long_name: groundwater head<xarray.Dataset> Size: 3MB\n",
"Dimensions: (y: 48, x: 44, time: 205)\n",
"Coordinates:\n",
" * y (y) float32 192B 2.543e+03 2.544e+03 ... 2.566e+03 2.567e+03\n",
" * x (x) float32 176B 599.2 599.8 600.2 600.8 ... 619.8 620.2 620.8\n",
" * time (time) datetime64[ns] 2kB 1995-07-01 1995-08-01 ... 2012-07-01\n",
"Data variables:\n",
" spli (time, y, x) float64 3MB nan nan nan nan ... 0.1627 0.519 0.6357\n",
"Attributes:\n",
" varname: CHARGE\n",
" units: m\n",
" missing_value: 9999.0\n",
" standard_name: water_table_level\n",
" long_name: groundwater head