Index: TOOLS/MOSAIX/CalvingWeights.py
===================================================================
--- TOOLS/MOSAIX/CalvingWeights.py	(revision 6064)
+++ TOOLS/MOSAIX/CalvingWeights.py	(revision 6066)
@@ -14,5 +14,5 @@
 ##  personal.
 ##
-import netCDF4, numpy as np
+import numpy as np, xarray as xr
 import sys, os, platform, argparse, textwrap, time
 from scipy import ndimage
@@ -36,13 +36,19 @@
 
 # Adding arguments
-parser.add_argument ('--oce'     , help='oce model name', type=str, default='eORCA1.2', choices=['ORCA2.3', 'eORCA1.2', 'eORCA025', 'eORCA025.1'] )
+parser.add_argument ('--oce'     , help='oce model name', type=str, default='eORCA1.2',
+                         choices=['ORCA2.3', 'eORCA1.2', 'eORCA1.4', 'eORCA1.4.2', 'eORCA025', 'eORCA025.1', 'eORCA025.4'] )
 parser.add_argument ('--atm'     , help='atm model name (ICO* or LMD*)', type=str, default='ICO40'    )
 parser.add_argument ('--type'    , help='Type of distribution', type=str, choices=['iceshelf', 'iceberg', 'nosouth', 'full'], default='full'  )
-parser.add_argument ('--dir'     , help='Directory of input file', type=str, default='./' )
+## parser.add_argument ('--dir'     , help='Directory of input file', type=str, default='./' )
 parser.add_argument ('--repartition_file', help='Data files with iceberg melting' , type=str, default='./runoff-icb_DaiTrenberth_Depoorter_eORCA1_JD.nc' )
 parser.add_argument ('--repartition_var' , help='Variable name for iceshelf'      , type=str, default=None)
+parser.add_argument ('--grids' , help='grids file name', default='grids.nc' )
+parser.add_argument ('--areas' , help='masks file name', default='areas.nc' )
+parser.add_argument ('--masks' , help='areas file name', default='masks.nc' )
+parser.add_argument ('--o2a'   , help='o2a file name'  , default='o2a.nc'   )
 parser.add_argument ('--output'  , help='output rmp file name', default='rmp_tlmd_to_torc_calving.nc' )
-parser.add_argument ('--fmt'     , help='NetCDF file format, using nco syntax', default='netcdf4', choices=['classic', 'netcdf3', '64bit', '64bit_data', '64bit_data', 'netcdf4', 'netcdf4_classsic'] )
-parser.add_argument ('--ocePerio'   , help='periodicity of ocean grid', type=int, default=0 )
+parser.add_argument ('--fmt'     , help='NetCDF file format, using nco syntax', default='netcdf4',
+                         choices=['classic', 'netcdf3', '64bit', '64bit_data', '64bit_data', 'netcdf4', 'netcdf4_classsic'] )
+parser.add_argument ('--ocePerio', help='periodicity of ocean grid', type=int, default=0 )
 
 # Parse command line
@@ -89,25 +95,26 @@
 
 # Reading domains characteristics
-areas = myargs.dir + '/areas_' + dst_Name + 'x' + src_Name + '.nc' 
-masks = myargs.dir + '/masks_' + dst_Name + 'x' + src_Name + '.nc'
-grids = myargs.dir + '/grids_' + dst_Name + 'x' + src_Name + '.nc'
+grids = myargs.grids
+areas = myargs.areas
+masks = myargs.masks
+o2a   = myargs.o2a
 
 print ('Opening ' + areas)
-f_areas = netCDF4.Dataset ( areas, "r" )
+f_areas = xr.open_dataset ( areas )
 print ('Opening ' + masks)
-f_masks = netCDF4.Dataset ( masks, "r" )
+f_masks = xr.open_dataset ( masks )
 print ('Opening ' + grids)
-f_grids = netCDF4.Dataset ( grids, "r" )
-
-src_lon = f_grids.variables ['t' + src_name + '.lon'][:]
-src_lat = f_grids.variables ['t' + src_name + '.lat'][:]
-dst_lon = f_grids.variables ['t' + dst_name + '.lon'][:]
-dst_lat = f_grids.variables ['t' + dst_name + '.lat'][:]
-
-src_msk = f_masks.variables ['t' + src_name + '.msk'][:]
-dst_msk = f_masks.variables ['t' + dst_name + '.msk'][:]
+f_grids = xr.open_dataset ( grids )
+
+src_lon = f_grids ['t' + src_name + '.lon']
+src_lat = f_grids ['t' + src_name + '.lat']
+dst_lon = f_grids ['t' + dst_name + '.lon']
+dst_lat = f_grids ['t' + dst_name + '.lat']
+
+src_msk = f_masks ['t' + src_name + '.msk']
+dst_msk = f_masks ['t' + dst_name + '.msk']
 dst_mskutil = 1-dst_msk # Reversing the convention : 0 on continent, 1 on ocean
