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