Beispiel #1
0
def set_stokes(fieldset):
    data_dir = '/projects/0/topios/hydrodynamic_data/WWIII/'
    fnames = []
    years = range(2000, 2005)
    for y in years:
        basepath = data_dir + str(y) + '/ww3.*_uss.nc'
        fnames += sorted(glob(str(basepath)))
    dimensionsU = {'lon': 'longitude', 'lat': 'latitude', 'time': 'time'}
    dimensionsV = {'lon': 'longitude', 'lat': 'latitude', 'time': 'time'}
    Uuss = Field.from_netcdf(fnames, ('Uuss', 'uuss'),
                             dimensionsU,
                             fieldtype='U',
                             allow_time_extrapolation=False)
    Vuss = Field.from_netcdf(fnames, ('Vuss', 'vuss'),
                             dimensionsV,
                             fieldtype='V',
                             allow_time_extrapolation=False,
                             grid=Uuss.grid,
                             dataFiles=Uuss.dataFiles)
    fieldset.add_field(Uuss)
    fieldset.add_field(Vuss)
    fieldset.Uuss.vmax = 5
    fieldset.Vuss.vmax = 5
    uv_uss = VectorField('UVuss', fieldset.Uuss, fieldset.Vuss)
    fieldset.add_vector_field(uv_uss)
Beispiel #2
0
def Ratio_Test(dfile):
    Field.from_netcdf(dfile,
                      dimensions={
                          'lon': 'nav_lon',
                          'lat': 'nav_lat',
                          'time': 'time_counter',
                          'data': 'TagDensity'
                      },
                      filenames=[dfile])
Beispiel #3
0
def simulate_parcels(source_url,
                     output_filename,
                     lat,
                     lon,
                     wind_drift_factor=0.0,
                     velocity_average=True,
                     duration=23):
    """
    source_url: local file or list of local files with fielddata in NetCDF-format
    output_filename: name of file in which to save calculated trajectory
    lat, lon: initial coordinates of single drifter or lists with coordinates for multiple drifters
    wind_drift_factor: fraction of wind-speed at which objects will be advected. Default is 0 (no direct wind-drift)
    velocity_average: Boolean variable deciding whether averaged horisontal velocities or surface velocities will be used. 
                      Default is average which is consistent with GPU Ocean
    duration: duration of the simulation in hours. Default is 24 hours.
    TODO: Add functionality to start drifters at a later time in the simulation like in GPU Ocean.
    """
    filenames = {'U': source_url, 'V': source_url}
    dimensions = {'lat': 'lat', 'lon': 'lon', 'time': 'time'}

    if velocity_average:
        variables = {'U': 'ubar', 'V': 'vbar'}
    else:
        variables = {'U': 'u', 'V': 'v'}

    fieldset = FieldSet.from_netcdf(filenames,
                                    variables,
                                    dimensions,
                                    interp_method='cgrid_velocity')

    if wind_drift_factor:
        Uwind = Field.from_netcdf(source_url, ('U', 'Uwind'),
                                  dimensions,
                                  field_chunksize='auto',
                                  interp_method='cgrid_velocity')
        Vwind = Field.from_netcdf(source_url, ('V', 'Vwind'),
                                  dimensions,
                                  field_chunksize='auto',
                                  interp_method='cgrid_velocity')
        Uwind.set_scaling_factor(wind_drift_factor)
        Vwind.set_scaling_factor(wind_drift_factor)
        fieldset = FieldSet(U=fieldset.U + Uwind, V=fieldset.V + Vwind)

    pset = ParticleSet.from_list(fieldset=fieldset,
                                 pclass=JITParticle,
                                 lon=lon,
                                 lat=lat)
    output_file = pset.ParticleFile(name=output_filename,
                                    outputdt=timedelta(minutes=5))

    pset.execute(AdvectionRK4,
                 runtime=timedelta(hours=duration),
                 dt=timedelta(minutes=5),
                 output_file=output_file)

    output_file.export()
Beispiel #4
0
def set_skimulator_fieldset():
    filenames = '/projects/0/topios/hydrodynamic_data/SKIMulator/scisoc_trpac?.nc'
    variables = {'U': 'Ux_skim', 'V': 'Uy_skim'}
    dimensions = {'lat': 'lat', 'lon': 'lon', 'time': 'time'}
    fset = FieldSet.from_netcdf(filenames, variables, dimensions)
    fset.add_field(Field.from_netcdf('skimulator_boundaryvelocities.nc', 'MaskUvel',
                                     {'lon': 'Longitude', 'lat': 'Latitude'}, fieldtype='U'))
    fset.add_field(Field.from_netcdf('skimulator_boundaryvelocities.nc', 'MaskVvel',
                                     {'lon': 'Longitude', 'lat': 'Latitude'}, fieldtype='V'))
    return fset
Beispiel #5
0
def set_cmems_surface_fieldset():
    fnames = '/projects/0/topios/hydrodynamic_data/GLOBAL_ANALYSIS_FORECAST_PHY_001_024/cmems*'
    filenames = {'U': fnames + 'uo.nc', 'V': fnames + 'vo.nc'}
    variables = {'U': 'uo', 'V': 'vo'}
    dimensions = {'lat': 'latitude', 'lon': 'longitude', 'time': 'time'}
    fset = FieldSet.from_netcdf(filenames, variables, dimensions)
    fset.add_field(Field.from_netcdf('cmems_boundaryvelocities.nc', 'MaskUvel',
                                     {'lon': 'Longitude', 'lat': 'Latitude'}, fieldtype='U'))
    fset.add_field(Field.from_netcdf('cmems_boundaryvelocities.nc', 'MaskVvel',
                                     {'lon': 'Longitude', 'lat': 'Latitude'}, fieldtype='V'))
    return fset
