# pylint: disable=no-member
import netCDF4
import numpy as np

def read_grid(id_begi, id_begj, id_lon, id_lat, nc_atmos, data_filename):
    gf = netCDF4.Dataset(data_filename, 'r')
    # Remember Fortran is column-first while Python is row-first
    dda_lon = gf['lon'][id_begj:id_begj+id_lat,id_begi:id_begi+id_lon]
    dda_lat = gf['lat'][id_begj:id_begj+id_lat,id_begi:id_begi+id_lon]
    dda_clo = gf['clo'][id_begj:id_begj+id_lat,id_begi:id_begi+id_lon,:nc_atmos]
    dda_cla = gf['cla'][id_begj:id_begj+id_lat,id_begi:id_begi+id_lon,:nc_atmos]
    dda_srf = gf['srf'][id_begj:id_begj+id_lat,id_begi:id_begi+id_lon]
    ida_mask = gf['imask'][id_begi:,id_begj:]
    gf.close()

    # OASIS3 mask convention (1=masked, 0=not masked) is opposite to usual one)
    ida_mask ^= 1

    return dda_lon, dda_lat, dda_clo, dda_cla, dda_srf, ida_mask