def load_andremap_MLD(firstyear, lastyear, mesh, filetmp):
    "Load and compute (mean) mixed layer depth for the period (firstyear---lastyear)."

    # load the files into one dataset
    files = [filetmp.format(d) for d in range(firstyear, lastyear + 1, 1)]
    fl = MFDataset(files)

    print 'Computing mean, max, min ...'
    MLDmean = fl.variables['mixlay'][:, :].mean(axis=0)  # 1x 2D field
    MLDmax = fl.variables['mixlay'][:, :].max(axis=0)  # 1x 2D field
    MLDmin = fl.variables['mixlay'][:, :].min(axis=0)  # 1x 2D field
    print 'Done.'

    # load climatology
    climpath = '/mnt/lustre01/work/bm0944/a270046/DATA/climatology/'
    clim = pf.climatology(climpath, climname='phc')  # climname='phc'|'woa05'

    # map fesom data to PHC climatology grid
    xx, yy = np.meshgrid(clim.x, clim.y)
    zz_MLDmean = np.zeros((clim.T.shape[1], clim.T.shape[2]))
    zz_MLDmax = np.zeros((clim.T.shape[1], clim.T.shape[2]))
    zz_MLDmin = np.zeros((clim.T.shape[1], clim.T.shape[2]))

    distances, inds = pf.create_indexes_and_distances(mesh,
                                                      xx,
                                                      yy,
                                                      k=10,
                                                      n_jobs=2)

    # remap mean
    zz_MLDmean[:,:] = pf.fesom2regular(MLDmean, mesh, xx, yy, distances=distances,\
                                inds=inds, how='idist', k=10,\
                                radius_of_influence=200000)
    zz_MLDmax[:,:]  = pf.fesom2regular(MLDmax , mesh, xx, yy, distances=distances,\
                                inds=inds, how='idist', k=10,\
                                radius_of_influence=200000)
    zz_MLDmin[:,:]  = pf.fesom2regular(MLDmin , mesh, xx, yy, distances=distances,\
                                inds=inds, how='idist', k=10,\
                                radius_of_influence=200000)

    # set land to NaN
    zz_MLDmean[np.isnan(clim.T[0, :, :])] = np.nan
    zz_MLDmax[np.isnan(clim.T[0, :, :])] = np.nan
    zz_MLDmin[np.isnan(clim.T[0, :, :])] = np.nan

    return xx, yy, zz_MLDmean, zz_MLDmax, zz_MLDmin
