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]
def test_find_nemo_exp_output(): ofiles = find_nemo_output(os.path.join(os.path.dirname(__file__), "test_data", "nemo_dummy"), "exp") eq_(len(ofiles), 2) ok_(os.path.join(os.path.dirname(__file__), "test_data", "nemo_dummy", "exp_1m_19991231_20000505_gridT.nc") in ofiles) ok_(os.path.join(os.path.dirname(__file__), "test_data", "nemo_dummy", "exp_1d_19991221_20000101_icemod.nc") in ofiles)
def test_find_nemo_output(): ofiles = find_nemo_output( os.path.join(os.path.dirname(__file__), "test_data", "nemo_dummy")) assert len(ofiles) == 2 assert os.path.join(os.path.dirname(__file__), "test_data", "nemo_dummy", "exp_1m_19991231_20000505_gridT.nc") \ in ofiles assert os.path.join(os.path.dirname(__file__), "test_data", "nemo_dummy", "exp_1d_19991221_20000101_icemod.nc") \ in ofiles
def test_nemo_output_timestamps(self): ofiles = find_nemo_output( os.path.join(os.path.dirname(__file__), "test_data", "nemo_dummy"), "exp") dates = [get_nemo_interval(f) for f in ofiles] starts = sorted([t[0] for t in dates]) eq_([datetime.datetime(1999, 12, 21), datetime.datetime(1999, 12, 31)], starts) ends = sorted([t[1] for t in dates]) eq_([datetime.datetime(2000, 1, 1), datetime.datetime(2000, 5, 5)], ends)
def test_nemo_bad_output(self): ofiles = find_nemo_output( os.path.join(os.path.dirname(__file__), "test_data", "nemo_dummy"), "bad") eq_(len(ofiles), 0)
def test_nemo_bad_output(): ofiles = find_nemo_output( os.path.join(os.path.dirname(__file__), "test_data", "nemo_dummy"), "bad") assert not any(ofiles)