def path_test(): cmor.setup(inpath='Test',netcdf_file_action=cmor.CMOR_REPLACE) cmor.dataset_json("Test/test_python_YYYMMDDHH_exp_fmt.json") table='Tables/CMIP6_Amon.json' cmor.load_table(table) axes = [ {'table_entry': 'time', 'units': 'days since 2000-01-01 00:00:00', 'coord_vals': [15], 'cell_bounds': [0, 30] }, {'table_entry': 'latitude', 'units': 'degrees_north', 'coord_vals': [0], 'cell_bounds': [-1, 1]}, {'table_entry': 'longitude', 'units': 'degrees_east', 'coord_vals': [90], 'cell_bounds': [89, 91]}, ] axis_ids = list() for axis in axes: axis_id = cmor.axis(**axis) axis_ids.append(axis_id) varid = cmor.variable('ts', 'K', axis_ids) cmor.write(varid, [273]) path=cmor.close(varid, file_name=True) print "Saved file: ",path
def testCMIP6(self): try: # ------------------------------------------- # Try to call cmor with a bad institution_ID # ------------------------------------------- cmor.setup( inpath='Tables', netcdf_file_action=cmor.CMOR_REPLACE) cmor.dataset_json("Test/test_python_CMIP6_CV_badgridlabel.json") # ------------------------------------------ # load Omon table and create masso variable # ------------------------------------------ cmor.load_table("CMIP6_Omon.json") itime = cmor.axis(table_entry="time", units='months since 2010', coord_vals=numpy.array([0, 1, 2, 3, 4.]), cell_bounds=numpy.array([0, 1, 2, 3, 4, 5.])) ivar = cmor.variable(table_entry="masso", axis_ids=[itime], units='kg') data = numpy.random.random(5) for i in range(0, 5): cmor.write(ivar, data[i:i]) except: os.dup2(self.newstdout, 1) os.dup2(self.newstderr, 2) testOK = self.getAssertTest() sys.stdout = os.fdopen(self.newstdout, 'w', 0) sys.stderr = os.fdopen(self.newstderr, 'w', 0) # ------------------------------------------ # Check error after signal handler is back # ------------------------------------------ self.assertIn("\"gs1n\"", testOK)
def testCMIP6(self): ''' ''' # ------------------------------------------- # Try to call cmor with a bad institution_ID # ------------------------------------------- cmor.setup(inpath='Tables', netcdf_file_action=cmor.CMOR_REPLACE) cmor.dataset_json("Test/test_python_CMIP6_CV_HISTORY.json") cmor.load_table("CMIP6_Omon.json") cmor.set_cur_dataset_attribute("history", "set for CMIP6 unittest") # ------------------------------------------ # load Omon table and create masso variable # ------------------------------------------ cmor.load_table("CMIP6_Omon.json") itime = cmor.axis(table_entry="time", units='months since 2010', coord_vals=numpy.array([0, 1, 2, 3, 4.]), cell_bounds=numpy.array([0, 1, 2, 3, 4, 5.])) ivar = cmor.variable(table_entry="masso", axis_ids=[itime], units='kg') data = numpy.random.random(5) for i in range(0, 5): cmor.write(ivar, data[i:i]) cmor.close() f = cdms2.open(cmor.get_final_filename(), "r") a = f.getglobal("history") self.assertIn("set for CMIP6 unittest", a) os.dup2(self.newstdout, 1) os.dup2(self.newstderr, 2) sys.stdout = os.fdopen(self.newstdout, 'w', 0) sys.stderr = os.fdopen(self.newstderr, 'w', 0)
def test_mode(mode): cmor.setup(inpath="Tables", netcdf_file_action=mode) cmor.dataset( "pre-industrial control", "ukmo", "HadCM3", "360_day", institute_id="ukmo", model_id="HadCM3", forcing="TO", contact="Derek Jeter", history="some global history", parent_experiment_id="lgm", parent_experiment_rip="r1i1p1", branch_time=0, ) table = "CMIP5_fx" cmor.load_table(table) axes = [ {"table_entry": "latitude", "units": "degrees_north", "coord_vals": [0], "cell_bounds": [-1, 1]}, {"table_entry": "longitude", "units": "degrees_east", "coord_vals": [90], "cell_bounds": [89, 91]}, ] values = numpy.array([5000], numpy.float32) axis_ids = list() for axis in axes: axis_id = cmor.axis(**axis) axis_ids.append(axis_id) for var, units in (("deptho", "m"),): varid = cmor.variable(var, units, axis_ids, history="variable history") cmor.write(varid, values) fnm = cmor.close(varid, file_name=True) cmor.close() return fnm
def testCMIP6(self): try: # ------------------------------------------- # Try to call cmor with a bad institution_ID # ------------------------------------------- cmor.setup(inpath='Tables', netcdf_file_action=cmor.CMOR_REPLACE, logfile=self.tmpfile) cmor.dataset_json( "Test/test_python_CMIP6_CV_badsourcetypeCHEMAER.json") # ------------------------------------------ # load Omon table and create masso variable # ------------------------------------------ cmor.load_table("CMIP6_Omon.json") itime = cmor.axis(table_entry="time", units='months since 2010', coord_vals=numpy.array([0, 1, 2, 3, 4.]), cell_bounds=numpy.array([0, 1, 2, 3, 4, 5.])) ivar = cmor.variable( table_entry="masso", axis_ids=[itime], units='kg') data = numpy.random.random(5) for i in range(0, 5): cmor.write(ivar, data[i:i]) self.delete_files += [cmor.close(ivar, True)] cmor.close() except BaseException: pass self.assertCV("invalid source")
def testCMIP6(self): ''' ''' # ------------------------------------------- # Try to call cmor with a bad institution_ID # ------------------------------------------- cmor.setup(inpath='Tables', netcdf_file_action=cmor.CMOR_REPLACE, logfile=self.tmpfile) cmor.dataset_json("Test/CMOR_input_example.json") cmor.load_table("CMIP6_Omon.json") cmor.set_cur_dataset_attribute("history", "set for CMIP6 unittest") # ------------------------------------------ # load Omon table and create masso variable # ------------------------------------------ cmor.load_table("CMIP6_Omon.json") itime = cmor.axis(table_entry="time", units='months since 2010', coord_vals=numpy.array([0, 1, 2, 3, 4.]), cell_bounds=numpy.array([0, 1, 2, 3, 4, 5.])) ivar = cmor.variable(table_entry="masso", axis_ids=[itime], units='kg') data = numpy.random.random(5) for i in range(0, 5): cmor.write(ivar, data[i:i]) self.delete_files += [cmor.close(ivar, True)] f = cdms2.open(cmor.get_final_filename(), "r") a = f.getglobal("history") self.assertIn("set for CMIP6 unittest", a)
def test(): cmor.setup(inpath='Tables',netcdf_file_action=cmor.CMOR_REPLACE) cmor.dataset_json("Test/test_python_obs4MIPs.json") table='CMIP6_Amon.json' cmor.load_table(table) axes = [ {'table_entry': 'time', 'units': 'days since 2000-01-01 00:00:00', }, {'table_entry': 'latitude', 'units': 'degrees_north', 'coord_vals': [0], 'cell_bounds': [-1, 1]}, {'table_entry': 'longitude', 'units': 'degrees_east', 'coord_vals': [90], 'cell_bounds': [89, 91]}, ] axis_ids = list() for axis in axes: axis_id = cmor.axis(**axis) axis_ids.append(axis_id) varid = cmor.variable('ts', 'K', axis_ids) cmor.write(varid, [275], time_vals = [15], time_bnds = [ [0,30] ]) cmor.close(varid)
def testCMIP6(self): try: # ------------------------------------------- # Try to call cmor with a bad institution_ID # ------------------------------------------- global testOK cmor.setup(inpath='Tables', netcdf_file_action=cmor.CMOR_REPLACE) cmor.dataset_json("Test/test_python_CMIP6_CV_trackingNoprefix.json") # ------------------------------------------ # load Omon table and create masso variable # ------------------------------------------ cmor.load_table("CMIP6_Omon.json") itime = cmor.axis(table_entry="time", units='months since 2010', coord_vals=numpy.array([0, 1, 2, 3, 4.]), cell_bounds=numpy.array([0, 1, 2, 3, 4, 5.])) ivar = cmor.variable(table_entry="masso", axis_ids=[itime], units='kg') data = numpy.random.random(5) for i in range(0, 5): cmor.write(ivar, data[i:i]) filen = cmor.close() os.dup2(self.newstdout, 1) os.dup2(self.newstderr, 2) sys.stdout = os.fdopen(self.newstdout, 'w', 0) sys.stderr = os.fdopen(self.newstderr, 'w', 0) f = cdms2.open(cmor.get_final_filename(), "r") a = f.getglobal("tracking_id").split('/')[0] self.assertNotIn("hdl:21.14100/", a) except: raise
def testCMIP6(self): # ------------------------------------------- # Try to call cmor with a bad institution_ID # ------------------------------------------- try: cmor.setup(inpath='Tables', netcdf_file_action=cmor.CMOR_REPLACE, logfile=self.tmpfile) cmor.dataset_json("Test/CMOR_input_example.json") cmor.set_cur_dataset_attribute("grid_label", "gr-0") # ------------------------------------------ # load Omon table and create masso variable # ------------------------------------------ cmor.load_table("CMIP6_Omon.json") itime = cmor.axis(table_entry="time", units='months since 2010', coord_vals=numpy.array([0, 1, 2, 3, 4.]), cell_bounds=numpy.array([0, 1, 2, 3, 4, 5.])) ivar = cmor.variable( table_entry="masso", axis_ids=[itime], units='kg') data = numpy.random.random(5) for i in range(0, 5): cmor.write(ivar, data[i:i]) self.delete_files += [cmor.close(ivar, True)] cmor.close() except BaseException: pass # ------------------------------------------ # Check error after signal handler is back # ------------------------------------------ self.assertCV("\"gr-0\"")
def testCMIP6(self): try: # ------------------------------------------- # Try to call cmor with a bad institution_ID # ------------------------------------------- global testOK cmor.setup(inpath='Tables', netcdf_file_action=cmor.CMOR_REPLACE, logfile=self.tmpfile) cmor.dataset_json("Test/CMOR_input_example.json") cmor.set_cur_dataset_attribute("source_id", "invalid") # ------------------------------------------ # load Omon table and create masso variable # ------------------------------------------ cmor.load_table("CMIP6_Omon.json") itime = cmor.axis(table_entry="time", units='months since 2010', coord_vals=numpy.array([0, 1, 2, 3, 4.]), cell_bounds=numpy.array([0, 1, 2, 3, 4, 5.])) ivar = cmor.variable( table_entry="masso", axis_ids=[itime], units='kg') data = numpy.random.random(5) for i in range(0, 5): cmor.write(ivar, data[i:i]) cmor.close() except BaseException: pass self.assertCV("invalid")
def setUp(self): cmor.setup(inpath='Test', netcdf_file_action=cmor.CMOR_REPLACE_4) cmor.dataset_json("Test/CMOR_input_example.json") self.path = None
def testCMIP6(self): try: # ------------------------------------------- # Try to call cmor with a bad institution_ID # ------------------------------------------- global testOK cmor.setup(inpath="Tables", netcdf_file_action=cmor.CMOR_REPLACE) cmor.dataset_json("Test/test_python_CMIP6_CV_furtherinfourl.json") # ------------------------------------------ # load Omon table and create masso variable # ------------------------------------------ cmor.load_table("CMIP6_Omon.json") itime = cmor.axis( table_entry="time", units="months since 2010", coord_vals=numpy.array([0, 1, 2, 3, 4.0]), cell_bounds=numpy.array([0, 1, 2, 3, 4, 5.0]), ) ivar = cmor.variable(table_entry="masso", axis_ids=[itime], units="kg") data = numpy.random.random(5) for i in range(0, 5): cmor.write(ivar, data[i:i]) cmor.close() except: raise os.dup2(self.newstdout, 1) os.dup2(self.newstderr, 2) sys.stdout = os.fdopen(self.newstdout, "w", 0) sys.stderr = os.fdopen(self.newstderr, "w", 0) f = cdms2.open(cmor.get_final_filename(), "r") a = f.getglobal("further_info_url") self.assertEqual("http://furtherinfo.es-doc.org/CMIP6.NCC.MIROC-ESM.piControl-withism.none.r1i1p1f1", a)
def testCMIP6(self): try: # ------------------------------------------- # Try to call cmor with a bad institution_ID # ------------------------------------------- cmor.setup(inpath="Tables", netcdf_file_action=cmor.CMOR_REPLACE) cmor.dataset_json("Test/test_python_CMIP6_CV_badsourcetypeRequired.json") # ------------------------------------------ # load Omon table and create masso variable # ------------------------------------------ cmor.load_table("CMIP6_Omon.json") itime = cmor.axis( table_entry="time", units="months since 2010", coord_vals=numpy.array([0, 1, 2, 3, 4.0]), cell_bounds=numpy.array([0, 1, 2, 3, 4, 5.0]), ) ivar = cmor.variable(table_entry="masso", axis_ids=[itime], units="kg") data = numpy.random.random(5) for i in range(0, 5): a = cmor.write(ivar, data[i:i]) except: os.dup2(self.newstdout, 1) os.dup2(self.newstderr, 2) sys.stdout = os.fdopen(self.newstdout, "w", 0) sys.stderr = os.fdopen(self.newstderr, "w", 0) testOK = self.getAssertTest() # ------------------------------------------ # Check error after signal handler is back # ------------------------------------------ self.assertIn('"AOGCM ISM"', testOK)
def main(): missing = -99. cmor.setup(inpath='Tables', netcdf_file_action = cmor.CMOR_REPLACE) cmor.dataset_json("Test/test_python_jamie_3.json") table = 'CMIP6_Amon.json' cmor.load_table(table) axes = [ {'table_entry': 'time', 'units': 'days since 2000-01-01 00:00:00', }, {'table_entry': 'latitude', 'units': 'degrees_north', 'coord_vals': [0], 'cell_bounds': [-1, 1]}, {'table_entry': 'longitude', 'units': 'degrees_east', 'coord_vals': [90], 'cell_bounds': [89, 91]}, ] values = numpy.array([missing], numpy.float32) myma = numpy.ma.masked_values(values, missing) axis_ids = list() for axis in axes: axis_id = cmor.axis(**axis) axis_ids.append(axis_id) varid = cmor.variable('ts', 'K', axis_ids, missing_value = myma.fill_value) cmor.write(varid, myma, time_vals = [15], time_bnds = [ [0,30] ]) cmor.close(varid)
def testCMIP6(self): try: cmor.setup(inpath='Tables', netcdf_file_action=cmor.CMOR_REPLACE) cmor.dataset_json("Test/CMOR_input_example.json") cmor.set_cur_dataset_attribute("experiment_id", "piControlbad") cmor.load_table("CMIP6_Omon.json") itime = cmor.axis(table_entry="time", units='months since 2010', coord_vals=numpy.array( [0, 1, 2, 3, 4.]), cell_bounds=numpy.array([0, 1, 2, 3, 4, 5.])) ivar = cmor.variable( table_entry="masso", axis_ids=[itime], units='kg') data = numpy.random.random(5) for i in range(0, 5): # ,time_vals=numpy.array([i,]),time_bnds=numpy.array([i,i+1])) cmor.write(ivar, data[i:i]) cmor.close() except BaseException: os.dup2(self.newstdout, 1) os.dup2(self.newstderr, 2) sys.stdout = os.fdopen(self.newstdout, 'w', 0) sys.stderr = os.fdopen(self.newstderr, 'w', 0) testOK = self.getAssertTest() self.assertIn('piControlbad', testOK)
def testCMIP6(self): try: inpath = 'Tables' # 01.00.27b1 cmor.setup(inpath=inpath, netcdf_file_action=cmor.CMOR_REPLACE, logfile=self.tmpfile) error_flag = cmor.dataset_json('Test/CMOR_input_example.json') table_id = cmor.load_table('CMIP6_6hrLev_bad_specs.json') time = cmor.axis(table_entry='time1', units='days since 2000-01-01', coord_vals=numpy.array(range(1)), cell_bounds=numpy.array(range(2))) latitude = cmor.axis(table_entry='latitude', units='degrees_north', coord_vals=numpy.array(range(5)), cell_bounds=numpy.array(range(6))) longitude = cmor.axis(table_entry='longitude', units='degrees_east', coord_vals=numpy.array(range(5)), cell_bounds=numpy.array(range(6))) plev3 = cmor.axis(table_entry='plev3', units='Pa', coord_vals=numpy.array([85000., 50000., 25000.])) axis_ids = [longitude, latitude, plev3, time] ua_var_id = cmor.variable(table_entry='ua', axis_ids=axis_ids, units='m s-1') ta_var_id = cmor.variable(table_entry='ta', axis_ids=axis_ids, units='K') data = numpy.random.random(75) reshaped_data = data.reshape((5, 5, 3, 1)) # This doesn't: cmor.write(ta_var_id, reshaped_data) cmor.write(ua_var_id, reshaped_data) #cmor.close() except BaseException: pass self.assertCV("data_specs_version")
def TestCase(self): try: # ------------------------------------------- # Try to call cmor with a bad institution_ID # ------------------------------------------- cmor.setup(inpath='Tables', netcdf_file_action=cmor.CMOR_REPLACE) cmor.dataset_json("Test/test_python_CMIP6_CV_badsourcetypeCHEMAER.json") # ------------------------------------------ # load Omon table and create masso variable # ------------------------------------------ cmor.load_table("CMIP6_Omon.json") itime = cmor.axis(table_entry="time", units='months since 2010', coord_vals=numpy.array([0, 1, 2, 3, 4.]), cell_bounds=numpy.array([0, 1, 2, 3, 4, 5.])) ivar = cmor.variable(table_entry="masso", axis_ids=[itime], units='kg') data = numpy.random.random(5) for i in range(0, 5): cmor.write(ivar, data[i:i]) except: raise os.dup2(self.newstdout, 1) os.dup2(self.newstderr, 2) sys.stdout = os.fdopen(self.newstdout, 'w', 0) sys.stderr = os.fdopen(self.newstderr, 'w', 0)
def testCMIP6(self): # ------------------------------------------- # Try to call cmor with a bad institution_ID # ------------------------------------------- cmor.setup(inpath='Tables', netcdf_file_action=cmor.CMOR_REPLACE, logfile=self.tmpfile) cmor.dataset_json("Test/CMOR_input_example.json") # ------------------------------------------ # load Omon table and create masso variable # ------------------------------------------ cmor.load_table("CMIP6_Omon.json") itime = cmor.axis(table_entry="time", units='months since 2000', coord_vals=numpy.array([0, 1, 2, 3, 4.]), cell_bounds=numpy.array([0, 1, 2, 3, 4, 5.])) ivar = cmor.variable( table_entry="masso", axis_ids=[itime], units='kg') data = numpy.random.random(5) for i in range(0, 5): cmor.write(ivar, data[i:i]) self.delete_files += [cmor.close(ivar, True)] cmor.close() f = cdms2.open(cmor.get_final_filename(), "r") a = f.getglobal("further_info_url") self.assertEqual( "https://furtherinfo.es-doc.org/CMIP6.PCMDI.PCMDI-test-1-0.piControl-withism.none.r3i1p1f1", a)
def main(): cmor.setup(inpath='Tables', netcdf_file_action = cmor.CMOR_REPLACE) cmor.dataset('pre-industrial control', 'mohc', 'HadGEM2: source', '360_day', institute_id = 'ukmo', model_id = 'HadGEM2', history = 'some global history', forcing = 'N/A', parent_experiment_id = 'N/A', parent_experiment_rip = 'N/A', branch_time = 0., contact = 'bob') table = 'CMIP5_6hrLev' cmor.load_table(table) axes = [ {'table_entry': 'time1', 'units': 'days since 2000-01-01 00:00:00', }, {'table_entry': 'latitude', 'units': 'degrees_north', 'coord_vals': [0], 'cell_bounds': [-1, 1]}, {'table_entry': 'longitude', 'units': 'degrees_east', 'coord_vals': [90], 'cell_bounds': [89, 91]}, {'table_entry': 'hybrid_height', 'coord_vals': [0, 1], 'cell_bounds': [[0., 0.5], [0.5, 1.]], 'units': 'm', }, ] values = numpy.array([1.2,1.2], numpy.float32) numpy.reshape(values, (2,1,1,1)) axis_ids = list() for axis in axes: axis_id = cmor.axis(**axis) axis_ids.append(axis_id) print 'cmor.axis calls complete' cmor.zfactor(axis_ids[3], 'b', '', axis_ids[3:4], 'd', [0., 0.5], [[0., 0.25], [0.25, 1.]]) cmor.zfactor(axis_ids[3], 'orog', 'm', axis_ids[1:3], 'd', [[0.]]) print 'cmor.zfactor calls complete' varid = cmor.variable('ua', 'm s-1', axis_ids, missing_value = -99 ) print 'cmor.variable call complete' cmor.write(varid, values, time_vals = [6.0]) print 'cmor.write call complete' cmor.close()
def path_test(): cmor.setup(inpath='Test',netcdf_file_action=cmor.CMOR_REPLACE) cmor.dataset('mytest2010030812', 'ukmo', 'HadCM3', '360_day', institute_id="PCMDI", model_id='HadCM3',forcing='co2') table='CMIP5_Amon_YYYYMMDDHH' cmor.load_table(table) axes = [ {'table_entry': 'time', 'units': 'days since 2000-01-01 00:00:00', 'coord_vals': [15], 'cell_bounds': [0, 30] }, {'table_entry': 'latitude', 'units': 'degrees_north', 'coord_vals': [0], 'cell_bounds': [-1, 1]}, {'table_entry': 'longitude', 'units': 'degrees_east', 'coord_vals': [90], 'cell_bounds': [89, 91]}, ] axis_ids = list() for axis in axes: axis_id = cmor.axis(**axis) axis_ids.append(axis_id) varid = cmor.variable('ts', 'K', axis_ids) cmor.write(varid, [273]) path=cmor.close(varid, file_name=True) print "Saved file: ",path
def testCMIP6(self): cmor.setup(inpath='Tables', netcdf_file_action=cmor.CMOR_REPLACE, logfile=self.tmpfile) cmor.dataset_json("Test/CMOR_input_example.json") cmor.set_cur_dataset_attribute("tracking_prefix", "hdl:21.14100") # ------------------------------------------ # load Omon table and create masso variable # ------------------------------------------ cmor.load_table("CMIP6_Omon.json") itime = cmor.axis(table_entry="time", units='months since 2011', coord_vals=numpy.array([0, 1, 2, 3, 4.]), cell_bounds=numpy.array([0, 1, 2, 3, 4, 5.])) ivar = cmor.variable( table_entry="masso", axis_ids=[itime], units='kg') data = numpy.random.random(5) for i in range(0, 5): a = cmor.write(ivar, data[i:i]) self.delete_files += [cmor.close(ivar, True)] cmor.close() f = cdms2.open(cmor.get_final_filename(), "r") a = f.getglobal("tracking_id").split('/')[0] self.assertIn("hdl:21.14100", a)
def prep_cmor(): cmor.setup(inpath=ipth,set_verbosity=cmor.CMOR_QUIET, netcdf_file_action = cmor.CMOR_REPLACE, exit_control = cmor.CMOR_EXIT_ON_MAJOR); cmor.dataset( outpath = opth, experiment_id = "lgm", institution = "GICC (Generic International Climate Center, Geneva, Switzerland)", source = "GICCM1 (2002): atmosphere: GICAM3 (gicam_0_brnchT_itea_2, T63L32); ocean: MOM (mom3_ver_3.5.2, 2x3L15); sea ice: GISIM4; land: GILSM2.5", calendar = "standard", realization = 1, contact = "Rusty Koder (koder@middle_earth.net)", history = "Output from archive/giccm_03_std_2xCO2_2256.", comment = "Equilibrium reached after 30-year spin-up after which data were output starting with nominal date of January 2030", references = "Model described by Koder and Tolkien (J. Geophys. Res., 2001, 576-591). Also see http://www.GICC.su/giccm/doc/index.html 2XCO2 simulation described in Dorkey et al. '(Clim. Dyn., 2003, 323-357.)", leap_year=0, leap_month=0, institute_id="PCMDI", month_lengths=None,model_id="GICCM1",forcing="Nat", parent_experiment_id="N/A", parent_experiment_rip="N/A", branch_time=0.) tables=[] a = cmor.load_table("Tables/CMIP5_Omon") tables.append(a) tables.append(cmor.load_table("Tables/CMIP5_Amon")) return
def testCMIP6(self): cmor.setup(inpath='Tables', netcdf_file_action=cmor.CMOR_REPLACE, logfile=self.tmpfile) cmor.dataset_json("Test/common_user_input_hier.json") cmor.load_table("CMIP6_Omon.json") itime = cmor.axis(table_entry="time", units='months since 2010', coord_vals=numpy.array( [0, 1, 2, 3, 4.]), cell_bounds=numpy.array([0, 1, 2, 3, 4, 5.])) ivar = cmor.variable( table_entry="masso", axis_ids=[itime], units='kg') data = numpy.random.random(5) for i in range(0, 5): # ,time_vals=numpy.array([i,]),time_bnds=numpy.array([i,i+1])) cmor.write(ivar, data[i:i]) self.delete_files += [cmor.close(ivar, True)] cmor.close() f = cdms2.open(cmor.get_final_filename() , 'r') self.assertEqual(f.coder, "Denis Nadeau") self.assertEqual(f.hierarchical_attr_setting, "information") self.assertEqual(f.creator, "PCMDI") self.assertEqual(f.model, "Ocean Model") self.assertEqual(f.country, "USA") f.close()
def multi_call_test(): cmor.setup(inpath='Tables',netcdf_file_action=cmor.CMOR_REPLACE) cmor.dataset_json("Test/test_python_jamie_2.json") table='CMIP6_Amon.json' cmor.load_table(table) axes = [ {'table_entry': 'time', 'units': 'days since 2000-01-01 00:00:00', }, {'table_entry': 'latitude', 'units': 'degrees_north', 'coord_vals': [0], 'cell_bounds': [-1, 1]}, {'table_entry': 'longitude', 'units': 'degrees_east', 'coord_vals': [90], 'cell_bounds': [89, 91]}, ] axis_ids = list() for axis in axes: axis_id = cmor.axis(**axis) axis_ids.append(axis_id) varid = cmor.variable('ts', 'K', axis_ids) cmor.write(varid, [275], time_vals = [15], time_bnds = [ [0,30] ]) print 'First write worked as expected' try: cmor.write(varid, [275], time_vals = [15], time_bnds = [ [0], [30] ]) raise Exception,"We shouldn't be getting in here" except: print 'Second write that should have failed did fail, good!' pass cmor.close(varid) print 'Success'
def setup_cmor(): #------------------------------------------------------------------------- # Initialise CMOR library cmor.setup(inpath=MIP_TABLE_DIR, netcdf_file_action=cmor.CMOR_REPLACE_3, set_verbosity=cmor.CMOR_NORMAL, create_subdirectories=0) # Create CMOR dataset cmor.dataset_json("Test/CMOR_input_example.json")
def setup_cmor() : #--------------------------------------------------------------------------------------------------- # Initialise CMOR library cmor.setup(inpath=MIP_TABLE_DIR, netcdf_file_action=cmor.CMOR_REPLACE_3, set_verbosity=cmor.CMOR_NORMAL, create_subdirectories=0) # Create CMOR dataset cmor.dataset_json("Test/test_python_cfmip_site_axis_test.json")
def prep_cmor(): cmor.setup(inpath="Tables",set_verbosity=cmor.CMOR_QUIET, netcdf_file_action = cmor.CMOR_REPLACE, exit_control = cmor.CMOR_EXIT_ON_MAJOR); cmor.dataset_json("Test/test_python_user_interface_03.json") tables=[] a = cmor.load_table("CMIP6_Omon.json") tables.append(a) tables.append(cmor.load_table("CMIP6_Amon.json")) return
def testLoadTables(self): tables = glob.glob("Tables/CMIP6*json") for table in tables: if "formula_terms" in table: continue cmor.setup(inpath='Tables', netcdf_file_action=cmor.CMOR_REPLACE) cmor.dataset_json("Test/CMOR_input_example.json") print("Loading table:", table) ierr = cmor.load_table(table) self.assertEqual(ierr, 0) cmor.close()
def test_Directory(self): # ------------------------------------------- # Try to call cmor with a bad institution_ID # ------------------------------------------- cmor.setup(inpath='Tables', netcdf_file_action=cmor.CMOR_REPLACE, logfile=self.tmpfile) try: cmor.dataset_json("Test/baddirectory.json") except BaseException: pass self.assertCV("unable to create this directory")
def main(): cmor.setup(inpath='/git/cmip5-cmor-tables/Tables', netcdf_file_action = cmor.CMOR_REPLACE_3) cmor.dataset('pre-industrial control', 'ukmo', 'HadCM3', '360_day', institute_id = 'ukmo', model_id = 'HadCM3', history = 'some global history', forcing = 'N/A', parent_experiment_id = 'N/A', parent_experiment_rip = 'N/A', branch_time = 0, contact = 'brian clough') table = 'CMIP5_Oclim' cmor.load_table(table) axes = [ {'table_entry': 'time2', 'units': 'days since 1850-01-01 00:00:00', 'coord_vals' : [15.5, 45,], 'cell_bounds':[[0,396],[31,424]] }, {'table_entry': 'depth_coord', 'units': 'm', 'coord_vals': [5000., 3000., 2000., 1000.], 'cell_bounds': [ 5000., 3000., 2000., 1000.,0]}, {'table_entry': 'latitude', 'units': 'degrees_north', 'coord_vals': [0], 'cell_bounds': [-1, 1]}, {'table_entry': 'longitude', 'units': 'degrees_east', 'coord_vals': [90], 'cell_bounds': [89, 91]}, ] axis_ids = list() for axis in axes: print 'doing:',axis axis_id = cmor.axis(**axis) axis_ids.append(axis_id) for var, units, value in (('difvso', 'm2 s-1', 274.),): values = numpy.ones(map(lambda x: len(x["coord_vals"]),axes))*value values=values.astype("f") varid = cmor.variable(var, units, axis_ids, history = 'variable history', missing_value = -99 ) cmor.write(varid, values) cmor.close()
lon = d.getLongitude() print d.shape #time = d.getTime() ; # Assumes variable is named 'time', for the demo file this is named 'months' time = d.getAxis(0) ; # Rather use a file dimension-based load statement # Deal with problematic "months since" calendar/time axis #####time_bounds = time.getBounds() #####time_bounds[:,0] = time[:] #####time_bounds[:-1,1] = time[1:] #####time_bounds[-1,1] = time_bounds[-1,0]+1 #####time.setBounds() #####time_bounds) #####del(time_bounds) ; # Cleanup #%% Initialize and run CMOR # For more information see https://cmor.llnl.gov/mydoc_cmor3_api/ cmor.setup(inpath='./',netcdf_file_action=cmor.CMOR_REPLACE_4) #,logfile='cmorLog.txt') cmor.dataset_json(inputJson) cmor.load_table(cmorTable) #cmor.set_cur_dataset_attribute('history',f.history) ; # Force input file attribute as history axes = [ {'table_entry': 'time', 'units': time.units, # 'days since 1870-01-01', }, {'table_entry': 'latitude', 'units': 'degrees_north', 'coord_vals': lat[:], 'cell_bounds': lat.getBounds()}, {'table_entry': 'longitude', 'units': 'degrees_east', 'coord_vals': lon[:], 'cell_bounds': lon.getBounds()}, ]
def dump_cmor(A, s, time, bounds): inst = checkCMORAttribute("institution") src = checkCMORAttribute("source") exp = checkCMORAttribute("experiment_id") xtra = {} for x in cmor_xtra_args: try: xtra[x] = checkCMORAttribute(x) except Exception: pass cal = data.getTime().getCalendar() # cmor understand cdms calendars cal_name = getCalendarName(cal) if A.verbose: cmor_verbose = cmor.CMOR_NORMAL else: cmor_verbose = cmor.CMOR_QUIET tables_dir = os.path.dirname(A.table) cmor.setup( inpath=tables_dir, netcdf_file_action=cmor.CMOR_REPLACE, set_verbosity=cmor_verbose, exit_control=cmor.CMOR_NORMAL, # logfile='logfile', create_subdirectories=int(A.drs)) tmp = tempfile.NamedTemporaryFile(mode="w") tmp.write("""{{ "_control_vocabulary_file": "CMIP6_CV.json", "_AXIS_ENTRY_FILE": "CMIP6_coordinate.json", "_FORMULA_VAR_FILE": "CMIP6_formula_terms.json", "_cmip6_option": "CMIP6", "tracking_prefix": "hdl:21.14100", "activity_id": "ISMIP6", "#output": "Root directory where files are written", "outpath": "{}", "#experiment_id": "valid experiment_ids are found in CMIP6_CV.json", "experiment_id": "{}", "sub_experiment_id": "none", "sub_experiment": "none", "source_type": "AOGCM", "mip_era": "CMIP6", "calendar": "{}", "realization_index": "{}", "initialization_index": "{}", "physics_index": "{}", "forcing_index": "1", "#contact ": "Not required", "contact ": "Python Coder ([email protected])", "#history": "not required, supplemented by CMOR", "history": "Output from archivcl_A1.nce/giccm_03_std_2xCO2_2256.", "#comment": "Not required", "comment": "", "#references": "Not required", "references": "Model described by Koder and Tolkien (J. Geophys. Res., 2001, 576-591). Also see http://www.GICC.su/giccm/doc/index.html 2XCO2 simulation described in Dorkey et al . '(Clim. Dyn., 2003, 323-357.)'", "grid": "gs1x1", "grid_label": "gr", "nominal_resolution": "5 km", "institution_id": "{}", "parent_experiment_id": "histALL", "parent_activity_id": "ISMIP6", "parent_mip_era": "CMIP6", "parent_source_id": "PCMDI-test-1-0", "parent_time_units": "days since 1970-01-01", "parent_variant_label": "r123i1p33f5", "branch_method": "Spin-up documentation", "branch_time_in_child": 2310.0, "branch_time_in_parent": 12345.0, "#run_variant": "Description of run variant (Recommended).", "run_variant": "forcing: black carbon aerosol only", "#source_id": "Model Source", "source_id": "{}", "#source": "source title, first part is source_id", "source": "PCMDI's PMP", "_history_template": "%s ;rewrote data to be consistent with <activity_id> for variable <variable_id> found in table <table_id>.", "#output_path_template": "Template for output path directory using tables keys or global attributes", "output_path_template": "<mip_era><activity_id><institution_id><source_id><experiment_id><_member_id><table><variable_id><grid_label><version>", "output_file_template": "<variable_id><table><source_id><experiment_id><_member_id><grid_label>", "license": "CMIP6 model data produced by Lawrence Livermore PCMDI is licensed under a Creative Commons Attribution ShareAlike 4.0 International License (https://creativecommons.org/licenses). Consult https://pcmdi.llnl.gov/CMIP6/TermsOfUse for terms of use governing CMIP6 output, including citation requirements and proper acknowledgment. Further information about this data, including some limitations, can be found via the further_info_url (recorded as a global attribute in this file) and at https:///pcmdi.llnl.gov/. The data producers and data providers make no warranty, either express or implied, including, but not limited to, warranties of merchantability and fitness for a particular purpose. All liabilities arising from the supply of the information (including any liability arising in negligence) are excluded to the fullest extent permitted by law." }} """.format(A.results_dir, exp, cal_name, r, i, p, inst.split()[0], src)) # noqa tmp.flush() cmor.dataset_json(tmp.name) if not os.path.exists(A.table): raise RuntimeError("No such file or directory for tables: %s" % A.table) print("Loading table: {}".format(os.path.abspath(A.table))) table_content = open(A.table).read().replace("time", "time2") table_content = table_content.replace("time22", "time2") table = tempfile.NamedTemporaryFile("w") table.write(table_content) table.flush() for table_name in ["formula_terms", "coordinate"]: nm = "CMIP6_{}.json".format(table_name) with open(os.path.join(os.path.dirname(table.name), nm), "w") as tmp: tmp.write(open(os.path.join(tables_dir, nm)).read()) table = cmor.load_table(table.name) # Ok CMOR is ready let's create axes cmor_axes = [] for ax in s.getAxisList(): if ax.isLatitude(): table_entry = "latitude" elif ax.isLongitude(): table_entry = "longitude" elif ax.isLevel(): # Need work here for sigma table_entry = "plevs" if ax.isTime(): table_entry = "time2" ntimes = len(ax) axvals = numpy.array(values) axbnds = numpy.array(bounds) axunits = Tunits else: axvals = ax[:] axbnds = ax.getBounds() axunits = ax.units ax_id = cmor.axis(table_entry=table_entry, units=axunits, coord_vals=axvals, cell_bounds=axbnds) cmor_axes.append(ax_id) # Now create the variable itself if A.cf_var is not None: var_entry = A.cf_var else: var_entry = data.id units = A.units if units is None: units = data.units kw = eval(A.variable_extra_args) if not isinstance(kw, dict): raise RuntimeError( "invalid evaled type for -X args, should be evaled as a dict, e.g: -X '{\"positive\":\"up\"}'" ) var_id = cmor.variable(table_entry=var_entry, units=units, axis_ids=cmor_axes, type=s.typecode(), missing_value=s.missing_value, **kw) # And finally write the data data2 = s.filled(s.missing_value) cmor.write(var_id, data2, ntimes_passed=ntimes) # Close cmor path = cmor.close(var_id, file_name=True) if season.lower() == "ann": suffix = "ac" else: suffix = season path2 = path.replace("-clim.nc", "-clim-%s.nc" % suffix) os.rename(path, path2) if A.verbose: print("Saved to:", path2) cmor.close() if A.verbose: print("closed cmor")
def main(): cmor.setup(inpath='Tables', netcdf_file_action=cmor.CMOR_REPLACE_3) cmor.dataset('pre-industrial control', 'ukmo', 'HadCM3', '360_day', institute_id='ukmo', model_id='HadCM3', history='some global history', forcing='N/A', parent_experiment_id='N/A', parent_experiment_rip='N/A', branch_time=0, contact='brian clough') table = 'CMIP5_Amon' cmor.load_table(table) axes = [ { 'table_entry': 'time', 'units': 'days since 2000-01-01 00:00:00', }, { 'table_entry': 'plevs', 'units': 'Pa', 'coord_vals': '100000. 92500. 85000. 70000. 60000. 50000. 40000. 30000. 25000. 20000. 15000. 10000. 7000. 5000. 3000. 2000. 1000.' .split(' ') }, { 'table_entry': 'latitude', 'units': 'degrees_north', 'coord_vals': [0], 'cell_bounds': [-1, 1] }, { 'table_entry': 'longitude', 'units': 'degrees_east', 'coord_vals': [90], 'cell_bounds': [89, 91] }, ] axis_ids = list() for axis in axes: print 'doing:', axis axis_id = cmor.axis(**axis) axis_ids.append(axis_id) for var, units, value in (('ta', 'K', 274), ('ua', 'm s-1', 10)): values = numpy.array([ value, ] * len(axes[1]['coord_vals']), numpy.float32) varid = cmor.variable(var, units, axis_ids, history='variable history', missing_value=-99) cmor.set_variable_attribute(varid, 'cell_measures', '') cmor.write(varid, values, time_vals=[15], time_bnds=[[0, 30]]) cmor.close()
def test_mode(mode, i, suffix=''): cmor.setup(inpath='Tables', netcdf_file_action=mode) cmor.dataset_json("Test/CMOR_input_example.json") table = 'CMIP6_Amon.json' cmor.load_table(table) levels = [100000., 92500., 85000., 70000., 60000., 50000., 40000., 30000., 25000., 20000., 15000., 10000., 7000., 5000., 3000., 2000., 1000., 999, 998, 997, 996, 995, 994, 500, 100] axes = [{'table_entry': 'time', 'units': 'months since 2000-01-01 00:00:00', }, {'table_entry': 'latitude', 'units': 'degrees_north', 'coord_vals': [0], 'cell_bounds': [-1, 1]}, {'table_entry': 'longitude', 'units': 'degrees_east', 'coord_vals': [90], 'cell_bounds': [89, 91]}, {'table_entry': 'plev19', 'units': 'Pa', 'coord_vals': levels}, ] values = numpy.array(range(len(levels)), numpy.float32) + 195 axis_ids = list() for axis in axes: axis_id = cmor.axis(**axis) axis_ids.append(axis_id) for var, units in (('ta', 'K'),): varid = cmor.variable(var, units, axis_ids, history='variable history', missing_value=-99 ) print("Sending time bounds:", [[i, i + 1]]) cmor.write(varid, values, time_vals=[i], time_bnds=[[i, i + 1]]) fnm = cmor.close(varid, file_name=True) cmor.close() return fnm
def handle(infile, tables, user_input_path): """ Transform E3SM.QREFHT into CMIP.huss CMIP5_Amon hurs relative_humidity longitude latitude time height2m atmos 1 RHREFHT RHREFHT no change """ msg = f'Starting {__name__} with {infile}' logging.info(msg) print_message(msg, 'ok') # extract data from the input file f = cdms2.open(infile) data = f('QREFHT') lat = data.getLatitude()[:] lon = data.getLongitude()[:] lat_bnds = f('lat_bnds') lon_bnds = f('lon_bnds') time = data.getTime() time_bnds = f('time_bnds') f.close() # setup cmor logfile = os.path.join(os.getcwd(), 'logs') if not os.path.exists(logfile): os.makedirs(logfile) _, tail = os.path.split(infile) logfile = os.path.join(logfile, tail.replace('.nc', '.log')) cmor.setup( inpath=tables, netcdf_file_action=cmor.CMOR_REPLACE, logfile=logfile) cmor.dataset_json(user_input_path) table = 'CMIP6_Amon.json' try: cmor.load_table(table) except: raise Exception('Unable to load table from {}'.format(__name__)) # create axes axes = [{ 'table_entry': 'time', 'units': time.units }, { 'table_entry': 'latitude', 'units': 'degrees_north', 'coord_vals': lat[:], 'cell_bounds': lat_bnds[:] }, { 'table_entry': 'longitude', 'units': 'degrees_east', 'coord_vals': lon[:], 'cell_bounds': lon_bnds[:] }] axis_ids = list() for axis in axes: axis_id = cmor.axis(**axis) axis_ids.append(axis_id) # create the cmor variable varid = cmor.variable('huss', '1.0', axis_ids) # write out the data try: for index, val in enumerate(data.getTime()[:]): cmor.write(varid, data[index, :], time_vals=val, time_bnds=[time_bnds[index, :]]) except: raise finally: cmor.close(varid) return 'QREFHT'
nlat = 10 dlat = 180. / nlat nlon = 20 dlon = 360. / nlon nlev = 17 ntimes = 1 lats = numpy.arange(90 - dlat / 2., -90, -dlat) blats = numpy.arange(90, -90 - dlat, -dlat) lats2 = numpy.arange(-90 + dlat / 2., 90, dlat) blats2 = numpy.arange(-90, 90 + dlat, dlat) lons = numpy.arange(0 + dlon / 2., 360., dlon) blons = numpy.arange(0, 360. + dlon, dlon) cmor.setup(inpath='.', netcdf_file_action=cmor.CMOR_REPLACE) cmor.dataset_json("Test/test_python_reverted_lats.json") table = 'Tables/CMIP6_Amon.json' cmor.load_table(table) data = lats[:, numpy.newaxis] * lons[numpy.newaxis, :] data = (data + 29000) / 750. + 233.2 ilat = cmor.axis(table_entry='latitude', coord_vals=lats, cell_bounds=blats, units='degrees_north') ilat2 = cmor.axis(table_entry='latitude', coord_vals=lats2, cell_bounds=blats2,
import cmor, numpy error_flag = cmor.setup(inpath='Tables', netcdf_file_action=cmor.CMOR_REPLACE) error_flag = cmor.dataset_json("Test/common_user_input.json") n_lev = 40 zlevs = 480. * numpy.arange(0, n_lev) + 240. zbnds = numpy.zeros((n_lev, 2)) zbnds[:, 0] = zlevs - 240. zbnds[:, 1] = zlevs + 240. # creates 1 degree grid cmor.load_table("CMIP6_cf3hr.json") ialt40 = cmor.axis("alt40", units="m", coord_vals=zlevs, cell_bounds=zbnds) itm = cmor.axis("time1", units="months since 2000") iloc = cmor.axis("location", units="1", coord_vals=numpy.arange(2)) igrid = cmor.grid(axis_ids=[iloc, itm]) print igrid ilat = cmor.time_varying_grid_coordinate(igrid, table_entry='latitude', units='degrees_north') ilon = cmor.time_varying_grid_coordinate(igrid, table_entry='longitude', units='degrees_east')
def main(): cmor.setup(inpath='/git/cmip5-cmor-tables/Tables', netcdf_file_action = cmor.CMOR_REPLACE_3) cmor.dataset('pre-industrial control', 'ukmo', 'HadCM3', 'noleap', institute_id = 'ukmo', model_id = 'HadCM3', history = 'some global history', forcing = 'N/A', parent_experiment_id = 'N/A', parent_experiment_rip = 'N/A', branch_time = 0, contact = 'brian clough') table = 'CMIP5_Oclim' cmor.load_table(table) axes = [ {'table_entry': 'time2', 'units': 'days since 1861', 'coord_vals': [48925.5, 48955, 48984.5, 49015, 49045.5, 49076, 49106.5, 49137.5, 49168, 49198.5, 49229, 49259.5 ], 'cell_bounds': [ [ 45625, 52591], [ 45656, 52619,], [ 45684, 52650,], [ 45715, 52680,], [ 45745, 52711,], [45776, 52741,], [45806, 52772,], [45837, 52803,], [45868, 52833,], [45898, 52864,], [45929, 52894,], [45959, 52925]], }, {'table_entry': 'depth_coord', 'units': 'm', 'coord_vals': [ 500,1000.], 'cell_bounds': [ 0.,750.,1200.]}, {'table_entry': 'latitude', 'units': 'degrees_north', 'coord_vals': [0], 'cell_bounds': [-1, 1]}, {'table_entry': 'longitude', 'units': 'degrees_east', 'coord_vals': [90], 'cell_bounds': [89, 91]}, ] axis_ids = list() for axis in axes: print 'doing:',axis axis_id = cmor.axis(**axis) axis_ids.append(axis_id) for var, units, value in (('tnpeot', 'W m-2', 274),): values = numpy.array([value,]*len(axes[1]['coord_vals']), numpy.float32) varid = cmor.variable(var, units, axis_ids, history = 'variable history', missing_value = -99 ) for i in range(12): cmor.write(varid, values, ntimes_passed=1) cmor.close()
def testCMIP6(self): # ------------------------------------------------------ # Copy stdout and stderr file descriptor for cmor output # ------------------------------------------------------ newstdout = os.dup(1) newstderr = os.dup(2) # -------------- # Create tmpfile # -------------- tmpfile = tempfile.mkstemp() os.dup2(tmpfile[0], 1) os.dup2(tmpfile[0], 2) os.close(tmpfile[0]) # ------------------------------------------- # Try to call cmor with a bad institution_ID # ------------------------------------------- try: cmor.setup(inpath='Tables', netcdf_file_action=cmor.CMOR_REPLACE) cmor.dataset_json("Test/CMOR_input_example.json") cmor.set_cur_dataset_attribute("experiment_id", "ssp434") cmor.set_cur_dataset_attribute("parent_experiment_id", "historical") cmor.set_cur_dataset_attribute("parent_activity_id", "CMIP") cmor.set_cur_dataset_attribute("activity_id", "ScenarioMIP") cmor.set_cur_dataset_attribute("source_type", "AOGCM") cmor.set_cur_dataset_attribute("sub_experiment_id", "none") cmor.set_cur_dataset_attribute("parent_variant_label", "r11i123p4556f333") cmor.set_cur_dataset_attribute("parent_source_id", "child") cmor.set_cur_dataset_attribute("parent_mip_era", "CMIP6") # ------------------------------------------ # load Omon table and create masso variable # ------------------------------------------ cmor.load_table("CMIP6_Omon.json") cmor.load_table("CMIP6_Amon.json") cmor.load_table("CMIP6_6hrPlev.json") cmor.load_table("CMIP6_6hrPlevPt.json") cmor.load_table("CMIP6_AERday.json") cmor.load_table("CMIP6_AERfx.json") cmor.load_table("CMIP6_AERhr.json") cmor.load_table("CMIP6_AERmon.json") cmor.load_table("CMIP6_AERmonZ.json") cmor.load_table("CMIP6_Amon.json") cmor.load_table("CMIP6_CFday.json") cmor.load_table("CMIP6_CFmon.json") cmor.load_table("CMIP6_CFsubhr.json") cmor.load_table("CMIP6_CFsubhrOff.json") cmor.load_table("CMIP6_day.json") cmor.load_table("CMIP6_E1hrClimMon.json") cmor.load_table("CMIP6_E1hr.json") cmor.load_table("CMIP6_E3hr.json") cmor.load_table("CMIP6_E6hrZ.json") cmor.load_table("CMIP6_EdayZ.json") cmor.load_table("CMIP6_Efx.json") cmor.load_table("CMIP6_EmonZ.json") cmor.load_table("CMIP6_Esubhr.json") cmor.load_table("CMIP6_Eyr.json") cmor.load_table("CMIP6_fx.json") cmor.load_table("CMIP6_grids.json") except BaseException: pass os.dup2(newstdout, 1) os.dup2(newstderr, 2) sys.stdout = os.fdopen(newstdout, 'w', 0) sys.stderr = os.fdopen(newstderr, 'w', 0) f = open(tmpfile[1], 'r') lines = f.readlines() for line in lines: if line.find('Error:') != -1: self.assertIn('30', line.strip()) break f.close() os.unlink(tmpfile[1])
import cdms2 except: print 'This test requires cdms2 for I/O' sys.exit() import cmor, numpy f = cdms2.open(os.path.join(sys.prefix, 'sample_data/clt.nc')) pth = os.path.split(os.path.realpath(os.curdir)) if pth[-1] == 'Test': ipth = opth = '.' else: ipth = opth = 'Test' cmor.setup(inpath=ipth, set_verbosity=cmor.CMOR_NORMAL, netcdf_file_action=cmor.CMOR_REPLACE) cmor.dataset(outpath=opth, experiment_id="historical", institution="PCMDI", source="GICCM1 2002", calendar="standard", contact="Tim Lincecum", institute_id="PCMDI", model_id="GICCM1", forcing="Nat", parent_experiment_id="N/A", parent_experiment_rip="N/A", branch_time=0.)
import cmor cmor.setup(inpath='Tables', netcdf_file_action=cmor.CMOR_REPLACE_4) cmor.dataset_json("Test/common_user_input.json") table = 'CMIP6_Amon.json' cmor.load_table(table) itime = cmor.axis(table_entry='time', units='days since 2000-01-01 00:00:00', coord_vals=[ 15, ], cell_bounds=[0, 30]) ilat = cmor.axis(table_entry='latitude', units='degrees_north', coord_vals=[0], cell_bounds=[-1, 1]) ilon = cmor.axis(table_entry='longitude', units='degrees_east', coord_vals=[90], cell_bounds=[89, 91]) axis_ids = [itime, ilat, ilon] varid = cmor.variable('ts', 'K', axis_ids) cmor.write(varid, [273]) outfile = cmor.close(varid, file_name=True) print "File written: ", outfile cmor.close()
def handle(infiles, tables, user_input_path, **kwargs): logger = logging.getLogger() msg = '{}: Starting'.format(VAR_NAME) logger.info(msg) logdir = kwargs.get('logdir') serial = kwargs.get('serial') # check that we have some input files for every variable zerofiles = False for variable in RAW_VARIABLES: if len(infiles[variable]) == 0: msg = '{}: Unable to find input files for {}'.format( VAR_NAME, variable) print_message(msg) logging.error(msg) zerofiles = True if zerofiles: return None # Create the logging directory and setup cmor if logdir: logpath = logdir else: outpath, _ = os.path.split(logger.__dict__['handlers'][0].baseFilename) logpath = os.path.join(outpath, 'cmor_logs') os.makedirs(logpath, exist_ok=True) logfile = os.path.join(logpath, VAR_NAME + '.log') cmor.setup( inpath=tables, netcdf_file_action=cmor.CMOR_REPLACE, logfile=logfile) cmor.dataset_json(str(user_input_path)) cmor.load_table(str(TABLE)) msg = '{}: CMOR setup complete'.format(VAR_NAME) logging.info(msg) # extract data from the input file msg = 'areacella: loading area' logger.info(msg) filename = infiles['area'][0] if not os.path.exists(filename): raise IOError("File not found: {}".format(filename)) f = cdms2.open(filename) # load the data for each variable variable_data = f('area') if not variable_data.any(): raise IOError("Variable data not found: {}".format(variable)) # load the lon and lat info & bounds data = { 'lat': variable_data.getLatitude(), 'lon': variable_data.getLongitude(), 'lat_bnds': f('lat_bnds'), 'lon_bnds': f('lon_bnds'), 'area': f('area') } msg = '{name}: loading axes'.format(name=VAR_NAME) logger.info(msg) axes = [{ str('table_entry'): str('latitude'), str('units'): data['lat'].units, str('coord_vals'): data['lat'][:], str('cell_bounds'): data['lat_bnds'][:] }, { str('table_entry'): str('longitude'), str('units'): data['lon'].units, str('coord_vals'): data['lon'][:], str('cell_bounds'): data['lon_bnds'][:] }] msg = 'areacella: running CMOR' logging.info(msg) axis_ids = list() for axis in axes: axis_id = cmor.axis(**axis) axis_ids.append(axis_id) varid = cmor.variable(VAR_NAME, VAR_UNITS, axis_ids) if serial: myMessage = progressbar.DynamicMessage('running') myMessage.__call__ = my_dynamic_message widgets = [ progressbar.DynamicMessage('running'), ' [', progressbar.Timer(), '] ', progressbar.Bar(), ' (', progressbar.ETA(), ') ' ] progressbar.DynamicMessage.__call__ = my_dynamic_message pbar = progressbar.ProgressBar( maxval=1, widgets=widgets) pbar.start() r = 6.37122e6 outdata = data['area'] * pow(r, 2) cmor.write( varid, outdata) if serial: pbar.update(1, running=msg) pbar.finish() msg = '{}: write complete, closing'.format(VAR_NAME) logger.debug(msg) cmor.close() msg = '{}: file close complete'.format(VAR_NAME) logger.debug(msg) return 'areacella'