Tools for reading simulations : the utils module

Other tools

NetCDF comparisons

Script utils/operational_reproductibility/compare2versions.py help:

Usage: USAGE compare2versions.py [fichier1 fichier2] [DATEBEGIN DATEEND]

Options:
  -h, --help            show this help message and exit
  --old=OLD             path of old file
  --new=NEW             path of new file
  -b DATEBEGIN, --begin=DATEBEGIN
                        First date (YYYYMMDD)
  -e DATEEND, --end=DATEEND
                        Last date (YYYYMMDD)
  -n NMEMBERS, --nmembers=NMEMBERS
                        Last date (YYYYMMDD)
  -f, --fast            Last date (YYYYMMDD)
  --double              Reference is double

Module content:

File conversions : Met and netCDF

Met2Netcdf tool

This script creates FORCING files for the Col de Porte site. It uses MET files (ie meteorogical files) from 1993 and s2m reanalysis before.

Values of MET files come from different sensor in the Col de Porte site and benefit from a human expertise.

The FORCING files which is created is also use to complete the doi of the article “57 years…” (under the name CRYOBSCLIM.CDP.2018.MetInsitu.nc).

What has to be controled in Met2Netcdf.py script

This script completes the MET files with values coming from different sources: bdniv database, s2m reanalasys. The use of the different sources imply to define in the script some dates. Roughly, before one date, we use a data source and after, another data source. So, this is important to change dates inside the script before using it.

HARD CODED PART OF THE CODE

year of last MET file (to be changed each year)

year of name changing for cdp60mn database (to be changed each year) (ie cdp60mn becomes cdp60mn_2223 for example)

path to MET files (normally stable path) path to reanalysis files (normally stable path)

EXAMPLES OF USE

python3 Met2Netcdf.py -b 2000080106 -e 2001080106
python3 Met2Netcdf.py -b 1993080106 -e 2023080106 -o MAJ_MetInsitu.nc
python3 Met2Netcdf.py -b 2000080106 -e 2001080106 --one_file -p partial_MET.txt

Options:

  • -o output.nc -> The output file is named output.nc

  • -c ( for constant) -> Avoid taking PSurf and Wind_DIR from cdp60mn. By default, -c is not activated.

  • -b -> the starting date for the FORCING file

  • -e -> the ending date for the FORCING file

  • -s -> the number of the location site. By default ‘38472401’ which is the Col de Porte site

  • –one_file -> create a FORCING file only for a specific MET file

  • -p -> the path for this only MET file (required when –one_file is activated)

utils.Met2Netcdf.compilation_ttes_periodes(date_entree_debut, date_entree_fin, site, option_recup)[source]

Compilation of all the different one_year period

Parameters:
  • site (str) – site of interest (number of 8 figures with string format)

  • option_recup (boolean) – PSurf et Wind DIR filled with database cdp60mn

  • date_entree_debut (datetime.datetime) – starting date for the FORCING

  • date_entree_fin (datetime.datetime) – ending date for the FORCING

Returns:

an xarray with all fields of interest

utils.Met2Netcdf.complete_obs_with_model_1period(filename, option_recup, date_entree_debut, date_entree_fin, site)[source]

For a one year period (YYYY080106 - (YYYY+1)080106), return a complete file This file is made from the MET informations + eventually Psurf and WindDIR from cdp60mn database. Then, for the missing dates, this is completed with Safran reanalysis.

Parameters:
  • filename (str) – path to the MET file which is going to be completed

  • site (str) – site of interest (number of 8 figures with string format)

  • option_recup (boolean) – PSurf et Wind DIR filled with database cdp60mn

  • date_entree_debut (datetime.datetime) – starting date of the year

  • date_entree_fin (datetime.datetime) – ending date of the year

Returns:

an xarray with all fields of interest

utils.Met2Netcdf.create_netcdf(output, xr, Tableau_valeurs_nbpoint)[source]

Create NetCDF output file

Parameters:
  • output (str) – name of the output file

  • xr (xarray) – fields of interest (PSurf, Rayt, Snowf, …)

  • Tableau_valeurs_nbpoint (np array) – fields defining the site (LAT, LON, ZS, …)

Returns:

in a pythonic way, nothing (write a netCDF named out_met2netcdf.nc by default)

utils.Met2Netcdf.decoupe_periode(date_entree_debut, date_entree_fin)[source]

split period of dates in yearly period and give the list of MET files MET file name is supposed to be in a fix format: MET_1996_1997_fmt

Parameters:
  • date_entree_debut (Date (extension of datetime.datetime)) – starting date for the FORCING file

  • date_entree_fin (Date (extension of datetime.datetime)) – ending date for the FORCING file

Returns:

un tuple of 3 lists: MET list, starting date list, ending date list

utils.Met2Netcdf.open_met_file_and_create_xr(filename, option_recup, site)[source]

Get all differents fields in MET file

Parameters:
  • filename (str) – path to MET file

  • option_recup (boolean) – if True, use database cdp60mn to complete PSurf and windDIR

  • site (str) – site of interest (number of 8 figures with string format)

Returns:

an xarray with all the fields, indexed by dates of MET file

utils.Met2Netcdf.read_info_met(filename)[source]

Get informations for an annual MET file: size of the header, begin date, end date. NB: we suppose that the lines of the header are beginning with a letter. NB2: 2 date format: YYYYMMDDHH or DD/MM/YYYY HH:M NB3: “old” MET doesn’t have full yearly data -> needs to complete -> needs to know where to start and stop

Parameters:

filename (str) – path to MET file

Returns:

list: [number of lines in header, starting date for MET datas; ending date for MET datas]

utils.Met2Netcdf.recup_cdp(date_time_deb, date_time_fin)[source]

Get field Wind Direction and Surface Pressure from cdp60mn database

Parameters:
  • date_time_deb (datetime.datetime) – starting date

  • date_time_fin (datetime.datetime) – ending date

Returns:

np array of dimension (list_of_dates,3) [list_of_dates, pressure, wind dir]

utils.Met2Netcdf.recup_donnees_site(numero_site)[source]

Give infos (Latitude, longitude, altitude, etc…) from a site (using site number) More precisely, the infos are: [LAT, LON, UREF, ZREF, ZS, aspect, slope, site number], site name

Parameters:

numero_site (str) – site of interest (number of 8 figures with string format)

Returns:

un tuple of 2 terms: one is the list of infos and other is the site name

utils.Met2Netcdf.recup_safran(date_1, date_2, site)[source]

Complete missing values of the MET file by Safran reanalysis

Parameters:
  • site (str) – site of interest (number of 8 figures with string format)

  • date_1 (datetime.datetime) – starting date for getting fields

  • date_2 (datetime.datetime) – ending date for getting fields

Returns:

an xarray with all fields of interest

This module is also an executable script:

Script utils/Met2Netcdf.py help:

usage: Met2Netcdf.py [-h] [-o OUTPUT] [-c] [-b BEGIN] [-e END] [-s SITE]
                     [--one_file] [-p PATH_MET] [--version]

options:
  -h, --help            show this help message and exit
  -o OUTPUT, --output OUTPUT
                        Name for output file
  -c, --constant        PSurf and Wind_DIR are constant
  -b BEGIN, --begin BEGIN
                        Beginning date for nc file
  -e END, --end END     Ending date for nc file
  -s SITE, --site SITE  Site location
  --one_file            Create forcing for one MET file
  -p PATH_MET, --path_MET PATH_MET
                        Path of the only MET
  --version             show program's version number and exit

Read observations from csv files

Created on 29 oct. 2012

@author: lafaysse

Dates management

Warning

If you want to parse and manage dates, please use bronx tools.