1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
# If this example is not executed from the directory containing the
# CMOR code, please first complete the following steps:
#
# 1. In any directory, create 'Tables/', 'Test/' and 'CMIP6/' directories.
#
# 2. Download
# https://github.com/PCMDI/cmor/blob/master/TestTables/CMIP6_Omon.json
# and https://github.com/PCMDI/cmor/blob/master/TestTables/CMIP6_CV.json
# to the 'Tables/' directory.
#
# 3. Download
# https://github.com/PCMDI/cmor/blob/master/Test/<filename>.json
# to the 'Test/' directory.
import cmor
import numpy
import unittest
import sys
import os
import tempfile
import base_CMIP6_CV
class TestCase(base_CMIP6_CV.BaseCVsTest):
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")
# ------------------------------------------
# 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:
raise
f = open(self.tmpfile, 'r')
lines = f.readlines()
count = 0
# We need 6 attributes to be replaced
for line in lines:
if line.find('replaced') != -1:
count = count + 1
self.assertEqual(count, 6)
f.close()
if __name__ == '__main__':
unittest.main()
|