Esempio n. 1
0
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
Esempio n. 2
0
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
Esempio n. 3
0
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=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
Esempio n. 4
0
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()
Esempio n. 5
0
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
Esempio n. 6
0
def create_hybrid_level_axis(task):
    pref = 80000  # TODO: Move reference pressure level to model config
    path = getattr(task, cmor_task.output_path_key)
    ds = None
    try:
        ds = netCDF4.Dataset(path)
        am = ds.variables["hyam"]
        aunit = getattr(am, "units")
        bm = ds.variables["hybm"]
        bunit = getattr(bm, "units")
        hcm = am[:] / pref + bm[:]
        n = hcm.shape[0]
        ai = ds.variables["hyai"]
        abnds = numpy.empty([n, 2])
        abnds[:, 0] = ai[0:n]
        abnds[:, 1] = ai[1:n + 1]
        bi = ds.variables["hybi"]
        bbnds = numpy.empty([n, 2])
        bbnds[:, 0] = bi[0:n]
        bbnds[:, 1] = bi[1:n + 1]
        hcbnds = abnds / pref + bbnds
        axisid = cmor.axis(table_entry="alternate_hybrid_sigma",
                           coord_vals=hcm,
                           cell_bounds=hcbnds,
                           units="1")
        cmor.zfactor(zaxis_id=axisid,
                     zfactor_name="ap",
                     units=str(aunit),
                     axis_ids=[axisid],
                     zfactor_values=am[:],
                     zfactor_bounds=abnds)
        cmor.zfactor(zaxis_id=axisid,
                     zfactor_name="b",
                     units=str(bunit),
                     axis_ids=[axisid],
                     zfactor_values=bm[:],
                     zfactor_bounds=bbnds)
        axes = []
        if hasattr(task, "grid_id"):
            task_grid_id = getattr(task, "grid_id")
            if isinstance(task_grid_id, tuple):
                axes.extend(task_grid_id)
            else:
                axes.append(task_grid_id)
        axes.append(getattr(task, "t_axis_id"))
        zfactor_name = "ps"
        if "time1" in getattr(task.target, cmor_target.dims_key, []):
            zfactor_name = "ps1"
        elif "time2" in getattr(task.target, cmor_target.dims_key, []):
            zfactor_name = "ps2"
        storewith = cmor.zfactor(zaxis_id=axisid,
                                 zfactor_name=zfactor_name,
                                 axis_ids=axes,
                                 units="Pa")
        return axisid, storewith
    finally:
        if ds is not None:
            ds.close()
Esempio n. 7
0
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()
Esempio n. 8
0
    def testCMIP6(self):
        # -------------------------------------------
        # Run CMOR using unicode strings
        # -------------------------------------------
        try:
            cmor.setup(inpath=u'Tables', netcdf_file_action=cmor.CMOR_REPLACE, logfile=self.tmpfile)
            cmor.dataset_json(u"Test/CMOR_input_example.json")
            cmor.load_table(u"CMIP6_CFsubhr.json")

            axes = [{u'table_entry': u'time1',
                    u'units': u'days since 2000-01-01 00:00:00',
                    },
                    {u'table_entry': u'site',
                    u'units': u'',
                    u'coord_vals': [0]},
                    {u'table_entry': u'hybrid_height',
                    u'units': u'm',
                    u'coord_vals': numpy.array(range(2), dtype=numpy.float64),
                    u'cell_bounds': [[x, x + 1] for x in range(2)],
                    },
                    ]

            values = numpy.array([0.5, 0.5], numpy.float32)

            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], u'b', axis_ids=[axis_ids[2]],
                        zfactor_values=numpy.array(range(2), dtype=numpy.float64), 
                        zfactor_bounds=[[x, x + 1] for x in range(2)])

            cmor.zfactor(axis_ids[2], u'orog', u'm', axis_ids=[igrid],
                        zfactor_values=[0.] )

            ids_for_var = [axis_ids[0], igrid, axis_ids[2]]
            varid = cmor.variable(u'tnhus',
                                u's-1',
                                ids_for_var,
                                history=u'variable history',
                                missing_value=-99,
                                )

            for time in [x * 1800. / 86400 for x in range(48)]:
                cmor.write(varid, values, time_vals=[time])
            self.delete_files += [cmor.close(varid, True)]

            cmor.close()
        except BaseException:
            raise
Esempio n. 9
0
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()
Esempio n. 10
0
def create_hybrid_level_axis(task):
    pref = 80000  # TODO: Move reference pressure level to model config
    path = getattr(task, cmor_task.output_path_key)
    ds = None
    try:
        ds = netCDF4.Dataset(path)
        am = ds.variables["hyam"]
        aunit = getattr(am, "units")
        bm = ds.variables["hybm"]
        bunit = getattr(bm, "units")
        hcm = am[:] / pref + bm[:]
        n = hcm.shape[0]
        ai = ds.variables["hyai"]
        abnds = numpy.empty([n, 2])
        abnds[:, 0] = ai[0:n]
        abnds[:, 1] = ai[1:n + 1]
        bi = ds.variables["hybi"]
        bbnds = numpy.empty([n, 2])
        bbnds[:, 0] = bi[0:n]
        bbnds[:, 1] = bi[1:n + 1]
        hcbnds = abnds / pref + bbnds
        axisid = cmor.axis(table_entry="alternate_hybrid_sigma",
                           coord_vals=hcm,
                           cell_bounds=hcbnds,
                           units="1")
        cmor.zfactor(zaxis_id=axisid,
                     zfactor_name="ap",
                     units=str(aunit),
                     axis_ids=[axisid],
                     zfactor_values=am[:],
                     zfactor_bounds=abnds)
        cmor.zfactor(zaxis_id=axisid,
                     zfactor_name="b",
                     units=str(bunit),
                     axis_ids=[axisid],
                     zfactor_values=bm[:],
                     zfactor_bounds=bbnds)
        storewith = cmor.zfactor(
            zaxis_id=axisid,
            zfactor_name="ps",
            axis_ids=[getattr(task, "grid_id"),
                      getattr(task, "time_axis")],
            units="Pa")
        return axisid, storewith
    finally:
        if ds is not None:
            ds.close()
Esempio n. 11
0
    a_coeff = numpy.array(( 0.2, ))
    b_coeff = numpy.array(( 0.0, ))
    
##  a_coeff_bnds=(/0.,.15, .25, .25, .15, 0./)
    a_coeff_bnds=numpy.array((0.,.3,))
    b_coeff_bnds=numpy.array((0.,.05,))
    
