#!/usr/bin/env python

import sys
from mpi4py import MPI
from nes import *

comm = MPI.COMM_WORLD
rank = comm.Get_rank()

# CREATE GRID
lat_1 = 37
lat_2 = 43
lon_0 = -3
lat_0 = 40
nx = 10
ny = 15
inc_x = 4000
inc_y = 4000
x_0 = -807847.688
y_0 = -797137.125
nessy_1 = create_nes(comm=None, info=False, projection='lcc',
                      lat_1=lat_1, lat_2=lat_2, lon_0=lon_0, lat_0=lat_0, 
                      nx=nx, ny=ny, inc_x=inc_x, inc_y=inc_y, x_0=x_0, y_0=y_0)

# CALCULATE AREA OF EACH CELL IN GRID
nessy_1.calculate_grid_area()

print('Rank: {0}. Calcule cell area: {1}'.format(rank, nessy_1.cell_measures['cell_area']))

# WRITE
nessy_1.to_netcdf('nessy_1_nes_area.nc')

# REOPEN
nessy_2 = open_netcdf('nessy_1_nes_area.nc')

print('Rank: {0}. Write cell area: {1}'.format(rank, nessy_2.cell_measures['cell_area']))

comm.Barrier()
sys.stdout.flush()