Ejemplo n.º 2
0
def showfile(ifile, variable, depth, meshpath, box, res, influence, timestep,
             levels, quiet, ofile, mapproj, abg, clim, cmap, interp, ptype, k):
    '''
    meshpath - Path to the folder with FESOM1.4 mesh files.

    ifile    - Path to FESOM1.4 netCDF file.

    variable - The netCDF variable to be plotted.
    '''
    if not quiet:
        click.secho('Mesh: {}'.format(meshpath))
        click.secho('File: {}'.format(ifile))
        click.secho('Variable: {}'.format(variable), fg='red')
        click.secho('Depth: {}'.format(depth), fg='red')
        click.secho('BOX: {}'.format(box))
        click.secho('Resolution: {}'.format(res))
        click.secho('Influence raduis: {} meters'.format(influence), fg='red')
        click.secho('Timestep: {}'.format(timestep))
        if levels:
            click.secho('Levels: {}'.format(levels), fg='red')
        else:
            click.secho('Levels: auto', fg='red')

    if cmap:
        if cmap in cmo.cmapnames:
            colormap = cmo.cmap_d[cmap]
        elif cmap in plt.cm.datad:
            colormap = plt.get_cmap(cmap)
        else:
            raise ValueError(
                'Get unrecognised name for the colormap `{}`. Colormaps should be from standard matplotlib set of from cmocean package.'
                .format(cmap))
    else:
        if clim:
            colormap = cmo.cmap_d['balance']
        else:
            colormap = plt.get_cmap('Spectral_r')

    sstep = timestep
    radius_of_influence = influence

    left, right, down, up = box
    lonNumber, latNumber = res

    mesh = pf.load_mesh(meshpath, abg=abg, usepickle=False, usejoblib=True)
    flf = Dataset(ifile)
    lonreg = np.linspace(left, right, lonNumber)
    latreg = np.linspace(down, up, latNumber)
    lonreg2, latreg2 = np.meshgrid(lonreg, latreg)

    dind = (abs(mesh.zlevs - depth)).argmin()
    realdepth = mesh.zlevs[dind]

    level_data, nnn = pf.get_data(flf.variables[variable][sstep], mesh,
                                  realdepth)
    if interp == 'nn':
        ofesom = pf.fesom2regular(level_data,
                                  mesh,
                                  lonreg2,
                                  latreg2,
                                  radius_of_influence=radius_of_influence)
    elif interp == 'idist':
        ofesom = pf.fesom2regular(level_data,
                                  mesh,
                                  lonreg2,
                                  latreg2,
                                  radius_of_influence=radius_of_influence,
                                  how='idist',
                                  k=k)
    elif interp == 'linear':
        points = np.vstack((mesh.x2, mesh.y2)).T
        qh = qhull.Delaunay(points)
        ofesom = LinearNDInterpolator(qh, level_data)((lonreg2, latreg2))

    elif interp == 'cubic':
        points = np.vstack((mesh.x2, mesh.y2)).T
        qh = qhull.Delaunay(points)
        ofesom = CloughTocher2DInterpolator(qh, level_data)((lonreg2, latreg2))

    if clim:
        if variable == 'temp':
            climvar = 'T'
        elif variable == 'salt':
            climvar = 'S'
        else:
            raise ValueError(
                'You have selected --clim/-c option, but variable `{}` is not in climatology. Acceptable values are `temp` and `salt` only.'
                .format(variable))
        #os.path.join(os.path.dirname(__file__), "../")
        pathToClim = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                  "../data/")
        print(pathToClim)
        w = pf.climatology(pathToClim, clim)
        xx, yy, oclim = pf.clim2regular(
            w,
            climvar,
            lonreg2,
            latreg2,
            levels=[realdepth],
            radius_of_influence=radius_of_influence)
        oclim = oclim[0, :, :]
        data = ofesom - oclim
    else:
        data = ofesom

    if mapproj == 'merc':
        ax = plt.subplot(111, projection=ccrs.Mercator())
    elif mapproj == 'pc':
        ax = plt.subplot(111, projection=ccrs.PlateCarree())
    elif mapproj == 'np':
        ax = plt.subplot(111, projection=ccrs.NorthPolarStereo())
    elif mapproj == 'sp':
        ax = plt.subplot(111, projection=ccrs.SouthPolarStereo())
    elif mapproj == 'rob':
        ax = plt.subplot(111, projection=ccrs.Robinson())

    ax.set_extent([left, right, down, up], crs=ccrs.PlateCarree())

    if levels:
        mmin, mmax, nnum = levels
        nnum = int(nnum)
    else:
        mmin = np.nanmin(data)
        mmax = np.nanmax(data)
        nnum = 40

    data_levels = np.linspace(mmin, mmax, nnum)
    if ptype == 'cf':
        mm = ax.contourf(lonreg,\
                     latreg,\
                     data,
                     levels = data_levels,
                     transform=ccrs.PlateCarree(),
                     cmap=colormap,
                    extend='both')
    elif ptype == 'pcm':
        data_cyc, lon_cyc = add_cyclic_point(data, coord=lonreg)
        mm = ax.pcolormesh(lon_cyc,\
                         latreg,\
                         data_cyc,
                         vmin = mmin,
                         vmax = mmax,
                         transform=ccrs.PlateCarree(),
                         cmap=colormap,
                        )
    else:
        raise ValueError('Inknown plot type {}'.format(ptype))

    ax.coastlines(resolution='50m', lw=0.5)
    ax.add_feature(
        cfeature.GSHHSFeature(levels=[1], scale='low', facecolor='lightgray'))
    cb = plt.colorbar(mm, orientation='horizontal', pad=0.03)
    cb.set_label(flf.variables[variable].units)
    plt.title('{} at {}m.'.format(variable, realdepth))
    plt.tight_layout()
    if ofile:
        plt.savefig(ofile, dpi=100)
    else:
        plt.show()