-print ('dst_msk     : ' + str(np.sum(dst_msk)))
-print ('dst_mskutil : ' + str(np.sum(dst_mskutil)))
+print ('dst_msk     : ', dst_msk.sum().values )
+print ('dst_mskutil : ', dst_mskutil.sum().values )
 
 # Ocean grid periodicity
@@ -116,9 +123,8 @@
 # Periodicity masking for NEMO
 if nperio_dst == 0 :
-    if dst_Name == 'ORCA2.3'    : nperio_dst = 4
-    if dst_Name == 'eORCA1.2'   : nperio_dst = 6
-    if dst_Name == 'ORCA025'    : nperio_dst = 6
-    if dst_Name == 'eORCA025'   : nperio_dst = 6
-    if dst_Name == 'eORCA025.1' : nperio_dst = 6
+    if dst_Name in ['ORCA2.3', 'ORCA2.4']            : nperio_dst = 4
+    if dst_Name in ['eORCA1.2', 'eORCA1.3']          : nperio_dst = 6
+    if dst_Name in ['ORCA025', 'eORCA025', 'eORCA025.1', 'eORCA025.4'] : nperio_dst = 6
+        
 print ('nperio_dst: ' + str(nperio_dst) )
 dst_mskutil = nemo.lbc_mask (dst_mskutil, nperio=nperio_dst, cd_type='T', sval=0 )
@@ -144,5 +150,5 @@
     dst_mskutil[997:1012,907] = 0
     
-if dst_Name in [ 'eORCA1.2', 'eORCA1.3', 'eORCA1.4' ] :
+if dst_Name in [ 'eORCA1.2', 'eORCA1.3', 'eORCA1.4', 'eORCA1.4.2' ] :
     print ('Filling some seas for ' + dst_Name)
     # Set Gibraltar strait to 0 to fill Mediterrean sea
@@ -184,8 +190,8 @@
 dst_srfutil_sum = np.sum( dst_srfutil)
 
-src_clo = f_grids.variables ['t' + src_name + '.clo'][:]
-src_cla = f_grids.variables ['t' + src_name + '.cla'][:]
-dst_clo = f_grids.variables ['t' + dst_name + '.clo'][:]
-dst_cla = f_grids.variables ['t' + dst_name + '.cla'][:]
+src_clo = f_grids ['t' + src_name + '.clo']
+src_cla = f_grids ['t' + src_name + '.cla']
+dst_clo = f_grids ['t' + dst_name + '.clo']
+dst_cla = f_grids ['t' + dst_name + '.cla']
 
 # Indices
@@ -198,6 +204,6 @@
     # Reading data file for calving or iceberg geometry around Antarctica
     print ( 'Opening ' + myargs.repartition_file)
-    f_repartition = netCDF4.Dataset ( myargs.repartition_file, "r" )
-    repartition = np.sum ( f_repartition.variables [repartitionVar][:], axis=0 )
+    f_repartition = xr.open_dataset ( myargs.repartition_file )
+    repartition = np.sum ( f_repartition.variables [repartitionVar], axis=0 )
 
 ## Before loop on basins
@@ -246,9 +252,9 @@
 
         # Integral and normalisation
-        sum_repartition = np.sum ( key_repartition[n_bas] * dst_srfutil )
+        sum_repartition = np.sum ( key_repartition[n_bas] * dst_srfutil ).values
         key_repartition = key_repartition / sum_repartition
     
         print ( 'Sum of repartition key                      : {:12.3e}'.format (np.sum (key_repartition[n_bas]               )) )
-        print ( 'Integral (area weighted) of repartition key : {:12.3e}'.format (np.sum (key_repartition[n_bas] * dst_srfutil )) )
+        print ( 'Integral (area weighted) of repartition key : {:12.3e}'.format (np.sum (key_repartition[n_bas] * dst_srfutil ).values) )
     
         # Basin surface (modulated by repartition key)
