source: CPL/oasis3-mct_5.0/examples/regrid_environment/ESMF/OasisGridsToESMF.py @ 6328

Last change on this file since 6328 was 6328, checked in by aclsce, 17 months ago

First import of oasis3-mct_5.0 (from oasis git server, branch OASIS3-MCT_5.0)

  • Property svn:executable set to *
File size: 5.5 KB
Line 
1#!/usr/bin/env python3
2
3__doc__ = """This python script converts a grid from the OASIS-SCRIP file format to the ESMF-SCRIP file format.
4This format is capable of storing either 2D logically rectangular grids or 2D unstructured grids.
5The grid should be defined in 3 existing input files: grids.nc, masks.nc, areas.nc
6The grid is written in 1 output file: [gridname]_ESMF_ScripFmt.nc
7
8Input: grid name to be converted (first argument of this script)
9Output: netcdf grid file in ESMF-SCRIP format
10        netcdf grid file in ESMF unstructured format if the grid is unstructured
11"""
12
13__author__ = "Gabriel Jonville - July 2019 - Updated february 2021"
14
15import os
16import sys
17import numpy as np
18import netCDF4 as nc
19import time
20import subprocess
21
22#print(str(sys.argv))
23if len(sys.argv) == 3:
24    grid = sys.argv[1]
25    fgridoasis_path = sys.argv[2]
26else:
27    print("USAGE: python ./{} gridname gridpath".format(sys.argv[0]))
28    sys.exit()
29
30fgridoasis = 'grids.nc'
31
32##### TESTER si grid est bien dans fgridoasis
33#if grid not in gridnames:
34#    print("STOP! Gridname should be in {}\n".format(gridnames))
35#    sys.exit()
36
37
38def grid_is_unstruct(gr):
39    return gr in ("t12e", "icos", "icoh", "bt42", "ssea", "sse7", "t127", "t359","t799")
40
41
42fgridesmf_s=grid+'_ESMF_ScripFmt.nc'
43print("--\nConvert grid {} from OASIS-SCRIP to ESMF-SCRIP format".format(grid))
44print("Writting in {}\n--".format(fgridesmf_s))
45
46
47### OASIS grid
48# Open a netCDF file in read mode
49frnc = nc.Dataset(os.path.join(fgridoasis_path,fgridoasis), 'r')
50# Open a new netCDF file in write mode
51fwnc = nc.Dataset(fgridesmf_s, 'w', format='NETCDF4_CLASSIC')
52# Read netCDF dimension
53x = len(frnc.dimensions['x_'+grid])
54y = len(frnc.dimensions['y_'+grid])
55crn = len(frnc.dimensions['crn_'+grid])
56if y==1:
57    rank=1
58else:
59    rank=2
60# Read netCDF variable
61lat = frnc.variables[grid+'.lat'][:,:]
62lon = frnc.variables[grid+'.lon'][:,:]
63cla = frnc.variables[grid+'.cla'][:,:,:]
64clo = frnc.variables[grid+'.clo'][:,:,:]
65# Close netCDF file
66frnc.close()
67
68
69### ESMF grid
70# Create netCDF dimension
71grid_size = len(fwnc.createDimension('grid_size',x*y))
72grid_corners = len(fwnc.createDimension('grid_corners',crn))
73grid_rank = len(fwnc.createDimension('grid_rank',rank))
74# Create netCDF variable / Put data / Set attribute
75grid_dims = fwnc.createVariable('grid_dims',np.int32,('grid_rank',))
76if y==1:
77    grid_dims[:] = (x,)
78else:
79    grid_dims[:] = (x,y) 
80
81grid_center_lat = fwnc.createVariable('grid_center_lat',np.float64,('grid_size',))
82grid_center_lat[:] = np.reshape(lat, grid_size, order='C')   # read/write the elements using C-like index order
83grid_center_lat.units = 'degrees'
84
85grid_center_lon = fwnc.createVariable('grid_center_lon',np.float64,('grid_size',))
86grid_center_lon[:] = np.reshape(lon, grid_size, order='C')   # read/write the elements using C-like index order
87grid_center_lon.units = 'degrees'
88
89grid_corner_lat = fwnc.createVariable('grid_corner_lat',np.float64,('grid_size','grid_corners'))
90grid_corner_lat[:,:] = np.reshape(cla.T,(grid_size, grid_corners), order='F')   # read/write the elements using Fortran-like index order
91grid_corner_lat.units = 'degrees'
92
93grid_corner_lon = fwnc.createVariable('grid_corner_lon',np.float64,('grid_size','grid_corners'))
94grid_corner_lon[:,:] = np.reshape(clo.T,(grid_size, grid_corners), order='F')   # read/write the elements using Fortran-like index order
95grid_corner_lon.units = 'degrees'
96
97
98### OASIS mask
99fgridoasis = 'masks.nc'
100# Open a netCDF file in read mode
101frnc = nc.Dataset(os.path.join(fgridoasis_path,fgridoasis), 'r')
102# Read netCDF variable
103msk = frnc.variables[grid+'.msk'][:,:]
104# Close netCDF file
105frnc.close()
106
107
108### ESMF mask
109# Create netCDF variable / Put data / Set attribute
110grid_imask = fwnc.createVariable('grid_imask',np.int32,('grid_size',))
111grid_imask[:] = np.reshape(msk, grid_size, order='C')
112grid_imask[:] = np.invert(grid_imask)+2   # invert mask for ESMF format
113grid_imask.masked_value = "0 (land)"
114grid_imask.unmasked_value = "1 (sea)"
115
116
117### OASIS areas
118#SV fgridoasis = 'areas.nc'
119# Open a netCDF file in read mode
120#SV frnc = nc.Dataset(os.path.join(fgridoasis_path,fgridoasis), 'r')
121# Read netCDF variable
122#SV srf = frnc.variables[grid+'.srf'][:,:]
123# Close netCDF file
124#SV frnc.close()
125
126
127### ESMF areas
128# Create netCDF variable / Put data / Set attribute
129#SV grid_area = fwnc.createVariable('grid_area',np.float64,('grid_size',))
130#SV grid_area[:] = np.reshape(srf, grid_size, order='C')
131#SV grid_area.units = 'm^2'
132
133
134# Global netCDF attributes
135fwnc.description = grid+' grid in ESMF-SCRIP format'
136fwnc.history = 'Created '+time.ctime(time.time())
137fwnc.source = 'netCDF4 python '+sys.argv[0]
138
139## all of the Dimension instances in the Dataset are stored in a dictionary
140#for dimname in fwnc.dimensions.keys() :
141#    dim = fwnc.dimensions[dimname]
142#    print(dimname, len(dim))
143## all of the variables in the Dataset are stored in a dictionnary
144#for varname in fwnc.variables.keys() :
145#    var = fwnc.variables[varname]
146#    print(varname, var.dtype, var.dimensions, var.shape)
147
148# Write and close the netCDF file
149fwnc.close()
150
151
152fgridesmf = grid+'_ESMF.nc'
153if os.path.exists(os.path.join('.',fgridesmf)):
154    os.remove(os.path.join('.',fgridesmf))
155
156if grid_is_unstruct(grid):
157    fgridesmf_u = grid+'_ESMF_unstruct.nc'
158    cmd = 'ESMF_Scrip2Unstruct {} {} 0'.format(fgridesmf_s,fgridesmf_u)
159    #from python3.5#subprocess.run(cmd,shell=True)
160    subprocess.call(cmd,shell=True)
161    os.symlink(os.path.join('.',fgridesmf_u),os.path.join('.',fgridesmf))
162    print("--")
163else:
164    os.symlink(os.path.join('.',fgridesmf_s),os.path.join('.',fgridesmf))
165
Note: See TracBrowser for help on using the repository browser.