Source code for sloop.models.croco.__init__

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
CROCO utilities

"""
import logging
import os

from ...__init__ import SLOOP_SRC_DIR, SloopError


[docs]class SloopCrocoError(SloopError): pass
LOGGER = logging.getLogger(__name__) _THISDIR = os.path.dirname(__file__) #: CROCO vconf CROCO_VCONF = ["manga", "med", "indien", "gijon"] #: CROCO platform root dir CROCO_DIR = os.path.join(SLOOP_SRC_DIR, 'CROCO') #: CROCO GENERAL CONFI INI FILE CROCO_CONF_INI_FILE = os.path.join(_THISDIR, "croco_config.ini") CFG_INI=CROCO_CONF_INI_FILE #: CROCO initial and open boundary root dir CROCO_IBC_DIR = os.path.join(CROCO_DIR, 'EXTRACT') #: CROCO EXTRACT EXECUTABLE CROCO_IBC_EXE = os.path.join(CROCO_IBC_DIR, 'extract_exe') #: CROCO INPUTS DIR CROCO_INPUTS_DIR='./FORCING' #: CROCO_RESTART_NAME CROCO_RST_NAME='croco_rst.nc' #: CROCO EXTRACT TEMPLATE NAMELIST CROCO_IBC_NAMELIST_TPL=os.path.join(CROCO_IBC_DIR,'IN/namelist.tpl') #: CROCO EXTRACT INI FILE CROCO_IBC_INPUT_FILE=os.path.join(CROCO_INPUTS_DIR,'obc_collect/INPUT_EXTRACT.nc') #: CROCO EXTRACT INI FILE CROCO_EXTRACT_INI_FILE = os.path.join(_THISDIR, "extract.ini") #: CROCO grid file CROCO_GRID_FILE = os.path.join(CROCO_INPUTS_DIR,'croco_grd.nc') #: CROCO croco.in parameters file CROCO_RUN_INPUT_FILE = "TPL/croco.in.tpl" #CROCO FORCING FILE CROCO_FRC_TIDE=os.path.join(CROCO_INPUTS_DIR,"croco_frc.nc") #CROCO BLK FORCING FILE CROCO_FRC_BLK=os.path.join(CROCO_INPUTS_DIR,"croco_blk.nc") #CROCO BRY FORCING FILE CROCO_FRC_OBC=os.path.join(CROCO_INPUTS_DIR,"croco_obc.nc") #CROCO CLM FORCING FILE CROCO_FRC_CLM=os.path.join(CROCO_INPUTS_DIR,"croco_clm.nc") #CROCO INI FORCING FILE CROCO_FRC_INI=os.path.join(CROCO_INPUTS_DIR,"croco_ini.nc") #CROCO ATM FORCING FILE CROCO_FRC_ATM=os.path.join(CROCO_INPUTS_DIR,"croco_atm.nc") #CROCO_RUNOFF_FILE CROCO_FRC_RIVER=os.path.join(CROCO_INPUTS_DIR,"croco_runoff.nc") #CROCO CPPDEF FILE CROCO_PREPROC_FILE="TPL/cppdefs.h.tpl" #CROCO PARAM FILE CROCO_PARAM_FILE="TPL/param.h.tpl" #CROCO COMPILE FILE CROCO_COMPILE_FILE="TPL/jobcomp.tpl" #: CROCO 3D model directory CROCO_MODEL_DIR = os.path.join(CROCO_DIR, 'Modele3d') #: CROCO 3D model executable CROCO_MODEL_EXE = os.path.join( CROCO_MODEL_DIR, 'croco') #: CROCO rivers directory CROCO_RIVERS_DIR = os.path.join(CROCO_DIR, 'Rivers') #: CROCO rivers flow climatology CROCO_RIVERS_FLOW_CLIM = os.path.join(CROCO_RIVERS_DIR, "hycom_rivers.d") #: CROCO rivers hydro climatology CROCO_RIVERS_HYDRO_CLIM = os.path.join( CROCO_RIVERS_DIR, "fleuves_climato_hydro") CROCO_SIGMA_TO_STMT_FNS="" CROCO_CONTEXT_TO_STMT_FNS="" CROCO_IBC_GEN_INICON="" CROCO_IBC_GEN_INICON_VCONF="" CROCO_MODEL_DEFSTRETCH_FILES="" CROCO_GRID_AFILE="" CROCO_BLKDAT_INPUT_FILE="" #. CROCO OUTPUT NAMES CROCO_HIS_NAME="croco_his_{date1}_{date2}.nc" CROCO_AVG_NAME="croco_avg_{date1}_{date2}.nc" CROCO_HIS_SURF_NAME="croco_his_surf_{date1}_{date2}.nc" CROCO_AVG_SURF_NAME="croco_avg_surf_{date1}_{date2}.nc" #. CROCO RUN DIR CROCO_RUN_DIR="croco-{vconf}-{date1}-{date2}" #: CROCO postproduction root dir CROCO_POSTPROD_DIR = os.path.join(CROCO_DIR, 'PostProduction') #: CROCO postproduction time filtering executable CROCO_POSTPROD_FILTER = os.path.join( CROCO_POSTPROD_DIR, 'cdfTraitementTemporel3D') #: CROCO postproduction file conversion executable CROCO_POSTPROD_INTERPOLATION = os.path.join( CROCO_POSTPROD_DIR, 'cdfHycomTodataSHOMouSOAP') #: CROCO postproduction temperature conversion executable CROCO_POSTPROD_TEMPCONVERSION = os.path.join( CROCO_POSTPROD_DIR, 'temppot2tempis_postProd') #: CROCO postproduction traductions noms longs CROCO_POSTPROD_LONGNAMES = "PARAMETERS{rank}./traductions_noms_longs_{postprod}" #: CROCO postproduction traductions noms standards CROCO_POSTPROD_STDNAMES = "PARAMETERS{rank}./traductions_noms_standards_2" #: CROCO postproduction traductions units CROCO_POSTPROD_UNITS = "PARAMETERS{rank}./traductions_unites" #: CROCO savefield.input parameters file CROCO_SAVEFIELD_INPUT_FILE = "PARAMETERS{rank}./savefield.input" #: CROCO blkdat_cmo.input parameters file CROCO_SAVEFIELD_INPUT_FILES = { "spinup":["PARAMETERS{rank}./savefield_C.input", "PARAMETERS{rank}./savefield_A.input"], "forecast":["PARAMETERS{rank}./savefield_C.input", "PARAMETERS{rank}./savefield_P.input"], "spnudge_free":["PARAMETERS{rank}./savefield_Assim_SN.input"], "spnudge_relax":["PARAMETERS{rank}./savefield_C.input", "PARAMETERS{rank}./savefield_A.input"], "free":["PARAMETERS{rank}./savefield_C.input", "PARAMETERS{rank}./savefield_A.input"], } #: CROCO MPI tiling file CROCO_PATCH_INPUT_FILE = "SPLIT{rank}./patch.input" #: CROCO run.input.tpl parameters file CROCO_RUN_INPUT_TPL_FILE = "PARAMETERS{rank}./run.input.tpl" #: CROCO post-production configuration files CROCO_POSTPROD_CFG_FILE = "PARAMETERS{rank}./postprod.cfg" #: CROCO post-production configuration specs file CROCO_POSTPROD_INI_FILE = os.path.join(_THISDIR, "postprod.ini") #: CROCO post-production configuration output files CROCO_POSTPROD_CONFIG_FILE = "PARAMETERS{rank}./postprod_{postprod}.cfg" #: CROCO check promises CROCO_PROMISES = "PARAMETERS{rank}./promises.cfg" #: CROCO rivers configuration specs file CROCO_RIVERS_INI = os.path.join(_THISDIR, "rivers.ini") #: CROCO rivers configuration file CROCO_RIVERS_CFG = 'FORCING{rank}./nest/rivers.cfg' #: CROCO spectral nudging parameters json file CROCO_SPNUDGE_JSON_FILE = "PARAMETERS{rank}./spnudging_parameters.json" #: From standard_name to name CROCO_STANDARD_NAME_TO_NAME = { 'sea_water_potential_temperature': 'temp', 'sea_water_salinity': 'saln', 'sea_surface_height_above_geoid': 'ssh', } #: Required environment variables for each context REQUIRED_ENV_VARIABLES = { # Pre- and post-processing tasks 'prepost': [ 'FC', 'LDF', 'CC', 'FFLAGS', 'LDFLAGS', 'CPPFLAGS', 'MAKE', 'MODEL_FFLAGS', 'MODEL_CFLAGS', 'MODEL_LDFLAGS' ], # Model 'modelcompile': [ 'HOME_MODELE_SRC', 'MPIDIR','MPI_COMP', 'FFLAGS', 'CPP', 'CPPFLAGS', 'LDFLAGS', 'CCFLAGS', 'FC', 'CC', 'MAKE' ] , 'modelrun': [ 'HOME_MODELE_SRC', 'MPIDIR', ], } REQUIRED_JOB_OPTIONS = { 'prepost': [ 'submitcmd', 'queue', 'time' ], 'modelrun': [ 'submitcmd', 'queue', 'time' ], 'recdata': [ 'submitcmd', 'queue', 'refill', 'time' ], } #: Patterns of geographic constant files CROCO_GEO_CONSTANT_PATTERNS = { "rivers": CROCO_FRC_RIVER, "tides": CROCO_FRC_TIDE, "atm": CROCO_FRC_ATM, "grid":CROCO_GRID_FILE, "blk":CROCO_FRC_BLK, "obc":CROCO_FRC_OBC, "ini":CROCO_FRC_INI, "clm":CROCO_FRC_CLM, } # Pattern of TPL files CROCO_TPL_FILES= { "param": CROCO_PARAM_FILE, "preproc":CROCO_PREPROC_FILE, "input":CROCO_RUN_INPUT_FILE, "compile":CROCO_COMPILE_FILE, } """"" "run": [ CROCO_RUN_INPUT_FILE, ], "postprod": [ CROCO_POSTPROD_CFG_FILE, CROCO_POSTPROD_CONFIG_FILE.format( rank="{rank}", postprod="*") ], "savefield": ( [CROCO_SAVEFIELD_INPUT_FILE] + list(set(p for pp in CROCO_SAVEFIELD_INPUT_FILES.values() for p in pp)) ), "split": [ CROCO_PATCH_INPUT_FILE ], "promises": [ CROCO_PROMISES ], } """"" #: Patterns of non-geographic constant files CROCO_NOT_GEO_CONSTANT_PATTERNS = { "naming": [ "PARAMETERS{rank}./traductions_*", ] } #: CROCO specifications for the :mod:`xoa.cf` module use CROCO_CF_SPECS = { "register":{ "name":"croco" }, "vertical": { "positive":"up", "type":"sigma", }, "sglocator":{ "valid_locations":"rho,w,u,v,r" }, "coords":{ "x":{ "name":"xi", "search_order":"n", "standard_name":"x_grid_index", "add_loc":True, }, "y":{ "name":"eta", "search_order":"n", "standard_name":"y_grid_index", "add_loc":True, }, "sig":{ "name":"s", "add_loc":True, }, "time":{ "name":"time", }, }, "data_vars": { "temp": { "name": "temp" }, "sal": { "name": "salt" }, "ssh": { "name": "zeta" }, "bathy": { "name": "h" }, "sig": { "name": "s", }, "mask": { "mask": "mask", "add_loc":True, }, "cs": { "name": "Cs", "add_loc":True, }, "ex": { "name": "pm", }, "ey": { "name": "pn", }, "ubt": { "name": "ubar", "loc":"u", }, "vbt": { "name": "vbar", "loc":"v", }, } } CROCO_PARAM = { "u10m": { "name": [ "u10m", "u10", "u" ], "attrs": { "units": "m/s", "standard_name": "eastward_wind", "long_name": "Zonal wind speed at 10m", "header": "zonal wind" } }, "v10m": { "name": [ "v10", "v" ], "attrs": { "units": "m/s", "standard_name": "northward_wind", "long_name": "Meridional wind speed at 10m", "header": "Meridional wind" } }, "pmer": { "name": [ "patm", "pmsl", "preatm", "prmsl" ], "attrs": { "units": "Pa", "standard_name": "air_pressure_at_mean_sea_level", "long_name": "Pressure reduced to MSL", "header": "pression atmospherique" } }, "t2m": { "name": [ "airtmp", "t2m" ], "attrs": { "units": "degree Celsius", "standard_name": [ "air_temperature", "air_temperature_at_2m" ], "long_name": "2 metre temperature", "header": "temperature de surface", "diurnal": "True" } }, "flsolaire": { "name": [ "flsolaire", "swhf", "shwflx", "ssr" ], "attrs": { "units": "W m-2 s-1", "standard_name": [ "short_wave_fluxes" ], "long_name": "surface net solar radiation", "header": "flux radiatifs Short Wave", "diurnal": "True" } }, "fltherm": { "name": [ "fltherm", "lwhf", "radflx", "str", ], "attrs": { "units": "W m-2 s-1", "standard_name": [ "radiative_fluxes" ], "long_name": "total radiative fluxes", "header": "flux radiatifs IR", "diurnal": "True" } }, "prate": { "name": [ "eau", "prate", "rain" ], "attrs": { "units": "m s-1", "standard_name": [ "precipitations" ], "long_name": "Total Precipitation", "header": "precipitations" } }, "hu2m": { "name": [ "hu2m", "rh", "rh2m" ], "attrs": { "units": "frac", "standard_name": [ "relative_humidity" ], "long_name": "Relative humidity at 2m", "header": "Relative humidity" } }, "wspd": { "name": [ "wdspd", ], "attrs": { "units": "m s-1", "standard_name": [ "wind_speed" ], "long_name": "10 metre wind speed", "header": "module du vent" } }, } _CACHE = {}
[docs]def get_tpl_files(): """Get dict of CROCO TPL files""" return CROCO_TPL_FILES
[docs]def get_constant_patterns(geo): """Get dict of CROCO constant file glob patterns for each pack A file pattern typically contains the `{rank}` subsitution pattern Parameters ---------- geo: bool Bounded to a geograhpical area? """ return CROCO_GEO_CONSTANT_PATTERNS if geo else CROCO_NOT_GEO_CONSTANT_PATTERNS
[docs]def get_cf_specs(): """Get a :class:`xoa.cf.CFSpecs` object for Croco""" if "cf_specs" not in _CACHE: import xoa.cf _CACHE["cf_specs"] = xoa.cf.CFSpecs(CROCO_CF_SPECS) return _CACHE["cf_specs"]
[docs]def format_ds(ds): """Format all variables in a dataset or dataarray to be Croco compliant""" # Time units time = get_cf_specs().coords.get(ds, "time") if time is not None: ds[time.name].encoding["units"] = "seconds since 1900-01-01" # Other encodings for vname, var in ds.items(): for attr in 'dtype', 'scale_factor', 'add_offset': if attr in ds[vname].encoding: del ds[vname].encoding[attr] if ds[vname].dtype.kind in "fd": ds[vname].encoding['_FillValue'] = -1e34 return ds