Ejemplo n.º 3
0
def convert(meshpath, ipath, opath, variable, depths, box,
            res, influence, timestep, abg, interp, ncore, k):
    '''
    meshpath - Path to the folder with FESOM1.4 mesh files.

    ipath    - Path to FESOM1.4 netCDF file or files (with wildcard).

    opath    - path where the output will be stored.

    variable - The netCDF variable to be converted.
    '''
    print(ipath)
    mesh = pf.load_mesh(meshpath, abg=abg, usepickle=False, usejoblib=True)

    sstep = timestep
    radius_of_influence = influence

    left, right, down, up = box
    lonNumber, latNumber = res

    lonreg = np.linspace(left, right, lonNumber)
    latreg = np.linspace(down, up, latNumber)
    lonreg2, latreg2 = np.meshgrid(lonreg, latreg)

    localdir = os.path.dirname(os.path.abspath(__file__))
    # print(os.path.abspath(__file__))
    print('localdir='+localdir)
    with open(localdir+'/CMIP6_Omon.json') as data_file:
        cmore_table = json.load(data_file, object_pairs_hook=OrderedDict)

    with open(localdir+'/CMIP6_SIday.json') as data_file:
        cmore_table_ice = json.load(data_file, object_pairs_hook=OrderedDict)

    depths = np.array(depths.split(' '),dtype='float32')
    if depths[0] == -1:
        dind = range(mesh.zlevs.shape[0])
        realdepth = mesh.zlevs
    else:
        dind = []
        realdepth = []
        for depth in depths:
            ddepth = abs(mesh.zlevs-depth).argmin()
            dind.append(ddepth)
            realdepth.append(mesh.zlevs[ddepth])
    print(dind)
    print(realdepth)
    #realdepth = mesh.zlevs[dind]
    
    distances, inds = pf.create_indexes_and_distances(mesh, lonreg2, latreg2,\
                                                      k=k, n_jobs=4)

    ind_depth_all = []
    ind_noempty_all = []
    ind_empty_all = []

    for i in range(len(mesh.zlevs)):
        ind_depth, ind_noempty, ind_empty = pf.ind_for_depth(mesh.zlevs[i], mesh)
        ind_depth_all.append(ind_depth)
        ind_noempty_all.append(ind_noempty)
        ind_empty_all.append(ind_empty)
    if interp == 'nn':
        topo_interp = pf.fesom2regular(mesh.topo, mesh, lonreg2, latreg2, distances=distances,
                                inds=inds, radius_of_influence=radius_of_influence, n_jobs=1)
        k = 1
        distances, inds = pf.create_indexes_and_distances(mesh, lonreg2, latreg2,\
                                                          k=k, n_jobs=4)
        points, qh = None, None
    elif interp == 'idist':
        topo_interp = pf.fesom2regular(mesh.topo, mesh, lonreg2, latreg2, distances=distances,
                                inds=inds, radius_of_influence=radius_of_influence, n_jobs=1, how='idist')
        k = k
        distances, inds = pf.create_indexes_and_distances(mesh, lonreg2, latreg2,\
                                                          k=k, n_jobs=4)
        points, qh = None, None
    elif interp == 'linear':
        points = np.vstack((mesh.x2, mesh.y2)).T
        qh = qhull.Delaunay(points)
        topo_interp = LinearNDInterpolator(qh, mesh.topo)((lonreg2, latreg2))
        distances, inds = None, None
    elif interp == 'cubic':
        points = np.vstack((mesh.x2, mesh.y2)).T
        qh = qhull.Delaunay(points)
        topo_interp = CloughTocher2DInterpolator(qh, mesh.topo)((lonreg2, latreg2))
        distances, inds = None, None

    mdata = maskoceans(lonreg2, latreg2, topo_interp, resolution = 'h', inlands=False)
    topo = np.ma.masked_where(~mdata.mask, topo_interp)
    
    # Backend is switched to threading for linear and cubic interpolations
    # due to problems with memory mapping.
    # One have to test threading vs multiprocessing.
    if (interp == 'linear') or (interp=='cubic'):
        backend = 'threading'
    else:
        backend = 'multiprocessing'

    Parallel(n_jobs=ncore, backend=backend, verbose=50)(delayed(scalar2geo)(ifile, opath, variable,
                                       mesh, ind_noempty_all,
                                       ind_empty_all,ind_depth_all, cmore_table, lonreg2, latreg2,
                                       distances, inds, radius_of_influence, topo, points, interp, qh, timestep, dind, realdepth) for ifile in ipath)
