<commit_before>import numpy as np
from netCDF4 import Dataset
from parcels.field import Field


__all__ = ['NEMOGrid']


class NEMOGrid(object):
    """Grid class used to generate and read NEMO output files

    :param lon_u: Longitude coordinates of the U components
    :param lat_u: Latitude coordinates of the U components
    :param lon_v: Longitude coordinates of the V components
    :param lat_v: Latitude coordinates of the V components
    :param depth: Depth coordinates of the grid
    :param time: Time coordinates of the grid
    :param U: Zonal velocity component
    :param V: Meridional velocity component"""

    # Particle set
    _particles = []

    def __init__(self, lon_u, lat_u, lon_v, lat_v, depth, time, U, V):
        """Initialise Grid object from raw data"""
        # Grid dimension arrays
        self.depth = depth
        self.time_counter = time

        # Velocity data
        self.U = Field('U', U, lon_u, lat_u)
        self.V = Field('V', V, lon_v, lat_v)

    @classmethod
    def from_file(cls, filename):
        """Initialises grid data from files using NEMO conventions.

        :param filename: Base name of a set of NEMO files
        """
        dset_u = Dataset('%s_U.nc' % filename, 'r', format="NETCDF4")
        dset_v = Dataset('%s_V.nc' % filename, 'r', format="NETCDF4")

        # Get U, V and flow-specific lat/lon from netCF file
        lon_u = dset_u['nav_lon'][0, :]
        lat_u = dset_u['nav_lat'][:, 0]
        lon_v = dset_v['nav_lon'][0, :]
        lat_v = dset_v['nav_lat'][:, 0]
        depth = np.zeros(1, dtype=np.float32)
        time = np.zeros(1, dtype=np.float32)

        u = dset_u['vozocrtx'][0, 0, :, :]
        v = dset_v['vomecrty'][0, 0, :, :]

        # Hack around the fact that NaN values propagate in SciPy's interpolators
        u[np.isnan(u)] = 0.
        v[np.isnan(v)] = 0.
<commit_msg>Grid: Explicitly check if velocity grid files exist<commit_after>import numpy as np
from netCDF4 import Dataset
from parcels.field import Field
from py import path


__all__ = ['NEMOGrid']


class NEMOGrid(object):
    """Grid class used to generate and read NEMO output files

    :param lon_u: Longitude coordinates of the U components
    :param lat_u: Latitude coordinates of the U components
    :param lon_v: Longitude coordinates of the V components
    :param lat_v: Latitude coordinates of the V components
    :param depth: Depth coordinates of the grid
    :param time: Time coordinates of the grid
    :param U: Zonal velocity component
    :param V: Meridional velocity component"""

    # Particle set
    _particles = []

    def __init__(self, lon_u, lat_u, lon_v, lat_v, depth, time, U, V):
        """Initialise Grid object from raw data"""
        # Grid dimension arrays
        self.depth = depth
        self.time_counter = time

        # Velocity data
        self.U = Field('U', U, lon_u, lat_u)
        self.V = Field('V', V, lon_v, lat_v)

    @classmethod
    def from_file(cls, filename):
        """Initialises grid data from files using NEMO conventions.

        :param filename: Base name of a set of NEMO files
        """
        filepath_u = path.local("%s_U.nc" % filename)
        filepath_v = path.local("%s_V.nc" % filename)
        if not filepath_u.exists():
            raise IOError("Grid file not found: %s" % filepath_u)
        if not path.local(filepath_v).exists():
            raise IOError("Grid file not found: %s" % filepath_v)
        dset_u = Dataset(str(filepath_u), 'r', format="NETCDF4")
        dset_v = Dataset(str(filepath_v), 'r', format="NETCDF4")

        # Get U, V and flow-specific lat/lon from netCF file
        lon_u = dset_u['nav_lon'][0, :]
        lat_u = dset_u['nav_lat'][:, 0]
        lon_v = dset_v['nav_lon'][0, :]
        lat_v = dset_v['nav_lat'][:, 0]
        depth = np.zeros(1, dtype=np.float32)
        time = np.zeros(1, dtype=np.float32)

        u = dset_u['vozocrtx'][0, 0, :, :]
        v = dset_v['vomecrty'][0, 0, :, :]

        # Hack around the fact that NaN values propagate in SciPy's interpolators
        u[np.isnan(u)] = 0.
        v[np.isnan(v)] = 0.

{'fraction_non_alphanumeric': 0.11434108527131782, 'fraction_numerical': 0.009205426356589148, 'mean_word_length': 2.6833184656556646, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4585194', 'n_tokens_mistral': 1404, 'n_tokens_neox': 1375, 'n_words': 431}