def density(eosType,
            salt,
            temp,
            press,
            rhoConst=None,
            Tref=None,
            Sref=None,
            tAlpha=None,
            sBeta=None):

    # Check if pressure is a constant value
    if isinstance(press, float) or isinstance(press, int):
        # Make it an array
        press = np.zeros(temp.shape) + press

    if eosType == 'MDJWF':
        from MITgcmutils.mdjwf import densmdjwf
        return densmdjwf(salt, temp, press)
    elif eosType == 'JMD95':
        from MITgcmutils.jmd95 import densjmd95
        return densjmd95(salt, temp, press)
    elif eosType == 'LINEAR':
        if None in [rhoConst, Tref, Sref, tAlpha, sBeta]:
            print 'Error (density): for eosType LINEAR, you must set rhoConst, Tref, Sref, tAlpha, sBeta'
            sys.exit()
        return dens_linear(salt, temp, rhoConst, Tref, Sref, tAlpha, sBeta)
    else:
        print 'Error (density): invalid eosType ' + eosType
        sys.exit()
Exemple #2
0
def compare_keith_ctd (ctd_dir='/work/n02/n02/kaight/raw_input_data/ctd_data/', output_dir='./', grid_dir='../grid/', pload_file='/work/n02/n02/shared/baspog/MITgcm/WS/WSK/pload_WSK', fig_dir='./', rhoConst=1035., prec=64):

    # Site names
    sites = ['S1', 'S2', 'S3', 'S4', 'S5', 'F1', 'F2', 'F3', 'F4']
    num_sites = len(sites)

    ctd_dir = real_dir(ctd_dir)
    output_dir = real_dir(output_dir)
    grid_dir = real_dir(grid_dir)
    fig_dir = real_dir(fig_dir)

    # Build Grid object
    grid = Grid(grid_dir)
    # Read pressure load anomaly
    pload_anom_2d = read_binary(pload_file, [grid.nx, grid.ny], 'xy', prec=prec)
    
    # Make one figure for each site
    for i in range(num_sites):
        
        # Construct the filename for this site
        site_file = ctd_dir + sites[i] + '.nc'
        # Read arrays
        ctd_press = read_netcdf(site_file, 'pressure')
        ctd_temp = read_netcdf(site_file, 'theta')
        ctd_salt = read_netcdf(site_file, 'salt')
        # Read global attributes
        id = nc.Dataset(site_file, 'r')
        ctd_lat = id.lat
        ctd_lon = id.lon
        ctd_year = int(id.year)
        ctd_month = int(id.month)
        id.close()

        # Get a list of all the output files from MITgcm
        output_files = build_file_list(output_dir)
        # Find the right file and time index corresponding to the given year and month
        file0 = None
        for file in output_files:
            time = netcdf_time(file)
            for t in range(time.size):
                if time[t].year == ctd_year and time[t].month == ctd_month:
                    file0 = file
                    t0 = t
                    date_string = parse_date(time[t])
                    break
            if file0 is not None:
                break
        if file0 is None:
            print(("Error (compare_keith_ctd): couldn't find " + str(ctd_month) + '/' + str(ctd_year) + ' in model output'))
            sys.exit()

        # Read variables
        mit_temp_3d = read_netcdf(file0, 'THETA', time_index=t0)
        mit_salt_3d = read_netcdf(file0, 'SALT', time_index=t0)

        # Interpolate to given point
        # Temperature and land mask
        mit_temp, hfac = interp_bilinear(mit_temp_3d, ctd_lon, ctd_lat, grid, return_hfac=True)
        # Salinity
        mit_salt = interp_bilinear(mit_salt_3d, ctd_lon, ctd_lat, grid)
        # Pressure loading anomaly
        pload_anom = interp_bilinear(pload_anom_2d, ctd_lon, ctd_lat, grid)
        # Ice shelf draft
        draft = interp_bilinear(grid.draft, ctd_lon, ctd_lat, grid)

        # Calculate density, assuming pressure in dbar equals depth in m (this term is small)
        rho = densmdjwf(mit_salt, mit_temp, abs(grid.dz))
        # Now calculate pressure in dbar
        mit_press = (pload_anom + rhoConst*gravity*abs(draft) + np.cumsum(rho*gravity*grid.dz*hfac))*1e-4

        # Mask all arrays with hfac
        mit_temp = np.ma.masked_where(hfac==0, mit_temp)
        mit_salt = np.ma.masked_where(hfac==0, mit_salt)
        mit_press = np.ma.masked_where(hfac==0, mit_press)

        # Find the bounds on pressure, adding 5% for white space in the plot
        press_min = 0.95*min(np.amin(ctd_press), np.amin(mit_press))
        press_max = 1.05*max(np.amax(ctd_press), np.amax(mit_press))

        # Set up the plot
        fig, gs_1, gs_2 = set_panels('CTD')

        # Plot temperature and salinity
        # First wrap some T/S parameters up in arrays so we can iterate
        ctd_data = [ctd_temp, ctd_salt]
        mit_data = [mit_temp, mit_salt]
        var_name = ['Temperature ('+deg_string+'C)', 'Salinity (psu)']
        for j in range(2):
            ax = plt.subplot(gs_1[0,j])
            ax.plot(ctd_data[j], ctd_press, color='blue', label='CTD')
            ax.plot(mit_data[j], mit_press, color='red', label='Model')
            ax.set_ylim([press_max, press_min])
            ax.grid(True)
            plt.title(var_name[j], fontsize=16)
            if j==0:
                plt.ylabel('Pressure (dbar)', fontsize=14)
            if j==1:
                # No need for pressure labels
                ax.set_yticklabels([])
                # Legend in bottom right, below the map
                ax.legend(loc=(1.2, 0), ncol=2)

        # Plot map
        ax = plt.subplot(gs_2[0,0])
        plot_empty(grid, ax=ax, zoom_fris=True)
        ax.plot(ctd_lon, ctd_lat, '*', color='red', markersize=15)

        # Main title with month and year
        plt.suptitle(sites[i] + ', ' + date_string, fontsize=22)        

        finished_plot(fig, fig_name=fig_dir+sites[i]+'.png')