Ejemplo n.º 4
0
def scalar2geo(ifile, opath, variable,
               mesh, ind_noempty_all,
               ind_empty_all,ind_depth_all, cmore_table,
               lonreg2, latreg2, distances, inds, radius_of_influence,
               topo, points, interp, qh, timestep, dind, realdepth):
    print(ifile)
    ext = variable
    #ifile = ipath
    ofile = os.path.join(opath, '{}_{}.nc'.format(os.path.basename(ifile)[:-3], ext))

    fl = Dataset(ifile)
    if fl.variables[variable].shape[1] == mesh.n2d:
        dim3d = False
        dind = [dind[0]]
        realdepth = [realdepth[0]]
    elif fl.variables[variable].shape[1] == mesh.n3d:
        dim3d = True
    else:
        raise ValueError('Variable size {} is not equal to number of 2d ({}) or 3d ({}) nodes'.format(fl.variables[variable].shape[1], mesh.n2d, mesh.n3d))

    fw = Dataset(ofile, mode='w',data_model='NETCDF4_CLASSIC', )

    fw.createDimension('latitude', lonreg2.shape[0])
    fw.createDimension('longitude', latreg2.shape[1])
    fw.createDimension('time', None)
    fw.createDimension('depth_coord',  len(realdepth) )

    lat = fw.createVariable('latitude', 'd', ('latitude'))
    lat.setncatts(noempty_dict(cmore_table['axis_entry']['latitude']))
    lat[:] = latreg2[:,0].flatten()

    lon = fw.createVariable('longitude', 'd', ('longitude'))
    lon.setncatts(noempty_dict(cmore_table['axis_entry']['longitude']))
    lon[:] = lonreg2[0,:].flatten()

    depth = fw.createVariable('depth_coord','d',('depth_coord'))
    depth.setncatts(noempty_dict(cmore_table['axis_entry']['depth_coord']))
    depth[:] = realdepth[:]

    time = fw.createVariable('time','d',('time'))
    time.setncatts(noempty_dict(cmore_table['axis_entry']['time']))
    time.units = fl.variables['time'].units
    if timestep == -1:
        time[:] = fl.variables['time'][:]
    else:
        time[:] = fl.variables['time'][timestep]

    # ii = LinearNDInterpolator(qh, mesh.topo)
    if timestep == -1:
        timesteps = range(fl.variables[variable].shape[0])
    else:
        timesteps = range(timestep, timestep+1)
    if True:
        temp = fw.createVariable(variable,'d',\
                                ('time','depth_coord','latitude','longitude'), \
                                fill_value=-9999, zlib=False, complevel=1)

        for ttime in timesteps:

            all_layers = fl.variables[variable][ttime,:]
            level_data=np.zeros(shape=(mesh.n2d))
            inter_data=np.zeros(shape=(len(mesh.zlevs),lonreg2.shape[0], lonreg2.shape[1]))
            # inter_data=np.ma.masked_where(topo[0,:,:].mask, inter_data)
            #for i in range(len(mesh.zlevs)):
            for n, i in enumerate(dind):
                #level_data=np.zeros(shape=(mesh.n2d))
                
                level_data[ind_noempty_all[i]]=all_layers[ind_depth_all[i][ind_noempty_all[i]]]
                level_data[ind_empty_all[i]] = np.nan
                if interp == 'nn':
                    air_nearest = pf.fesom2regular(level_data, mesh, lonreg2, latreg2, distances=distances,
                                            inds=inds, radius_of_influence=radius_of_influence, n_jobs=1)
                elif interp == 'idist':
                    air_nearest = pf.fesom2regular(level_data, mesh, lonreg2, latreg2, distances=distances,
                                            inds=inds, radius_of_influence=radius_of_influence, n_jobs=1, how='idist')
                elif interp == 'linear':
                    #level_data = level_data.copy()
                    #lonreg2 = lonreg2.tolist()
                    #latreg2 = latreg2.tolist()
                    air_nearest = LinearNDInterpolator(qh, level_data)((lonreg2, latreg2))
                elif interp == 'cubic':
                    air_nearest =CloughTocher2DInterpolator(qh, level_data)((lonreg2, latreg2))
                    print('cubic')
                    print(air_nearest.min())

                air_nearest = np.ma.masked_where(topo.mask, air_nearest)
                if timestep == -1:
                    temp[ttime,n,:,:] = air_nearest[:,:].filled(-9999)
                else:
                    temp[0,n,:,:] = air_nearest[:,:].filled(-9999)

    fl.close()
    fw.close()
