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/common_user_input.json") cmor.set_cur_dataset_attribute("source_type", "AOGCM ISM CHEM AER") # ------------------------------------------ # 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: 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: # ------------------------------------------- # 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): cmor.setup(inpath='Tables', netcdf_file_action=cmor.CMOR_REPLACE) 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]) 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') 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 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 testCMIP6(self): try: cmor.setup(inpath='Tables', netcdf_file_action=cmor.CMOR_REPLACE) cmor.dataset_json("Test/CMOR_input_example.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]) 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) self.assertIn('d', testOK)
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 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 cmor_define_and_write(values, axes): table = 'CMIP6_CFsubhr.json' cmor.load_table(table) axis_ids = list() for axis in axes: axis_id = cmor.axis(**axis) axis_ids.append(axis_id) igrid = cmor.grid([axis_ids[1]], [0.], [0.]) cmor.zfactor(axis_ids[2], 'b', axis_ids=[axis_ids[2]], zfactor_values=numpy.array(range(2), dtype=numpy.float64), zfactor_bounds=[[x - 0.5, x + 0.5] for x in range(2)]) cmor.zfactor(axis_ids[2], 'orog', 'm', axis_ids=[igrid], zfactor_values=[0.]) ids_for_var = [axis_ids[0], igrid, axis_ids[2]] varid = cmor.variable( 'tnhus', 's-1', ids_for_var, history='variable history', missing_value=-99, ) for time in [x * 1800. / 86400 for x in range(48)]: cmor.write(varid, values, time_vals=[time]) return varid
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): 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 testCMIP6(self): # ------------------------------------------- # Try to call cmor with a bad institution_ID # ------------------------------------------- global testOK error_flag = cmor.setup(inpath='Tables', netcdf_file_action=cmor.CMOR_REPLACE) error_flag = cmor.dataset_json("Test/test_python_CMIP6_CV_longrealizationindex.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) try: for i in range(0,1): a = cmor.write(ivar,data[i:i]) except: os.dup2(newstdout,1) os.dup2(newstderr,2) sys.stdout = os.fdopen(newstdout, 'w', 0) sys.stderr = os.fdopen(newstderr, 'w', 0) time.sleep(.1) # ------------------------------------------ # Check error after signal handler is back # ------------------------------------------ self.assertIn("\"Denis1209374928349823498274987234987\"", testOK)
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 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): # ------------------------------------------- # Try to call cmor with a bad institution_ID # ------------------------------------------- global testOK error_flag = cmor.setup(inpath='Tables', netcdf_file_action=cmor.CMOR_REPLACE) error_flag = cmor.dataset_json( "Test/test_python_CMIP6_CV_badgridresolution.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): a = cmor.write(ivar, data[i:i]) os.dup2(newstdout, 1) os.dup2(newstderr, 2) sys.stdout = os.fdopen(newstdout, 'w', 0) sys.stderr = os.fdopen(newstderr, 'w', 0) time.sleep(.1) # ------------------------------------------ # Check error after signal handler is back # ------------------------------------------ self.assertIn("\"335 km\"", testOK)
def define_write_landcoverfrac(): cmor.load_table('Tables/CMIP6_Lmon.json') 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] }, { 'table_entry': 'vegtype', 'coord_vals': ['landcover'], 'units': '1', }, ] axis_ids = define_axes(axes) values = numpy.array([2.], numpy.float32) values = numpy.reshape(values, (1, 1, 1, 1)) define_write_var(axis_ids, 'landCoverFrac', '1', values)
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/common_user_input.json") cmor.set_cur_dataset_attribute("nominal_resolution", "2x2") # ------------------------------------------ # 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: 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) # ------------------------------------------ # Check error after signal handler is back # ------------------------------------------ testOK = self.getAssertTest() self.assertIn("\"2x2\"", testOK)
def testCMIP6(self): ''' This test will not fail we veirfy the attribute further_info_url''' # ------------------------------------------- # Try to call cmor with a bad institution_ID # ------------------------------------------- global testOK error_flag = cmor.setup(inpath='Tables', netcdf_file_action=cmor.CMOR_REPLACE) error_flag = cmor.dataset_json( "Test/test_python_CMIP6_CV_nomipera.json") # ------------------------------------------ # load Omon table and create masso variable # ------------------------------------------ cmor.load_table("CMIP6_badOmon.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): a = cmor.write(ivar, data[i:i]) file = cmor.close() os.dup2(newstdout, 1) os.dup2(newstderr, 2) sys.stdout = os.fdopen(newstdout, 'w', 0) sys.stderr = os.fdopen(newstderr, 'w', 0) self.assertIn("mip_era", testOK)
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 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): ''' This test will not fail we veirfy the attribute further_info_url''' # ------------------------------------------- # Try to call cmor with a bad institution_ID # ------------------------------------------- global testOK error_flag = cmor.setup(inpath='Tables', netcdf_file_action=cmor.CMOR_REPLACE) error_flag = cmor.dataset_json("Test/test_python_CMIP6_CV_nomipera.json") # ------------------------------------------ # load Omon table and create masso variable # ------------------------------------------ cmor.load_table("CMIP6_badOmon.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): a = cmor.write(ivar,data[i:i]) file = cmor.close() os.dup2(newstdout,1) os.dup2(newstderr,2) sys.stdout = os.fdopen(newstdout, 'w', 0) sys.stderr = os.fdopen(newstderr, 'w', 0) self.assertIn("mip_era", testOK);
def cmor_define_and_write(values, axes): table = 'CMIP5_day' cmor.load_table(table) axis_ids = list() for axis in axes: axis_ids.append(cmor.axis(**axis)) varid = cmor.variable('rlut', 'W m-2', axis_ids, history='variable history', missing_value=-99, positive='up' ) for time in (15, 16, 17): cmor.write( varid, values, time_vals=[time], time_bnds=[ time - 0.45, time + 0.45])
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: # ------------------------------------------- # 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/common_user_input.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: # ------------------------------------------- # 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 prep_var(self, var, units): # creates 1 degree grid dlat = 180 / nlat dlon = 360. / nlon alats = numpy.arange(-90 + dlat / 2., 90, dlat) bnds_lat = numpy.arange(-90, 90 + dlat, dlat) alons = numpy.arange(0 + dlon / 2., 360., dlon) - 180. bnds_lon = numpy.arange(0, 360. + dlon, dlon) - 180. cmor.load_table("Tables/CMIP6_Omon.json") # cmor.load_table("Test/IPCC_table_A1") ilat = cmor.axis(table_entry='latitude', units='degrees_north', length=nlat, coord_vals=alats, cell_bounds=bnds_lat) ilon = cmor.axis(table_entry='longitude', length=nlon, units='degrees_east', coord_vals=alons, cell_bounds=bnds_lon) itim = cmor.axis(table_entry='time', units='days since 2010-1-1') ivar1 = cmor.variable(var, axis_ids=[itim, ilat, ilon], units=units, missing_value=0.) return ivar1
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 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 define_write_landcoverfrac(): cmor.load_table('CMIP5_Lmon') 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]}, {'table_entry': 'vegtype', 'coord_vals': ['landcover'], 'units': '1', }, ] axis_ids = define_axes(axes) values = numpy.array([2.], numpy.float32) values = numpy.reshape(values, (1, 1, 1, 1)) define_write_var(axis_ids, 'landCoverFrac', '1', values)
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 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 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 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 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 # ------------------------------------------- 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 cmor_define_and_write(values, axes): table = 'CMIP5_cfSites' cmor.load_table(table) axis_ids = list() for axis in axes: axis_id = cmor.axis(**axis) axis_ids.append(axis_id) igrid = cmor.grid([axis_ids[1]], [0.], [0.]) cmor.zfactor(axis_ids[2], 'b', axis_ids = [axis_ids[2]], zfactor_values = range(2), zfactor_bounds = [[x-0.5, x+0.5] for x in range(2)]) cmor.zfactor(axis_ids[2], 'orog', 'm', axis_ids = [igrid], zfactor_values = [0]) ids_for_var = [axis_ids[0], igrid, axis_ids[2]] varid = cmor.variable('tnhus', 's-1', ids_for_var, history = 'variable history', missing_value = -99, ) for time in [x * 1800./ 86400 for x in range(48)]: time += 1./3600./24. tr = cdtime.reltime(time,axes[0]["units"]) print "Writing: %.03f" % time,"|",tr.tocomp(cdtime.Calendar360),"|",tr.tocomp() cmor.write(varid, values, time_vals = [time]) return varid
def testCMIP6(self): ''' This test will not fail we veirfy the attribute further_info_url''' # ------------------------------------------- # Try to call cmor with a bad institution_ID # ------------------------------------------- global testOK error_flag = cmor.setup(inpath='Tables', netcdf_file_action=cmor.CMOR_REPLACE) error_flag = 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.]), 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]) file = cmor.close() print file os.dup2(newstdout,1) os.dup2(newstderr,2) sys.stdout = os.fdopen(newstdout, 'w', 0) sys.stderr = os.fdopen(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.s1968.r1i1p1f1", 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 test_Institution(self): # ------------------------------------------- # Try to call cmor with a bad institution_ID # ------------------------------------------- global testOK error_flag = cmor.setup(inpath='Tables', netcdf_file_action=cmor.CMOR_REPLACE) error_flag = cmor.dataset_json("Test/test_python_CMIP6_CV_badinstitutionIDNotSet.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): a = cmor.write(ivar,data[i:i]) os.dup2(newstdout,1) os.dup2(newstderr,2) sys.stdout = os.fdopen(newstdout, 'w', 0) sys.stderr = os.fdopen(newstderr, 'w', 0) time.sleep(.1) self.assertIn("Control Vocabulary file", testOK)
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 define_write_clisccp(): cmor.load_table('CMIP5_cfMon') 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]}, {'table_entry': 'plev7', 'coord_vals': [90000., 74000., 62000., 50000., 37500., 24500., 9000.], 'cell_bounds': [[100000., 80000.], [80000., 68000.], [68000., 56000.], [56000., 44000.], [44000., 31000.], [31000., 18000.], [18000., 0.]], 'units': 'Pa', }, {'table_entry': 'tau', 'coord_vals': [0.15, 0.8, 2.45, 6.5, 16.2, 41.5, 100.], 'cell_bounds':[ [0.0, 0.3], [0.3, 1.3], [1.3, 3.6], [3.6, 9.4], [9.4, 23.0], [23.0, 60.0], [60.0, 100000]], 'units': '1'} ] axis_ids = define_axes(axes) values = numpy.array([0.0004,]*49, numpy.float32) values = numpy.reshape(values, (1, 1, 1, 7, 7)) define_write_var(axis_ids, 'clisccp', '1', values)
def cmor_define_and_write(values, axes): table = 'CMIP6_cfSites.json' cmor.load_table(table) axis_ids = list() for axis in axes: axis_id = cmor.axis(**axis) axis_ids.append(axis_id) igrid = cmor.grid([axis_ids[1]], [0.], [0.]) cmor.zfactor(axis_ids[2], 'b', axis_ids = [axis_ids[2]], zfactor_values = range(2), zfactor_bounds = [[x-0.5, x+0.5] for x in range(2)]) cmor.zfactor(axis_ids[2], 'orog', 'm', axis_ids = [igrid], zfactor_values = [0]) ids_for_var = [axis_ids[0], igrid, axis_ids[2]] varid = cmor.variable('tnhus', 's-1', ids_for_var, history = 'variable history', missing_value = -99, ) for time in [x * 1800./ 86400 for x in range(48)]: cmor.write(varid, values, time_vals = [time]) return 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 testParentExperimentID(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/common_user_input.json") cmor.set_cur_dataset_attribute("parent_experiment_id", "historical") cmor.set_cur_dataset_attribute("parent_activity_id", "DCPP") cmor.set_cur_dataset_attribute("experiment_id", "dcppC-forecast-addAgung") cmor.set_cur_dataset_attribute("activity_id", "DCPP") cmor.set_cur_dataset_attribute("source_type", "AOGCM AER") cmor.set_cur_dataset_attribute("sub_experiment_id", "s2014") # ------------------------------------------ # 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 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('parent_experiment_id', line.strip()) break f.close() os.unlink(tmpfile[1])
def path_test(): cmor.setup(inpath='TestTables', netcdf_file_action=cmor.CMOR_REPLACE) cmor.dataset_json("Test/CMOR_input_example.json") table = 'CMIP6_Amon.json' cmor.load_table(table) axes = [ { 'table_entry': 'time2', 'units': 'months since 2000-01-01 00:00:00', # 'coord_vals': times, # 'cell_bounds': times_bnds, }, { 'table_entry': 'plev19', 'units': 'Pa', 'coord_vals': [ 100000., 92500., 85000., 70000., 60000., 50000., 40000., 30000., 25000., 20000., 15000., 10000., 7000., 5000., 3000., 2000., 1000., 500, 100 ] }, { '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('co2Clim', '1.e-6', axis_ids) import numpy data = numpy.array([3, 4, 5]) data.resize((3, 19, 1, 1)) # cmor.write(varid, data) # for i in range(len(data)): # cmor.write(varid, data[i], time_vals=times[i], time_bnds=times_bnds[i]) cmor.write(varid, data, time_vals=[12, 15, 18], time_bnds=[[10, 1234], [12, 1125], [15, 1128]]) path = cmor.close(varid, file_name=True) print(path)
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='bob') table = 'CMIP5_Amon' 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([1.], numpy.float32) + 200 axis_ids = list() for axis in axes: axis_id = cmor.axis(**axis) axis_ids.append(axis_id) for var, units, positive in ( ('ts', 'K', ''), ('rsut', 'W m-2', 'up'), ('rlut', 'W m-2', 'down'), ): varid = cmor.variable(var, units, axis_ids, history='variable history', missing_value=-99, positive=positive) cmor.write(varid, values, time_vals=[15], time_bnds=[[0, 30]]) cmor.close()
def main(): cmor.setup(inpath='Tables', netcdf_file_action=cmor.CMOR_REPLACE_3) cmor.dataset_json("Test/test_python_jamie_9.json") table = 'CMIP6_Amon.json' cmor.load_table(table) axes = [ { 'table_entry': 'time', 'units': 'days since 2000-01-01 00:00:00', }, { 'table_entry': 'plev19', 'units': 'Pa', 'coord_vals': map( float, '100000. 92500. 85000. 70000. 60000. 50000. 40000. 30000. 25000. 20000. 15000. 10000. 7000. 5000. 3000. 2000. 1000. 500. 100.' .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 main(): cmor.setup(inpath='Tables', netcdf_file_action=cmor.CMOR_REPLACE) cmor.dataset_json("Test/CMOR_input_example.json") table = 'CMIP6_6hrLev.json' 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], time_bnds=[3., 12.]) print 'cmor.write call complete' cmor.close()
def create_grids(tasks): global grid_ids_ cmor.load_table(table_root_ + "_grids.json") task_groups = cmor_utils.group(tasks, lambda t: getattr(t, cmor_task.output_path_key, None)) for filename, task_list in task_groups.iteritems(): if filename is not None: grid = read_grid(filename) grid_id = write_grid(grid) grid_ids_[grid.name] = grid_id for task in task_list: setattr(task, "grid_id", grid_id)
def main(): cmor.setup(inpath='Tables', netcdf_file_action=cmor.CMOR_REPLACE_3) cmor.dataset_json("Test/CMOR_input_example.json") table = 'CMIP6_Oclim.json' 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, 31], [31, 62]] }, { '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()
def test_mode(mode, i, suffix=''): cmor.setup(inpath='Tables', netcdf_file_action=mode) cmor.dataset_json("Test/common_user_input.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 tstCMIP6(self): nlat = 10 dlat = 180. / nlat nlon = 20 dlon = 360. / nlon nlev = 5 ntimes = 5 lats = numpy.arange(90 - dlat / 2., -90, -dlat) blats = numpy.arange(90, -90 - dlat, -dlat) lons = numpy.arange(0 + dlon / 2., 360., dlon) blons = numpy.arange(0, 360. + dlon, dlon) # ------------------------------------------- # 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 masscello 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.])) ilat = cmor.axis( table_entry='latitude', coord_vals=lats, cell_bounds=blats, units='degrees_north') ilon = cmor.axis( table_entry='longitude', coord_vals=lons, cell_bounds=blons, units='degrees_east') ilev = cmor.axis(table_entry='depth_coord', length=5, cell_bounds=numpy.arange(0, 12000, 2000), coord_vals=numpy.arange(0, 10000, 2000), units="m") ivar = cmor.variable( table_entry="masscello", axis_ids=[ itime, ilev, ilat, ilon, ], units='kg/m2') data = numpy.random.random((ntimes, nlev, nlat, nlon)) * 100. cmor.write(ivar, data) self.delete_files += [cmor.close(ivar, True)] cmor.close() f = cdms2.open(cmor.get_final_filename(), "r") a = f.getglobal("external_variables") self.assertEqual("areacello volcello", a)
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/common_user_input.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]) cmor.close() except BaseException: raise 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() count = 0 # We need 8 attributes to be replaced for line in lines: if line.find('replaced') != -1: count = count + 1 # self.assertEqual(count, 9) f.close() os.unlink(tmpfile[1])
def initialize(path, expname, tabledir, prefix, refdate): """initialize the cmorization for TM5 Description: Input variables: path, String: path to TM5 files expname, string: name of the experiment tabledir, string: path to tables prefix, string: table prefix Returns: boolean: success """ global log, tm5_files_, exp_name_, table_root_, ref_date_, plev39_, plev19_, areacella_, path_ exp_name_ = expname path_ = path table_root_ = os.path.join(tabledir, prefix) # select all TM5 files with expname from path tm5_files_ = cmor_utils.find_tm5_output(path, expname) if len(tm5_files_) == 0: log.error('no TM5 varibles found, exiting!') exit() areacella_file = cmor_utils.find_tm5_output(path, expname, 'areacella', 'fx') if len(areacella_file) == 0: log.error('Areacella not found!') exit() else: areacella_ = netCDF4.Dataset(areacella_file[0], 'r').variables['areacella'][:] cal = None ref_date_ = refdate # read pressure level definitions from CMIP6_coordante file # and save globally coordfile = os.path.join(tabledir, prefix + "_coordinate.json") if os.path.exists(coordfile): with open(coordfile) as f: data = json.loads(f.read()) axis_entries = data.get("axis_entry", {}) axis_entries = {k.lower(): v for k, v in axis_entries.iteritems()} plev19 = numpy.array([ numpy.float(value) for value in axis_entries['plev19']['requested'] ]) plev19_ = plev19 plev39 = numpy.array([ numpy.float(value) for value in axis_entries['plev39']['requested'] ]) plev39_ = plev39 else: log.warning('Using default pressure level definitions') cmor.load_table(table_root_ + "_grids.json") return True
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/CMOR_input_example.json") tables = [] a = cmor.load_table("CMIP6_Omon.json") tables.append(a) tables.append(cmor.load_table("CMIP6_Amon.json")) return
def main(): cmor.setup(inpath='Tables', netcdf_file_action=cmor.CMOR_REPLACE) cmor.dataset('historical', 'ukmo', 'HadCM3', '360_day', institute_id='ukmo', forcing='SO', model_id='HadCM3', contact="Dusty Baker (even though he was a Dodgers", parent_experiment_rip="r1i3p2", parent_experiment_id="lgm", branch_time=0) table = 'CMIP5_Amon' 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) for var, units, val in (('ts', 'K', 278), ('ps', 'hPa', 974.2)): varid = cmor.variable( var, units, axis_ids, ) values = numpy.array([val], numpy.float32) cmor.write(varid, values, time_vals=[15], time_bnds=[[0, 30]]) cmor.close()
def test_Institution(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: error_flag = cmor.setup(inpath='Tables', netcdf_file_action=cmor.CMOR_REPLACE) error_flag = cmor.dataset_json( "Test/test_python_CMIP6_CV_badinstitution.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]) error_flag = cmor.close() finally: 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('Warning:') != -1: self.assertIn('bad institution', line.strip()) break f.close() os.unlink(tmpfile[1])
def testCMIP6(self): ''' This test will not fail we veirfy the attribute further_info_url''' # ------------------------------------------- # Try to call cmor with a bad institution_ID # ------------------------------------------- global testOK nlat = 10 dlat = 180. / nlat nlon = 20 dlon = 360. / nlon error_flag = cmor.setup(inpath='Tables', netcdf_file_action=cmor.CMOR_REPLACE) error_flag = cmor.dataset_json( "Test/test_python_CMIP6_CV_fxtable.json") cmor.load_table("CMIP6_fx.json") 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) 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') ilon = cmor.axis(table_entry='longitude', coord_vals=lons, cell_bounds=blons, units='degrees_east') # ------------------------------------------ # load Omon table and create masso variable # ------------------------------------------ ivar = cmor.variable(table_entry="areacello", axis_ids=[ilat, ilon], units='m2') a = cmor.write(ivar, data) file = cmor.close() os.dup2(newstdout, 1) os.dup2(newstderr, 2) sys.stdout = os.fdopen(newstdout, 'w', 0) sys.stderr = os.fdopen(newstderr, 'w', 0)
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.]}, {'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', 'BLABLABLA') cmor.write(varid, values, time_vals = [15], time_bnds = [ [0,30] ]) cmor.close()
def testNotEnoughData(self): cmor.setup(inpath='Tables', netcdf_file_action=cmor.CMOR_REPLACE) cmor.dataset_json("Test/CMOR_input_example.json") table = 'CMIP6_Omon.json' cmor.load_table(table) axes = [{'table_entry': 'time', 'units': 'days since 1850-01-01 00:00:00', 'coord_vals': [15.5, 45, ], 'cell_bounds':[[0, 31], [31, 62]] }, {'table_entry': 'depth_coord_half', '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: axis_id = cmor.axis(**axis) axis_ids.append(axis_id) for var, units, value in (('zhalfo', 'm', 274.),): values = numpy.ones([len(x['coord_vals']) for x in axes]) * value values = values.astype("f") varid = cmor.variable(var, units, axis_ids, history='variable history', missing_value=-99 ) try: cmor.write(varid, values, ntimes_passed=3) except Exception as inst: self.assertEqual(str(inst), "not enough data is being passed " "for the number of times passed") else: raise Exception("cmor.write did not throw expected exception") cmor.close()