@@ -257,5 +263,5 @@
         # Weights and links
         poids = 1.0 / basin_srfutil
-        matrix_local   = np.where ( basin_msk[n_bas].ravel() > 0.5, key_repartition[n_bas].ravel()*poids, 0. )
+        matrix_local   = np.where ( basin_msk[n_bas].ravel() > 0.5, key_repartition[n_bas].ravel()*poids.values, 0. )
         matrix_local   = matrix_local[matrix_local > 0.0] # Keep only non zero values
         num_links      = np.count_nonzero (matrix_local)
@@ -297,161 +303,147 @@
 # Output file
 calving = myargs.output
-f_calving = netCDF4.Dataset ( calving, 'w', format=FmtNetcdf )
     
 print ('Output file: ' + calving )
 
-f_calving.Conventions     = "CF-1.6"
-f_calving.source          = "IPSL Earth system model"
-f_calving.group           = "ICMC IPSL Climate Modelling Center"
-f_calving.Institution     = "IPSL https.//www.ipsl.fr"
-f_calving.Ocean           = dst_Name + " https://www.nemo-ocean.eu"
-f_calving.Atmosphere      = src_Name + " http://lmdz.lmd.jussieu.fr"
-if myargs.type in ['iceberg', 'iceshelf' ]: f_calving.originalFiles = myargs.repartition_file
-f_calving.associatedFiles = grids + " " + areas + " " + masks
-f_calving.directory       = os.getcwd ()
-f_calving.description     = "Generated with XIOS http://forge.ipsl.jussieu.fr/ioserver and MOSAIX https://forge.ipsl.jussieu.fr/igcmg/browser/TOOLS/MOSAIX"
-f_calving.title           = calving
-f_calving.Program         = "Generated by " + sys.argv[0] + " with flags " + str(sys.argv[1:])
-f_calving.repartitionType = myargs.type
-if myargs.type in [ 'iceberg', 'iceshelf' ] :
-    f_calving.repartitionFile = myargs.repartition_file
-    f_calving.repartitionVar  = repartitionVar
-f_calving.gridsFile       = grids
-f_calving.areasFile       = areas
-f_calving.masksFile       = masks
-f_calving.timeStamp       = time.asctime()
-f_calving.HOSTNAME        = platform.node()
-#f_calving.LOGNAME         = os.getlogin()
-f_calving.Python          = "Python version " +  platform.python_version()
-f_calving.OS              = platform.system()
-f_calving.release         = platform.release()
-f_calving.hardware        = platform.machine()
-f_calving.conventions     = "SCRIP"
-if src_name == 'lmd' : f_calving.source_grid = "curvilinear"
-if src_name == 'ico' : f_calving.source_grid = "unstructured"
-f_calving.dest_grid       = "curvilinear"
-f_calving.Model           = "IPSL CM6"
-f_calving.SVN_Author      = "$Author$"
-f_calving.SVN_Date        = "$Date$"
-f_calving.SVN_Revision    = "$Revision$"
-f_calving.SVN_Id          = "$Id$"
-f_calving.SVN_HeadURL     = "$HeadURL$"
-
-d_nb_zone   = f_calving.createDimension ('nb_zone'         ,   nb_zone )
-d_num_links = f_calving.createDimension ('num_links'       , num_links )
-d_num_wgts  = f_calving.createDimension ('num_wgts'        ,         1 )
-
-d_src_grid_size    = f_calving.createDimension ('src_grid_size'   , src_grid_size )
-d_src_grid_corners = f_calving.createDimension ('src_grid_corners', src_clo.shape[0]  )
-d_src_grid_rank    = f_calving.createDimension ('src_grid_rank'   ,        2  )
-
-d_dst_grid_size    = f_calving.createDimension ('dst_grid_size'   , dst_grid_size )
-d_dst_grid_corners = f_calving.createDimension ('dst_grid_corners', dst_clo.shape[0] )
-d_dst_grid_rank    = f_calving.createDimension ('dst_grid_rank'   ,        2  )
-
-v_remap_matrix = f_calving.createVariable ( 'remap_matrix', 'f8', ('num_links', 'num_wgts') )
-v_src_address  = f_calving.createVariable ( 'src_address' , 'i4', ('num_links',) )
-v_src_address.convention = "Fortran style addressing, starting at 1"
-v_dst_address  = f_calving.createVariable ( 'dst_address' , 'i4', ('num_links',) )
-v_dst_address.convention = "Fortran style addressing, starting at 1"
-
-v_remap_matrix[:] = remap_matrix
-v_src_address [:] = src_address
-v_dst_address [:] = dst_address
-
-v_src_grid_dims       = f_calving.createVariable ( 'src_grid_dims'      , 'i4', ('src_grid_rank',) )
-v_src_grid_center_lon = f_calving.createVariable ( 'src_grid_center_lon', 'i4', ('src_grid_size',) )
-v_src_grid_center_lat = f_calving.createVariable ( 'src_grid_center_lat', 'i4', ('src_grid_size',) )
-v_src_grid_center_lon.units='degrees_east'  ; v_src_grid_center_lon.long_name='Longitude'
-v_src_grid_center_lon.long_name='longitude' ; v_src_grid_center_lon.bounds="src_grid_corner_lon" 
-v_src_grid_center_lat.units='degrees_north' ; v_src_grid_center_lat.long_name='Latitude'
-v_src_grid_center_lat.long_name='latitude ' ; v_src_grid_center_lat.bounds="src_grid_corner_lat" 
-v_src_grid_corner_lon = f_calving.createVariable ( 'src_grid_corner_lon', 'f8', ('src_grid_size', 'src_grid_corners')  )
-v_src_grid_corner_lat = f_calving.createVariable ( 'src_grid_corner_lat', 'f8', ('src_grid_size', 'src_grid_corners')  )
-v_src_grid_corner_lon.units="degrees_east"
-v_src_grid_corner_lat.units="degrees_north"
-v_src_grid_area       = f_calving.createVariable ( 'src_grid_area'      , 'f8', ('src_grid_size',)  )
-v_src_grid_area.long_name="Grid area" ; v_src_grid_area.standard_name="cell_area" ; v_src_grid_area.units="m2"
-v_src_grid_imask      = f_calving.createVariable ( 'src_grid_imask'     , 'f8', ('src_grid_size',)  )
-v_src_grid_imask.long_name="Land-sea mask" ; v_src_grid_imask.units="Land:1, Ocean:0"
-
-v_src_grid_dims      [:] = ( src_jpi, src_jpi ) 
-v_src_grid_center_lon[:] = src_lon[:].ravel()
-v_src_grid_center_lat[:] = src_lat[:].ravel()
-v_src_grid_corner_lon[:] = src_clo.reshape( (src_jpi*src_jpj, d_src_grid_corners.__len__()) )
-v_src_grid_corner_lat[:] = src_cla.reshape( (src_jpi*src_jpj, d_src_grid_corners.__len__()) )
-v_src_grid_area      [:] = src_srf[:].ravel()
-v_src_grid_imask     [:] = src_msk[:].ravel()
+remap_matrix = xr.DataArray (np.reshape(remap_matrix, (num_links, 1)), dims = ['num_links', 'num_wgts'] )
+src_address  = xr.DataArray (src_address.astype(np.int32) , dims = ['num_links',] )
+src_address.attrs['convention'] = "Fortran style addressing, starting at 1"
+dst_address  = xr.DataArray (dst_address.astype(np.int32) , dims =  ['num_links',] )
+dst_address.attrs['convention'] = "Fortran style addressing, starting at 1"
+
+src_grid_dims       = xr.DataArray ( np.array (( src_jpi, src_jpi ), dtype=np.int32), dims =  ['src_grid_rank',] )
+src_grid_center_lon = xr.DataArray ( src_lon.values.ravel(), dims = ['src_grid_size',] )
+src_grid_center_lat = xr.DataArray ( src_lat.values.ravel(), dims = ['src_grid_size',] )
+src_grid_center_lon.attrs['units']='degrees_east'  ; src_grid_center_lon.attrs['long_name']='Longitude'
+src_grid_center_lon.attrs['long_name']='longitude' ; src_grid_center_lon.attrs['bounds']="src_grid_corner_lon" 
+src_grid_center_lat.attrs['units']='degrees_north' ; src_grid_center_lat.attrs['long_name']='Latitude'
+src_grid_center_lat.attrs['long_name']='latitude ' ; src_grid_center_lat.attrs['bounds']="src_grid_corner_lat" 
+src_grid_corner_lon = xr.DataArray ( src_clo.values.reshape( (src_jpi*src_jpj, src_clo.shape[0]) ), dims = ['src_grid_size', 'src_grid_corners'] )
+src_grid_corner_lat = xr.DataArray ( src_cla.values.reshape( (src_jpi*src_jpj, src_clo.shape[0]) ), dims = ['src_grid_size', 'src_grid_corners' ] )
+src_grid_corner_lon.attrs['units']="degrees_east"
+src_grid_corner_lat.attrs['units']="degrees_north"
+src_grid_area       = xr.DataArray ( src_srf.values.ravel(), dims = ['src_grid_size',] )
+src_grid_area.attrs['long_name']="Grid area" ; src_grid_area.attrs['standard_name']="cell_area" ; src_grid_area.attrs['units']="m2"
+src_grid_imask      = xr.DataArray ( src_msk.values.ravel().astype(np.int32) , dims = ['src_grid_size',] )
+src_grid_imask.attrs['long_name']="Land-sea mask" ; src_grid_imask.attrs['units']="Land:1, Ocean:0"
 
 # --