## error_flag = cmor.zfactor(  
##     zaxis_id=ilev,                      
##     zfactor_name='ptop',                  
##     units='Pa',                         
##     zfactor_values = p0)
    
    error_flag = cmor.zfactor(  
        zaxis_id=ilev,                        
        zfactor_name='b',
        axis_ids= numpy.array( (ilev, )),                
        zfactor_values = b_coeff,            
        zfactor_bounds = b_coeff_bnds  )
    
##     error_flag = cmor.zfactor(  
##         zaxis_id=ilev,                       
##         zfactor_name='lev',                    
##         axis_ids= numpy.array(( ilev, )),
##         units='m',
##         zfactor_values = a_coeff,            
##         zfactor_bounds = a_coeff_bnds )
    
    data2d = numpy.random.random((180,360)).astype('f')*8000
    
    zfactor_id = cmor.zfactor(  
        zaxis_id=ilev,                         
Esempio n. 12
0
def load_axis(data, levels=None):

    # create axes
    axes = []
    if levels and levels.get('time_name'):
        axes.append({
            str('table_entry'): levels.get('time_name'),
            str('units'): data[levels.get('time_name')].units
        })
    else:
        axes.append({
            str('table_entry'): str('time'),
            str('units'): data['time'].units
        })

    axes.append({
        str('table_entry'): str('latitude'),
        str('units'): data['lat'].units,
        str('coord_vals'): data['lat'][:],
        str('cell_bounds'): data['lat_bnds'][:]
    })
    axes.append({
        str('table_entry'): str('longitude'),
        str('units'): data['lon'].units,
        str('coord_vals'): data['lon'][:],
        str('cell_bounds'): data['lon_bnds'][:]
    })

    # axes = [{
    #     str('table_entry'): str('time'),
    #     str('units'): data['time'].units
    # }, {
    #     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'][:]
    # }]

    if levels:
        lev_axis = {
            str('table_entry'): str(levels.get('name')),
            str('units'): str(levels.get('units')),
            str('coord_vals'): data[levels.get('e3sm_axis_name')][:]
        }
        axis_bnds = levels.get('e3sm_axis_bnds')
        if axis_bnds:
            lev_axis['cell_bounds'] = data[axis_bnds][:]
        axes.insert(1, lev_axis)

    axis_ids = list()
    for axis in axes:
        axis_id = cmor.axis(**axis)
        axis_ids.append(axis_id)

    ips = None

    # add hybrid level formula terms
    if levels and levels.get('name') in [
            'standard_hybrid_sigma', 'standard_hybrid_sigma_half'
    ]:
        cmor.zfactor(zaxis_id=axis_ids[1],
                     zfactor_name=str('a'),
                     axis_ids=[
                         axis_ids[1],
                     ],
                     zfactor_values=data['hyam'][:],
                     zfactor_bounds=data['hyai'][:])
        cmor.zfactor(zaxis_id=axis_ids[1],
                     zfactor_name=str('b'),
                     axis_ids=[
                         axis_ids[1],
                     ],
                     zfactor_values=data['hybm'][:],
                     zfactor_bounds=data['hybi'][:])
        cmor.zfactor(zaxis_id=axis_ids[1],
                     zfactor_name=str('p0'),
                     units=str('Pa'),
                     zfactor_values=data['p0'])
        ips = cmor.zfactor(zaxis_id=axis_ids[1],
                           zfactor_name=str('ps'),
                           axis_ids=[axis_ids[0], axis_ids[2], axis_ids[3]],
                           units=str('Pa'))

    return axis_ids, ips
    units='degrees_east',
    cell_bounds=bnds_lon)

print("ILON:", ilon)

cmor.load_table("Tables/CMIP6_Omon.json")
itim = cmor.axis(
    table_entry="time",
    units="days since 1850",
    length=ntimes)

print("ITIME:",itim)
zlevs = numpy.array([.1, .3, .55, .7, .9])
zlev_bnds = numpy.array([0., .2, .42, .62, .8, 1.])

ilev_half = cmor.axis(table_entry='standard_hybrid_sigma',
                 units='1',
                 coord_vals=zlevs, cell_bounds=zlev_bnds)
print("ILEVL half:",ilev_half)

cmor.zfactor(zaxis_id=ilev_half, zfactor_name='p0', units='hPa', zfactor_values=p0)
print("p0 1/2")
failed = False
try:
    cmor.zfactor(zaxis_id=ilev_half, zfactor_name='b_half', axis_ids=[ilev_half, ],
                zfactor_values=b_coeff)
except:
    failed = True

if not failed:
    raise RuntimeError("This code should have error exited")
##                       parameter_values = pvalues,
##                       parameter_units = punits)

cmor.set_table(tables[1])
myaxes[3] = cmor.axis(table_entry = 'time',
                      units = 'months since 1980')
# Now sets up the ocn sigma stuff
levs=-numpy.arange(lev)/float(lev+1.)
blevs=-numpy.arange(lev+1)/float(lev+1.)
print 'Defining zlevs'
myaxes[4] = cmor.axis(table_entry='ocean_sigma',coord_vals=levs,cell_bounds=blevs,units='1')

print 'definnig zfactor depth',myaxes[2]
depth = numpy.random.random((lon,lat))*5000.
print 'Depth:',depth.shape,depth.dtype
idpth = cmor.zfactor(zaxis_id=myaxes[4],units='m',zfactor_name='depth',axis_ids=numpy.array([myaxes[2],]),zfactor_values=depth)

print 'defining zfactor eta'
ieta = cmor.zfactor(zaxis_id=myaxes[4],units='m',zfactor_name='eta',axis_ids=[myaxes[2],myaxes[3]])
print 'ieta:',ieta
pass_axes = [myaxes[4],myaxes[2],myaxes[3]]
print 'defining variable'
myvars[0] = cmor.variable( table_entry = 'thetao',
                           units = 'K',
                           axis_ids = pass_axes,
                           positive = 'down'
                           )
