def get_monthly_average_2d(filePrefix, botIndices):
    field, itrs, metadata = rdmds(filePrefix, rec=[0], returnmeta=True)
    ny, nx = field.shape
    # print nx, ny, nz
    yearCount = metadata['nrecords'][0] // 12
    dims = [12, nx, ny]

    mask2D = botIndices == -1
    xIndices, yIndices = numpy.meshgrid(numpy.arange(nx),
                                        numpy.arange(ny),
                                        indexing='ij')
    monthlyClimatologies = numpy.ma.masked_all(dims)
    for month in range(12):
        first = True
        for year in range(yearCount):
            print('{:04d}-{:02d}'.format(year + 2005, month + 1))
            recordIndex = year * 12 + month
            field = rdmds(filePrefix, rec=[recordIndex])
            field = field.transpose(1, 0)

            field = numpy.ma.masked_array(field, mask=mask2D)
            if first:
                monthlyClimatologies[month, :, :] = field / float(yearCount)
                first = False
            else:
                monthlyClimatologies[month, :, :] = \
                    monthlyClimatologies[month, :, :] + \
                    field/float(yearCount)

    monthlyClimatologies = monthlyClimatologies.transpose(0, 2, 1)
    return monthlyClimatologies
Example #2
0
File: utils.py Project: joernc/NA
def get_dx_dy(folder):
    DXC = rdmds(folder + "DXC")
    DYC = rdmds(folder + "DYC")
    DXG = rdmds(folder + "DXG")
    DYG = rdmds(folder + "DYG")
    DXC, DYC = rearrange_grid_spacing(DXC, DYC)
    DXG, DYG = rearrange_grid_spacing(DXG, DYG)
    return DXC, DYC, DXG, DYG
Example #3
0
def make_TS_ds(sose_dir, records=None):
    '''Reads in SothernOceanStateEstimate Temperatures and Salinities and returns them in a Xarray dataset.'''

    #load grid data
    print("load grid")
    grid_path = os.path.join(sose_dir, 'grid.mat')
    grid_raw = sio.loadmat(grid_path)

    longitude = grid_raw["XC"][:, 0]
    latitude = grid_raw['YC'][0, :]
    depth = grid_raw['RC'].squeeze()
    DRC = grid_raw['DRC'].squeeze()
    maskC = np.flipud(np.rot90(grid_raw['maskCtrlC']))
    DXC = np.flipud(np.rot90(grid_raw['DXC']))
    DYC = np.flipud(np.rot90(grid_raw['DYC']))
    Depth = np.flipud(np.rot90(grid_raw['Depth']))

    #load temperature data
    print("load temperature")
    temp_path = os.path.join(sose_dir, 'THETA_mnthlyBar')
    temp_raw = mds.rdmds(temp_path, 100, rec=records, fill_value=np.NaN)

    #load salt data
    print("load salt")
    salt_path = os.path.join(sose_dir, 'SALT_mnthlyBar')
    salt_raw = mds.rdmds(salt_path, 100, rec=records, fill_value=np.NaN)

    #define array of datetime range
    time = pd.period_range('2005-01', periods=len(temp_raw), freq='M')
    time_stamp = pd.Timestamp('2005-01')

    #construct Xarray dataset
    print("construct Xarray dataset")
    ds = xr.Dataset(
        {
            'temperature':
            (['time', 'depth', 'latitude', 'longitude'], temp_raw),
            'salinity': (['time', 'depth', 'latitude', 'longitude'], salt_raw),
            'maskC': (['latitude', 'longitude', 'depth'], maskC),
            'DRC': (['depth'], DRC),
            'DXC': (['latitude', 'longitude'], DXC),
            'DYC': (['latitude', 'longitude'], DYC),
            'Depth': (['latitude', 'longitude'], Depth)
        },
        coords={
            'longitude': (('longitude'), longitude),
            'latitude': (('latitude'), latitude),
            'depth': (('depth'), depth),
            'time': (('time'), time),
            'reference_time': time_stamp
        })

    print("done!")

    return ds