-
-v_dst_grid_dims       = f_calving.createVariable ( 'dst_grid_dims'      , 'i4', ('dst_grid_rank',) )
-v_dst_grid_center_lon = f_calving.createVariable ( 'dst_grid_center_lon', 'i4', ('dst_grid_size',) )
-v_dst_grid_center_lat = f_calving.createVariable ( 'dst_grid_center_lat', 'i4', ('dst_grid_size',) )
-v_dst_grid_center_lon.units='degrees_east'  ; v_dst_grid_center_lon.long_name='Longitude'
-v_dst_grid_center_lon.long_name='longitude' ; v_dst_grid_center_lon.bounds="dst_grid_corner_lon" 
-v_dst_grid_center_lat.units='degrees_north' ; v_dst_grid_center_lat.long_name='Latitude'
-v_dst_grid_center_lat.long_name='latitude'  ; v_dst_grid_center_lat.bounds="dst_grid_corner_lat" 
-v_dst_grid_corner_lon = f_calving.createVariable ( 'dst_grid_corner_lon', 'f8', ('dst_grid_size', 'dst_grid_corners')  )
-v_dst_grid_corner_lat = f_calving.createVariable ( 'dst_grid_corner_lat', 'f8', ('dst_grid_size', 'dst_grid_corners')  )
-v_dst_grid_corner_lon.units="degrees_east"
-v_dst_grid_corner_lat.units="degrees_north"
-v_dst_grid_area       = f_calving.createVariable ( 'dst_grid_area'      , 'f8', ('dst_grid_size',)  )
-v_dst_grid_area.long_name="Grid area" ; v_dst_grid_area.standard_name="cell_area" ; v_dst_grid_area.units="m2"
-v_dst_grid_imask      = f_calving.createVariable ( 'dst_grid_imask'     , 'f8', ('dst_grid_size',)  )
-v_dst_grid_imask.long_name="Land-sea mask" ; v_dst_grid_imask.units="Land:1, Ocean:0"
-
-v_dst_bassin      = f_calving.createVariable ( 'dst_bassin'      , 'f8', ('nb_zone', 'dst_grid_size',)  )
-v_dst_repartition = f_calving.createVariable ( 'dst_repartition' , 'f8', ('nb_zone', 'dst_grid_size',)  )
-
-v_dst_southLimit = f_calving.createVariable ('dst_southLimit', 'f4', ('nb_zone',) )
-v_dst_northLimit = f_calving.createVariable ('dst_northLimit', 'f4', ('nb_zone',) )
-v_dst_southLimit[:] = np.min(limit_lat, axis=(1,) ) 
-v_dst_northLimit[:] = np.max(limit_lat, axis=(1,) ) 
-
-v_dst_grid_dims      [:] = ( dst_jpi, dst_jpi ) 
-v_dst_grid_center_lon[:] = dst_lon[:].ravel()
-v_dst_grid_center_lat[:] = dst_lat[:].ravel()
-v_dst_grid_corner_lon[:] = dst_clo.reshape( (dst_jpi*dst_jpj, d_dst_grid_corners.__len__()) )
-v_dst_grid_corner_lat[:] = dst_cla.reshape( (dst_jpi*dst_jpj, d_dst_grid_corners.__len__()) )
-v_dst_grid_area      [:] = dst_srf[:].ravel()
-v_dst_grid_imask     [:] = dst_msk[:].ravel()
-
-v_dst_bassin[:]      = basin_msk.reshape      ( (nb_zone,dst_grid_size) )
-v_dst_repartition[:] = key_repartition.reshape( (nb_zone,dst_grid_size) )
+dst_grid_dims       = xr.DataArray ( np.array(( dst_jpi, dst_jpi), dtype=np.int32) , dims = ['dst_grid_rank', ])
+dst_grid_center_lon = xr.DataArray ( dst_lon.values.ravel(), dims = ['dst_grid_size', ])
+dst_grid_center_lat = xr.DataArray ( dst_lat.values.ravel(), dims = ['dst_grid_size', ])
+dst_grid_center_lon.attrs['units']='degrees_east'  ; dst_grid_center_lon.attrs['long_name']='Longitude'
+dst_grid_center_lon.attrs['long_name']='longitude' ; dst_grid_center_lon.attrs['bounds']="dst_grid_corner_lon" 
+dst_grid_center_lat.attrs['units']='degrees_north' ; dst_grid_center_lat.attrs['long_name']='Latitude'
+dst_grid_center_lat.attrs['long_name']='latitude'  ; dst_grid_center_lat.attrs['bounds']="dst_grid_corner_lat" 
+dst_grid_corner_lon = xr.DataArray ( dst_clo.values.reshape(dst_jpi*dst_jpj, dst_clo.shape[0] ), dims = ['dst_grid_size', 'dst_grid_corners'] )
+dst_grid_corner_lat = xr.DataArray ( dst_cla.values.reshape(dst_jpi*dst_jpj, dst_clo.shape[0] ), dims = ['dst_grid_size', 'dst_grid_corners'] )
+dst_grid_corner_lon.attrs['units']="degrees_east"
+dst_grid_corner_lat.attrs['units']="degrees_north"
+dst_grid_area       = xr.DataArray ( dst_srf.values.ravel(), dims = ['dst_grid_size', ] )
+dst_grid_area.attrs['long_name']="Grid area" ; dst_grid_area.attrs['standard_name']="cell_area" ; dst_grid_area.attrs['units']="m2"
+dst_grid_imask      = xr.DataArray ( dst_msk.values.ravel(), dims =  ['dst_grid_size',]  )
+dst_grid_imask.attrs['long_name']="Land-sea mask" ; dst_grid_imask.attrs['units']="Land:1, Ocean:0"
+
+dst_bassin      = xr.DataArray ( basin_msk.reshape ( (nb_zone,dst_grid_size) ) , dims = ['nb_zone', 'dst_grid_size',  ] )
+dst_repartition = xr.DataArray ( key_repartition.reshape( (nb_zone,dst_grid_size) ) , dims = ['nb_zone', 'dst_grid_size',  ] )
+
+dst_southLimit = xr.DataArray ( np.min(limit_lat, axis=(1,)).astype(np.float32) , dims = ['nb_zone', ] )
+dst_northLimit = xr.DataArray ( np.min(limit_lat, axis=(1,)).astype(np.float32) , dims = ['nb_zone', ] )
 
 # Additionnal fields for debugging
 # ================================