Time = numpy.zeros(ntimes,dtype='d')
bnds_time = numpy.zeros(ntimes*2,dtype='d')
Time[0],bnds_time[0:2] = read_time(0)
Time[1],bnds_time[2:4] = read_time(1)
Esempio n. 15
0
def testUnicode():

    # -------------------------------------------
    # Run CMOR using unicode strings
    # -------------------------------------------

    cmor.setup(inpath=u'Tables', netcdf_file_action=cmor.CMOR_REPLACE)
    cmor.dataset_json(u"Test/CMOR_input_example.json")
    cmor.load_table(u"CMIP6_CFsubhr.json")

    axes = [
        {
            u'table_entry': u'time1',
            u'units': u'days since 2000-01-01 00:00:00',
        },
        {
            u'table_entry': u'site',
            u'units': u'',
            u'coord_vals': [0]
        },
        {
            u'table_entry': u'hybrid_height',
            u'units': u'm',
            u'coord_vals': numpy.array(range(2), dtype=numpy.float64),
            u'cell_bounds': [[x, x + 1] for x in range(2)],
        },
    ]

    values = numpy.array([0.5, 0.5], numpy.float32)

    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],
                 u'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],
                 u'orog',
                 u'm',
                 axis_ids=[igrid],
                 zfactor_values=[0.])

    ids_for_var = [axis_ids[0], igrid, axis_ids[2]]
    varid = cmor.variable(
        u'tnhus',
        u's-1',
        ids_for_var,
        history=u'variable history',
        missing_value=-99,
    )

    for time in [x * 1800. / 86400 for x in range(48)]:
        cmor.write(varid, values, time_vals=[time])

    cmor.close()
   site_lons = numpy.array([0.0, 0.0, 0.0], dtype=numpy.float32)

   # Create CMOR axes and grids
   table_id = cmor.load_table('CMIP6_cfSites.json')
   taxis_id = cmor.axis('time1', units='days since 2000-01-01 00:00:00') #, length=1, interval='30 minutes')
   print 'ok: created time axis'

   saxis_id = cmor.axis('site', units='1', coord_vals=[1,2,3])
   print 'ok: created site axis',saxis_id

   zaxis_id = cmor.axis('hybrid_height', units='m', coord_vals=[1.0], cell_bounds=[0.0,2.0])
   print 'ok: created height axis',zaxis_id

   # Create zfactors for b and orog for hybrid height axis.
   # Where do these get used, if anywhere?
   bfact_id = cmor.zfactor(zaxis_id, 'b', '1', [zaxis_id], 'd', zfactor_values=[1.0],
      zfactor_bounds=[0.0,2.0])
   print 'ok: created b zfactors'

   # Create grid object to link site-dimensioned variables to (lat,long).
   # Need to make CMIP6_grids the current MIP table for this to work.
   table_id = cmor.load_table('CMIP6_grids.json')
   gaxis_id = cmor.grid([saxis_id], site_lats, site_lons)
   print 'ok: created site grid'

   # Create CMOR variable for cloud area fraction: MIP name = 'cl', STASH = m01s02i261*100
   table_id = cmor.load_table('CMIP6_cfSites.json')
   var_id = cmor.variable('cl', '%', [taxis_id, gaxis_id, zaxis_id], type='f',
      missing_value=-99.0, original_name='STASH m01s02i261*100')
   print 'ok: created variable for "cl"'

   ofact_id = cmor.zfactor(zaxis_id, 'orog', 'm', [gaxis_id], 'd',
Esempio n. 17
0
                 units= 'Pa',
                 coord_vals= plev[:],
                 cell_bounds= plev_bnds[:,:])

ialev = cmor.axis(table_entry= "standard_hybrid_sigma",
                    units='1',
                    coord_vals=alev[:],
                    cell_bounds=alev_bnds[:])

axis_ids_2d 	= [itime,ilat,ilon]
axis_ids_plev_3d = [itime,iplev,ilat,ilon]
axis_ids_alev_3d = [itime, ialev, ilat, ilon]

ierr = cmor.zfactor(zaxis_id=ialev,
                    zfactor_name='a',
                    axis_ids=[ialev, ],
                    zfactor_values=hyam[:],
                    zfactor_bounds=hyam_bnds[:])
ierr = cmor.zfactor(zaxis_id=ialev,
                    zfactor_name='b',
                    axis_ids=[ialev, ],
                    zfactor_values=hybm[:],
                    zfactor_bounds=hybm_bnds[:])
P0=100000
ierr = cmor.zfactor(zaxis_id=ialev,
                    zfactor_name='p0',
                    units='Pa',
                    zfactor_values=P0)