Beispiel #6
0
def get_wind_MERCATOR(fieldset,
                      path,
                      start_date=0,
                      finish_date=-1,
                      chunksize=False):
    fdir = sorted(glob(path + '*.nc'))
    len_path = len(path)

    if type(start_date) == datetime:
        start_date = start_date.strftime("%Y%m%d")
        finish_date = finish_date.strftime("%Y%m%d")
        for i, e in enumerate(fdir):
            if e.find(str(start_date), len_path) > 0:
                start_index = i
            elif e.find(str(finish_date), len_path) > 0:
                finish_index = i

    if start_date > finish_date:
        filenames = {
            'uwnd': fdir[finish_index:start_index + 1],
            'vwnd': fdir[finish_index:start_index + 1]
        }
    else:
        filenames = {
            'uwnd': fdir[start_index:finish_index + 1],
            'vwnd': fdir[start_index:finish_index + 1]
        }
    variables = {'uwnd': 'uwnd', 'vwnd': 'vwnd'}
    dimensions = {'time': 'time', 'lat': 'latitude', 'lon': 'longitude'}
    Uwnd = Field.from_netcdf(filenames['uwnd'],
                             variables['uwnd'],
                             dimensions,
                             fieldtype='U',
                             vmax=150,
                             vmin=-150,
                             allow_time_extrapolation=False,
                             field_chunksize=chunksize)
    Vwnd = Field.from_netcdf(filenames['vwnd'],
                             variables['vwnd'],
                             dimensions,
                             fieldtype='V',
                             vmax=150,
                             vmin=-150,
                             grid=Uwnd.grid,
                             dataFiles=Uwnd.dataFiles,
                             allow_time_extrapolation=False,
                             field_chunksize=chunksize)
    fieldset.add_field(Uwnd, 'uwnd')
    fieldset.add_field(Vwnd, 'vwnd')
    UV_wnd = VectorField('UV_wnd', fieldset.uwnd, fieldset.vwnd)
    fieldset.add_vector_field(UV_wnd)
    return fieldset
Beispiel #7
0
def get_stokes_MERCATOR(fieldset,
                        path,
                        start_date=0,
                        finish_date=-1,
                        chunksize=False):
    fdir = sorted(glob(path + '*.nc'))
    len_path = len(path)

    if type(start_date) == datetime:
        start_date = start_date.strftime("%Y%m%d")
        finish_date = finish_date.strftime("%Y%m%d")
        for i, e in enumerate(fdir):
            if e.find(str(start_date), len_path) > 0:
                start_index = i
            elif e.find(str(finish_date), len_path) > 0:
                finish_index = i

    if start_date > finish_date:
        filenames = {
            'U': fdir[finish_index:start_index + 1],
            'V': fdir[finish_index:start_index + 1]
        }
    else:
        filenames = {
            'U': fdir[start_index:finish_index + 1],
            'V': fdir[start_index:finish_index + 1]
        }
    variables = {'U': 'uuss', 'V': 'vuss'}
    dimensions = {'lat': 'latitude', 'lon': 'longitude', 'time': 'time'}
    Ustokes = Field.from_netcdf(filenames['U'],
                                variables['U'],
                                dimensions,
                                fieldtype='U',
                                vmax=2,
                                vmin=-2,
                                allow_time_extrapolation=False,
                                field_chunksize=chunksize)
    Vstokes = Field.from_netcdf(filenames['V'],
                                variables['V'],
                                dimensions,
                                fieldtype='V',
                                vmax=2,
                                vmin=-2,
                                grid=Ustokes.grid,
                                dataFiles=Ustokes.dataFiles,
                                allow_time_extrapolation=False,
                                field_chunksize=chunksize)
    fieldset.add_field(Ustokes, 'uuss')
    fieldset.add_field(Vstokes, 'vuss')
    UV_Stokes = VectorField('UV_Stokes', fieldset.uuss, fieldset.vuss)
    fieldset.add_vector_field(UV_Stokes)
    return fieldset
Beispiel #8
0
def set_cmems(fieldset):
    data_dir = '/projects/0/topios/hydrodynamic_data/CMEMS/NORTHWESTSHELF_REANALYSIS_PHYS_004_009/MetO-NWS-REAN-PHYS-daily-CUR/'
    fnames = []
    years = range(2000, 2005)
    for y in years:
        basepath = data_dir + str(
            y) + '/*/' + 'metoffice_foam1_amm7_NWS_RFVL_dm*.nc'
        fnames += sorted(glob(str(basepath)))
    dimensionsU = {'lon': 'lon', 'lat': 'lat', 'time': 'time'}
    dimensionsV = {'lon': 'lon', 'lat': 'lat', 'time': 'time'}
    indices = {
        'lon': range(1, 296),
        'lat': range(1, 374)
    }  # cmems puts nan values at its borders
    Ucmems = Field.from_netcdf(fnames, ('Ucmems', 'vozocrtx'),
                               dimensionsU,
                               fieldtype='U',
                               indices=indices,
                               allow_time_extrapolation=False)
    Vcmems = Field.from_netcdf(fnames, ('Vcmems', 'vomecrty'),
                               dimensionsV,
                               fieldtype='V',
                               indices=indices,
                               allow_time_extrapolation=False,
                               grid=Ucmems.grid,
                               dataFiles=Ucmems.dataFiles)
    fieldset.add_field(Ucmems)
    fieldset.add_field(Vcmems)
    fieldset.Ucmems.vmax = 5
    fieldset.Vcmems.vmax = 5

    fieldset.Unemo = fieldset.U
    fieldset.Unemo.name = 'Unemo'
    fieldset.Vnemo = fieldset.V
    fieldset.Vnemo.name = 'Vnemo'

    U = NestedField('U', [fieldset.Ucmems, fieldset.Unemo])
    V = NestedField('V', [fieldset.Vcmems, fieldset.Vnemo])
    fieldset.U = U
    fieldset.V = V

    fieldset.cmems = True
