1 | ### =========================================================================== |
---|
2 | ### |
---|
3 | ### Part of CPL Restart IPSL packages |
---|
4 | ### |
---|
5 | ### =========================================================================== |
---|
6 | ## |
---|
7 | ## Warning, to install, configure, run, use any of Olivier Marti's |
---|
8 | ## software or to read the associated documentation you'll need at least |
---|
9 | ## one (1) brain in a reasonably working order. Lack of this implement |
---|
10 | ## will void any warranties (either express or implied). |
---|
11 | ## O. Marti assumes no responsability for errors, omissions, |
---|
12 | ## data loss, or any other consequences caused directly or indirectly by |
---|
13 | ## the usage of his software by incorrectly or partially configured |
---|
14 | ## personal. |
---|
15 | ## |
---|
16 | ### |
---|
17 | ### Documentation : https://forge.ipsl.jussieu.fr/igcmg/wiki/IPSLCM6/MOSAIX |
---|
18 | ### |
---|
19 | ## SVN information |
---|
20 | # $Author: omamce $ |
---|
21 | # $Date: 2020-09-18 17:02:09 +0200 (Fri, 18 Sep 2020) $ |
---|
22 | # $Revision: 5157 $ |
---|
23 | # $Id: CreateRestartAtm4Oasis.bash 5157 2020-09-18 15:02:09Z omamce $ |
---|
24 | # $HeadURL: svn+ssh://omamce@forge.ipsl.jussieu.fr/ipsl/forge/projets/igcmg/svn/TOOLS/CPLRESTART/CreateRestartAtm4Oasis.bash $ |
---|
25 | |
---|
26 | import numpy as np, xarray as xr |
---|
27 | import sys, argparse, textwrap |
---|
28 | |
---|
29 | |
---|
30 | ## Reading the command line arguments |
---|
31 | # |
---|
32 | # Creating a parser |
---|
33 | # The first step in using the argparse is creating an ArgumentParser object: |
---|
34 | parser = argparse.ArgumentParser (description = textwrap.dedent (""" |
---|
35 | create_flxat |
---|
36 | """), epilog='-------- This is the end of the help message --------') |
---|
37 | |
---|
38 | # Adding arguments |
---|
39 | parser.add_argument ('-v', '--verbose', help='verbosity', action="store_true", default=False ) |
---|
40 | parser.add_argument ('-l', '--level', help='verbosity level', type=int, default=0, choices=[0, 1, 2] ) |
---|
41 | parser.add_argument ('-i', '--input' , metavar='inputFile' , help="input file" , nargs='?', default="flxat_fields_notime.nc" ) |
---|
42 | parser.add_argument ('-o', '--output' , metavar='outputFile', help="output file", nargs='?', default="tmp_flxat.nc" ) |
---|
43 | parser.add_argument ('-f', '--format' , metavar='NetCDF_Format', help="NetCDF format", nargs='?', default="NETCDF4", |
---|
44 | choices=["NETCDF4","NETCDF4_CLASSIC", "NETCDF3_64BIT", "NETCDF3_CLASSIC", "64bits"]) |
---|
45 | parser.add_argument ('--IsUnstructured', default='no', choices=['yes', 'no'] ) |
---|
46 | |
---|
47 | # Parse command line |
---|
48 | myargs = parser.parse_args () |
---|
49 | |
---|
50 | NcFormat = myargs.format |
---|
51 | if NcFormat == '64bit' : NcFormat = NETCDF4 |
---|
52 | |
---|
53 | ## Read Data - promote them to 64 bits |
---|
54 | d_In = xr.open_dataset (myargs.input) |
---|
55 | |
---|
56 | lon = d_In.lon.astype(np.float64).values |
---|
57 | lat = d_In.lat.astype(np.float64).values |
---|
58 | evap_oce = d_In.evap_oce.astype(np.float64).values |
---|
59 | evap_sic = d_In.evap_sic.astype(np.float64).values |
---|
60 | fract_oce = d_In.fract_oce.astype(np.float64).values |
---|
61 | fract_sic = d_In.fract_sic.astype(np.float64).values |
---|
62 | precip = d_In.precip.astype(np.float64).values |
---|
63 | snow = d_In.snow.astype(np.float64).values |
---|
64 | soll = d_In.soll.astype(np.float64).values |
---|
65 | sols = d_In.sols.astype(np.float64).values |
---|
66 | taux_oce = d_In.taux_oce.astype(np.float64).values |
---|
67 | taux_sic = d_In.taux_sic.astype(np.float64).values |
---|
68 | tauy_oce = d_In.tauy_oce.astype(np.float64).values |
---|
69 | tauy_sic = d_In.tauy_sic.astype(np.float64).values |
---|
70 | wind10m = d_In.wind10m.astype(np.float64).values |
---|
71 | |
---|
72 | ## |
---|
73 | yxshape = lat.shape |
---|
74 | ny, nx = yxshape |
---|
75 | |
---|
76 | ## Computations |
---|
77 | np.seterr (divide='ignore', invalid='ignore') |
---|
78 | fract_oce_plus_sic = (fract_oce + fract_sic) ; ## ocean fraction |
---|
79 | fract_oce_norm = np.where (fract_oce_plus_sic > 0.0, fract_oce/fract_oce_plus_sic, 0.0) # free ocean vs. total ocen fraction |
---|
80 | fract_sic_norm = np.where (fract_oce_plus_sic > 0.0, fract_sic/fract_oce_plus_sic, 0.0) # sea ice vs. total ocean fraction |
---|
81 | ## |
---|
82 | COTOTRAI = xr.DataArray (precip-snow, dims=('y', 'x')) |
---|
83 | COTOTRAI.attrs['long_name'] = 'Liquid precip' |
---|
84 | COTOTRAI.attrs['coordinates'] = "lat lon" |
---|
85 | |
---|
86 | COTOTSNO = xr.DataArray (snow , dims=('y', 'x')) |
---|
87 | COTOTSNO.attrs['long_name'] ='Solid precipitation' |
---|
88 | COTOTSNO.attrs['coordinates'] = "lat lon" |
---|
89 | |
---|
90 | COTOTEVA = xr.DataArray (evap_oce*fract_oce_norm + evap_sic*fract_sic_norm, dims=('y', 'x')) |
---|
91 | COTOTEVA.attrs['coordinates'] = "lat lon" |
---|
92 | |
---|
93 | COICEVAP = xr.DataArray (evap_sic , dims=('y', 'x')) |
---|
94 | COICEVAP.attrs['long_name'] = 'Evaporation on sea ice' |
---|
95 | COICEVAP.attrs['coordinates'] = "lat lon" |
---|
96 | |
---|
97 | COQSRMIX = xr.DataArray (sols , dims=('y', 'x')) |
---|
98 | COQSRMIX.attrs['long_name'] = 'Heat flux short wave' |
---|
99 | COQSRMIX.attrs['units'] = 'W/m2' |
---|
100 | COQSRMIX.attrs['coordinates'] = "lat lon" |
---|
101 | |
---|
102 | COQNSMIX = xr.DataArray (soll , dims=('y', 'x')) |
---|
103 | COQNSMIX.attrs['long_name'] = 'Heat flux minus short wave' |
---|
104 | COQNSMIX.attrs['units'] = 'W/m2' |
---|
105 | COQNSMIX.attrs['coordinates'] = "lat lon" |
---|
106 | |
---|
107 | COSHFICE = xr.DataArray (sols , dims=('y', 'x')) |
---|
108 | COSHFICE.attrs['long_name'] = 'Heat flux short wave over sea ice' |
---|
109 | COSHFICE.attrs['units'] = 'W/m2' |
---|
110 | COSHFICE.attrs['coordinates'] = "lat lon" |
---|
111 | |
---|
112 | CONSFICE = xr.DataArray (soll , dims=('y', 'x')) |
---|
113 | CONSFICE.attrs['long_name'] = 'Heat flux minus short wave over sea ice' |
---|
114 | CONSFICE.attrs['units'] = 'W/m2' |
---|
115 | CONSFICE.attrs['coordinates'] = "lat lon" |
---|
116 | |
---|
117 | COWINDSP = xr.DataArray (wind10m , dims=('y', 'x')) |
---|
118 | COWINDSP.attrs['long_name'] = 'Wind speed at 10m high' |
---|
119 | COWINDSP.attrs['units'] = 'm/s' |
---|
120 | COWINDSP.attrs['coordinates'] = "lat lon" |
---|
121 | |
---|
122 | CODFLXDT = xr.DataArray (-20.0*np.ones(yxshape), dims=('y', 'x')) |
---|
123 | CODFLXDT.attrs['long_name'] = 'dQ/dT - Derivative over temperatue of turbulent heat fluxes' |
---|
124 | CODFLXDT.attrs['units'] = 'W/m2/K' |
---|
125 | CODFLXDT.attrs['coordinates'] = "lat lon" |
---|
126 | |
---|
127 | COCALVIN = xr.DataArray ( 0.0*np.ones(yxshape), dims=('y', 'x')) |
---|
128 | COCALVIN.attrs['long_name'] = 'Calving of icebergs, solid' |
---|
129 | COCALVIN.attrs['coordinates'] = "lat lon" |
---|
130 | |
---|
131 | COLIQRUN = xr.DataArray ( 0.0*np.ones(yxshape), dims=('y', 'x')) |
---|
132 | COLIQRUN.attrs['long_name'] = 'River run-off, liquid' |
---|
133 | COLIQRUN.attrs['coordinates'] = "lat lon" |
---|
134 | |
---|
135 | ## Wind stress |
---|
136 | tau_x = (taux_oce*fract_oce_norm + taux_sic*fract_sic_norm) |
---|
137 | tau_y = (tauy_oce*fract_oce_norm + tauy_sic*fract_sic_norm) |
---|
138 | COTAUMOD = xr.DataArray (np.sqrt ( (tau_x*tau_x + tau_y*tau_y) ) , dims=('y', 'x')) |
---|
139 | COTAUMOD.attrs['long_name'] = 'Wind stress modulus' |
---|
140 | COTAUMOD.attrs['units'] = 'Pa' |
---|
141 | COTAUMOD.attrs['coordinates'] = "lat lon" |
---|
142 | |
---|
143 | ## Wind stress, from east/north components to geocentric |
---|
144 | rad = np.deg2rad (1.0) |
---|
145 | COTAUXXU = xr.DataArray (-tau_x * np.sin(rad * lon) - tau_y * np.sin(rad * lat) * np.cos(rad * lon) , dims=('y', 'x')) |
---|
146 | COTAUYYU = xr.DataArray ( tau_x * np.cos(rad * lon) - tau_y * np.sin(rad * lat) * np.sin(rad * lon) , dims=('y', 'x')) |
---|
147 | COTAUZZU = xr.DataArray ( tau_y * np.cos(rad * lat) , dims=('y', 'x')) |
---|
148 | |
---|
149 | |
---|
150 | ## Value at North Pole |
---|
151 | if myargs.IsUnstructured == 'yes' : |
---|
152 | ## Value at North Pole for DYNAMICO grid |
---|
153 | COTAUXXU = xr.where ( lat >= 89.999, -tau_y, COTAUXXU) |
---|
154 | COTAUYYU = xr.where ( lat >= 89.999, tau_x, COTAUYYU) |
---|
155 | ## Value at South Pole for DYNAMICO grid ? |
---|
156 | |
---|
157 | else : |
---|
158 | ## Value at North Pole for LMDZ lon/lat grid |
---|
159 | COTAUXXU[0,:] = ( -tau_x [0, 0] ) |
---|
160 | COTAUYYU[0,:] = ( -tau_y [0, 0] ) |
---|
161 | COTAUZZU[0,:] = 0.0 ; |
---|
162 | ## Value at south Pole for LMDZ lon/lat grid |
---|
163 | COTAUXXU[-1,:] = ( -tau_x [-1, 0] ) |
---|
164 | COTAUYYU[-1,:] = ( -tau_y [-1, 0] ) |
---|
165 | |
---|
166 | ## |
---|
167 | COTAUXXU.attrs['long_name'] = 'Wind stress in geocentric referential - x-component' |
---|
168 | COTAUYYU.attrs['long_name'] = 'Wind stress in geocentric referential - y-component' |
---|
169 | COTAUZZU.attrs['long_name'] = 'Wind stress in geocentric referential - z-component' |
---|
170 | |
---|
171 | COTAUXXU.attrs['units'] = 'Pa' |
---|
172 | COTAUYYU.attrs['units'] = 'Pa' |
---|
173 | COTAUZZU.attrs['units'] = 'Pa' |
---|
174 | |
---|
175 | COTAUXXU.attrs['coordinates'] = "lat lon" |
---|
176 | COTAUYYU.attrs['coordinates'] = "lat lon" |
---|
177 | COTAUZZU.attrs['coordinates'] = "lat lon" |
---|
178 | |
---|
179 | COTAUXXV = COTAUXXU ; COTAUYYV = COTAUYYU ; COTAUZZV = COTAUZZU |
---|
180 | |
---|
181 | ## check if bounds for lon lat are present and add them to dataset or drop them |
---|
182 | ifbnds=True if ('bounds_lon' in d_In.data_vars) and ('bounds_lat' in d_In.data_vars) else False |
---|
183 | |
---|
184 | ## Creates final Dataset |
---|
185 | lon = xr.DataArray (lon, dims=('y', 'x')) |
---|
186 | lon.attrs['name'] = 'longitude' |
---|
187 | lon.attrs['long_name'] = 'Longitude' |
---|
188 | lon.attrs['units'] = 'degrees_east' |
---|
189 | if ifbnds: lon.attrs['bounds'] = 'bounds_lon' |
---|
190 | |
---|
191 | lat = xr.DataArray (lat, dims=('y', 'x')) |
---|
192 | lat.attrs['name'] = 'latitude' |
---|
193 | lat.attrs['long_name'] = 'Latitude' |
---|
194 | lat.attrs['units'] = 'degrees_north' |
---|
195 | if ifbnds: lat.attrs['bounds'] = 'bounds_lat' |
---|
196 | |
---|
197 | if ifbnds: |
---|
198 | bounds_lon = d_In.bounds_lon.values.astype (np.float64) |
---|
199 | bounds_lat = d_In.bounds_lat.values.astype (np.float64) |
---|
200 | nvertex = bounds_lon.shape[-1] |
---|
201 | |
---|
202 | bounds_lon = xr.DataArray ( np.reshape (bounds_lon, (ny,nx,nvertex)), dims=('y', 'x', 'nvertex')) |
---|
203 | bounds_lat = xr.DataArray ( np.reshape (bounds_lat, (ny,nx,nvertex)), dims=('y', 'x', 'nvertex')) |
---|
204 | |
---|
205 | bounds_lon.attrs['units'] = 'degrees_east' |
---|
206 | bounds_lat.attrs['units'] = 'degrees_north' |
---|
207 | |
---|
208 | # prepare dictionnary to export dataset to netcdf file with or without bounds |
---|
209 | dictdataset = {'lat':lat, 'lon':lon } |
---|
210 | if ifbnds: dictdataset.update({'bounds_lon':bounds_lon, 'bounds_lat':bounds_lat}) |
---|
211 | dictdataset.update({'COTOTRAI':COTOTRAI, |
---|
212 | 'COTOTSNO':COTOTSNO, |
---|
213 | 'COTOTEVA':COTOTEVA, |
---|
214 | 'COICEVAP':COICEVAP, |
---|
215 | 'COQSRMIX':COQSRMIX, |
---|
216 | 'COQNSMIX':COQNSMIX, |
---|
217 | 'COSHFICE':COSHFICE, |
---|
218 | 'CONSFICE':CONSFICE, |
---|
219 | 'CODFLXDT':CODFLXDT, |
---|
220 | 'COCALVIN':COCALVIN, |
---|
221 | 'COLIQRUN':COLIQRUN, |
---|
222 | 'COWINDSP':COWINDSP, |
---|
223 | 'COTAUMOD':COTAUMOD, |
---|
224 | 'COTAUXXU':COTAUXXU, |
---|
225 | 'COTAUYYU':COTAUYYU, |
---|
226 | 'COTAUZZU':COTAUZZU, |
---|
227 | 'COTAUXXV':COTAUXXV, |
---|
228 | 'COTAUYYV':COTAUYYV, |
---|
229 | 'COTAUZZV':COTAUZZV } ) |
---|
230 | |
---|
231 | d_Out = xr.Dataset (dictdataset) |
---|
232 | |
---|
233 | d_Out.attrs['Associated files'] = myargs.input |
---|
234 | d_Out.attrs['Conventions'] = "CF-1.6" |
---|
235 | d_Out.attrs['source'] = "IPSL Earth system model" |
---|
236 | d_Out.attrs['group'] = "ICMC IPSL Climate Modelling Center" |
---|
237 | d_Out.attrs['Institution'] = "IPSL https.//www.ipsl.fr" |
---|
238 | try : d_Out.attrs['directory'] = os.getcwd () |
---|
239 | except : pass |
---|
240 | try : d_Out.attrs['HOSTNAME'] = platform.node () |
---|
241 | except : pass |
---|
242 | try : d_Out.attrs['LOGNAME'] = os.getlogin () |
---|
243 | except : pass |
---|
244 | try : d_Out.attrs['Python'] = "Python version " + platform.python_version () |
---|
245 | except : pass |
---|
246 | try : d_Out.attrs['OS'] = platform.system () |
---|
247 | except : pass |
---|
248 | try : d_Out.attrs['release'] = platform.release () |
---|
249 | except : pass |
---|
250 | try : d_Out.attrs['hardware'] = platform.machine () |
---|
251 | except : pass |
---|
252 | d_Out.attrs['conventions'] = "SCRIP" |
---|
253 | d_Out.attrs['dest_grid'] = "curvilinear" |
---|
254 | d_Out.attrs['Model'] = "IPSL CM6" |
---|
255 | d_Out.attrs['SVN_Author'] = "$Author: omamce $" |
---|
256 | d_Out.attrs['SVN_Date'] = "$Date: 2022-07-06 11:06:07 +0200 (Wed, 06 Jul 2022) $" |
---|
257 | d_Out.attrs['SVN_Revision'] = "$Revision: 6190 $" |
---|
258 | d_Out.attrs['SVN_Id'] = "$Id: CalvingWeights.py 6190 2022-07-06 09:06:07Z omamce $" |
---|
259 | d_Out.attrs['SVN_HeadURL'] = "$HeadURL: svn+ssh://omamce@forge.ipsl.jussieu.fr/ipsl/forge/projets/igcmg/svn/TOOLS/MOSAIX/CalvingWeights.py $" |
---|
260 | |
---|
261 | |
---|
262 | d_Out.to_netcdf ( myargs.output, format=NcFormat ) |
---|
263 | d_Out.close() |
---|
264 | ## =========================================================================== |
---|
265 | ## |
---|
266 | ## That's all folk's !!! |
---|
267 | ## |
---|
268 | ## =========================================================================== |
---|