Skip to content

Commit

Permalink
Merge pull request #803 from EC-Earth/798-no-NEMO-dependency
Browse files Browse the repository at this point in the history
798 no nemo dependency
  • Loading branch information
treerink authored Feb 17, 2024
2 parents 227d158 + 35c7803 commit 9ddf1d3
Showing 1 changed file with 0 additions and 103 deletions.
103 changes: 0 additions & 103 deletions ece2cmor3/lpjg2cmor.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,6 @@ def execute(tasks):
% (getattr(task, cmor_task.output_path_key), task.source.variable()))
break

# special treatment of fco2nat=fco2nat_lpjg+fgco2_nemo
if outname=="fco2nat" and freq=="mon" and table=="Amon" and (cmor.get_cur_dataset_attribute("source_id") == "EC-Earth3-CC"):
if not add_nemo_variable(task, ncfile, "fgco2", "1m"):
log.error("There was a problem adding nemo variable %s to %s in table %s... "
"exiting ece2cmor3" % ("fgco2", task.target.variable, task.target.table))
exit(-1)

dataset = netCDF4.Dataset(ncfile, 'r')
# Create the grid, need to do only once as all LPJG variables will be on same grid
# Currently create_grid just creates latitude and longitude axis since that should be all that is needed
Expand Down Expand Up @@ -527,102 +520,6 @@ def create_lpjg_netcdf(freq, inputfile, outname, outdims):

return ncfile


# finds the nemo output containing varname at nemo_freq
# this uses code in nemo2cmor.py, which could be moved to cmor_utils
def find_nemo_file(varname, nemo_freq):
# find the nemo output folder for this leg (assumes it is in the runtime/output/nemo/??? folder)
path_output_lpjg, leg_no = os.path.split(lpjg_path_)
path_output = os.path.split(path_output_lpjg)
nemo_path = os.path.join(path_output[0],'nemo',leg_no)
# get the file which contains fgco2
try:
nemo_files = cmor_utils.find_nemo_output(nemo_path, exp_name_)
except OSError:
log.error("Cannot find any nemo output files in %s"
% (nemo_path))
return ""
print((str(nemo_files)))
file_candidates = [f for f in nemo_files if cmor_utils.get_nemo_frequency(f, exp_name_) == nemo_freq]
results = []
for ncfile in file_candidates:
ds = netCDF4.Dataset(ncfile)
if varname in ds.variables:
results.append(ncfile)
ds.close()
# simplified error reporting
if len(results) !=1:
log.error("Cannot find any suitable nemo output files in %s"
% (nemo_path))
return ""
return results[0]

# this function builds upon a combination of _get and save_nc functions from the out2nc.py tool originally by Michael
# Mischurow
def add_nemo_variable(task, ncfile, nemo_var_name, nemo_var_freq):
# find nemo raw output file
nemo_ifile = find_nemo_file(nemo_var_name, nemo_var_freq)
if nemo_ifile == "":
log.error("NEMO variable %s needed for target %s in table %s was not found in nemo output... "
% (nemo_var_name, task.target.variable, task.target.table))
return False

# define auxiliary and temp. files
nemo_maskfile = os.path.join(os.path.dirname(__file__), "resources", "nemo-mask-ece.nc")
nemo_ofile = os.path.join(ncpath_, "tmp_nemo.nc")
interm_file = os.path.join(ncpath_, "intermediate.nc")
if os.path.exists(nemo_ofile):
os.remove(nemo_ofile)
if os.path.exists(interm_file):
os.remove(interm_file)

# make sure the mask and input file have same dimensions
ds_maskfile = netCDF4.Dataset(nemo_maskfile, 'r')
ds_ifile = netCDF4.Dataset(nemo_ifile, 'r')
dims_maskfile = ds_maskfile.dimensions
dims_ofile = ds_ifile.dimensions
if dims_maskfile['x'].size != dims_ofile['x'].size or dims_maskfile['y'].size != dims_ofile['y'].size:
log.error("NEMO mask and output file, required for NEMO variable %s needed for target %s in table %s do not have same dimensions... "
% (nemo_var_name, task.target.variable, task.target.table))
return False
ds_maskfile.close()
ds_ifile.close()

# perform the conservative remapping using cdo
#cdo -L selvar,${varname} ${ifile} tmp1.nc
#CDO_REMAP_NORM=destarea cdo -L invertlat -setmisstoc,0 -remapycon,n128 -selindexbox,2,361,2,292 -mul tmp1.nc $mask $ofile
log.info("Using the following cdo version for conservative remapping")
os.system("cdo -V")
os.system("cdo -L selvar,"+nemo_var_name+" "+nemo_ifile+" "+interm_file)
if target_grid_ == "T159":
remap_grid='n80'
elif target_grid_ == "T255":
remap_grid='n128'
else:
log.error("WRONG GRID %s in function add_nemo_variable in lpjg2cmor.py!" % target_grid_)
exit(-1)
os.system("CDO_REMAP_NORM=destarea cdo -L invertlat -setmisstoc,0 -remapycon,"+remap_grid+" -selindexbox,2,361,2,292 -mul "+interm_file+" "+nemo_maskfile+" "+nemo_ofile)

if not os.path.exists(nemo_ofile):
log.error("There was a problem remapping %s variable in nemo output file %s needed for %s in table %s... "
% (nemo_var_name, nemo_ifile, task.target.variable, task.target.table))
return False

# add the nemo output to the lpjg output
if os.path.exists(interm_file):
os.remove(interm_file)
os.system("cdo -L add -selvar,"+task.target.variable+" "+ncfile+" "+nemo_ofile+" "+interm_file)
if not os.path.exists(interm_file):
log.error("There was a problem adding remapped %s variable from nemo output file %s to %s in table %s... "
% (nemo_var_name, nemo_ifile, task.target.variable, task.target.table))
return False

# overwrite final ncfile and cleanup
os.remove(nemo_ofile)
shutil.move(interm_file, ncfile)

return True

# Extracts single column from the .out-file
def get_lpjg_datacolumn(df, freq, colname, months_as_cols):
if freq == "day":
Expand Down

0 comments on commit 9ddf1d3

Please sign in to comment.