Beispiel #9
0
def set_unbeaching(fieldset):
    files = '/home/philippe/data/ORCA%s-N06_unbeaching_vel.nc' % fieldset.nemo_res
    filenames = files
    variables = {'Unemo_unbeach': 'unBeachU', 'Vnemo_unbeach': 'unBeachV'}
    dimensions = {'lon': 'glamf', 'lat': 'gphif'}
    fieldsetUnBeach = FieldSet.from_nemo(filenames,
                                         variables,
                                         dimensions,
                                         tracer_interp_method='cgrid_velocity')
    fieldset.add_field(fieldsetUnBeach.Unemo_unbeach)
    fieldset.add_field(fieldsetUnBeach.Vnemo_unbeach)

    if fieldset.cmems:
        fname = '/home/philippe/data/cmems_NWS_rean_004_009_unbeaching_vel.nc'
        dimensionsU = {'lon': 'lon', 'lat': 'lat'}
        Ucmems_unbeach = Field.from_netcdf(fname,
                                           ('Ucmems_unbeach', 'unBeachU'),
                                           dimensionsU,
                                           fieldtype='U')
        dimensionsV = {'lon': 'lon', 'lat': 'lat'}
        Vcmems_unbeach = Field.from_netcdf(fname,
                                           ('Vcmems_unbeach', 'unBeachV'),
                                           dimensionsV,
                                           fieldtype='V')
        fieldset.add_field(Ucmems_unbeach)
        fieldset.add_field(Vcmems_unbeach)

        UVnemo_unbeach = VectorField('UVnemo_unbeach', fieldset.Unemo_unbeach,
                                     fieldset.Vnemo_unbeach)
        UVcmems_unbeach = VectorField('UVcmems_unbeach',
                                      fieldset.Ucmems_unbeach,
                                      fieldset.Vcmems_unbeach)
        UVunbeach = NestedField('UVunbeach', [UVcmems_unbeach, UVnemo_unbeach])
        fieldset.add_vector_field(UVunbeach)
    else:
        UVunbeach = VectorField('UVunbeach', fieldset.Unemo_unbeach,
                                fieldset.Vnemo_unbeach)
        fieldset.add_vector_field(UVunbeach)
Beispiel #10
0
def calculateDensityRatio(dfiles, output):
    Fields = []
    for dfile in dfiles:
        print("Loading %s" % dfile)
        Fields.append(Field.from_netcdf(dfile,
                                        dimensions={'lon': 'nav_lon', 'lat': 'nav_lat', 'time': 'time_counter', 'data': 'TagDensity'},
                                        filenames=[dfile]))

    limits = [0, 0, 0, 0]
    limits[0] = np.max([field.lon[0] for field in Fields])
    limits[1] = np.min([field.lon[-1] for field in Fields])
    limits[2] = np.max([field.lat[0] for field in Fields])
    limits[3] = np.min([field.lat[-1] for field in Fields])
    #limits[1] = (np.min(field.lon[-1]) for field in Fields)
    #limits[2] = (np.max(field.lat[0]) for field in Fields)
    #limits[3] = (np.min(field.lat[-1]) for field in Fields)
    time_lim = [np.max([field.time[0] for field in Fields]), np.min([field.time[-1] for field in Fields])]
    #time_lim = [Fields[0].time[0], Fields[0].time[-1]]

    lon = np.arange(start=limits[0], stop=limits[1]+1, dtype=np.float32)
    lat = np.arange(start=limits[2], stop=limits[3]+1, dtype=np.float32)
    time = np.arange(time_lim[0], time_lim[1]+1, 30*24*60*60, dtype=np.float32)
    Ratio = np.zeros([len(time), len(lat), len(lon)], dtype=np.float32)
    print(limits)
    print(Fields[0].lon)
    print(Fields[1].lon)
    print(lon)
    print(Fields[0].lat)
    print(Fields[1].lat)
    print(lat)
    print(Fields[0].time)
    print(Fields[1].time)
    print(time)
    for t in range(len(time)):
        for x in range(len(lon)):
            for y in range(len(lat)):
                tagged = Fields[0].data[np.where(Fields[0].time == time[t])[0][0],
                                     np.where(Fields[0].lat == lat[y])[0][0],
                                     np.where(Fields[0].lon == lon[x])[0][0]]
                pop = Fields[1].data[np.where(Fields[1].time == time[t])[0][0],
                                     np.where(Fields[1].lat == lat[y])[0][0],
                                     np.where(Fields[1].lon == lon[x])[0][0]]
                #print('%s - %s' % (tagged, pop))
                if pop == 0:
                    Ratio[t, y, x] = 0
                else:
                    Ratio[t, y, x] = tagged/pop

    Ratios = Field('DensityRatio', Ratio, lon, lat, time=time)
    Ratios.write(filename=output)
Beispiel #11
0
def run_particles(fieldsetname):
    if fieldsetname == 'cmems_surface':
        fieldset = set_cmems_surface_fieldset()
    elif fieldsetname == 'cmems_50m':
        fieldset = set_cmems_50m_fieldset()
        dz = np.gradient(fieldset.U.depth)
        DZ = np.moveaxis(np.tile(dz, (fieldset.U.grid.ydim, fieldset.U.grid.xdim+10, 1)), [0, 1, 2], [1, 2, 0])

        def compute(fieldset):
            # Calculating vertical weighted average
            for f in [fieldset.U, fieldset.V]:
                for tind in f.loaded_time_indices:
                    f.data[tind, :] = np.sum(f.data[tind, :] * DZ, axis=0) / sum(dz)

        fieldset.compute_on_defer = compute

    elif fieldsetname == 'skimulator':
        # requires ncatted -a units,time,o,c,"days since 2016-01-01 00:00:00" scisoc_trpac_compressed.nc for decode_cf
        fieldset = set_skimulator_fieldset()
    else:
        raise NotImplementedError('FieldSet %s not implemented')

    fieldset.add_constant('max_drift_time', delta(days=180).total_seconds())
    size2D = (fieldset.U.grid.ydim, fieldset.U.grid.xdim)
    fieldset.add_field(Field('Kh_zonal', data=10*np.ones(size2D),
                             lon=fieldset.U.grid.lon, lat=fieldset.U.grid.lat,
                             mesh='spherical', allow_time_extrapolation=True))
    fieldset.add_field(Field('Kh_meridional', data=10*np.ones(size2D),
                             lon=fieldset.U.grid.lon, lat=fieldset.U.grid.lat,
                             mesh='spherical', allow_time_extrapolation=True))

    fieldset.add_periodic_halo(zonal=True, meridional=False, halosize=5)

    fieldset.add_field(Field.from_netcdf('EEZ_Field.nc', 'EEZ',
                                         {'lon': 'lon', 'lat': 'lat'},
                                         allow_time_extrapolation=True))

    pset = createFADset(fieldset, 'dFADsets.txt', nperid=10)
    pset.execute(SampleEEZ, dt=-1, runtime=0)  # setting the EEZ

    ofile = pset.ParticleFile(name='fadtracks_antibeaching_%s' % fieldsetname,
                              outputdt=delta(days=5))

    kernels = WrapLon + pset.Kernel(AdvectionRK4) + BrownianMotion2D + AntiBeaching + \
              SampleEEZ + DriftTime
    pset.execute(kernels, dt=delta(minutes=-10), output_file=ofile,
                 recovery={ErrorCode.ErrorOutOfBounds: OutOfBounds})