Ejemplo n.º 5
0
def convertit(year):
    ifile = os.path.join(path_to_data, ifile_template.format(str(year)))
    ofile = os.path.join(path_to_output, ofile_template.format(str(year)))

    print('Open {}'.format(ifile))
    fl = Dataset(ifile)
    fw = Dataset(
        ofile,
        mode='w',
        data_model='NETCDF4_CLASSIC',
    )

    var2d = 0
    var3d = 0
    for varname in out_vars:
        if vardir[varname]['dims'] == '2D':
            var2d += 1
        elif vardir[varname]['dims'] == '3D':
            var3d += 1
    var3d = var3d * len(levels) * fl.variables['time'].shape[0]
    var2d = var2d * fl.variables['time'].shape[0]
    progress_total = var3d + var2d
    progress_passed = 0

    # create dimensions
    fw.createDimension('latitude', lons.shape[0])
    fw.createDimension('longitude', lats.shape[1])
    fw.createDimension('time', None)
    fw.createDimension('depth_coord', levels.shape[0])

    lat = fw.createVariable('latitude', 'd', ('latitude'))
    lat.setncatts(noempty_dict(cmore_table['axis_entry']['latitude']))
    lat[:] = lats[:, 0].flatten()

    lon = fw.createVariable('longitude', 'd', ('longitude'))
    lon.setncatts(noempty_dict(cmore_table['axis_entry']['longitude']))
    lon[:] = lons[0, :].flatten()

    depth = fw.createVariable('depth_coord', 'd', ('depth_coord'))
    depth.setncatts(noempty_dict(cmore_table['axis_entry']['depth_coord']))
    depth[:] = levels

    time = fw.createVariable('time', 'd', ('time'))
    time.setncatts(cmore_table['axis_entry']['time'])
    if distribute_timesteps:
        nsteps = fl.variables['time'].shape[0]
        td = datetime.timedelta(days=365 / nsteps)
        sdate = datetime.datetime(year, 1, 1, 0, 0, 0)
        seconds = []
        for i in range(1, nsteps + 1):
            workdate = sdate + td * i
            seconds.append((workdate - sdate).total_seconds())
        time.units = 'seconds since {}-01-01 00:00:00'.format(str(year))
        time[:] = seconds

    elif fl.variables['time'].units.strip().startswith('seconds since'):
        time.units = fl.variables['time'].units
        time[:] = fl.variables['time'][:]
    elif fl.variables['time'].shape[0] == 12:
        sdate = datetime.datetime(year, 1, 1, 0, 0, 0)
        td = datetime.timedelta(days=14.5)
        seconds = []
        for i in range(1, 13):
            workdate = datetime.datetime(year, i, 1, 0, 0, 0) + td
            seconds.append((workdate - sdate).total_seconds())
        time.units = 'seconds since {}-01-01 00:00:00'.format(str(year))
        time[:] = seconds
    else:
        time.units = 'seconds since {}-01-01 00:00:00'.format(str(year))
        time[:] = fl.variables['time'][:]

    # Store processed variables (to not repeat
    # processing for vector variables)
    completed = []
    # variables loop
    for varname in out_vars:

        # check if we have to convert two variables at once
        # for vector variables.
        do_two_vars = (vardir[varname].has_key('rotate_with') is True)

        #print("Converting {}.".format(varname))

        # skip if the variable was already converted
        if varname in completed:
            pass
        # 3D variables processing
        elif vardir[varname]['dims'] == '3D':

            # Create netCDF variable
            temp = fw.createVariable(vardir[varname]['cname'],'d',\
                                     ('time','depth_coord','latitude','longitude'), \
                                     fill_value=-99999, zlib=zlib, complevel=1)
            # add CMOR complient attributes
            temp.setncatts(
                noempty_dict(
                    cmore_table['variable_entry'][vardir[varname]['cname']]))

            # If we have two convert two variables at once, create netCDF variable for
            # the second variable
            if do_two_vars is True:

                varname2 = vardir[varname]['rotate_with']
                temp2 = fw.createVariable(
                    vardir[varname2]['cname'],
                    'd', ('time', 'depth_coord', 'latitude', 'longitude'),
                    fill_value=-99999,
                    zlib=zlib,
                    complevel=1)
                temp2.setncatts(
                    noempty_dict(cmore_table['variable_entry'][vardir[varname2]
                                                               ['cname']]))

            # Loop over timesteps for 3D variables
            for i in range(fl.variables[varname].shape[0]):
                #for i in range(2):
                # Get the whole 3D field in to memory. It turns out that this is more
                # effective than to select individual levels from the file located on the disk.
                all_layers = fl.variables[varname][i, :]

                # Get the data for the second variable if needed
                if do_two_vars is True:
                    #print("Also converting {}, triggered by {}.".format(varname2, varname))
                    all_layers2 = fl.variables[varname2][i, :]
                # Loop over vertical levels
                for dlev, llev in enumerate(levels):
                    # get indeces of the gridpoints that corespond to the level
                    ind_depth, ind_noempty, ind_empty = pf.ind_for_depth(
                        llev, mesh)

                    # get the data for the level
                    level_data = np.zeros(shape=(mesh.n2d))
                    level_data[ind_noempty] = all_layers[
                        ind_depth[ind_noempty]]
                    level_data[ind_empty] = np.nan

                    # Spetial treatment of the vector variables that need rotation
                    if do_two_vars is True:

                        # get the data for the level of the second variable
                        level_data2 = np.zeros(shape=(mesh.n2d))
                        level_data2[ind_noempty] = all_layers2[
                            ind_depth[ind_noempty]]
                        level_data2[ind_empty] = np.nan

                        #print('Rotate {} and {}'.format(varname, varname2))

                        # Rotate vector variables to geographical grid
                        uunr,vunr = pf.vec_rotate_r2g(angles_for_rotation[0],angles_for_rotation[1], \
                                                angles_for_rotation[2], mesh.x2, mesh.y2,\
                                                level_data, level_data2, 1)

                        # Interpolate rotated variables
                        #print('interpolation, layer {}'.format(str(llev)))
                        air_nearest = pf.fesom2regular(uunr, mesh, lons, lats, distances=distances,\
                                           inds=inds, radius_of_influence=radius_of_influence, n_jobs=1)

                        air_nearest2 = pf.fesom2regular(vunr, mesh, lons, lats, distances=distances,\
                                           inds=inds, radius_of_influence=radius_of_influence, n_jobs=1)

                        # Put values to the netCDF variables
                        temp[i, dlev, :, :] = air_nearest[:, :].filled(-99999)
                        temp2[i,
                              dlev, :, :] = air_nearest2[:, :].filled(-99999)

                    else:
                        # Interpolate scalar variable
                        #print('interpolation, layer {}'.format(str(llev)))
                        air_nearest = pf.fesom2regular(level_data, mesh, lons, lats, distances=distances,\
                                           inds=inds, radius_of_influence=radius_of_influence, n_jobs=1)
                        # Put values to the netCDF variable
                        temp[i, dlev, :, :] = air_nearest[:, :].filled(-99999)

                    progress_passed += 1

                    if do_two_vars is True:
                        progress_passed += 1

                    progressbar(progress_total, progress_passed, year,\
                                varname, i, llev, time)

            # END Loop over timesteps for 3D variables
            # add variable to the list of processed variables
            completed.append(varname)

            if do_two_vars is True:
                completed.append(varname2)
        # End 3D variables processing

        # 2D variables processing
        elif vardir[varname]['dims'] == '2D':
            # Create netCDF variable
            temp = fw.createVariable(vardir[varname]['cname'],'d',\
                                     ('time','latitude','longitude'), \
                                     fill_value=-99999, zlib=zlib, complevel=1)

            # add CMOR complient attributes
            temp.setncatts(
                noempty_dict(
                    cmore_table['variable_entry'][vardir[varname]['cname']]))

            # If we have two convert two variables at once, create netCDF variable for
            # the second variable
            if do_two_vars is True:

                varname2 = vardir[varname]['rotate_with']
                temp2 = fw.createVariable(vardir[varname2]['cname'],'d',\
                                          ('time','latitude','longitude'), \
                                          fill_value=-99999, zlib=zlib, complevel=1)
                temp2.setncatts(
                    noempty_dict(cmore_table['variable_entry'][vardir[varname2]
                                                               ['cname']]))

            # For sea ice variables we have to open different file, so
            # open ether ocean or sea ice input file.
            if vardir[varname]['realm'] == 'ocean':
                temp.setncatts(
                    noempty_dict(cmore_table['variable_entry'][vardir[varname]
                                                               ['cname']]))

                ncfile_handler = fl

            elif vardir[varname]['realm'] == 'seaice':
                temp.setncatts(
                    noempty_dict(cmore_table['variable_entry'][vardir[varname]
                                                               ['cname']]))

                ifile_ice = os.path.join(path_to_data,
                                         ifile_template_ice.format(str(year)))
                ncfile_handler = Dataset(ifile_ice)

            # Loop over timesteps for 2D variables
            for i in range(ncfile_handler.variables[varname].shape[0]):
                #for i in range(2):
                # Get the whole 3D field in to memory. It turns out that this is more
                # effective than to select individual levels from the file located on the disk.
                all_layers = ncfile_handler.variables[varname][i, :]

                # Get the data for the second variable if needed
                if do_two_vars is True:
                    print("Also converting {}, triggered by {}.".format(
                        varname2, varname))
                    all_layers2 = ncfile_handler.variables[varname2][i, :]

                # get indeces of the gridpoints that corespond to the surface level
                ind_depth, ind_noempty, ind_empty = pf.ind_for_depth(0, mesh)

                # get the data for the surface level
                level_data = np.zeros(shape=(mesh.n2d))
                level_data[ind_noempty] = all_layers[ind_depth[ind_noempty]]
                level_data[ind_empty] = np.nan

                # Spetial treatment of the vector variables that need rotation
                if do_two_vars is True:
                    # get the data for the surface level of the second variable
                    level_data2 = np.zeros(shape=(mesh.n2d))
                    level_data2[ind_noempty] = all_layers2[
                        ind_depth[ind_noempty]]
                    level_data2[ind_empty] = np.nan

                    # Rotate vector variables to geographical grid
                    print('Rotate {} and {}'.format(varname, varname2))
                    uunr,vunr = pf.vec_rotate_r2g(angles_for_rotation[0],angles_for_rotation[1], \
                                            angles_for_rotation[2], mesh.x2, mesh.y2,\
                                            level_data, level_data2, 1)

                    # Interpolate rotated variables )
                    air_nearest = pf.fesom2regular(uunr, mesh, lons, lats, distances=distances,\
                                       inds=inds, radius_of_influence=radius_of_influence, n_jobs=1)

                    air_nearest2 = pf.fesom2regular(vunr, mesh, lons, lats, distances=distances,\
                                       inds=inds, radius_of_influence=radius_of_influence, n_jobs=1)

                    # fill in netCDF variables
                    temp[i, :, :] = air_nearest[:, :].filled(-99999)
                    temp2[i, :, :] = air_nearest2[:, :].filled(-99999)

                else:
                    # Interpolate scalar variable and fill in netCDF variable.
                    air_nearest = pf.fesom2regular(level_data, mesh, lons, lats, distances=distances,\
                                       inds=inds, radius_of_influence=radius_of_influence, n_jobs=1)
                    temp[i, :, :] = air_nearest[:, :].filled(-99999)

                progress_passed += 1

                if do_two_vars is True:
                    progress_passed += 1

                progressbar(progress_total, progress_passed, year,\
                                varname, i, 0, time)

            # END Loop over timesteps for 2D variables

            completed.append(varname)

            if do_two_vars is True:
                completed.append(varname2)

    # end variables loop

    fw.close()
    print('The {} is ready'.format(ofile))
