#!/usr/bin/env python

import sys
import os
from venv import create
from mpi4py import MPI
import warnings
from nes.nc_projections.rotated_nes import RotatedNes
from nes.nc_projections.latlon_nes import LatLonNes

def create_nes(comm=None, info=False, projection=None, create_nes=True, **kwargs):
    
    if comm is None:
        comm = MPI.COMM_WORLD
    else:
        comm = comm

    # Check if the parameters that are required to create the object have been defined in kwargs
    kwargs_list = []
    for name, value in kwargs.items():
        kwargs_list.append(name)

    if projection == 'regular':
        required_vars = ['lat_orig', 'lon_orig', 'inc_lat', 'inc_lon', 'n_lat', 'n_lon']
    elif projection == 'rotated':
        required_vars = ['centre_lat', 'centre_lon', 'west_boundary', 'south_boundary', 'inc_rlat', 'inc_rlon']
    elif projection == 'lcc':
        required_vars = ['lat_1', 'lat_2', 'lon_0', 'lat_0', 'nx', 'ny', 'inc_x', 'inc_y', 'x_0', 'y_0']
    elif projection == 'mercator':
        required_vars = ['lat_ts', 'lon_0', 'nx', 'ny', 'inc_x', 'inc_y', 'x_0', 'y_0']

    for var in required_vars:
        if var not in kwargs_list:
            msg = 'WARNING!!! '
            msg += 'Variable {0} has not been defined.'.format(var)
            warnings.warn(msg)

    if projection == 'regular':
        nessy = LatLonNes(comm=comm, dataset=None, xarray=None, info=info, parallel_method=None,
                          avoid_first_hours=0, avoid_last_hours=0, first_level=0, last_level=None,
                          create_nes=create_nes, **kwargs)

    elif projection == 'rotated':
        nessy = RotatedNes(comm=comm, dataset=None, xarray=None, info=info, parallel_method=None,
                           avoid_first_hours=0, avoid_last_hours=0, first_level=0, last_level=None, 
                           create_nes=create_nes, **kwargs)
    
    return nessy