Beispiel #12
0
def set_diffusion(fieldset, diffusivity):
    fname = '/home/philippe/data/ORCA0083-N06_meshSize.nc'
    dimensions = {'lon': 'glamt', 'lat': 'gphit'}
    meshSize = Field.from_netcdf(fname,
                                 'meshSize',
                                 dimensions,
                                 interp_method='nearest')
    fieldset.add_field(meshSize)
    fieldset.add_field(
        Field('Kh_zonal',
              data=diffusivity * np.ones(meshSize.data.shape),
              grid=meshSize.grid,
              mesh='spherical'))
    fieldset.add_field(
        Field('Kh_meridional',
              data=diffusivity * np.ones(meshSize.data.shape),
              grid=meshSize.grid,
              mesh='spherical'))
Beispiel #13
0
def set_fields():
    datestr = '201[0-2]'
    hycomfiles = sorted(
        glob(
            '/Volumes/data01/HYCOMdata/GLBu0.08_expt_19.1_surf/hycom_GLBu0.08_191_%s*'
            % datestr))
    dimensions = {'lat': 'lat', 'lon': 'lon', 'time': 'time'}
    uhycom = Field.from_netcdf(hycomfiles,
                               'water_u',
                               dimensions,
                               fieldtype='U')
    vhycom = Field.from_netcdf(hycomfiles,
                               'water_v',
                               dimensions,
                               fieldtype='V',
                               grid=uhycom.grid,
                               dataFiles=uhycom.dataFiles)
    uhycom.vmin = -99.
    uhycom.set_scaling_factor(0.001)
    vhycom.set_scaling_factor(0.001)
    vhycom.vmin = -99.

    stokesfiles = sorted(
        glob('/Volumes/data01/WaveWatch3data/WW3-GLOB-30M_%s*' % datestr))
    dimensions = {'lat': 'latitude', 'lon': 'longitude', 'time': 'time'}
    uuss = Field.from_netcdf(stokesfiles, 'uuss', dimensions, fieldtype='U')
    vuss = Field.from_netcdf(stokesfiles,
                             'vuss',
                             dimensions,
                             fieldtype='V',
                             grid=uuss.grid,
                             dataFiles=uuss.dataFiles)

    windfiles = sorted(
        glob('/Users/erik/Desktop/WaveWatchWind/WaveWatchWind*.nc'))
    dimensions = {'lat': 'latitude', 'lon': 'longitude', 'time': 'time'}
    uwnd = Field.from_netcdf(windfiles, 'uwnd', dimensions, fieldtype='U')
    vwnd = Field.from_netcdf(windfiles,
                             'vwnd',
                             dimensions,
                             fieldtype='V',
                             grid=uwnd.grid,
                             dataFiles=uwnd.dataFiles)
    uwnd.set_scaling_factor(0.01)
    vwnd.set_scaling_factor(0.01)

    fieldset = FieldSet(U=uhycom + uuss + uwnd, V=vhycom + vuss + vwnd)

    fieldset.add_periodic_halo(zonal=True, meridional=False, halosize=5)
    return fieldset
Beispiel #14
0
def test_globcurrent_startparticles_between_time_arrays(
        mode, dt, with_starttime):
    fieldset = set_globcurrent_fieldset()

    fnamesFeb = sorted(
        glob(
            path.join(path.dirname(__file__), 'GlobCurrent_example_data',
                      '200202*.nc')))
    fieldset.add_field(
        Field.from_netcdf(fnamesFeb,
                          ('P', 'eastward_eulerian_current_velocity'), {
                              'lat': 'lat',
                              'lon': 'lon',
                              'time': 'time'
                          }))

    class MyParticle(ptype[mode]):
        sample_var = Variable('sample_var', initial=0.)

    def SampleP(particle, fieldset, time):
        particle.sample_var += fieldset.P[time, particle.depth, particle.lat,
                                          particle.lon]

    if with_starttime:
        time = fieldset.U.grid.time[0] if dt > 0 else fieldset.U.grid.time[-1]
        pset = ParticleSet(fieldset,
                           pclass=MyParticle,
                           lon=[25],
                           lat=[-35],
                           time=time)
    else:
        pset = ParticleSet(fieldset, pclass=MyParticle, lon=[25], lat=[-35])

    if with_starttime:
        with pytest.raises(TimeExtrapolationError):
            pset.execute(pset.Kernel(AdvectionRK4) + SampleP,
                         runtime=delta(days=1),
                         dt=dt)
    else:
        pset.execute(pset.Kernel(AdvectionRK4) + SampleP,
                     runtime=delta(days=1),
                     dt=dt)
