Skip to content
Snippets Groups Projects
acspo_validate.py 4.54 KiB
Newer Older
tomrink's avatar
tomrink committed
import numpy as np
import h5py
tomrink's avatar
tomrink committed
import xarray as xr
tomrink's avatar
tomrink committed

from util.util import get_grid_values_all


tomrink's avatar
tomrink committed
def acspo_validate(oper_file, cspp_file, rel_tol=0.001, outfile_nc=None):
tomrink's avatar
tomrink committed

    h5f_oper = h5py.File(oper_file, 'r')
    h5f_cspp = h5py.File(cspp_file, 'r')

    lon_cspp = get_grid_values_all(h5f_cspp, 'lon')
    lat_cspp = get_grid_values_all(h5f_cspp, 'lat')
    print('cspp shape: ', lat_cspp.shape)
    c_idx = lat_cspp.shape[1] // 2
    cntr_lon_cspp = lon_cspp[:, c_idx]
    cntr_lat_cspp = lat_cspp[:, c_idx]

    sst_cspp = get_grid_values_all(h5f_cspp, 'sea_surface_temperature')[0, ]
    l2p_flags_cspp = get_grid_values_all(h5f_cspp, 'l2p_flags')[0, ]

    lon_oper = get_grid_values_all(h5f_oper, 'lon')
    lat_oper = get_grid_values_all(h5f_oper, 'lat')
    cntr_lon_oper = lon_oper[:, c_idx]
    cntr_lat_oper = lat_oper[:, c_idx]
    print('oper shape: ', lat_oper.shape)
    sst_oper = get_grid_values_all(h5f_oper, 'sea_surface_temperature')[0, ]
    l2p_flags_oper = get_grid_values_all(h5f_oper, 'l2p_flags')[0, ]

tomrink's avatar
tomrink committed
    # generate a ndarray of boolean, True: clear or probably clear
tomrink's avatar
tomrink committed
    cspp_clear = (l2p_flags_cspp & (1 << 15)) == 0
    oper_clear = (l2p_flags_oper & (1 << 15)) == 0
tomrink's avatar
tomrink committed

tomrink's avatar
tomrink committed
    start_idx_oper, stop_idx_oper = -1, -1
    start_idx_cspp, stop_idx_cspp = -1, -1
tomrink's avatar
tomrink committed

    for k in range(len(cntr_lat_oper)):
tomrink's avatar
tomrink committed
        c_a = np.logical_and(np.isclose(cntr_lat_oper[k], cntr_lat_cspp), np.isclose(cntr_lon_oper[k], cntr_lon_cspp))
tomrink's avatar
tomrink committed
        if np.size(np.nonzero(c_a)[0]) == 1:
            if start_idx_oper == -1:
                start_idx_oper = k
                start_idx_cspp = np.nonzero(c_a)[0][0]
            else:
                stop_idx_oper = k
                stop_idx_cspp = np.nonzero(c_a)[0][0]

tomrink's avatar
tomrink committed
    print('oper start, stop ', start_idx_oper, stop_idx_oper)
    print('cspp start, stop ', start_idx_cspp, stop_idx_cspp)
tomrink's avatar
tomrink committed

tomrink's avatar
tomrink committed
    lon_cspp = lon_cspp[start_idx_cspp:stop_idx_cspp, :]
    lat_cspp = lat_cspp[start_idx_cspp:stop_idx_cspp, :]
    lon_oper = lon_oper[start_idx_oper:stop_idx_oper, :]
    lat_oper = lat_oper[start_idx_oper:stop_idx_oper, :]
tomrink's avatar
tomrink committed
    cspp_clear = cspp_clear[start_idx_cspp:stop_idx_cspp, :]
tomrink's avatar
tomrink committed
    oper_clear = oper_clear[start_idx_oper:stop_idx_oper, :]
    overlap_shape = lon_cspp.shape
    print('overlap shape, size: ', overlap_shape, np.size(lon_cspp))
tomrink's avatar
tomrink committed