ips = cmor.zfactor(zaxis_id=ialev,
                   zfactor_name='ps',
                   axis_ids=[itime, ilat, ilon],
cmor.load_table("Tables/CMIP6_Omon.json")
itim = cmor.axis(table_entry="time", units="days since 1850", length=ntimes)

print("ITIME:", itim)
zlevs = numpy.array([.1, .3, .55, .7, .9])
zlev_bnds = numpy.array([0., .2, .42, .62, .8, 1.])

ilev_half = cmor.axis(table_entry='standard_hybrid_sigma',
                      units='1',
                      coord_vals=zlevs,
                      cell_bounds=zlev_bnds)
print("ILEVL half:", ilev_half)

cmor.zfactor(zaxis_id=ilev_half,
             zfactor_name='p0',
             units='hPa',
             zfactor_values=p0)
print("p0 1/2")
failed = False
try:
    cmor.zfactor(zaxis_id=ilev_half,
                 zfactor_name='b_half',
                 axis_ids=[
                     ilev_half,
                 ],
                 zfactor_values=b_coeff)
except:
    failed = True

if not failed:
    raise RuntimeError("This code should have error exited")
Esempio n. 19
0
def create_hybrid_level_axis(task,leveltype='alevel'):
    """Create hybrud levels
    Args:
        task (cmor.task-object): task for which levels are created
        leveltype (string): which kind (alevel, alevhalf)
    Returns:
        axisid (cmor.axis-object): axis id for levels
        storewith (cmor.zfactor-object): surface pressure field for saving into same file. needed for calculation of pressure on model levels.
    """
    global time_axes_,store_with_ps_,dim_ids_,zfactor_ids
    # define grid axes and time axis for hybrid levels
    axes=[getattr(task, 'lat'), getattr(task, 'lon'), getattr(task, "time_axis")]

    # define before hybrid factors, and have the same
    # for 
    pref = 80000  # TODO: Move reference pressure level to model config
    path = getattr(task, cmor_task.output_path_key)
    ds = None
    try:
        ds = netCDF4.Dataset(path)
        am = ds.variables["hyam"]
        aunit = getattr(am, "units")
        bm = ds.variables["hybm"]
        bunit = getattr(bm, "units")
        hcm = am[:] / pref + bm[:]
        n = hcm.shape[0]
        if "hyai" in ds.variables: 
            ai = ds.variables["hyai"]
            abnds = numpy.empty([n, 2])
            abnds[:, 0] = ai[0:n]
            abnds[:, 1] = ai[1:n + 1]
            bi = ds.variables["hybi"]
            bbnds = numpy.empty([n, 2])
            bbnds[:, 0] = bi[0:n]
            bbnds[:, 1] = bi[1:n + 1]
            hcbnds = abnds / pref + bbnds
            hci = ai[:] / pref + bi[:]
            n = hci.shape[0]
        else:
            log.critical("Interface values for hybrid levels not present!")
        if leveltype=='alevel':
            axisid = cmor.axis(table_entry="alternate_hybrid_sigma", coord_vals=hcm, cell_bounds=hcbnds, units="1")
            cmor.zfactor(zaxis_id=axisid, zfactor_name="ap", units=str(aunit), axis_ids=[axisid], zfactor_values=am[:],
                         zfactor_bounds=abnds)
            cmor.zfactor(zaxis_id=axisid, zfactor_name="b", units=str(bunit), axis_ids=[axisid], zfactor_values=bm[:],
                     zfactor_bounds=bbnds)
        elif leveltype=='alevhalf':
            axisid = cmor.axis(table_entry="alternate_hybrid_sigma_half", coord_vals=hci, units="1")

            cmor.zfactor(zaxis_id=axisid, zfactor_name="ap_half", units=str(aunit), axis_ids=[axisid,], zfactor_values=ai[:])
            cmor.zfactor(zaxis_id=axisid, zfactor_name="b_half", units=str(bunit), axis_ids=[axisid,], zfactor_values=bi[:])
        # Use the same ps for both types of hybrid levels,
        # for some reason defining their own confuses the cmor
        # to check in case of Half levels, for the ps from full levels
        if task.target.variable=='ec550aer':
            psvarname='ps1'
        else:
            psvarname='ps'
        #if depth_axis_ids[('task.table',leveltype)]!=None:

        #    setattr(task,'store_with',depth_axis_ids[('task.table',leveltype)])
        if task.target.table not in zfactor_ids:
            storewith = cmor.zfactor(zaxis_id=axisid, zfactor_name=psvarname,
                                axis_ids=axes, units="Pa")
            setattr(task,'store_with',storewith)
        else:
            storewith=zfactor_ids[task.target.table]
        return axisid, storewith
    finally:
        if ds is not None:
            ds.close()
Esempio n. 20
0
def prep_var(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_6hrLev.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)

    zlevs = numpy.zeros(5, dtype='d')
    zlevs[0] = 0.1999999999999999999
    zlevs[1] = 0.3
    zlevs[2] = 0.55
    zlevs[3] = 0.7
    zlevs[4] = 0.99999999

    zlev_bnds = numpy.zeros(6, dtype='d')
    zlev_bnds[0] = 0.
    zlev_bnds[1] = 0.2
    zlev_bnds[2] = 0.42
    zlev_bnds[3] = 0.62
    zlev_bnds[4] = 0.8
    zlev_bnds[5] = 1.

    itim = cmor.axis(table_entry='time1', units='days since 2010-1-1')

    ilev = cmor.axis(table_entry="alternate_hybrid_sigma",
                     units='1',
                     coord_vals=zlevs,
                     cell_bounds=zlev_bnds)

    p0 = numpy.array([
        1.e5,
    ])
    a_coeff = numpy.array([0.1, 0.2, 0.3, 0.22, 0.1])
    b_coeff = numpy.array([0.0, 0.1, 0.2, 0.5, 0.8])
    a_coeff_bnds = numpy.array([0., .15, .25, .25, .16, 0.])
    b_coeff_bnds = numpy.array([0., .05, .15, .35, .65, 1.])

    ierr = cmor.zfactor(zaxis_id=ilev,
                        zfactor_name='ap',
                        units='Pa',
                        axis_ids=[
                            ilev,
                        ],
                        zfactor_values=a_coeff,
                        zfactor_bounds=a_coeff_bnds)

    ierr = cmor.zfactor(zaxis_id=ilev,
                        zfactor_name='b',
                        axis_ids=[
                            ilev,
                        ],
                        zfactor_values=b_coeff,
                        zfactor_bounds=b_coeff_bnds)

    # ierr = cmor.zfactor(zaxis_id=ilev,
    # zfactor_name='p0',
    # units='Pa',
    # zfactor_values=p0)

    ips = cmor.zfactor(zaxis_id=ilev,
                       zfactor_name='ps1',
                       axis_ids=[itim, ilat, ilon],
                       units='Pa')

    ivar1 = cmor.variable(var,
                          axis_ids=[itim, ilev, ilat, ilon],
                          units=units,
                          missing_value=0.)
    return ivar1, ips
Esempio n. 21
0
varin3d = ["MC", ]

n3d = len(varin3d)

pth = os.getcwd()
common.init_cmor(pth, "CMOR_input_example.json")
ierr = cmor.load_table(os.path.join(pth, 'Tables', 'CMIP6_Amon.json'))
itim, ilat, ilon = common.read_cmor_time_lat_lon()

ilev_half = cmor.axis(table_entry='standard_hybrid_sigma_half',
                      units='1',
                      coord_vals=common.zlevs)
print("ILEVL half:", ilev_half)

cmor.zfactor(zaxis_id=ilev_half, zfactor_name='p0',
             units='hPa', zfactor_values=common.p0)
print("p0 1/2")
cmor.zfactor(zaxis_id=ilev_half, zfactor_name='b_half', axis_ids=[ilev_half, ],
             zfactor_values=common.b_coeff)
print("b 1/2")
cmor.zfactor(zaxis_id=ilev_half, zfactor_name='a_half', axis_ids=[ilev_half, ],
             zfactor_values=common.a_coeff)
print("a 1/2")
ps_var = cmor.zfactor(zaxis_id=ilev_half, zfactor_name='ps',
                      axis_ids=[ilon, ilat, itim], units='hPa')
print("ps 1/2")

var3d_ids = []
for m in varin3d:
    print("3d VAR:", m)
    var3d_ids.append(
Esempio n. 22
0
cmorLon = cmor.axis("longitude",
                    coord_vals=lon,
                    cell_bounds=lon_bnds,
                    units="degrees_east")
cmorTime = cmor.axis("time",
                     coord_vals=time,
                     cell_bounds=time_bnds,
                     units="days since 2018")
cmorLev = cmor.axis("standard_hybrid_sigma",
                    units='1',
                    coord_vals=lev,
                    cell_bounds=lev_bnds)
axes = [cmorTime, cmorLev, cmorLat, cmorLon]
ierr = cmor.zfactor(zaxis_id=cmorLev,
                    zfactor_name='a',
                    axis_ids=[cmorLev, ],
                    zfactor_values=a,
                    zfactor_bounds=a_bnds)
ierr = cmor.zfactor(zaxis_id=cmorLev,
                    zfactor_name='b',
                    axis_ids=[cmorLev, ],
                    zfactor_values=b,
                    zfactor_bounds=b_bnds)
ierr = cmor.zfactor(zaxis_id=cmorLev,
                    zfactor_name='p0',
                    units='Pa',
                    zfactor_values=p0)
ips = cmor.zfactor(zaxis_id=cmorLev,
                   zfactor_name='ps',
                   axis_ids=[cmorTime, cmorLat, cmorLon],
                   units='Pa')
Esempio n. 23
0
def handle(infiles, tables, user_input_path, **kwargs):

    logger = logging.getLogger()
    msg = '{}: Starting'.format(VAR_NAME)
    logger.info(msg)

    serial = kwargs.get('serial')
    logdir = kwargs.get('logdir')

    # 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
    logdir = kwargs.get('logdir')
    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)

    data = {}

    # assuming all year ranges are the same for every variable
    num_files_per_variable = len(infiles[RAW_VARIABLES[0]])

    # sort the input files for each variable
    for var_name in RAW_VARIABLES:
        infiles[var_name].sort()

    for index in range(num_files_per_variable):

        # load data for each variable
        for var_name in RAW_VARIABLES:

            # extract data from the input file
            msg = '{name}: loading {variable}'.format(
                name=VAR_NAME,
                variable=var_name)
            logger.info(msg)

            filename = infiles[var_name][index]
            new_data = {}

            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(var_name)

            if not variable_data.any():
                raise IOError("Variable data not found: {}".format(variable))

            data.update({
                variable: variable_data
            })

            # load the lon and lat info & bounds
            # load time & time bounds
            if var_name == 'PS':
                data.update({
                    'ps': f('PS'),
                    'lat': variable_data.getLatitude(),
                    'lon': variable_data.getLongitude(),
                    'lat_bnds': f('lat_bnds'),
                    'lon_bnds': f('lon_bnds'),
                    'time': variable_data.getTime(),
                    'time2': variable_data.getTime(),
                    'time_bnds': f('time_bnds')
                })

            if 'lev' in f.listdimension() and 'ilev' in f.listdimension():
                data.update({
                    'lev': f.getAxis('lev')[:]/1000,
                    'ilev': f.getAxis('ilev')[:]/1000
                })
            new_data = {i: f(i) for i in [
                'hyam', 'hybm', 'hyai', 'hybi'] if i in f.variables}

            data.update(new_data)

        msg = '{name}: loading axes'.format(name=VAR_NAME)
        logger.info(msg)

        axes = [{
            str('table_entry'): 'time2',
            str('units'): data['time2'].units
        }, {
            str('table_entry'): str('standard_hybrid_sigma'),
            str('units'): str('1'),
            str('coord_vals'): data['lev'][:],
            str('cell_bounds'): data['ilev'][:]
        }, {
            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'][:]
        }]

        axis_ids = list()
        for axis in axes:
            axis_id = cmor.axis(**axis)
            axis_ids.append(axis_id)

        # add hybrid level formula terms
        cmor.zfactor(
            zaxis_id=axis_ids[1],
            zfactor_name=str('a'),
            axis_ids=[axis_ids[1], ],
            zfactor_values=data['hyam'][:],
            zfactor_bounds=data['hyai'][:])
        cmor.zfactor(
            zaxis_id=axis_ids[1],
            zfactor_name=str('b'),
            axis_ids=[axis_ids[1], ],
            zfactor_values=data['hybm'][:],
            zfactor_bounds=data['hybi'][:])
        cmor.zfactor(
            zaxis_id=axis_ids[1],
            zfactor_name=str('p0'),
            units=str('Pa'),
            zfactor_values=100000)
        ips = cmor.zfactor(
            zaxis_id=axis_ids[1],
            zfactor_name=str('ps2'),
            axis_ids=[0, 2, 3],
            units=str('Pa'))

        data['ips'] = ips

        varid = cmor.variable(VAR_NAME, VAR_UNITS, axis_ids[:4])

        # write out the data
        msg = "{}: time {:1.1f} - {:1.1f}".format(
            VAR_NAME,
            data['time_bnds'][0][0],
            data['time_bnds'][-1][-1])
        logger.info(msg)

        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=len(data['time']), widgets=widgets)
            pbar.start()

        for index, val in enumerate(data['time']):
            if serial:
                pbar.update(index, running=msg)
            write_data(
                varid=varid,
                data=data,
                timeval=val,
                timebnds=[data['time_bnds'][index, :]],
                index=index,
                RAW_VARIABLES=RAW_VARIABLES)
        if serial:
            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 'pfull'
itim = cmor.axis(
    table_entry="time",
    units="days since 1850",
    length=ntimes)

print("ITIME:",itim)
zlevs = numpy.array([.1, .3, .55, .7, .9])
zlev_bnds = numpy.array([0., .2, .42, .62, .8, 1.])

ilev_half = cmor.axis(table_entry='standard_hybrid_sigma_half',
                 units='1',
                 coord_vals=zlevs, cell_bounds=zlev_bnds)
print("ILEVL half:",ilev_half)

cmor.zfactor(zaxis_id=ilev_half, zfactor_name='p0', units='hPa', zfactor_values=p0)
print("p0 1/2")
cmor.zfactor(zaxis_id=ilev_half, zfactor_name='b_half', axis_ids=[ilev_half, ],
             zfactor_values=b_coeff, zfactor_bounds=b_coeff_bnds)
print("b 1/2")
cmor.zfactor(zaxis_id=ilev_half, zfactor_name='a_half', axis_ids=[ilev_half, ],
             zfactor_values=a_coeff, zfactor_bounds=a_coeff_bnds)
print("a 1/2")
ps_var = cmor.zfactor(zaxis_id=ilev_half, zfactor_name='ps',
             axis_ids=[ilon, ilat, itim], units='hPa')
print("ps 1/2")

var3d_ids = []
for m in varin3d:
    print("3d VAR:",m)
    var3d_ids.append(
print("ILON:", ilon)

itim = cmor.axis(table_entry="time", units="days since 1850", length=ntimes)

print("ITIME:", itim)
zlevs = numpy.array([.1, .3, .55, .7, .9])
zlev_bnds = numpy.array([0., .2, .42, .62, .8, 1.])

ilev_half = cmor.axis(table_entry='standard_hybrid_sigma_half',
                      units='1',
                      coord_vals=zlevs,
                      cell_bounds=zlev_bnds)
print("ILEVL half:", ilev_half)

cmor.zfactor(zaxis_id=ilev_half,
             zfactor_name='p0',
             units='hPa',
             zfactor_values=p0)
print("p0 1/2")
cmor.zfactor(zaxis_id=ilev_half,
             zfactor_name='b_half',
             axis_ids=[
                 ilev_half,
             ],
             zfactor_values=b_coeff,
             zfactor_bounds=b_coeff_bnds)
print("b 1/2")
cmor.zfactor(zaxis_id=ilev_half,
             zfactor_name='a_half',
             axis_ids=[
                 ilev_half,
             ],
Esempio n. 26
0
def load_axis(data, levels=None, has_time=True):
    # use the special name for time if it exists
    if levels and levels.get('time_name'):
        name = levels.get('time_name')
        units = data[levels.get('time_name')].units
        time = cmor.axis(name, units=units)
    # else add the normal time name
    elif has_time:
        time = cmor.axis(has_time, units=data['time'].units)

    # use whatever level name this handler requires
    if levels:
        name = levels.get('name')
        units = levels.get('units')
        coord_vals = data[levels.get('e3sm_axis_name')][:]

        axis_bnds = levels.get('e3sm_axis_bnds')
        if axis_bnds:
            lev = cmor.axis(name,
                            units=units,
                            cell_bounds=data[axis_bnds][:],
                            coord_vals=coord_vals)
        else:
            lev = cmor.axis(name,
                            units=units,
                            coord_vals=coord_vals)

    # add lon/lat
    lat = cmor.axis('latitude',
                    units=data['lat'].units,
                    coord_vals=data['lat'][:],
                    cell_bounds=data['lat_bnds'][:])

    lon = cmor.axis('longitude',
                    units=data['lon'].units,
                    coord_vals=data['lon'][:],
                    cell_bounds=data['lon_bnds'][:])

    if levels:
        axes = [time, lev, lat, lon]
    elif has_time:
        axes = [time, lat, lon]
    else:
        axes = [lat, lon]

    ips = None
    # add hybrid level formula terms
    if levels and levels.get('name') in ['standard_hybrid_sigma', 'standard_hybrid_sigma_half']:
        cmor.zfactor(
            zaxis_id=lev,
            zfactor_name='a',
            axis_ids=[lev, ],
            zfactor_values=data['hyam'][:],
            zfactor_bounds=data['hyai'][:])
        cmor.zfactor(
            zaxis_id=lev,
            zfactor_name='b',
            axis_ids=[lev, ],
            zfactor_values=data['hybm'][:],
            zfactor_bounds=data['hybi'][:])
        cmor.zfactor(
            zaxis_id=lev,
            zfactor_name='p0',
            units='Pa',
            zfactor_values=data['p0'])
        ips = cmor.zfactor(
            zaxis_id=lev,
            zfactor_name='ps',
            axis_ids=[time, lat, lon],
            units='Pa')

    return axes, ips
    print 'ok: created time axis'

    saxis_id = cmor.axis('site', units='1', coord_vals=[1, 2, 3])
    print 'ok: created site axis', saxis_id

    zaxis_id = cmor.axis('hybrid_height',
                         units='m',
                         coord_vals=[1.0],
                         cell_bounds=[0.0, 2.0])
    print 'ok: created height axis', zaxis_id

    # Create zfactors for b and orog for hybrid height axis.
    # Where do these get used, if anywhere?
    bfact_id = cmor.zfactor(zaxis_id,
                            'b',
                            '1', [zaxis_id],
                            'd',
                            zfactor_values=[1.0],
                            zfactor_bounds=[0.0, 2.0])
    print 'ok: created b zfactors'

    # Create grid object to link site-dimensioned variables to (lat,long).
    # Need to make CMIP6_grids the current MIP table for this to work.
    table_id = cmor.load_table('CMIP6_grids.json')
    gaxis_id = cmor.grid([saxis_id], site_lats, site_lons)
    print 'ok: created site grid'

    # Create CMOR variable for cloud area fraction: MIP name = 'cl', STASH = m01s02i261*100
    table_id = cmor.load_table('CMIP6_cfSites.json')
    var_id = cmor.variable('cl',
                           '%', [taxis_id, gaxis_id, zaxis_id],
                           type='f',
Esempio n. 28
0
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()
Esempio n. 29
0
def handle(infiles, tables, user_input_path, **kwargs):
    """
    Parameters
    ----------
        infiles (List): a list of strings of file names for the raw input data
        tables (str): path to CMOR tables
        user_input_path (str): path to user input json file
    Returns
    -------
        var name (str): the name of the processed variable after processing is complete
    """
    logger = logging.getLogger()
    msg = '{}: Starting'.format(VAR_NAME)
    logger.info(msg)

    serial = kwargs.get('serial')
    logdir = kwargs.get('logdir')
    if kwargs.get('simple'):
        msg = f"{VAR_NAME} is not supported for simple conversion"
        print_message(msg)
        return

    # 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
    logdir = kwargs.get('logdir')
    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)

    data = {}

    # assuming all year ranges are the same for every variable
    num_files_per_variable = len(infiles[RAW_VARIABLES[0]])

    # sort the input files for each variable
    for var_name in RAW_VARIABLES:
        infiles[var_name].sort()

    for index in range(num_files_per_variable):

        # load data for each variable
        for var_name in RAW_VARIABLES:

            # extract data from the input file
            msg = '{name}: loading {variable}'.format(name=VAR_NAME,
                                                      variable=var_name)
            logger.info(msg)

            filename = infiles[var_name][index]
            new_data = {}

            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(var_name)

            if not variable_data.any():
                raise IOError("Variable data not found: {}".format(variable))

            data.update({variable: variable_data})

            # load the lon and lat info & bounds
            # load time & time bounds
            if var_name == 'PS':
                data.update({
                    'ps': f('PS'),
                    'lat': variable_data.getLatitude(),
                    'lon': variable_data.getLongitude(),
                    'lat_bnds': f('lat_bnds'),
                    'lon_bnds': f('lon_bnds'),
                    'time2': variable_data.getTime(),
                    'time_bnds': f('time_bnds')
                })

            if 'lev' in f.listdimension() and 'ilev' in f.listdimension():
                data.update({
                    'lev': f.getAxis('lev')[:] / 1000,
                    'ilev': f.getAxis('ilev')[:] / 1000
                })
            new_data = {
                i: f(i)
                for i in ['hyai', 'hybi', 'hyam', 'hybm'] if i in f.variables
            }

            data.update(new_data)

        msg = '{name}: loading axes'.format(name=VAR_NAME)
        logger.info(msg)

        # create the cmor variable and axis
        axes = [{
            str('table_entry'): 'time2',
            str('units'): data['time2'].units
        }, {
            str('table_entry'): str('standard_hybrid_sigma_half'),
            str('units'): str('1'),
            str('coord_vals'): data['lev'][:],
            str('cell_bounds'): data['ilev'][:]
        }, {
            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'][:]
        }]

        axis_ids = list()
        for axis in axes:
            axis_id = cmor.axis(**axis)
            axis_ids.append(axis_id)

        # add hybrid level formula terms
        cmor.zfactor(zaxis_id=axis_ids[1],
                     zfactor_name='a_half',
                     axis_ids=[
                         axis_ids[1],
                     ],
                     zfactor_values=data['hyam'][:])
        cmor.zfactor(zaxis_id=axis_ids[1],
                     zfactor_name='b_half',
                     axis_ids=[
                         axis_ids[1],
                     ],
                     zfactor_values=data['hybm'][:])
        cmor.zfactor(zaxis_id=axis_ids[1],
                     zfactor_name='p0',
                     units='Pa',
                     zfactor_values=100000)
        ips = cmor.zfactor(zaxis_id=axis_ids[1],
                           zfactor_name='ps2',
                           axis_ids=[0, 2, 3],
                           units='Pa')

        data['ips'] = ips

        varid = cmor.variable(VAR_NAME, VAR_UNITS, axis_ids[:4])

        # write out the data
        msg = "{}: time {:1.1f} - {:1.1f}".format(VAR_NAME,
                                                  data['time_bnds'][0][0],
                                                  data['time_bnds'][-1][-1])
        logger.info(msg)

        if serial:
            pbar = tqdm(total=len(data['time2']))

        for index, val in enumerate(data['time2']):
            if serial:
                pbar.update(1)
            write_data(varid=varid,
                       data=data,
                       timeval=val,
                       timebnds=[data['time_bnds'][index, :]],
                       index=index,
                       RAW_VARIABLES=RAW_VARIABLES)
        if serial:
            pbar.close()

    msg = '{}: write complete, closing'.format(VAR_NAME)
    logger.debug(msg)

    cmor.close()

    msg = '{}: file close complete'.format(VAR_NAME)
    logger.debug(msg)

    return 'phalf'
Esempio n. 30
0
def prep_var(var, units):
    # creates 1 degree grid
    dlat = 180 / nlat
    dlon = 360.0 / nlon
    alats = numpy.arange(-90 + dlat / 2.0, 90, dlat)
    bnds_lat = numpy.arange(-90, 90 + dlat, dlat)
    alons = numpy.arange(0 + dlon / 2.0, 360.0, dlon) - 180.0
    bnds_lon = numpy.arange(0, 360.0 + dlon, dlon) - 180.0
    cmor.load_table("Tables/CMIP6_6hrLev.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)

    zlevs = numpy.zeros(5, dtype="d")
    zlevs[0] = 0.1999999999999999999
    zlevs[1] = 0.3
    zlevs[2] = 0.55
    zlevs[3] = 0.7
    zlevs[4] = 0.99999999

    zlev_bnds = numpy.zeros(6, dtype="d")
    zlev_bnds[0] = 0.0
    zlev_bnds[1] = 0.2
    zlev_bnds[2] = 0.42
    zlev_bnds[3] = 0.62
    zlev_bnds[4] = 0.8
    zlev_bnds[5] = 1.0

    itim = cmor.axis(table_entry="time1", units="days since 2010-1-1")

    ilev = cmor.axis(table_entry="alternate_hybrid_sigma", units="1", coord_vals=zlevs, cell_bounds=zlev_bnds)

    p0 = numpy.array([1.0e5])
    a_coeff = numpy.array([0.1, 0.2, 0.3, 0.22, 0.1])
    b_coeff = numpy.array([0.0, 0.1, 0.2, 0.5, 0.8])
    a_coeff_bnds = numpy.array([0.0, 0.15, 0.25, 0.25, 0.16, 0.0])
    b_coeff_bnds = numpy.array([0.0, 0.05, 0.15, 0.35, 0.65, 1.0])

    ierr = cmor.zfactor(
        zaxis_id=ilev,
        zfactor_name="ap",
        units="Pa",
        axis_ids=[ilev],
        zfactor_values=a_coeff,
        zfactor_bounds=a_coeff_bnds,
    )

    ierr = cmor.zfactor(
        zaxis_id=ilev, zfactor_name="b", axis_ids=[ilev], zfactor_values=b_coeff, zfactor_bounds=b_coeff_bnds
    )

    ## ierr = cmor.zfactor(zaxis_id=ilev,
    ##                  zfactor_name='p0',
    ##                  units='Pa',
    ##                  zfactor_values=p0)

    ips = cmor.zfactor(zaxis_id=ilev, zfactor_name="ps", axis_ids=[itim, ilat, ilon], units="Pa")

    ivar1 = cmor.variable(var, axis_ids=[itim, ilev, ilat, ilon], units=units, missing_value=0.0)
    return ivar1, ips
Esempio n. 31
0
    a_coeff = numpy.array((0.2, ))
    b_coeff = numpy.array((0.0, ))

# a_coeff_bnds=(/0.,.15, .25, .25, .15, 0./)
    a_coeff_bnds = numpy.array((0., .3,))
    b_coeff_bnds = numpy.array((0., .05,))

# error_flag = cmor.zfactor(
# zaxis_id=ilev,
# zfactor_name='ptop',
# units='Pa',
# zfactor_values = p0)

    error_flag = cmor.zfactor(
        zaxis_id=ilev,
        zfactor_name='b',
        axis_ids=numpy.array((ilev, )),
        zfactor_values=b_coeff,
        zfactor_bounds=b_coeff_bnds)

# error_flag = cmor.zfactor(
# zaxis_id=ilev,
# zfactor_name='lev',
##         axis_ids= numpy.array(( ilev, )),
# units='m',
##         zfactor_values = a_coeff,
# zfactor_bounds = a_coeff_bnds )

    data2d = numpy.random.random((180, 360)).astype('f') * 8000

    zfactor_id = cmor.zfactor(
        zaxis_id=ilev,
Esempio n. 32
0
def prep_var(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_6hrLev.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)

    zlevs = numpy.zeros(5, dtype='d')
    zlevs[0] = 0.1999999999999999999
    zlevs[1] = 0.3
    zlevs[2] = 0.55
    zlevs[3] = 0.7
    zlevs[4] = 0.99999999

    zlev_bnds = numpy.zeros(6, dtype='d')
    zlev_bnds[0] = 0.
    zlev_bnds[1] = 0.2
    zlev_bnds[2] = 0.42
    zlev_bnds[3] = 0.62
    zlev_bnds[4] = 0.8
    zlev_bnds[5] = 1.

    itim = cmor.axis(
        table_entry='time1',
        units='days since 2010-1-1')

    ilev = cmor.axis(
        table_entry="alternate_hybrid_sigma",
        units='1',
        coord_vals=zlevs,
        cell_bounds=zlev_bnds)

    p0 = numpy.array([1.e5, ])
    a_coeff = numpy.array([0.1, 0.2, 0.3, 0.22, 0.1])
    b_coeff = numpy.array([0.0, 0.1, 0.2, 0.5, 0.8])
    a_coeff_bnds = numpy.array([0., .15, .25, .25, .16, 0.])
    b_coeff_bnds = numpy.array([0., .05, .15, .35, .65, 1.])

    ierr = cmor.zfactor(zaxis_id=ilev,
                        zfactor_name='ap',
                        units='Pa',
                        axis_ids=[ilev, ],
                        zfactor_values=a_coeff,
                        zfactor_bounds=a_coeff_bnds)

    ierr = cmor.zfactor(zaxis_id=ilev,
                        zfactor_name='b',
                        axis_ids=[ilev, ],
                        zfactor_values=b_coeff,
                        zfactor_bounds=b_coeff_bnds)

    # ierr = cmor.zfactor(zaxis_id=ilev,
    # zfactor_name='p0',
    # units='Pa',
    # zfactor_values=p0)

    ips = cmor.zfactor(zaxis_id=ilev,
                       zfactor_name='ps1',
                       axis_ids=[itim, ilat, ilon],
                       units='Pa')

    ivar1 = cmor.variable(
        var,
        axis_ids=[
            itim,
            ilev,
            ilat,
            ilon],
        units=units,
        missing_value=0.)
    return ivar1, ips
Esempio n. 33
0
]

n3d = len(varin3d)

pth = os.getcwd()
common.init_cmor(pth, "CMOR_input_example.json")
ierr = cmor.load_table(os.path.join(pth, 'Tables', 'CMIP6_Amon.json'))
itim, ilat, ilon = common.read_cmor_time_lat_lon()

ilev_half = cmor.axis(table_entry='standard_hybrid_sigma_half',
                      units='1',
                      coord_vals=common.zlevs)
print("ILEVL half:", ilev_half)

cmor.zfactor(zaxis_id=ilev_half,
             zfactor_name='p0',
             units='hPa',
             zfactor_values=common.p0)
print("p0 1/2")
cmor.zfactor(zaxis_id=ilev_half,
             zfactor_name='b_half',
             axis_ids=[
                 ilev_half,
             ],
             zfactor_values=common.b_coeff)
print("b 1/2")
cmor.zfactor(zaxis_id=ilev_half,
             zfactor_name='a_half',
             axis_ids=[
                 ilev_half,
             ],
             zfactor_values=common.a_coeff)
Esempio n. 34
0
                    coord_vals=lon,
                    cell_bounds=lon_bnds,
                    units="degrees_east")
cmorTime = cmor.axis("time",
                     coord_vals=time,
                     cell_bounds=time_bnds,
                     units="days since 2018")
cmorLev = cmor.axis("standard_hybrid_sigma",
                    units='1',
                    coord_vals=lev,
                    cell_bounds=lev_bnds)
axes = [cmorTime, cmorLev, cmorLat, cmorLon]
ierr = cmor.zfactor(zaxis_id=cmorLev,
                    zfactor_name='a',
                    axis_ids=[
                        cmorLev,
                    ],
                    zfactor_values=a,
                    zfactor_bounds=a_bnds)
ierr = cmor.zfactor(zaxis_id=cmorLev,
                    zfactor_name='b',
                    axis_ids=[
                        cmorLev,
                    ],
                    zfactor_values=b,
                    zfactor_bounds=b_bnds)
ierr = cmor.zfactor(zaxis_id=cmorLev,
                    zfactor_name='p0',
                    units='Pa',
                    zfactor_values=p0)
ips = cmor.zfactor(zaxis_id=cmorLev,
# Now sets up the ocn sigma stuff
levs = -numpy.arange(lev) / float(lev + 1.)
blevs = -numpy.arange(lev + 1) / float(lev + 1.)
print 'Defining zlevs'
myaxes[4] = cmor.axis(table_entry='ocean_sigma',
                      coord_vals=levs,
                      cell_bounds=blevs,
                      units='1')

print 'definnig zfactor depth', myaxes[2]
depth = numpy.random.random((lon, lat)) * 5000.
print 'Depth:', depth.shape, depth.dtype
idpth = cmor.zfactor(zaxis_id=myaxes[4],
                     units='m',
                     zfactor_name='depth',
                     axis_ids=numpy.array([
                         myaxes[2],
                     ]),
                     zfactor_values=depth)

print 'defining zfactor eta'
ieta = cmor.zfactor(zaxis_id=myaxes[4],
                    units='m',
                    zfactor_name='eta',
                    axis_ids=[myaxes[2], myaxes[3]])
print 'ieta:', ieta
pass_axes = [myaxes[4], myaxes[2], myaxes[3]]
print 'defining variable'
myvars[0] = cmor.variable(table_entry='thetao',
                          units='K',
                          axis_ids=pass_axes,
Esempio n. 36
0
def create_hybrid_level_axis(task):
    pref = 80000  # TODO: Move reference pressure level to model config
    path = getattr(task, cmor_task.output_path_key)
    print path, task.target.variable
    ds = None
    try:
        ds = netCDF4.Dataset(path)
        am = ds.variables["a"]
        aunit = getattr(am, "units")
        bm = ds.variables["b"]
        bunit = getattr(bm, "units")
        hcm = am[:] / pref + bm[:]
        n = hcm.shape[0]
        print n

        if "hyai" in ds.variables:
            ai = ds.variables["hyai"]
            abnds = numpy.empty([n, 2])
            abnds[:, 0] = ai[0:n]
            abnds[:, 1] = ai[1:n + 1]
            bi = ds.variables["hybi"]
            bbnds = numpy.empty([n, 2])
            bbnds[:, 0] = bi[0:n]
            bbnds[:, 1] = bi[1:n + 1]
            hcbnds = abnds / pref + bbnds
        # temporary solution for current output
        else:
            ai = numpy.empty([n + 1])
            bi = numpy.empty([n + 1])
            ai[1:-1] = (am[1:] + am[0:-1]) / 2

            ai[0] = am[0] / 2
            ai[-1] = am[-1]
            abnds = numpy.empty([n, 2])
            abnds[:, 0] = ai[0:n]
            abnds[:, 1] = ai[1:n + 1]

            bi[1:-1] = (bm[1:] + bm[0:-1]) / 2
            bi[0] = 1.0
            bi[-1] = 0.0
            bbnds = numpy.empty([n, 2])
            bbnds[:, 0] = bi[0:n]
            bbnds[:, 1] = bi[1:n + 1]
            hcbnds = abnds / pref + bbnds
            hcbnds[0, 0] = 1.0
        axisid = cmor.axis(table_entry="alternate_hybrid_sigma",
                           coord_vals=hcm,
                           cell_bounds=hcbnds,
                           units="1")
        cmor.zfactor(zaxis_id=axisid,
                     zfactor_name="ap",
                     units=str(aunit),
                     axis_ids=[axisid],
                     zfactor_values=am[:],
                     zfactor_bounds=abnds)
        cmor.zfactor(zaxis_id=axisid,
                     zfactor_name="b",
                     units=str(bunit),
                     axis_ids=[axisid],
                     zfactor_values=bm[:],
                     zfactor_bounds=bbnds)
        storewith = cmor.zfactor(
            zaxis_id=axisid,
            zfactor_name="ps",
            axis_ids=[getattr(task, "grid_id"),
                      getattr(task, "time_axis")],
            units="Pa")
        return axisid, storewith
    finally:
        if ds is not None:
            ds.close()