def set_fields(hycomfiles, stokesfiles):
    dimensions = {
        'lat': 'Latitude',
        'lon': 'Longitude',
        'time': 'MT',
        'depth': 'Depth'
    }
    bvelfile = '/Users/erik/Codes/ParcelsRuns/Hydrodynamics_AuxiliaryFiles/Hycom/boundary_velocities.nc'
    MaskUvel = Field.from_netcdf(bvelfile, 'MaskUvel', dimensions)
    MaskVvel = Field.from_netcdf(bvelfile, 'MaskVvel', dimensions)

    uhycom = Field.from_netcdf(hycomfiles, 'u', dimensions, fieldtype='U')
    vhycom = Field.from_netcdf(hycomfiles,
                               'v',
                               dimensions,
                               fieldtype='V',
                               grid=uhycom.grid,
                               timeFiles=uhycom.timeFiles)

    dimensions = {'lat': 'latitude', 'lon': 'longitude', 'time': 'time'}
    uuss = Field.from_netcdf(stokesfiles, 'uuss', dimensions, fieldtype='U')
    vuss = Field.from_netcdf(stokesfiles,
                             'vuss',
                             dimensions,
                             fieldtype='V',
                             grid=uuss.grid,
                             timeFiles=uuss.timeFiles)

    fieldset = FieldSet(U=uhycom + uuss, V=vhycom + vuss)
    fieldset.add_field(MaskUvel)
    fieldset.add_field(MaskVvel)
    fieldset.MaskUvel.units = fieldset.U[0].units
    fieldset.MaskVvel.units = fieldset.V[0].units

    fieldset.add_periodic_halo(zonal=True, meridional=False, halosize=5)
    return fieldset
Beispiel #16
0
if param['windage']:
    fieldset = FieldSet(U=fieldset_ocean.U + fieldset_windwave.U,
                        V=fieldset_ocean.V + fieldset_windwave.V)
elif param['stokes']:
    fieldset = FieldSet(U=fieldset_ocean.U + fieldset_wave.U,
                        V=fieldset_ocean.V + fieldset_wave.V)
else:
    fieldset = fieldset_ocean

# ADD ADDITIONAL FIELDS
# Country identifier grid (on psi grid, nearest)
iso_psi_all = Field.from_netcdf(fh['grid'],
                                variable='iso_psi_all',
                                dimensions={
                                    'lon': 'lon_psi',
                                    'lat': 'lat_psi'
                                },
                                interp_method='nearest',
                                allow_time_extrapolation=True)

# Source cell ID (on psi grid, nearest)
source_id_psi = Field.from_netcdf(fh['grid'],
                                  variable='source_id_psi',
                                  dimensions={
                                      'lon': 'lon_psi',
                                      'lat': 'lat_psi'
                                  },
                                  interp_method='nearest',
                                  allow_time_extrapolation=True)