-v_dst_grid_imaskutil      = f_calving.createVariable ( 'dst_grid_imaskutil'     , 'f8', ('dst_grid_size',)  )
-v_dst_grid_imaskutil.long_name="Land-sea mask" ; v_dst_grid_imaskutil.units="Land:1, Ocean:0"
-v_dst_grid_imaskclose      = f_calving.createVariable ( 'dst_grid_imaskclose'     , 'f8', ('dst_grid_size',)  )
-v_dst_grid_imaskclose.long_name="Land-sea mask" ; v_dst_grid_imaskclose.units="Land:1, Ocean:0"
-v_dst_grid_imaskutil [:] = dst_mskutil[:].ravel()
-v_dst_grid_imaskclose[:] = dst_closed[:].ravel()
-
-v_dst_lon_addressed = f_calving.createVariable ( 'dst_lon_addressed', 'f8', ('num_links',) )
-v_dst_lat_addressed = f_calving.createVariable ( 'dst_lat_addressed', 'f8', ('num_links',) )
-v_dst_lon_addressed.long_name="Longitude" ; v_dst_lon_addressed.standard_name="longitude" ; v_dst_lon_addressed.units="degrees_east"
-v_dst_lat_addressed.long_name="Latitude"  ; v_dst_lat_addressed.standard_name="latitude"  ; v_dst_lat_addressed.units="degrees_north"
-v_dst_area_addressed  = f_calving.createVariable ( 'dst_area_addressed', 'f8', ('num_links',) )
-v_dst_area_addressed.long_name="Grid area" ; v_dst_area_addressed.standard_name="cell_area" ; v_dst_grid_area.units="m2"
-v_dst_imask_addressed = f_calving.createVariable ( 'dst_imask_addressed', 'i4', ('num_links',) )
-v_dst_imask_addressed.long_name="Land-sea mask" ; v_dst_imask_addressed.units="Land:1, Ocean:0"
-v_dst_imaskutil_addressed = f_calving.createVariable ( 'dst_imaskutil_addressed', 'i4', ('num_links',) )
-v_dst_imaskutil_addressed.long_name="Land-sea mask" ; v_dst_imaskutil_addressed.units="Land:1, Ocean:0"
-
-v_dst_lon_addressed  [:] = dst_lon.ravel()[dst_address-1].ravel()
-v_dst_lat_addressed  [:] = dst_lat.ravel()[dst_address-1].ravel()
-v_dst_area_addressed [:] = dst_srf[:].ravel()[dst_address-1].ravel()
-v_dst_imask_addressed[:] = dst_msk[:].ravel()[dst_address-1].ravel()
-v_dst_imaskutil_addressed[:] = dst_mskutil.ravel()[dst_address-1].ravel()
-
-v_src_field = f_calving.createVariable ( 'src_field', 'f8', ('src_grid_size',) )
-v_dst_field = f_calving.createVariable ( 'dst_field', 'f8', ('dst_grid_size',) )
-v_src_field[:] = src_field
-v_dst_field[:] = dst_field
-
-f_calving.close ()
-
-
-### ===== That's all Folk's !! ==============================================
+dst_grid_imaskutil  = xr.DataArray ( dst_mskutil.ravel().astype(np.float32), dims = ['dst_grid_size',] )
+dst_grid_imaskutil.attrs['long_name']="Land-sea mask" ; dst_grid_imaskutil.attrs['units']="Land:1, Ocean:0"
+dst_grid_imaskclose = xr.DataArray ( dst_closed.values.ravel(), dims = ['dst_grid_size',] )
+dst_grid_imaskclose.attrs['long_name']="Land-sea mask" ; dst_grid_imaskclose.attrs['units']="Land:1, Ocean:0"
+
+dst_lon_addressed = xr.DataArray ( dst_lon.values.ravel()[dst_address-1].ravel(), dims = ['num_links',] )
+dst_lat_addressed = xr.DataArray ( dst_lat.values.ravel()[dst_address-1].ravel(), dims = ['num_links',] )
+dst_lon_addressed.attrs['long_name']="Longitude" ; dst_lon_addressed.attrs['standard_name']="longitude" ; dst_lon_addressed.attrs['units']="degrees_east"
+dst_lat_addressed.attrs['long_name']="Latitude"  ; dst_lat_addressed.attrs['standard_name']="latitude"  ; dst_lat_addressed.attrs['units']="degrees_north"
+dst_area_addressed  = xr.DataArray ( dst_srf.values.ravel()[dst_address-1].ravel(), dims = ['num_links',] )
+dst_area_addressed.attrs['long_name']="Grid area" ; dst_area_addressed.attrs['standard_name']="cell_area" ; dst_grid_area.attrs['units']="m2"
+dst_imask_addressed = xr.DataArray ( dst_msk.values.ravel()[dst_address-1].ravel(), dims = ['num_links', ] )
+dst_imask_addressed.attrs['long_name']="Land-sea mask" ; dst_imask_addressed.attrs['units']="Land:1, Ocean:0"
+dst_imaskutil_addressed = xr.DataArray ( dst_mskutil.ravel()[dst_address-1].astype(np.float32), dims = ['num_links'] )
+dst_imaskutil_addressed.attrs['long_name']="Land-sea mask" ; dst_imaskutil_addressed.attrs['units']="Land:1, Ocean:0"
+
+src_field = xr.DataArray ( src_field, dims = ['src_grid_size',] )
+dst_field = xr.DataArray ( dst_field, dims = ['dst_grid_size',] )
+
+f_calving =  xr.Dataset ( { 
+    'remap_matrix'            : remap_matrix,
+	'src_address'             : src_address,
+	'dst_address'             : dst_address,
+	'src_grid_dims'           : src_grid_dims,
+	'src_grid_center_lon'     : src_grid_center_lat,
+	'src_grid_center_lat'     : src_grid_center_lat,
+	'src_grid_corner_lon'     : src_grid_corner_lon,
+	'src_grid_corner_lat'     : src_grid_corner_lat,
+	'src_grid_area'           : src_grid_area,
+	'src_grid_imask'          : src_grid_imask,
+	'dst_grid_dims'           : dst_grid_dims,
+	'dst_grid_center_lon'     : dst_grid_center_lon,
+	'dst_grid_center_lat'     : dst_grid_center_lat,
+	'dst_grid_corner_lon'     : dst_grid_corner_lon,
+	'dst_grid_corner_lat'     : dst_grid_corner_lat,
+	'dst_grid_area'           : dst_grid_area,
+	'dst_grid_imask'          : dst_grid_imask,
+	'dst_bassin'              : dst_bassin,
+	'dst_repartition'         : dst_repartition,
+	'dst_southLimit'          : dst_southLimit,
+	'dst_northLimit'          : dst_northLimit,
+	'dst_grid_imaskutil'      : dst_grid_imaskutil,
+	'dst_grid_imaskclose'     : dst_grid_imaskclose,
+	'dst_lon_addressed'       : dst_lon_addressed,
+	'dst_lat_addressed'       : dst_lat_addressed,
+	'dst_area_addressed'      : dst_area_addressed,
+	'dst_imask_addressed'     : dst_imask_addressed,
+	'dst_imaskutil_addressed' : dst_imaskutil_addressed,
+	'src_field'               : src_field,
+	'dst_field'               : dst_field,
+    } )
+
+f_calving.attrs['Conventions']     = "CF-1.6"
+f_calving.attrs['source']          = "IPSL Earth system model"
+f_calving.attrs['group']           = "ICMC IPSL Climate Modelling Center"
+f_calving.attrs['Institution']     = "IPSL https.//www.ipsl.fr"
+f_calving.attrs['Ocean']           = dst_Name + " https://www.nemo-ocean.eu"
+f_calving.attrs['Atmosphere']      = src_Name + " http://lmdz.lmd.jussieu.fr"
+if myargs.type in ['iceberg', 'iceshelf' ]: f_calving.attrs['originalFiles'] = myargs.repartition_file
+f_calving.attrs['associatedFiles'] = grids + " " + areas + " " + masks
+f_calving.attrs['directory']       = os.getcwd ()
+f_calving.attrs['description']     = "Generated with XIOS http://forge.ipsl.jussieu.fr/ioserver and MOSAIX https://forge.ipsl.jussieu.fr/igcmg/browser/TOOLS/MOSAIX"
+f_calving.attrs['title']           = calving
+f_calving.attrs['Program']         = "Generated by " + sys.argv[0] + " with flags " + str(sys.argv[1:])
+f_calving.attrs['repartitionType'] = myargs.type
+if myargs.type in [ 'iceberg', 'iceshelf' ] :
+    f_calving.attrs['repartitionFile'] = myargs.repartition_file
+    f_calving.attrs['repartitionVar']  = repartitionVar
+f_calving.attrs['gridsFile']       = grids
+f_calving.attrs['areasFile']       = areas
+f_calving.attrs['masksFile']       = masks
+f_calving.attrs['timeStamp']       = time.asctime()
+f_calving.attrs['HOSTNAME']        = platform.node()
+f_calving.attrs['LOGNAME']         = os.getlogin()
+f_calving.attrs['Python']          = "Python version " +  platform.python_version()
+f_calving.attrs['OS']              = platform.system()
+f_calving.attrs['release']         = platform.release()
+f_calving.attrs['hardware']        = platform.machine()
+f_calving.attrs['conventions']     = "SCRIP"
+if src_name == 'lmd' : f_calving.attrs['source_grid'] = "curvilinear"
+if src_name == 'ico' : f_calving.attrs['source_grid'] = "unstructured"
+f_calving.attrs['dest_grid']       = "curvilinear"
+f_calving.attrs['Model']           = "IPSL CM6"
+f_calving.attrs['SVN_Author']      = "$Author$"
+f_calving.attrs['SVN_Date']        = "$Date$"
+f_calving.attrs['SVN_Revision']    = "$Revision$"
+f_calving.attrs['SVN_Id']          = "$Id$"
+f_calving.attrs['SVN_HeadURL']     = "$HeadURL$"
+
+##
+f_calving.to_netcdf ( calving, mode='w', format=FmtNetcdf )
+f_calving.close()
+
+print ('That''s all folks !')
+## ======================================================================================