Ejemplo n.º 6
0
                        # get the data for the level of the second variable
                        level_data2 = np.zeros(shape=(mesh.n2d))
                        level_data2[ind_noempty] = all_layers2[
                            ind_depth[ind_noempty]]
                        level_data2[ind_empty] = np.nan

                        #print('Rotate {} and {}'.format(varname, varname2))

                        # Rotate vector variables to geographical grid
                        uunr,vunr = pf.vec_rotate_r2g(angles_for_rotation[0],angles_for_rotation[1], \
                                                angles_for_rotation[2], mesh.x2, mesh.y2,\
                                                level_data, level_data2, 1)

                        # Interpolate rotated variables
                        #print('interpolation, layer {}'.format(str(llev)))
                        air_nearest = pf.fesom2regular(uunr, mesh, lons, lats, distances=distances,\
                                           inds=inds, radius_of_influence=radius_of_influence, n_jobs=1)

                        air_nearest2 = pf.fesom2regular(vunr, mesh, lons, lats, distances=distances,\
                                           inds=inds, radius_of_influence=radius_of_influence, n_jobs=1)

                        # Put values to the netCDF variables
                        temp[i, dlev, :, :] = air_nearest[:, :].filled(-99999)
                        temp2[i,
                              dlev, :, :] = air_nearest2[:, :].filled(-99999)

                    else:
                        # Interpolate scalar variable
                        #print('interpolation, layer {}'.format(str(llev)))
                        air_nearest = pf.fesom2regular(level_data, mesh, lons, lats, distances=distances,\
                                           inds=inds, radius_of_influence=radius_of_influence, n_jobs=1)
                        # Put values to the netCDF variable