Example #4
0
File: utils.py Project: joernc/NA
def get_x_y(folder, do_rearrange=True):
    XC = rdmds(folder + "XC")
    XG = rdmds(folder + "XG")
    YC = rdmds(folder + "YC")
    YG = rdmds(folder + "YG")
    if do_rearrange:
        XC = rearrange(XC)
        XG = rearrange(XG)
        YC = rearrange(YC)
        YG = rearrange(YG)
    return XC, YC, XG, YG
Example #5
0
# make sure correct location was chosen
print XC[iy0c:iy1c + 1, ixc:ixc + 2]
print YC[iy0c:iy1c + 1, ixc]
print DXC[iy0c:iy1c + 1, ixc]

# loop over hours
for hour in range(start, end + 1):

    # file to save record to
    savefile = savedir + "rec_{:010d}.npz".format(hour)

    # if this step is required
    if (not os.path.isfile(savefile)) or force_overwrite:

        step = hour * 3600 / dt
        print hour, step

        # read variables
        U = rdmds(diagdir + 'trsp_3d_set1', step, rec=0, lev=0)
        V = rdmds(diagdir + 'trsp_3d_set1', step, rec=1, lev=0)
        U, V = utils.rearrange_velocities(U, V)
        h = rdmds(diagdir + 'state_2d_set1', step, rec=0)
        h = utils.rearrange(h)

        # save to file
        np.savez(savefile,
                 h=h[iy0c:iy1c + 1, ixc:ixc + 2],
                 u=U[iy0c:iy1c + 1, ixc:ixc + 3],
                 v=V[iy0c:iy1c + 2, ixc:ixc + 2])
Example #6
0
# run dir
rundir = basedir + 'run_' + name + '/'

# subdir with diagnostics
diagdir = rundir + 'diags/'

# location where diag files are saved
savedir = basedir + 'diag/' + name + '/section_{:04d}/'.format(int(lon))

# create output dir if it doesn't exist yet
if not os.path.isdir(savedir):
    os.mkdir(savedir)

# get grid info
XC, YC, XG, YG = utils.get_x_y(rundir)
z = rdmds(rundir + "RC")[:, 0, 0]
z = z[utils.diag_lev]

# find index of nearest model point in rotated orientation

dist = (XC[0, :] - lon)**2
dist[np.isnan(dist)] = 999.
ix = np.argmin(dist)

# make sure correct location was chosen
print XC[0, ix]

# loop over hours
for hour in range(start, end + 1):

    # file to save record to
Example #7
0
# create output dir if it doesn't exist yet
if not os.path.isdir(savedir):
    os.mkdir(savedir)
if not os.path.isdir(savedir + "ins_eke_map/"):
    os.mkdir(savedir + "ins_eke_map/")

# save coordinates to file
np.savez(savedir + "eke_map/coord.npz", x=XC, y=YC)

# loop over hours
for hour in range(start, end + 1):

    # if this step is required
    if not os.path.isfile(savedir + 'ins_eke_map/rec_{0:010d}.npy'.format(hour)
                          ) or force_overwrite:

        # get time step
        step = hour * 3600 / dt

        print hour, step

        # read velocities
        U = rdmds(diagdir + 'trsp_3d_set1', step, rec=0, lev=level)
        V = rdmds(diagdir + 'trsp_3d_set1', step, rec=1, lev=level)
        U, V = utils.rearrange_velocities(U, V)

        eke = (U**2 + V**2) / 2

        # save KE
        np.save(savedir + 'ins_eke_map/rec_{0:010d}.npy'.format(hour), eke)
def load_TH_diags(path, sim_type):
    """
    Load temperature diagnostics for channel model.

    path: where to look look for the data
    sim_type: expt or cont
    """

    TOTTTEND = xr.open_dataset('{0}/TOTTTEND_{1}.nc'.format(path, sim_type))
    ADVx_TH = xr.open_dataset('{0}/ADVx_TH_{1}.nc'.format(path, sim_type))
    ADVy_TH = xr.open_dataset('{0}/ADVy_TH_{1}.nc'.format(path, sim_type))
    ADVr_TH = xr.open_dataset('{0}/ADVr_TH_{1}.nc'.format(path, sim_type))
    DFxE_TH = xr.open_dataset('{0}/DFxE_TH_{1}.nc'.format(path, sim_type))
    DFyE_TH = xr.open_dataset('{0}/DFyE_TH_{1}.nc'.format(path, sim_type))
    DFrE_TH = xr.open_dataset('{0}/DFrE_TH_{1}.nc'.format(path, sim_type))
    DFrI_TH = xr.open_dataset('{0}/DFrI_TH_{1}.nc'.format(path, sim_type))
    TFLUX = xr.open_dataset('{0}/TFLUX_{1}.nc'.format(path, sim_type))
    WTHMASS = xr.open_dataset('{0}/WTHMASS_{1}.nc'.format(path, sim_type))

    TOTTTEND_clim = xr.open_dataset('{0}/TOTTTEND_{1}_clim.nc'.format(
        path, sim_type))
    ADVx_TH_clim = xr.open_dataset('{0}/ADVx_TH_{1}_clim.nc'.format(
        path, sim_type))
    ADVy_TH_clim = xr.open_dataset('{0}/ADVy_TH_{1}_clim.nc'.format(
        path, sim_type))
    ADVr_TH_clim = xr.open_dataset('{0}/ADVr_TH_{1}_clim.nc'.format(
        path, sim_type))
    DFxE_TH_clim = xr.open_dataset('{0}/DFxE_TH_{1}_clim.nc'.format(
        path, sim_type))
    DFyE_TH_clim = xr.open_dataset('{0}/DFyE_TH_{1}_clim.nc'.format(
        path, sim_type))
    DFrE_TH_clim = xr.open_dataset('{0}/DFrE_TH_{1}_clim.nc'.format(
        path, sim_type))
    DFrI_TH_clim = xr.open_dataset('{0}/DFrI_TH_{1}_clim.nc'.format(
        path, sim_type))
    TFLUX_clim = xr.open_dataset('{0}/TFLUX_{1}_clim.nc'.format(
        path, sim_type))
    WTHMASS_clim = xr.open_dataset('{0}/WTHMASS_{1}_clim.nc'.format(
        path, sim_type))

    RAC_data = mds.rdmds('{0}/40/RAC'.format(path))
    DRF_data = np.squeeze(mds.rdmds('{0}/40/DRF'.format(path)))
    hFacC_data = mds.rdmds('{0}/40/hFacC'.format(path))

    hFacC_temp = xr.DataArray(data=hFacC_data,
                              coords={
                                  'Z': TOTTTEND.Z,
                                  'YC': TOTTTEND.YC,
                                  'XC': TOTTTEND.XC
                              },
                              dims=['Z', 'YC', 'XC'])
    hFacC = xr.Dataset(data_vars={'hFacC': hFacC_temp})

    DRF_temp = xr.DataArray(data=DRF_data,
                            coords={'Z': TOTTTEND.Z},
                            dims=['Z'])
    DRF = xr.Dataset(data_vars={'DRF': DRF_temp})

    RAC_temp = xr.DataArray(data=RAC_data,
                            coords={
                                'YC': TOTTTEND.YC,
                                'XC': TOTTTEND.XC
                            },
                            dims=['YC', 'XC'])
    RAC = xr.Dataset(data_vars={'RAC': RAC_temp})

    # combine into one dataset
    TH_diags = xr.merge([
        TOTTTEND, ADVx_TH, ADVy_TH, ADVr_TH, DFxE_TH, DFyE_TH, DFrE_TH,
        DFrI_TH, TFLUX, WTHMASS, TOTTTEND_clim, ADVx_TH_clim, ADVy_TH_clim,
        ADVr_TH_clim, DFxE_TH_clim, DFyE_TH_clim, DFrE_TH_clim, DFrI_TH_clim,
        TFLUX_clim, WTHMASS_clim, hFacC, DRF, RAC
    ])
    return TH_diags
Example #9
0
    return a, a1, a5

def rearrangeuv(U, V):
    U1 = np.reshape(U[:nx,:], (ny,nx))
    V1 = np.reshape(V[:nx,:], (ny,nx))
    U5 = np.reshape(V[nx:,:], (nx,ny))
    V5 = np.roll(np.reshape(-U[nx:,:], (nx,ny)), -1, axis=1)
    U = np.concatenate((U5[:,::-1].T, U1), axis=1)
    V = np.concatenate((V5[:,::-1].T, V1), axis=1)
    return U, V, U1, U5, V1, V5

rundir = '/nobackup1/joernc/patches/NA_2160/run_t1w1/'
diagdir = rundir + 'diags/'

# bathymetry
d = rdmds(rundir + 'Depth')
d[d==0] = np.nan
d, d1, d5 = rearrange(d)

# T, S
T = rdmds(diagdir + 'state_3d_set1', timestep, lev=level, rec=0)
S = rdmds(diagdir + 'state_3d_set1', timestep, lev=level, rec=1)
T[T==0] = np.nan
S[S==0] = np.nan
T, T1, T5 = rearrange(T)
S, S1, S5 = rearrange(S)

# U, V
U = rdmds(diagdir + 'trsp_3d_set1', timestep, lev=level, rec=0)
V = rdmds(diagdir + 'trsp_3d_set1', timestep, lev=level, rec=1)
U[U==0] = np.nan
Example #10
0
rundir = basedir + 'run_' + name + '/'

# subdir with diagnostics
diagdir = rundir + 'diags/'

# location where diag files are saved
savedir = basedir + "diag/" + name \
        + "/mooring_{:04d}_{:04d}/".format(int(lon),int(lat))

# create output dir if it doesn't exist yet
if not os.path.isdir(savedir):
    os.mkdir(savedir)

# get grid info
XC, YC, XG, YG = utils.get_x_y(rundir, do_rearrange=False)
z = rdmds(rundir + "RC")[:, 0, 0]

# save diag depths to file
np.save(savedir + "depths.npy", z[utils.diag_lev])

# find index of nearest model point (c, fx, fy) in original orientation

dist = (XC - lon)**2 + (YC - lat)**2
dist[np.isnan(dist)] = 999
iyc, ixc = np.unravel_index(np.argmin(dist), dist.shape)

dist = (XG - lon)**2 + (YC - lat)**2
dist[np.isnan(dist)] = 999
iyfx, ixfx = np.unravel_index(np.argmin(dist), dist.shape)

dist = (XC - lon)**2 + (YG - lat)**2
Example #11
0
# phixyz=np.reshape(phi,(nx,nt,ny,nr))
# phixy=phixyz[:,:,:,0]

suff = "0000117000"
klev = 50
fldCode = 'T'

if len(sys.argv) > 1:
    suff = "%10.10d" % (int(sys.argv[1]))
if len(sys.argv) > 2:
    klev = (int(sys.argv[2]))
if len(sys.argv) > 3:
    fldCode = sys.argv[3]

T = mds.rdmds("%s%s%s%s" % ('../run/', fldCode, '.', suff))
A = mds.rdmds('../run/RAZ')

Txy = np.reshape(T[klev, :, :], (ny, nt, nx))
# Txy=np.reshape(A,(ny,nt,nx))
plt.figure(1)
plt.clf()
plotxy(Txy)

phiBuf = np.zeros((ny, nx * nt))
phiXYave = np.zeros((nr))
Asum = np.sum(A)
for k in range(nr):
    phiBuf = T[k, :, :] * A
    phiSum = np.sum(phiBuf)
    phiXYave[k] = phiSum / Asum
Example #12
0
dist = (XC-lon)**2+(YC-lat0)**2
dist[np.isnan(dist)] = 999
iy0c, ixc = np.unravel_index(np.argmin(dist), dist.shape)

dist = (XC-lon)**2+(YC-lat1)**2
dist[np.isnan(dist)] = 999
iy1c, ixc = np.unravel_index(np.argmin(dist), dist.shape)

# make sure correct location was chosen
print XC[iy0c:iy1c+1,ixc]
print YC[iy0c:iy1c+1,ixc]

# loop over hours
for hour in range(start, end+1):

    # file to save record to
    savefile = savedir + "rec_{:010d}.npy".format(hour)

    # if this step is required
    if (not os.path.isfile(savefile)) or force_overwrite:

        step = hour*3600/dt
        print hour, step

        # read variable
        h = rdmds(diagdir + 'state_2d_set1', step, rec=0)
        h = utils.rearrange(h)

        # save to file
        np.save(savefile, h[iy0c:iy1c+1,ixc])