tomrink's avatar
tomrink committed
    # check lon/lat arrays ------------------------------------
tomrink's avatar
tomrink committed
    print(np.sum(np.isclose(lon_cspp, lon_oper, rtol=rel_tol)))
    print(np.sum(np.isclose(lat_cspp, lat_oper, rtol=rel_tol)))
tomrink's avatar
tomrink committed

tomrink's avatar
tomrink committed
    sst_cspp_2d = sst_cspp[start_idx_cspp:stop_idx_cspp, :]
    sst_oper_2d = sst_oper[start_idx_oper:stop_idx_oper, :]

    sst_cspp_2d = np.where(cspp_clear, sst_cspp_2d, np.nan)
    sst_oper_2d = np.where(oper_clear, sst_oper_2d, np.nan)

    sst_cspp = sst_cspp_2d.flatten()
    sst_oper = sst_oper_2d.flatten()
tomrink's avatar
tomrink committed
    oper_clear = oper_clear.flatten()
    cspp_clear = cspp_clear.flatten()
tomrink's avatar
tomrink committed
    lon_cspp = lon_cspp.flatten()
    lat_cspp = lat_cspp.flatten()
    lon_oper = lon_oper.flatten()
    lat_oper = lat_oper.flatten()
tomrink's avatar
tomrink committed
    both_clear = oper_clear & cspp_clear
    sst_cspp = sst_cspp[both_clear]
    sst_oper = sst_oper[both_clear]
tomrink's avatar
tomrink committed
    lon_cspp = lon_cspp[both_clear]
    lat_cspp = lat_cspp[both_clear]
    lon_oper = lon_oper[both_clear]
    lat_oper = lat_oper[both_clear]
tomrink's avatar
tomrink committed

tomrink's avatar
tomrink committed
    both_valid = np.invert(np.isnan(sst_cspp)) & np.invert(np.isnan(sst_oper))
tomrink's avatar
tomrink committed
    print('number of clear and valid SSTs in both: ', np.sum(both_valid))
tomrink's avatar
tomrink committed
    valid_sst_cspp = sst_cspp[both_valid]
    valid_sst_oper = sst_oper[both_valid]
    valid_lon_cspp = lon_cspp[both_valid]
    valid_lat_cspp = lat_cspp[both_valid]
    valid_lon_oper = lon_oper[both_valid]
    valid_lat_oper = lat_oper[both_valid]

    xarray_data = xr.Dataset({
tomrink's avatar
tomrink committed
        'sst_cspp': xr.DataArray(valid_sst_cspp, coords=None, dims=None, name='sst_cspp'),
        'sst_oper': xr.DataArray(valid_sst_oper, coords=None, dims=None, name='sst_oper'),
        'cspp_lat': xr.DataArray(valid_lat_cspp, coords=None, dims=None, name='cspp_lat'),
        'cspp_lon': xr.DataArray(valid_lon_cspp, coords=None, dims=None, name='cspp_lon'),
        'oper_lat': xr.DataArray(valid_lat_oper, coords=None, dims=None, name='oper_lat'),
        'oper_lon': xr.DataArray(valid_lon_oper, coords=None, dims=None, name='oper_lon'),
tomrink's avatar
tomrink committed
    })
tomrink's avatar
tomrink committed
    if outfile_nc is not None:
tomrink's avatar
tomrink committed
        xarray_data.to_netcdf(outfile_nc)
tomrink's avatar
tomrink committed

tomrink's avatar
tomrink committed
    print('fraction approx equal: ',
tomrink's avatar
tomrink committed
          np.sum(np.isclose(valid_sst_cspp, valid_sst_oper, rtol=rel_tol))/np.sum(both_valid))
tomrink's avatar
tomrink committed
    # print(np.histogram((sst_cspp[both_valid] - sst_oper[both_valid]), bins=10))
tomrink's avatar
tomrink committed

tomrink's avatar
tomrink committed
    h5f_oper.close()
tomrink's avatar
tomrink committed
    h5f_cspp.close()

    return sst_cspp_2d, sst_oper_2d