Skip to content
Snippets Groups Projects
Commit 847a0c06 authored by Friedemann Reum's avatar Friedemann Reum
Browse files

Activate handling of wrfchem initial conditions (only fetch, no read and write)

parent 04f72005
No related branches found
No related tags found
1 merge request!45Merge branch 'wrf' into 'master'
......@@ -53,6 +53,12 @@ def ini_mapper(model, transform_type, general_mapper={}, backup_comps={},
dict_surface, **{"input_dates": {
model.datei: np.array([[model.datei, model.datef]])}}
)
dict_ini = dict(
dict_surface, **{"input_dates": {
model.datei: np.array([[model.datei, model.datei]])}}
)
# Executable
mapper = {
......@@ -96,12 +102,11 @@ def ini_mapper(model, transform_type, general_mapper={}, backup_comps={},
for s in model.chemistry.emis_species.attributes
}
mapper["inputs"].update(emis)
# raise ValueError("look here")
# freum: comment inicond until I move them from preprocessing to cif
# inicond = {
# ("inicond", s): dict_ini for s in model.chemistry.acspecies.attributes
# }
# mapper["inputs"].update(inicond)
inicond = {
("inicond", s): dict_ini for s in model.chemistry.acspecies.attributes
}
mapper["inputs"].update(inicond)
# freum: from here, additional code in lmdz's ini_mapper that the
# WRF plugin doesn't use. Commented instead of deleted in case it's
......
......@@ -12,15 +12,10 @@ from ......utils import path
# Copied from chimere on 2022-01-03
def make_inicond(self, datastore, runsubdir, mode, datei, datef):
ddi = min(datei, datef)
def make_inicond(self, datastore, runsubdir, mode, datei):
raise NotImplementedError("Adapt to wrf")
# Fixed name for INI_CONCS files
fileout = "{}/INI_CONCS.0.nc".format(runsubdir)
fileoutincr = "{}/INI_CONCS.0.increment.nc".format(runsubdir)
nho = self.nho
# Fixed name for initial conditions file
fileout = "{}/wrfinput_d01".format(runsubdir)
# # Getting the forward initial concentrations (needed even for adjoint)
# # if chained period
......@@ -52,7 +47,7 @@ def make_inicond(self, datastore, runsubdir, mode, datei, datef):
continue
tracer = datastore[trid]
tracer_data = tracer["data"][ddi]
tracer_data = tracer["data"][datei]
# If no data is provided, just copy from original file
if "spec" not in tracer_data:
......@@ -61,57 +56,59 @@ def make_inicond(self, datastore, runsubdir, mode, datei, datef):
fileini = "{}/{}".format(dirorig, fileorig)
# If does not exist, just link
linked = False
# linked = False
if not os.path.isfile(fileout):
path.link(fileini, fileout)
linked = True
# Otherwise, check for difference
if not linked:
if not os.path.isfile(fileini):
warning("The initial condition file {} does not exist to "
"initialize the species {}. The concentrations will"
" be initialized to zero. Please check your yaml if"
" you expect a different behaviour"
.format(fileini, spec))
continue
if not filecmp.cmp(fileini, fileout):
with Dataset(fileini, "r") as ds:
if spec not in ds.variables:
warning("{} is not accounted "
"for in the initial conditions file {}. "
"Please check your IC whether it should be"
.format(spec, fileini))
continue
iniin = ds.variables[spec][:]
iniin = xr.DataArray(
iniin[np.newaxis, ...],
coords={"time": [min(datei, datef)]},
dims=("time", "lev", "lat", "lon"),
)
# If ini file is still a link, should be copied
# to be able to modify it locally
if os.path.islink(fileout):
file_orig = pathlib.Path(fileout).resolve()
os.unlink(fileout)
shutil.copy(file_orig, fileout)
# Now writes the new value for the corresponding species
self.inicond.write(spec, fileout, iniin,
comp_type="inicond")
# Repeat operations for tangent linear
if mode == "tl":
# If does not exist, just link
if not os.path.isfile(fileoutincr):
shutil.copy(fileini, fileoutincr)
with Dataset(fileoutincr, "a") as fout:
if spec in fout.variables:
fout.variables[spec][:] = 0.0
# linked = True
# freum: didn't need this in fluxes, so commenting it here too
# # Otherwise, check for difference
# if not linked:
# if not os.path.isfile(fileini):
# warning("The initial condition file {} does not exist to "
# "initialize the species {}. The concentrations will"
# " be initialized to zero. Please check your yaml if"
# " you expect a different behaviour"
# .format(fileini, spec))
# continue
#
# if not filecmp.cmp(fileini, fileout):
# with Dataset(fileini, "r") as ds:
# if spec not in ds.variables:
# warning("{} is not accounted "
# "for in the initial conditions file {}. "
# "Please check your IC whether it should be"
# .format(spec, fileini))
# continue
#
# iniin = ds.variables[spec][:]
# iniin = xr.DataArray(
# iniin[np.newaxis, ...],
# coords={"time": [min(datei, datef)]},
# dims=("time", "lev", "lat", "lon"),
# )
#
# # If ini file is still a link, should be copied
# # to be able to modify it locally
# if os.path.islink(fileout):
# file_orig = pathlib.Path(fileout).resolve()
# os.unlink(fileout)
# shutil.copy(file_orig, fileout)
#
# # Now writes the new value for the corresponding species
# self.inicond.write(spec, fileout, iniin,
# comp_type="inicond")
# freum: no tl mode for wrf
# # Repeat operations for tangent linear
# if mode == "tl":
# # If does not exist, just link
# if not os.path.isfile(fileoutincr):
# shutil.copy(fileini, fileoutincr)
#
# with Dataset(fileoutincr, "a") as fout:
# if spec in fout.variables:
# fout.variables[spec][:] = 0.0
else:
# Replace existing link by copy
......@@ -121,17 +118,21 @@ def make_inicond(self, datastore, runsubdir, mode, datei, datef):
# Write initial conditions
ini_fwd = tracer_data["spec"]
self.inicond.write(spec, fileout, ini_fwd, comp_type="inicond")
if mode == "tl":
path.copyfromlink(fileoutincr)
ini_tl = tracer_data.get("incr", 0.0 * ini_fwd)
self.inicond.write(
spec, fileoutincr, ini_tl, comp_type="inicond"
)
# Check that the dates are consistent with what CHIMERE expects
replace_dates(fileout, [min(datei, datef)], self.ignore_input_dates)
if mode == "tl":
replace_dates(fileoutincr, [min(datei, datef)],
self.ignore_input_dates)
# freum: no tl mode for wrf
# if mode == "tl":
# path.copyfromlink(fileoutincr)
# ini_tl = tracer_data.get("incr", 0.0 * ini_fwd)
# self.inicond.write(
# spec, fileoutincr, ini_tl, comp_type="inicond"
# )
# Check that the dates are consistent with what wrf expects
# Commented, because WRF checks that too.
# wrfinput_times, _ = self.wrfhelper.wrf_times([fileout])
# ok = False
# if len(wrfinput_times) == 1):
# if wrfinput_times[0] == datei:
# ok = True
# if not ok:
# raise ValueError("Dates in {} are not what WRF expects.")
......@@ -69,11 +69,8 @@ def native2inputs(
# Deals with initial conditions
elif input_type == "inicond":
msg = "Implement make_inicond here - IF needed"
logging.info(msg)
# pass
# raise NotImplementedError(msg)
make_inicond(self, datastore, runsubdir, mode, ddi, ddf)
ddi = min(datei, datef)
make_inicond(self, datastore, runsubdir, mode, ddi)
else:
msg = "No method implemented for input type '{}'.".format(input_type)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment