source: codes/icosagcm/devel/Python/dynamico/xios.py @ 694

Last change on this file since 694 was 694, checked in by dubos, 6 years ago

devel/unstructured : higher-level interface to XIOS

File size: 2.8 KB
Line 
1import numpy as np
2import cxios
3from unstructured import ker
4from dynamico.meshes import radian
5from cxios import finalize
6
7def cf_boundaries(degree,points,lon,lat):
8    n, nvertex = len(degree), degree.max()
9    bnd_lon, bnd_lat = np.zeros((n,nvertex)),np.zeros((n,nvertex))
10    for ij in range(n):
11        nb=degree[ij]
12        for k in range(nb):
13            vertex = points[ij,k]
14            bnd_lon[ij,k]=lon[vertex]
15            bnd_lat[ij,k]=lat[vertex]
16        for k in range(nb,nvertex):   # repeat last vertex if nb<nvertex
17            bnd_lon[ij,k]=bnd_lon[ij,nb-1]
18            bnd_lat[ij,k]=bnd_lat[ij,nb-1]
19    return bnd_lon, bnd_lat
20
21def setup_domain(cat,id, degree,vertex,lon,lat,lon_v,lat_v, ncell_glo, index_glo, displ): # cat in 'domain','domaingroup'
22    bnd_lon, bnd_lat = cf_boundaries(degree, vertex, lon_v, lat_v)
23    ncell, nvertex = bnd_lon.shape
24    mesh = cxios.Handle(cat,id)
25    mesh.set_attr(type='unstructured')
26    mesh.set_attr(ni_glo=ncell_glo, ni=ncell, ibegin=displ, i_index=index_glo)
27    mesh.set_attr(lonvalue_1d=lon, latvalue_1d=lat)
28    mesh.set_attr(nvertex=nvertex,bounds_lon_1d=bnd_lon, bounds_lat_1d=bnd_lat)
29
30class XIOS_Context:
31    def __init__(self, pmesh, mesh,nqtot):
32        self.mesh=mesh
33        ker.dynamico_setup_xios()
34        # vertical axis, nq
35        llm = mesh.llm
36        lev, levp1, nq = [cxios.Handle('axis',name) for name in 'lev', 'levp1', 'nq']
37        lev.set_attr(n_glo=llm, value=np.arange(llm))
38        levp1.set_attr(n_glo=llm+1, value=np.arange(llm+1))
39        nq.set_attr(n_glo=nqtot, value=np.arange(nqtot))
40        # primal mesh
41        lon_i, lat_i, lon_v, lat_v = [x*radian for x in mesh.lon_i, mesh.lat_i, mesh.lon_v, mesh.lat_v]
42        mesh_i=setup_domain('domaingroup','i', mesh.primal_own_deg, mesh.primal_vertex,
43                            lon_i[mesh.primal_own_loc], lat_i[mesh.primal_own_loc],
44                            lon_v, lat_v,
45                            pmesh.dim_primal.n, mesh.primal_own_glo, mesh.displ)
46        # calendar
47        calendar=cxios.Handle('calendar_wrapper')
48        dtime = cxios.Duration(second=3600.)
49        calendar.set_attr(timestep=dtime)
50        calendar.update_timestep()
51        std=cxios.Handle('fieldgroup','standard_output')
52        std.set_attr(freq_op=dtime)
53        freq_offset = cxios.Duration(second=0)
54        std.set_attr(freq_offset=freq_offset)
55        print 'cxios.context_close_definition()'
56        cxios.context_close_definition()
57    def send_field_primal(self,name, data):
58        own_loc = self.mesh.primal_own_loc
59        if data.ndim==1:
60            data = data[own_loc]
61        if data.ndim==2:
62            data = data[:,own_loc]
63        data = np.ascontiguousarray(data)
64        cxios.send_field(name, data)
65    def update_calendar(self, i):
66        ker.dynamico_xios_update_calendar(i)
67    def finalize(self):
68        cxios.context_finalize()
Note: See TracBrowser for help on using the repository browser.