# Sink cell ID (on psi grid, nearest)
Beispiel #17
0
@author: nooteboom
"""

import numpy as np
import matplotlib.pylab as plt
import matplotlib
from netCDF4 import Dataset
import math

from parcels import Field

filename = 'bathymetry_ORCA12_V3.3.nc'
Bath = Field.from_netcdf(filename,
                         variable='Bathymetry',
                         dimensions={
                             'lon': 'nav_lon',
                             'lat': 'nav_lat'
                         })
#Bath.grid.check_zonal_periodic()

# True if surface instead of amount of surface grid boxes
surfacebool = True

ddeg = 2  # resolution of the binning
sp = 6
dd = 10
res = 1

tmdir = '/Users/nooteboom/Documents/PhD/parcels/NEMO/atsf/Transition_matrices/'

data = np.load(tmdir + 'output/box-box/TMglobal_bin' + str(ddeg) + '_dd' +
Beispiel #18
0
def Ratio_Test(dfile):
    Field.from_netcdf(dfile, dimensions={'lon': 'nav_lon', 'lat': 'nav_lat', 'time': 'time_counter', 'data': 'TagDensity'},
                      filenames=[dfile])
Beispiel #19
0
def calculateDensityRatio(dfiles, output):
    Fields = []
    for dfile in dfiles:
        print("Loading %s" % dfile)
        Fields.append(
            Field.from_netcdf(dfile,
                              dimensions={
                                  'lon': 'nav_lon',
                                  'lat': 'nav_lat',
                                  'time': 'time_counter',
                                  'data': 'TagDensity'
                              },
                              filenames=[dfile]))

    limits = [0, 0, 0, 0]
    limits[0] = np.max([field.lon[0] for field in Fields])
    limits[1] = np.min([field.lon[-1] for field in Fields])
    limits[2] = np.max([field.lat[0] for field in Fields])
    limits[3] = np.min([field.lat[-1] for field in Fields])
    #limits[1] = (np.min(field.lon[-1]) for field in Fields)
    #limits[2] = (np.max(field.lat[0]) for field in Fields)
    #limits[3] = (np.min(field.lat[-1]) for field in Fields)
    time_lim = [
        np.max([field.time[0] for field in Fields]),
        np.min([field.time[-1] for field in Fields])
    ]
    #time_lim = [Fields[0].time[0], Fields[0].time[-1]]

    lon = np.arange(start=limits[0], stop=limits[1] + 1, dtype=np.float32)
    lat = np.arange(start=limits[2], stop=limits[3] + 1, dtype=np.float32)
    time = np.arange(time_lim[0],
                     time_lim[1] + 1,
                     30 * 24 * 60 * 60,
                     dtype=np.float32)
    Ratio = np.zeros([len(time), len(lat), len(lon)], dtype=np.float32)
    print(limits)
    print(Fields[0].lon)
    print(Fields[1].lon)
    print(lon)
    print(Fields[0].lat)
    print(Fields[1].lat)
    print(lat)
    print(Fields[0].time)
    print(Fields[1].time)
    print(time)
    for t in range(len(time)):
        for x in range(len(lon)):
            for y in range(len(lat)):
                tagged = Fields[0].data[np.where(
                    Fields[0].time == time[t])[0][0],
                                        np.where(
                                            Fields[0].lat == lat[y])[0][0],
                                        np.where(
                                            Fields[0].lon == lon[x])[0][0]]
                pop = Fields[1].data[np.where(Fields[1].time == time[t])[0][0],
                                     np.where(Fields[1].lat == lat[y])[0][0],
                                     np.where(Fields[1].lon == lon[x])[0][0]]
                #print('%s - %s' % (tagged, pop))
                if pop == 0:
                    Ratio[t, y, x] = 0
                else:
                    Ratio[t, y, x] = tagged / pop

    Ratios = Field('DensityRatio', Ratio, lon, lat, time=time)
    Ratios.write(filename=output)
Beispiel #20
0
def create(startDate,
           days,
           lonRange=(-180, 180),
           latRange=(55, 90),
           antiBeach=True,
           halo=False,
           **kwargs):
    """
    Creates a parcels.FieldSet object from hydrodynamic data in a netCDF file.
    
    Parameters
    ----------
    startDate : str, int
        String or int indicating the first date to load (YYYYMMDD)
    days : int
        Number of days to load    
    antiBeach : bool
        Load anti-beach field   
    halo : bool
        Add a periodic halo
        
    Returns
    ----------
    parcels.FieldSet    
    """
    readDir = "/data/oceanparcels/input_data/CMEMS/GLOBAL_REANALYSIS_PHY_001_030/"
    fieldFiles = sorted(glob(readDir + "mercatorglorys12v1_gl12_mean_*.nc"))
    unBeachFile = "mercatorglorys12v1_gl12_unbeaching_vel.nc"
    startFile = glob(readDir +
                     f"mercatorglorys12v1_gl12_mean_{startDate}_*.nc")
    assert len(startFile) == 1, "No file found for this `start_date`."
    startFileIndex = fieldFiles.index(startFile[0])
    endFileIndex = startFileIndex + days
    if endFileIndex >= len(fieldFiles) - 1:
        days = len(fieldFiles) - startFileIndex - 1
        endFileIndex = len(fieldFiles) - 1
        warnings.warn("\n Timespan of simulation exceeds the amount of data that is available. " \
                     +"Reducing the amount of `days` to " + str(days) +".")
    selectedFiles = fieldFiles[startFileIndex:endFileIndex]

    variables = {'U': 'uo', 'V': 'vo'}
    dimensions = {
        'U': {
            'time': 'time',
            'lat': 'latitude',
            'lon': 'longitude'
        },
        'V': {
            'time': 'time',
            'lat': 'latitude',
            'lon': 'longitude'
        }
    }
    mesh = fieldFiles[0]
    filenames = {
        'U': {
            'lon': mesh,
            'lat': mesh,
            'data': selectedFiles
        },
        'V': {
            'lon': mesh,
            'lat': mesh,
            'data': selectedFiles
        }
    }

    ds = xr.open_dataset(fieldFiles[0])

    minLonIdx = np.searchsorted(ds.longitude, lonRange[0])
    maxLonIdx = np.searchsorted(ds.longitude, lonRange[1])
    minLatIdx = np.searchsorted(ds.latitude, latRange[0])
    maxLatIdx = np.searchsorted(ds.latitude, latRange[1])

    indices = {
        'lon': range(minLonIdx, maxLonIdx),
        'lat': range(minLatIdx, maxLatIdx)
    }

    fieldset = FieldSet.from_netcdf(
        selectedFiles,
        variables,
        dimensions,
        indices=indices,
        allow_time_extrapolation=False,
    )

    if antiBeach:
        dimensions = {'lon': 'longitude', 'lat': 'latitude'}
        U_unbeach = Field.from_netcdf(readDir + unBeachFile,
                                      ('U_unbeach', 'unBeachU'),
                                      dimensions,
                                      fieldtype='U')
        V_unbeach = Field.from_netcdf(readDir + unBeachFile,
                                      ('V_unbeach', 'unBeachV'),
                                      dimensions,
                                      fieldtype='V')
        fieldset.add_field(U_unbeach)
        fieldset.add_field(V_unbeach)
        UVunbeach = VectorField('UVunbeach', fieldset.U_unbeach,
                                fieldset.V_unbeach)
        fieldset.add_vector_field(UVunbeach)

    if halo:
        fieldset.add_periodic_halo(zonal=True)

    fieldset.computeTimeChunk(fieldset.U.grid.time[0], 1)

    fieldset.landMask = np.isnan(ds.uo[0, 0, minLatIdx:maxLatIdx,
                                       minLonIdx:maxLonIdx].data)
    ds.close()
    return fieldset
Beispiel #21
0
def create_fieldset(param, ndays_simu, t_init):
    """
    Build fieldset from data files, dimensions and variables.
    """
    print('****************************************************')
    print("Building FieldSet...")
    print('****************************************************')
    t0 = time.time()
    key_alltracers = param['key_alltracers']
    mesh_phy = param['mesh_phy']
    mesh_food = param['mesh_food']
    #Forcings
    last_date = IO.find_last_date(param)
    date_start, date_end, time_periodic = IO.define_start_end(
        ndays_simu, param, t_init, last_date)
    ufiles = IO.forcing_list(param['U_dir'], param['U_suffix'], date_start,
                             date_end)
    vfiles = IO.forcing_list(param['V_dir'], param['V_suffix'], date_start,
                             date_end)

    #Filenames
    filenames = {
        'U': {
            'lon': mesh_phy,
            'lat': mesh_phy,
            'data': ufiles
        },
        'V': {
            'lon': mesh_phy,
            'lat': mesh_phy,
            'data': vfiles
        }
    }
    #Variables
    variables = {'U': param['U_var'], 'V': param['V_var']}
    #Dimensions: Caution, C-grids need f nodes
    dimensions = {
        'U': {
            'lon': param['lon_phy'],
            'lat': param['lat_phy'],
            'time': param['time_var_phy']
        },
        'V': {
            'lon': param['lon_phy'],
            'lat': param['lat_phy'],
            'time': param['time_var_phy']
        }
    }
    if time_periodic:
        time_periodic *= 86400  #days to seconds
    #Fieldset creation
    chs = define_chunksize(ufiles[0], param['U_var'])
    if param['grid_phy'] == 'A':
        fieldset = FieldSet.from_netcdf(filenames,
                                        variables,
                                        dimensions,
                                        time_periodic=time_periodic,
                                        field_chunksize=chs)
    else:
        fieldset = FieldSet.from_c_grid_dataset(filenames,
                                                variables,
                                                dimensions,
                                                time_periodic=time_periodic,
                                                field_chunksize=chs)

    if key_alltracers:
        tfiles = IO.forcing_list(param['T_dir'], param['T_suffix'], date_start,
                                 date_end)
        ffiles = IO.forcing_list(param['food_dir'], param['food_suffix'],
                                 date_start, date_end)

        # Filenames
        Tfiles = {'lon': mesh_phy, 'lat': mesh_phy, 'data': tfiles}
        NPPfiles = {'lon': mesh_food, 'lat': mesh_food, 'data': ffiles}

        # Dimensions
        if param['grid_phy'] == 'A':
            Tdim = {
                'lon': param['lon_phy'],
                'lat': param['lat_phy'],
                'time': param['time_var_phy']
            }
        else:
            Tdim = {
                'lon': param['lon_T'],
                'lat': param['lat_T'],
                'time': param['time_var_phy']
            }
        NPPdim = {
            'lon': param['lon_food'],
            'lat': param['lat_food'],
            'time': param['time_var_food']
        }

        # Field creation
        T = Field.from_netcdf(Tfiles, ('T', param['T_var']),
                              Tdim,
                              interp_method='linear_invdist_land_tracer',
                              time_periodic=time_periodic,
                              field_chunksize=chs)
        NPP = Field.from_netcdf(NPPfiles, ('NPP', param['food_var']),
                                NPPdim,
                                interp_method='linear_invdist_land_tracer',
                                time_periodic=time_periodic,
                                field_chunksize='auto')

        # Waves: physical grid, A-grid
        if param['wave_swim']:
            st_files = IO.forcing_list(param['wave_dir'], param['wave_suffix'],
                                       date_start, date_end)
            Stokes_files = {'lon': mesh_phy, 'lat': mesh_phy, 'data': st_files}
            Stokes_dim = {
                'lon': param['lon_phy'],
                'lat': param['lat_phy'],
                'time': param['time_var_phy']
            }

            Ustokes = Field.from_netcdf(
                Stokes_files, ('Ustokes', param['Ust_var']),
                Stokes_dim,
                interp_method='linear_invdist_land_tracer',
                time_periodic=time_periodic,
                field_chunksize=chs)
            Vstokes = Field.from_netcdf(
                Stokes_files, ('Vstokes', param['Vst_var']),
                Stokes_dim,
                interp_method='linear_invdist_land_tracer',
                time_periodic=time_periodic,
                field_chunksize=chs)

            fieldset.add_field(Ustokes)
            fieldset.add_field(Vstokes)

        # Add to fieldset
        fieldset.add_field(T)
        fieldset.add_field(NPP)

    # East/West periodicity
    if param['periodicBC'] and not param['halo']:
        add_halo(fieldset)

    # Time
    tt = time.time() - t0
    print('\n')
    print(' => FieldSet created in: ' + str(timedelta(seconds=int(tt))))
    print('\n')
    return fieldset
files_eastward = "/science/projects/oceanparcels/input_data/DataPlasticTides/FES/eastward_velocity/"
files_northward = "/science/projects/oceanparcels/input_data/DataPlasticTides/FES/northward_velocity/"

dimensions_Ua = {'data': 'Ua', 'lat': 'lat', 'lon': 'lon'}
dimensions_Ug = {'data': 'Ug', 'lat': 'lat', 'lon': 'lon'}
dimensions_Va = {'data': 'Va', 'lat': 'lat', 'lon': 'lon'}
dimensions_Vg = {'data': 'Vg', 'lat': 'lat', 'lon': 'lon'}

deg2rad = math.pi / 180.0  # factor to convert degrees to radians
""" --- M2 component --- """

filename_UM2 = files_eastward + 'm2.nc'
filename_VM2 = files_northward + 'm2.nc'

UaM2 = Field.from_netcdf(filename_UM2, 'UaM2', dimensions_Ua, fieldtype='U')
UaM2.set_scaling_factor(1e-2)  # convert from cm/s to m/s
UgM2 = Field.from_netcdf(filename_UM2, 'UgM2', dimensions_Ug)
UgM2.set_scaling_factor(deg2rad)  # convert from degrees to radians
VaM2 = Field.from_netcdf(filename_VM2, 'VaM2', dimensions_Va, fieldtype='V')
VaM2.set_scaling_factor(1e-2)
VgM2 = Field.from_netcdf(filename_VM2, 'VgM2', dimensions_Vg)
VgM2.set_scaling_factor(deg2rad)

fieldset.add_field(UaM2)
fieldset.add_field(UgM2)
fieldset.add_field(VaM2)
fieldset.add_field(VgM2)

omega_M2 = 28.9841042  # angular frequency of M2 in degrees per hour
fieldset.add_constant('omegaM2', (omega_M2 * deg2rad) /
Beispiel #23
0
def plotTrajectoriesFile(filename,
                         mode='2d',
                         tracerfile=None,
                         tracerfield='P',
                         tracerlon='x',
                         tracerlat='y',
                         recordedvar=None,
                         movie_forward=True,
                         bins=20,
                         show_plt=True,
                         central_longitude=0):
    """Quick and simple plotting of Parcels trajectories

    :param filename: Name of Parcels-generated NetCDF file with particle positions
    :param mode: Type of plot to show. Supported are '2d', '3d', 'hist2d',
                'movie2d' and 'movie2d_notebook'. The latter two give animations,
                with 'movie2d_notebook' specifically designed for jupyter notebooks
    :param tracerfile: Name of NetCDF file to show as background
    :param tracerfield: Name of variable to show as background
    :param tracerlon: Name of longitude dimension of variable to show as background
    :param tracerlat: Name of latitude dimension of variable to show as background
    :param recordedvar: Name of variable used to color particles in scatter-plot.
                Only works in 'movie2d' or 'movie2d_notebook' mode.
    :param movie_forward: Boolean whether to show movie in forward or backward mode (default True)
    :param bins: Number of bins to use in `hist2d` mode. See also https://matplotlib.org/api/_as_gen/matplotlib.pyplot.hist2d.html
    :param show_plt: Boolean whether plot should directly be show (for py.test)
    :param central_longitude: Degrees East at which to center the plot
    """

    environ["HDF5_USE_FILE_LOCKING"] = "FALSE"
    try:
        pfile = xr.open_dataset(str(filename), decode_cf=True)
    except:
        pfile = xr.open_dataset(str(filename), decode_cf=False)
    lon = np.ma.filled(pfile.variables['lon'], np.nan)
    lat = np.ma.filled(pfile.variables['lat'], np.nan)
    time = np.ma.filled(pfile.variables['time'], np.nan)
    z = np.ma.filled(pfile.variables['z'], np.nan)
    mesh = pfile.attrs[
        'parcels_mesh'] if 'parcels_mesh' in pfile.attrs else 'spherical'

    if (recordedvar is not None):
        record = np.ma.filled(pfile.variables[recordedvar], np.nan)
    pfile.close()

    if tracerfile is not None and mode != 'hist2d':
        tracerfld = Field.from_netcdf(tracerfile, tracerfield, {
            'lon': tracerlon,
            'lat': tracerlat
        })
        plt, fig, ax, cartopy = plotfield(tracerfld)
        if plt is None:
            return  # creating axes was not possible
        titlestr = ' and ' + tracerfield
    else:
        spherical = False if mode == '3d' or mesh == 'flat' else True
        plt, fig, ax, cartopy = create_parcelsfig_axis(
            spherical=spherical, central_longitude=central_longitude)
        if plt is None:
            return  # creating axes was not possible
        titlestr = ''

    if cartopy:
        for p in range(lon.shape[1]):
            lon[:, p] = [ln if ln < 180 else ln - 360 for ln in lon[:, p]]

    if mode == '3d':
        from mpl_toolkits.mplot3d import Axes3D  # noqa
        plt.clf()  # clear the figure
        ax = fig.gca(projection='3d')
        for p in range(len(lon)):
            ax.plot(lon[p, :], lat[p, :], z[p, :], '.-')
        ax.set_xlabel('Longitude')
        ax.set_ylabel('Latitude')
        ax.set_zlabel('Depth')
        ax.set_title('Particle trajectories')
    elif mode == '2d':
        if cartopy:
            ax.plot(np.transpose(lon),
                    np.transpose(lat),
                    '.-',
                    transform=cartopy.crs.Geodetic())
        else:
            ax.plot(np.transpose(lon), np.transpose(lat), '.-')
        ax.set_title('Particle trajectories' + titlestr)
    elif mode == 'hist2d':
        _, _, _, cs = plt.hist2d(lon[~np.isnan(lon)],
                                 lat[~np.isnan(lat)],
                                 bins=bins)
        cartopy_colorbar(cs, plt, fig, ax)
        ax.set_title('Particle histogram')
    elif mode in ('movie2d', 'movie2d_notebook'):
        if mesh == 'flat':
            ax.set_xlim(np.nanmin(lon), np.nanmax(lon))
        else:
            ax.set_xlim(np.nanmin((lon + central_longitude + 180) % 360 - 180),
                        np.nanmax((lon + central_longitude + 180) % 360 - 180))
        ax.set_ylim(np.nanmin(lat), np.nanmax(lat))
        plottimes = np.unique(time)
        if not movie_forward:
            plottimes = np.flip(plottimes, 0)
        if isinstance(plottimes[0], (np.datetime64, np.timedelta64)):
            plottimes = plottimes[~np.isnat(plottimes)]
        else:
            try:
                plottimes = plottimes[~np.isnan(plottimes)]
            except:
                pass
        b = time == plottimes[0]

        def timestr(plottimes, index):
            if isinstance(plottimes[index], np.timedelta64):
                if plottimes[-1] > np.timedelta64(1, 'h'):
                    return str(plottimes[index].astype('timedelta64[h]'))
                elif plottimes[-1] > np.timedelta64(1, 's'):
                    return str(plottimes[index].astype('timedelta64[s]'))
            else:
                return str(plottimes[index])

        if cartopy:
            scat = ax.scatter(lon[b],
                              lat[b],
                              s=20,
                              color='k',
                              transform=cartopy.crs.Geodetic())
        else:
            scat = ax.scatter(lon[b], lat[b], s=20, color='k')
        ttl = ax.set_title('Particles' + titlestr + ' at time ' +
                           timestr(plottimes, 0))
        frames = np.arange(0, len(plottimes))

        def animate(t):
            b = time == plottimes[t]
            scat.set_offsets(np.vstack((lon[b], lat[b])).transpose())
            ttl.set_text('Particle' + titlestr + ' at time ' +
                         timestr(plottimes, t))
            if recordedvar is not None:
                scat.set_array(record[b])
            return scat,

        rc('animation', html='html5')
        anim = animation.FuncAnimation(fig,
                                       animate,
                                       frames=frames,
                                       interval=100,
                                       blit=False)
    else:
        raise RuntimeError('mode %s not known' % mode)

    if mode == 'movie2d_notebook':
        plt.close()
        return anim
    else:
        if show_plt:
            plt.show()
        return plt
Beispiel #24
0
    lons, lats = np.meshgrid(np.arange(0,360)+0.5,np.arange(27, 45)+0.5)
    ind = {'lat':range(220, 350)}
elif(posidx==8):
    lons, lats = np.meshgrid(np.arange(0,360)+0.5,np.arange(45, 62)+0.5)
    ind = {'lat':range(270, 384)}
elif(posidx==9):
    lons, lats = np.meshgrid(np.arange(0,360)+0.5,np.arange(62, 71)+0.5)
    ind = {'lat':range(300, 384)}

# reshape longitudes and latitudes
lons = lons.flatten(); lats = lats.flatten();
lonsz = []; latsz = [];
lons[lons>320.01] -= 360

# Delete the particles on land
bathy = Field.from_netcdf([dirread_pop+'bathymetry_POP_lowres_320t384.nc'], 'Bathymetry', 
                         {'lon':'ULONG','lat':'ULAT'}, interp_method='bgrid_tracer')

grid = bathy.grid
grid.add_periodic_halo(zonal=True, meridional=False, halosize=20)
bathy.grid = grid

bathy.add_periodic_halo(zonal=True, meridional=False, halosize=20)
for i in range(lons.shape[0]):
    if(bathy[0,0,lats[i], lons[i]]>0):
        lonsz.append(lons[i])
        latsz.append(lats[i])

# The boundary of the grid is at 320 degrees
lonsz = np.array(lonsz); latsz = np.array(latsz);
lonsz[lonsz>320.01] -= 360