def test_ComponentFile_arg_subs(self): """ Check if argument substitutions work correctly""" self.assertEqual( ComponentFile("./", "./").copy_method, ComponentFile("./", "./", "copy").copy_method) self.assertEqual( ComponentFile("/foo/bar").dest, ComponentFile("/foo/bar", "bar").dest)
def test_ComponentFile_digest(self): """ Tests if digest works for ComponentFile """ with open("foo", "w") as f: f.write("a test file") test_comp_file = ComponentFile(src="foo", dest="/tmp") test_comp_file.digest() for f in "foo", "/tmp/foo": os.remove(f)
def test_FileDict_new_good_arguments(self): """FileDict can be initialized with correct arguments""" TestList = FileDict({"first_key": ComponentFile("/foo/bar")}) self.assertIsInstance(TestList, FileDict) TestList = FileDict({ "first_key": ComponentFile("/foo/bar"), "second_key": ComponentFile("/lar/mar") }) self.assertIsInstance(TestList, FileDict)
def test_read_filetables(self): """ ComponentCompute can read a default JSON filetable """ self.test_component_compute._prepare_read_filetables() self.assertEqual( self.test_component_compute.files["input"], FileDict({ "input_file_1": ComponentFile(src="/this/is/the/first/testfile/file1_source", dest="/this/is/the/first/testfile/file1_dest"), "input_file_2": ComponentFile(src="/this/is/the/second/testfile/file2_source", dest="/this/is/the/second/testfile/file2_dest") }))
def _generate_ice_forcing_file(self): """Makes a forcing file for an ice sheet. The following information is included: + ... """ logging.info( "\t\t Preparing echam6 file for processing in an ice sheet model..." ) start_year = self.calendar.coupling_start_date[self.name].format( 'YYYY') end_year = self.calendar.coupling_end_date[self.name].format('YYYY') file_list = self._construct_input_list(start_year, end_year) files_with_selected_variables = self._select_relevant_variables( file_list) final_output = self._concatenate_files(files_with_selected_variables) if self.ECHAM_TO_ISM_multiyear_mean: final_output = self._multiyear_mean(final_output) if self.ECHAM_TO_ISM_time_mean: final_output = self._time_mean(final_output) self.files["couple"]["atmosphere_file_for_ice"] = ComponentFile( src=final_output, dest=self.couple_dir + "/atmosphere_file_for_ice.nc") logging.info("\t\t ...done!")
def _write_grid_description(self): """ Gets the currently appropriate PISM grid in the couple dir """ self.files["couple"][self.Type + "_grid"] = ComponentFile( src=self.POOL_DIR + "/".join(["", "grids", self.Domain]) + "/" + "_".join([self.Executable, self.Domain, self.LateralResolution]) + ".griddes", dest=self.couple_dir + "/" + self.Type + ".griddes")
def _generate_ice_forcing_file(self): """ Generates information for a ice sheet The file rsl contains relative sea level information, but is continueously updated, so we should always cut out the **first** or **last** step Question: first or last timestep """ ofile = self.CDO.seltimestep("-1", input=self.files["outdata"]["rsl"]._current_location) self.files["couple"][self.Type+"_file"] = ComponentFile(src=ofile, dest=self.couple_dir+"/"+self.Type+"_file_for_ice.nc")
def test_FileDict_copyandrename(self): """FileDict can digest the a ComponentFile with the copy method, and rename it""" TestList = FileDict({ "my_testfile": ComponentFile(src="testfile_original", dest="testfile_copy") }) TestList.digest() assert os.path.isfile("./testfile_copy") self.cleanup_list.append("testfile_copy")
def test_env_override_filetables(self): """ ComponentCompute can override ComponentFile object `src` from enviornmental variables """ self.test_component_compute.files["input"] = FileDict( {"foo": ComponentFile("/foo/bar")}) with set_env(foo="/fiz/buzz"): self.test_component_compute._prepare_override_filetables_from_env() self.assertEqual( self.test_component_compute.files["input"]["foo"].src, "/fiz/buzz", "The actual input is %s" % self.test_component_compute.files["input"]["foo"].src)
def test_modify_filetables(self): """ ComponentCompute can modify src files based upon a modify JSON filetable """ self.test_component_compute._prepare_read_filetables() input_before = self.test_component_compute.files["input"].items() self.test_component_compute._prepare_modify_filetables() input_after = self.test_component_compute.files["input"].items() self.assertEqual( self.test_component_compute.files["input"], FileDict({ "input_file_1": ComponentFile( src="/this/is/the/first/testfile/file1_source_different", dest="/this/is/the/first/testfile/file1_dest"), "input_file_2": ComponentFile(src="/this/is/the/second/testfile/file2_source", dest="/this/is/the/second/testfile/file2_dest") }), msg="Input before was %s, Input after was %s" % (input_before, input_after))
def test_FileDict_digest_empty(self): """FileDict are empty after digestion with flag=work""" TestList = FileDict({ "my link": ComponentFile(src="testfile_original", dest="testfile_link_newlink", copy_method="link") }) TestList.digest(flag="work") self.assertEqual(len(TestList), 0) self.cleanup_list.append("testfile_link_newlink")
def test_FileDict_linkandrename(self): """FileDict can digest the a ComponentFile with the link method, and rename it""" TestList = FileDict({ "my link": ComponentFile(src="testfile_original", dest="testfile_link", copy_method="link") }) TestList.digest() assert os.path.islink("./testfile_link") self.cleanup_list.append("testfile_link")
def _generate_solid_earth_forcing_file(self): """ Generates a solid earth forcing from ice output. Some questions that still need to be clarified: 1. Do we want to send the newest timestep? 2. What happens if PISM restarts during a few chunks? """ last_timestep_of_extra_file = self.CDO.seltimestep( "-1", input=self.files["outdata"]["extra"]._current_location) ofile = self.CDO.selvar("thk", input=last_timestep_of_extra_file) self.files["couple"][self.Type + "_file"] = ComponentFile( src=ofile, dest=self.couple_dir + "/" + self.Type + "_file_for_solid_earth.nc")
def _write_grid_description(self): """ Writes echam6 grid descrption to atmosphere.griddes """ logging.info( "\t\t Writing echam6 grid description to generic file atmosphere.griddes..." ) logging.info("\t\t * generatic griddes") griddes = self.CDO.griddes( input=self.files["couple"]["atmosphere_file_for_ice"].src) ofile = open(self.couple_dir + "/griddes_file", "w") ofile.write("\n".join(griddes)) ofile.flush() self.files["couple"]["atmosphere_grid_description"] = ComponentFile( src=ofile.name, dest=self.couple_dir + "/atmosphere.griddes") self.__cleanup_list.append(ofile.name) logging.info("\t\t ...done!")
def test_ComponentFile_str(self): """ComponentFile __str__ works and is useful""" copy_file = ComponentFile(src="/foo/bar", dest="/foo/lar", copy_method="copy") self.assertEqual(copy_file.__str__(), "/foo/bar -- copied --> /foo/lar") link_file = ComponentFile(src="/foo/bar", dest="/foo/lar", copy_method="link") self.assertEqual(link_file.__str__(), "/foo/bar -- linked --> /foo/lar")
def _regrid_downscale_generate_elevation_difference(self): """Calculates ``elev_hi`` minus ``elev_lo`` Using the ``INPUT_FILE_pism`` from the ``files`` attribute, this method calculates the difference between the high and lo reolution. NOTE ---- Assumes that the variable named ``elevation`` is available in the lo-res elevation file Returns ------- diff : np.array You get back a numpy array of the differences between the high resolution and low resolution grids. """ ifile = self.files["input"]["INPUT_FILE_pism"] if using_xarray: diff = ifile["elevation"] - self.files["couple"][ "lo_res_elevation"]["elevation"] return diff #...? else: if "usurf" in CDO.pardes(input=ifile): hi_res_elevation = CDO.expr("elevation=usurf", input=ifile) elif ["thk", "topg"] in CDO.pardes(input=ifile): hi_res_elevation = CDO.expr("elevation=thk+topg", input=ifile) else: # TODO: Make CouplingError a real thing... raise CouplingError( "Insufficient information for hi resolution elevation, sorry!" ) hi_res_elevation = self.files["couple"]["hi_res_elevation"] \ = ComponentFile(src=hi_res_elevation, dest="hi_res_elevation_"+self.Name+"_"+str(self.calendar)+".nc") return CDO.sub(input=hi_res_elevation + " " + self.files["couple"]["lo_res_elevation"]) def _regrid_downscale_temperature(self): pass
def test_ComponentFile_new(self): """ComponentFile initializes correctly""" # Check if initializations work in general self.assertIsInstance(ComponentFile("/foo/bar"), ComponentFile) self.assertIsInstance(ComponentFile("/foo/bar", "/foo/lar"), ComponentFile)
def test_ComponentFile_linkcopy(self): """ Check that links are correctly different from copies""" self.assertNotEqual(ComponentFile("./", "./"), ComponentFile("./", "./", "link"))
def test_FileDict_update(self): """FileDict can be updated to properly""" self.test_list.update({"foo": ComponentFile("/foo/bar")}) self.assertEqual(self.test_list["foo"], ComponentFile("/foo/bar"))