Example #1
0
def test_cld_seviri(fdir):

    atm0 = atm_atmmod(levels=np.arange(21))

    fname_h4 = 'data/seviri.hdf'
    fname_seviri = '%s/seviri.pk' % fdir

    cld0 = cld_sev(fname_h4=fname_h4,
                   fname=fname_seviri,
                   extent=np.array([8.0, 10.0, -18.5, -13.5]),
                   coarsing=[2, 2, 1],
                   overwrite=True)
    # cld0 = cld_sev(fname_h4=fname_h4, fname=fname_seviri, atm_obj=atm0, extent=np.array([8.0, 10.0, -18.5, -13.5]), coarsing=[1, 1, 1], overwrite=True)

    for key in cld0.lay.keys():
        print(key, cld0.lay[key])
        print()

    ext_2d = np.sum(cld0.lay['extinction']['data'], axis=-1)
    print(ext_2d.min())
    print(ext_2d.max())

    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    fig = plt.figure(figsize=(8, 6))
    ax1 = fig.add_subplot(111)
    ax1.imshow(np.transpose(ext_2d))
    # ax1.set_xlim(())
    # ax1.set_ylim(())
    # ax1.set_xlabel('')
    # ax1.set_ylabel('')
    # ax1.set_title('')
    # ax1.legend(loc='upper right', fontsize=12, framealpha=0.4)
    # plt.savefig('test.png')
    plt.show()
    exit()
Example #2
0
def test_atm_atmmod(fdir):

    """
    Test for module er3t.pre.atm.atm_atmmod
    """

    levels = np.linspace(0.0, 20.0, 41)
    fname_atm  = '%s/atm.pk' % fdir

    print('Case 1: run with \'levels\' only ...')
    """
    Calculation with 'levels' only without writing file.
    """
    atm_obj = atm_atmmod(levels=levels, verbose=True)
    print()


    print('Case 2: run with \'levels\' and \'fname\' but overwrite if exists ...')
    """
    Run calculation with 'levels' and overwrite data into 'fname'
    """
    atm_obj = atm_atmmod(levels=levels, fname=fname_atm, overwrite=True, verbose=True)
    print()


    print('Case 3: run with \'levels\' and \'fname\' ...')
    """
    If 'levels' and 'fname' are both provided, the module will first check whether
    the 'fname' exists or not, if
    1. 'fname' not exsit, run with 'levels' and store data into 'fname'
    2. 'fname' exsit, restore data from 'fname'
    """
    atm_obj = atm_atmmod(levels=levels, fname=fname_atm, verbose=True)
    print()


    print('Case 4: run with \'fname\' only ...')
    """
    Restore data from 'fname'
    """
    atm_obj = atm_atmmod(fname=fname_atm, verbose=True)
Example #3
0
def test_abs_oco(fdir):

    # create atm file
    levels = np.linspace(0.0, 20.0, 41)
    fname_atm = '%s/atm.pk' % fdir
    atm0 = atm_atmmod(levels=levels, fname=fname_atm, overwrite=True)

    # create abs file, but we will need to input the created atm file
    fname_abs = '%s/abs.pk' % fdir
    wavelength = 760.0

    print('Case 1: run with \'wavelength\' only ...')
    """
    Calculation with 'wavelength' only without writing file.
    """
    abs_obj = abs_oco_idl(wavelength=wavelength, atm_obj=atm0, verbose=True)
    print()

    print(
        'Case 2: run with \'wavelength\' and \'fname\' but overwrite if exists ...'
    )
    """
    Run calculation with 'wavelength' and overwrite data into 'fname'
    """
    abs_obj = abs_oco_idl(wavelength=wavelength,
                          fname=fname_abs,
                          atm_obj=atm0,
                          overwrite=True,
                          verbose=True)
    print()

    print('Case 3: run with \'wavelength\' and \'fname\' ...')
    """
    If 'wavelength' and 'fname' are both provided, the module will first check whether
    the 'fname' exists or not, if
    1. 'fname' not exsit, run with 'levels' and store data into 'fname'
    2. 'fname' exsit, restore data from 'fname'
    """
    abs_obj = abs_oco_idl(wavelength=wavelength,
                          fname=fname_abs,
                          atm_obj=atm0,
                          verbose=True)
    print()

    print('Case 4: run with \'fname\' only ...')
    """
    Restore data from 'fname'
    """
    abs_obj = abs_oco_idl(fname=fname_abs, atm_obj=atm0, verbose=True)
Example #4
0
def test_non_equidistant_atm(fdir):

    """
    Test for module er3t.pre.atm.atm_atmmod
    """

    levels     = np.loadtxt('tmp-data/z_GRANDSAM.txt')/1000.0
    fname_atm  = '%s/atm.pk' % fdir

    atm_obj = atm_atmmod(levels=levels, verbose=True)

    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    import matplotlib as mpl
    import matplotlib.pyplot as plt
    from matplotlib.ticker import FixedLocator
    from matplotlib import rcParams

    fig = plt.figure(figsize=(3, 7))
    ax1 = fig.add_subplot(111)
    ax1.scatter(atm_obj.lev['pressure']['data'], atm_obj.lev['altitude']['data'])
    ax1.set_xlabel('Pressure [hPa]')
    ax1.set_ylabel('Altitude [km]')
    plt.show()
Example #5
0
def test_high_res_atm(fdir):

    """
    Test for module er3t.pre.atm.atm_atmmod
    """

    levels = np.linspace(0.0, 20.0, 1001)
    fname_atm  = '%s/atm.pk' % fdir

    atm_obj = atm_atmmod(levels=levels, verbose=True)

    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    import matplotlib as mpl
    import matplotlib.pyplot as plt
    from matplotlib.ticker import FixedLocator
    from matplotlib import rcParams

    fig = plt.figure(figsize=(3, 7))
    ax1 = fig.add_subplot(111)
    ax1.scatter(atm_obj.lev['pressure']['data'], atm_obj.lev['altitude']['data'])
    ax1.set_xlabel('Pressure [hPa]')
    ax1.set_ylabel('Altitude [km]')
    plt.show()
Example #6
0
def test_solar_spectra(fdir, date=datetime.datetime.now()):

    levels = np.linspace(0.0, 20.0, 41)
    atm0 = atm_atmmod(levels=levels)

    wvls = np.arange(300.0, 2301.0, 1.0)
    sols = np.zeros_like(wvls)

    sol_fac = cal_sol_fac(date)

    for i, wvl in enumerate(wvls):

        abs0 = abs_16g(wavelength=wvl, atm_obj=atm0)
        norm = sol_fac / (abs0.coef['weight']['data'] *
                          abs0.coef['slit_func']['data'][-1, :]).sum()
        sols[i] = norm * (abs0.coef['solar']['data'] *
                          abs0.coef['weight']['data'] *
                          abs0.coef['slit_func']['data'][-1, :]).sum()

    import matplotlib as mpl
    import matplotlib.pyplot as plt
    from matplotlib.ticker import FixedLocator
    from matplotlib import rcParams
    import matplotlib.gridspec as gridspec
    import matplotlib.patches as mpatches
    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    fig = plt.figure(figsize=(8, 6))
    ax1 = fig.add_subplot(111)
    ax1.scatter(wvls, sols, s=2)
    ax1.set_xlim((200, 2400))
    ax1.set_ylim((0.0, 2.4))
    ax1.set_xlabel('Wavelength [nm]')
    ax1.set_ylabel('Flux [$\mathrm{W m^{-2} nm^{-1}}$]')
    ax1.set_title('Solar Spectra')
    plt.savefig('solar_spectra.png')
    plt.show()
Example #7
0
    def process(self,
                sat_obj,
                cloud_geometrical_thickness=1.0,
                cloud_top_height=3.0,
                layer_thickness=1.0):

        self.lay = {}
        self.lev = {}

        keys = self.sat.data.keys()
        if ('lon_2d' not in keys) or ('lat_2d' not in keys) or (
                'cot_2d' not in keys) or ('cer_2d' not in keys):
            sys.exit(
                'Error   [cld_sat]: Please make sure \'sat_obj.data\' contains \'lon_2d\', \'lat_2d\', \'cot_2d\' and \'cer_2d\'.'
            )

        cloud_bottom_height = cloud_top_height - cloud_geometrical_thickness

        if isinstance(cloud_top_height, np.ndarray):
            if cloud_top_height.shape != self.sat.data['cot_2d']['data'].shape:
                sys.exit(
                    'Error   [cld_sat]: The dimension of \'cloud_top_height\' does not match \'lon_2d\', \'lat_2d\', \'cot_2d\' and \'cer_2d\'.'
                )

            cloud_bottom_height[
                cloud_bottom_height < layer_thickness] = layer_thickness
            h_bottom = max([np.nanmin(cloud_bottom_height), layer_thickness])
            h_top = min([np.nanmax(cloud_top_height), 30.0])

        else:

            if cloud_bottom_height < layer_thickness:
                cloud_bottom_height = layer_thickness
            h_bottom = max([cloud_bottom_height, layer_thickness])
            h_top = min([cloud_top_height, 30.0])

        if h_bottom >= h_top:
            sys.exit(
                'Error   [cld_sat]: Cloud bottom height is greater than cloud top height, check whether the input cloud top height \'cth\' is in the units of \'km\'.'
            )

        levels = np.arange(h_bottom, h_top + 0.1 * layer_thickness,
                           layer_thickness)
        self.atm = atm_atmmod(levels=levels)

        lon_1d = self.sat.data['lon_2d']['data'][:, 0]
        lat_1d = self.sat.data['lat_2d']['data'][0, :]

        dx = cal_dist(lon_1d[1] - lon_1d[0])
        dy = cal_dist(lat_1d[1] - lat_1d[0])

        x_1d = (lon_1d - lon_1d[0]) * dx
        y_1d = (lat_1d - lat_1d[0]) * dy

        Nx = x_1d.size
        Ny = y_1d.size

        self.lay['x'] = {'data': x_1d, 'name': 'X', 'units': 'km'}
        self.lay['y'] = {'data': y_1d, 'name': 'Y', 'units': 'km'}
        self.lay['nx'] = {'data': Nx, 'name': 'Nx', 'units': 'N/A'}
        self.lay['ny'] = {'data': Ny, 'name': 'Ny', 'units': 'N/A'}
        self.lay['dx'] = {'data': dx, 'name': 'dx', 'units': 'km'}
        self.lay['dy'] = {'data': dy, 'name': 'dy', 'units': 'km'}
        self.lay['altitude'] = copy.deepcopy(self.atm.lay['altitude'])
        self.lay['thickness'] = copy.deepcopy(self.atm.lay['thickness'])
        self.lay['lon'] = copy.deepcopy(self.sat.data['lon_2d'])
        self.lay['lat'] = copy.deepcopy(self.sat.data['lat_2d'])
        self.lay['cot'] = copy.deepcopy(self.sat.data['cot_2d'])

        self.lev['altitude'] = copy.deepcopy(self.atm.lev['altitude'])

        # temperature 3d
        t_1d = self.atm.lay['temperature']['data']
        Nz = t_1d.size
        t_3d = np.empty((Nx, Ny, Nz), dtype=t_1d.dtype)
        t_3d[...] = t_1d[None, None, :]

        cer_2d = self.sat.data['cer_2d']['data']
        cer_3d = np.empty((Nx, Ny, Nz), dtype=cer_2d.dtype)
        cer_3d[...] = cer_2d[:, :, None]

        self.lay['temperature'] = {
            'data': t_3d,
            'name': 'Temperature',
            'units': 'K'
        }

        # extinction 3d
        ext_3d = np.zeros((Nx, Ny, Nz), dtype=np.float64)

        alt = self.atm.lay['altitude']['data']

        for i in range(Nx):
            for j in range(Ny):
                if isinstance(cloud_top_height, np.ndarray):
                    cbh0 = cloud_bottom_height[i, j]
                    cth0 = cloud_top_height[i, j]
                else:
                    cbh0 = cloud_bottom_height
                    cth0 = cloud_top_height

                cot0 = self.lay['cot']['data'][i, j]
                cer0 = cer_2d[i, j]
                indices = np.where((alt >= cbh0) & (alt <= cth0))[0]
                if indices.size == 0:
                    indices = np.array([2])

                dz = self.atm.lay['thickness']['data'][indices].sum() * 1000.0
                ext_3d[i, j, indices] = cal_ext(cot0, cer0, dz=dz)

        ext_3d[np.isnan(ext_3d)] = 0.0
        cer_3d[np.isnan(ext_3d)] = 0.0
        self.lay['extinction'] = {
            'data': ext_3d,
            'name': 'Extinction coefficients',
            'units': 'm^-1'
        }
        self.lay['cer'] = {
            'data': cer_3d,
            'name': 'Effective radius',
            'units': 'mm'
        }

        self.Nx = Nx
        self.Ny = Ny
        self.Nz = Nz
Example #8
0
    def pre_gen(self, earth_radius=6378.0, cloud_thickness=1.0):

        self.lay = {}
        self.lev = {}

        f = SD(self.fname_h4, SDC.READ)

        # lon lat
        lon0 = f.select('Longitude')
        lat0 = f.select('Latitude')
        cot0 = f.select('Cloud_Optical_Thickness_16')
        cer0 = f.select('Cloud_Effective_Radius_16')
        cot_pcl0 = f.select('Cloud_Optical_Thickness_16_PCL')
        cer_pcl0 = f.select('Cloud_Effective_Radius_16_PCL')
        cth0 = f.select('Cloud_Top_Height')

        if 'actual_range' in lon0.attributes().keys():
            lon_range = lon0.attributes()['actual_range']
            lat_range = lat0.attributes()['actual_range']
        else:
            lon_range = [-180.0, 180.0]
            lat_range = [-90.0, 90.0]

        lon = lon0[:]
        lat = lat0[:]
        cot = np.float_(cot0[:])
        cer = np.float_(cer0[:])
        cot_pcl = np.float_(cot_pcl0[:])
        cer_pcl = np.float_(cer_pcl0[:])
        cth = np.float_(cth0[:])

        logic = (lon >= lon_range[0]) & (lon <= lon_range[1]) & (
            lat >= lat_range[0]) & (lat <= lat_range[1])
        lon = lon[logic]
        lat = lat[logic]
        cot = cot[logic]
        cer = cer[logic]
        cot_pcl = cot_pcl[logic]
        cer_pcl = cer_pcl[logic]
        cth = cth[logic]

        if self.extent is not None:
            logic = (lon >= self.extent[0]) & (lon <= self.extent[1]) & (
                lat >= self.extent[2]) & (lat <= self.extent[3])
            lon = lon[logic]
            lat = lat[logic]
            cot = cot[logic]
            cer = cer[logic]
            cot_pcl = cot_pcl[logic]
            cer_pcl = cer_pcl[logic]
            cth = cth[logic]

        xy = (self.extent[1] - self.extent[0]) * (self.extent[3] -
                                                  self.extent[2])
        N0 = np.sqrt(lon.size / xy)

        Nx = int(N0 * (self.extent[1] - self.extent[0]))
        if Nx % 2 == 1:
            Nx += 1

        Ny = int(N0 * (self.extent[3] - self.extent[2]))
        if Ny % 2 == 1:
            Ny += 1

        lon_1d0 = np.linspace(self.extent[0], self.extent[1], Nx + 1)
        lat_1d0 = np.linspace(self.extent[2], self.extent[3], Ny + 1)

        lon_1d = (lon_1d0[1:] + lon_1d0[:-1]) / 2.0
        lat_1d = (lat_1d0[1:] + lat_1d0[:-1]) / 2.0

        dx = (lon_1d[1] - lon_1d[0]) / 180.0 * (np.pi * earth_radius)
        dy = (lat_1d[1] - lat_1d[0]) / 180.0 * (np.pi * earth_radius)

        x_1d = (lon_1d - lon_1d[0]) * dx
        y_1d = (lat_1d - lat_1d[0]) * dy

        # lon, lat
        lat_2d, lon_2d = np.meshgrid(lat_1d, lon_1d)
        # lon_2d, lat_2d = np.meshgrid(lon_1d, lat_1d)

        # cot
        cot_range = cot0.attributes()['valid_range']
        cer_range = cer0.attributes()['valid_range']
        cot_pcl_range = cot_pcl0.attributes()['valid_range']
        cer_pcl_range = cer_pcl0.attributes()['valid_range']
        cth_range = cth0.attributes()['valid_range']

        # +
        # create cot_all/cer_all that contains both cot/cer and cot_pcl/cer_pcl
        cot_all = np.zeros(cot.size, dtype=np.float64)
        cer_all = np.zeros(cer.size, dtype=np.float64)
        cth_all = np.zeros(cth.size, dtype=np.float64)
        cth_all[...] = np.nan

        logic = (cot >= cot_range[0]) & (cot <= cot_range[1]) & (
            cer >= cer_range[0]) & (cer <= cer_range[1]) & (
                cth >= cth_range[0]) & (cth <= cth_range[1])
        cot_all[logic] = cot[logic] * cot0.attributes(
        )['scale_factor'] + cot0.attributes()['add_offset']
        cer_all[logic] = cer[logic] * cer0.attributes(
        )['scale_factor'] + cer0.attributes()['add_offset']
        cth_all[logic] = cth[logic] * cth0.attributes(
        )['scale_factor'] + cth0.attributes()['add_offset']

        logic_pcl = np.logical_not(logic) & (cot_pcl >= cot_pcl_range[0]) & (
            cot_pcl <= cot_pcl_range[1]) & (cer_pcl >= cer_pcl_range[0]) & (
                cer_pcl <= cer_pcl_range[1]) & (cth >= cth_range[0]) & (
                    cth <= cth_range[1])
        cot_all[logic_pcl] = cot_pcl[logic_pcl] * cot_pcl0.attributes(
        )['scale_factor'] + cot_pcl0.attributes()['add_offset']
        cer_all[logic_pcl] = cer_pcl[logic_pcl] * cer_pcl0.attributes(
        )['scale_factor'] + cer_pcl0.attributes()['add_offset']
        cth_all[logic_pcl] = cth[logic_pcl] * cth0.attributes(
        )['scale_factor'] + cth0.attributes()['add_offset']
        cth_all /= 1000.0

        logic_all = logic | logic_pcl
        # -

        cot = cot_all
        cer = cer_all
        cth = cth_all

        cot[np.logical_not(logic_all)] = 0.0
        cer[np.logical_not(logic_all)] = 0.0
        cth[np.logical_not(logic_all)] = np.nan

        points = np.transpose(np.vstack((lon, lat)))

        cot_2d = interpolate.griddata(points,
                                      cot, (lon_2d, lat_2d),
                                      method='nearest')
        cer_2d = interpolate.griddata(points,
                                      cer, (lon_2d, lat_2d),
                                      method='nearest')
        cth_2d = interpolate.griddata(points,
                                      cth, (lon_2d, lat_2d),
                                      method='nearest')

        f.end()

        self.atm = atm_atmmod(np.arange(int(np.nanmax(cth_2d)) + 2))
        self.lay['x'] = {'data': x_1d, 'name': 'X', 'units': 'km'}
        self.lay['y'] = {'data': y_1d, 'name': 'Y', 'units': 'km'}
        self.lay['nx'] = {'data': Nx, 'name': 'Nx', 'units': 'N/A'}
        self.lay['ny'] = {'data': Ny, 'name': 'Ny', 'units': 'N/A'}
        self.lay['dx'] = {'data': dx, 'name': 'dx', 'units': 'km'}
        self.lay['dy'] = {'data': dy, 'name': 'dy', 'units': 'km'}
        self.lay['altitude'] = copy.deepcopy(self.atm.lay['altitude'])
        self.lay['cot'] = {
            'data': cot_2d,
            'name': 'Cloud optical thickness',
            'units': 'N/A'
        }
        self.lay['cer'] = {
            'data': cer_2d,
            'name': 'Cloud effective radius',
            'units': 'micron'
        }
        self.lay['cth'] = {
            'data': cth_2d,
            'name': 'Cloud top height',
            'units': 'km'
        }
        self.lay['lon'] = {
            'data': lon_2d,
            'name': 'Longitude',
            'units': 'degree'
        }
        self.lay['lat'] = {
            'data': lat_2d,
            'name': 'Latitude',
            'units': 'degree'
        }

        # temperature 3d
        t_1d = self.atm.lay['temperature']['data']
        Nz = t_1d.size
        t_3d = np.empty((Nx, Ny, Nz), dtype=t_1d.dtype)
        t_3d[...] = t_1d[None, None, :]

        self.lay['temperature'] = {
            'data': t_3d,
            'name': 'Temperature',
            'units': 'K'
        }

        # extinction 3d
        ext_3d = np.zeros((Nx, Ny, Nz), dtype=np.float64)

        alt = self.atm.lay['altitude']['data']
        for i in range(Nx):
            for j in range(Ny):

                cld_top = cth_2d[i, j]

                if not np.isnan(cld_top):
                    lwp = 5.0 / 9.0 * 1.0 * cot_2d[i, j] * cer_2d[i, j] / 10.0
                    ext0 = 0.75 * 2.0 * lwp / cer_2d[i, j] / 100.0

                    # index = np.argmin(np.abs(cld_top-alt))
                    index = 0
                    ext_3d[i, j, index] = ext0

        self.lay['extinction'] = {
            'data': ext_3d,
            'name': 'Extinction coefficients'
        }

        self.Nx = Nx
        self.Ny = Ny
        self.Nz = Nz
Example #9
0
def cal_mca_rad_modis(date, sat, wavelength, cth=None, scale_factor=1.0, fdir='tmp-data', solver='3D', overwrite=True):

    """
    Calculate OCO2 radiance using cloud (MODIS level 1b) and surface properties (MOD09A1) from MODIS
    """

    # atm object
    # =================================================================================
    levels    = np.array([ 0. ,  0.5,  1. ,  1.5,  2. ,  2.5,  3. ,  3.5,  4. ,  4.5,  5. , 5.5,  6. ,  6.5,  7. ,  7.5,  8. ,  8.5,  9. ,  9.5, 10. , 11. , 12. , 13. , 14. , 20. , 25. , 30. , 35. , 40. ])
    fname_atm = '%s/atm.pk' % fdir
    atm0      = atm_atmmod(levels=levels, fname=fname_atm, overwrite=overwrite)
    # =================================================================================

    # abs object, in the future, we will implement OCO2 MET file for this
    # =================================================================================
    fname_abs = '%s/abs.pk' % fdir
    abs0      = abs_16g(wavelength=wavelength, fname=fname_abs, atm_obj=atm0, overwrite=overwrite)
    # =================================================================================

    # mca_sfc object
    # =================================================================================
    mod09 = modis_09a1(fnames=sat.fnames['mod_09'], extent=sat.extent)
    lon_2d, lat_2d, surf_ref_2d = grid_modis_by_extent(mod09.data['lon']['data'], mod09.data['lat']['data'], mod09.data['ref']['data'][0, ...], extent=sat.extent)
    mod09.data['alb_2d'] = dict(data=surf_ref_2d, name='Surface albedo', units='N/A')
    mod09.data['lon_2d'] = dict(data=lon_2d, name='Longitude', units='degrees')
    mod09.data['lat_2d'] = dict(data=lat_2d, name='Latitude' , units='degrees')

    fname_sfc = '%s/sfc.pk' % fdir
    sfc0      = sfc_sat(sat_obj=mod09, fname=fname_sfc, extent=sat.extent, verbose=True, overwrite=overwrite)
    sfc_2d    = mca_sfc_2d(atm_obj=atm0, sfc_obj=sfc0, fname='%s/mca_sfc_2d.bin' % fdir, overwrite=overwrite)
    # =================================================================================

    # pha object
    # =================================================================================
    pha0 = pha_mie(wvl0=wavelength)
    # =================================================================================

    # sca object
    # =================================================================================
    sca  = mca_sca(pha_obj=pha0)
    # =================================================================================

    # cld object
    # =================================================================================
    modl1b    = pre_cld(sat, cth=cth, scale_factor=scale_factor, solver=solver)
    fname_cld = '%s/cld.pk' % fdir
    cld0      = cld_sat(sat_obj=modl1b, fname=fname_cld, cth=cth, cgt=1.0, dz=np.unique(atm0.lay['thickness']['data'])[0], overwrite=overwrite)

    atm3d0  = mca_atm_3d(cld_obj=cld0, atm_obj=atm0, pha_obj=pha0, fname='%s/mca_atm_3d.bin' % fdir)
    atm1d0  = mca_atm_1d(atm_obj=atm0, abs_obj=abs0)
    atm_1ds = [atm1d0]
    atm_3ds = [atm3d0]
    # =================================================================================

    # modis 03 parameters
    # =================================================================================
    mod03   = modis_03(fnames=sat.fnames['mod_03'], extent=sat.extent)
    sza     = mod03.data['sza']['data'].mean()
    saa     = mod03.data['saa']['data'].mean()
    vza     = mod03.data['vza']['data'].mean()
    vaa     = mod03.data['vaa']['data'].mean()
    # =================================================================================

    # run mcarats
    # =================================================================================
    mca0 = mcarats_ng(
            date=date,
            atm_1ds=atm_1ds,
            atm_3ds=atm_3ds,
            sfc_2d=sfc_2d,
            sca=sca,
            Ng=abs0.Ng,
            target='radiance',
            solar_zenith_angle   = sza,
            solar_azimuth_angle  = saa,
            sensor_zenith_angle  = vza,
            sensor_azimuth_angle = vaa,
            fdir='%s/%.4fnm/oco2/rad_%s' % (fdir, wavelength, solver.lower()),
            Nrun=3,
            weights=abs0.coef['weight']['data'],
            photons=1e7,
            solver=solver,
            Ncpu=5,
            mp_mode='py',
            overwrite=overwrite
            )
    # =================================================================================

    # mcarats output
    # =================================================================================
    out0 = mca_out_ng(fname='%s/mca-out-rad-oco2-%s_%.4fnm.h5' % (fdir, solver.lower(), wavelength), mca_obj=mca0, abs_obj=abs0, mode='mean', squeeze=True, verbose=True, overwrite=overwrite)
    # =================================================================================

    # plot
    # =================================================================================
    rcParams['font.size'] = 12

    fig = plt.figure(figsize=(16, 5))
    ax1 = fig.add_subplot(131)
    ax2 = fig.add_subplot(132)
    ax3 = fig.add_subplot(133)

    img = mpl_img.imread(sat.fnames['mod_rgb'][0])
    cs = ax1.imshow(img, extent=sat.extent, zorder=0)
    ax1.set_xlabel('Longitude [$^\circ$]')
    ax1.set_ylabel('Latitude [$^\circ$]')
    ax1.set_xlim(sat.extent[:2])
    ax1.set_ylim(sat.extent[2:])
    ax1.set_title('MODIS RGB Imagery')

    cs = ax2.imshow(modl1b.data['rad_2d']['data'].T, cmap='Greys_r', origin='lower', vmin=0.0, vmax=0.3, extent=sat.extent, zorder=0)
    ax2.set_xlabel('Longitude [$^\circ$]')
    ax2.set_ylabel('Latitude [$^\circ$]')
    ax2.set_xlim(sat.extent[:2])
    ax2.set_ylim(sat.extent[2:])
    ax2.set_title('MODIS Band 1 (650nm)')

    cs = ax3.imshow(out0.data['rad']['data'].T, cmap='Greys_r', origin='lower', vmin=0.0, vmax=0.3, extent=sat.extent, zorder=0)
    ax3.set_xlabel('Longitude [$^\circ$]')
    ax3.set_ylabel('Latitude [$^\circ$]')
    ax3.set_xlim(sat.extent[:2])
    ax3.set_ylim(sat.extent[2:])
    ax3.set_title('Simulated Radiance at 650 nm (3D)')
    plt.subplots_adjust(wspace=0.4)
    plt.show()
Example #10
0
def test_cld_modis_l1b(fdir):

    atm0 = atm_atmmod(levels=np.arange(21))

    extent = [-112.0, -111.0, 29.4, 30.4]
    fname_l1b = 'data/MYD02QKM.A2017237.2035.061.2018034164525.hdf'
    fname_l2 = 'data/MYD06_L2.A2017237.2035.061.2018038220417.hdf'
    fname_cld = '%s/modis.pk' % fdir

    l1b = modis_l1b(fname=fname_l1b, extent=extent)
    l2 = modis_l2(fname=fname_l2,
                  extent=extent,
                  vnames=[
                      'Solar_Zenith', 'Solar_Azimuth', 'Sensor_Zenith',
                      'Sensor_Azimuth'
                  ])
    print(l2.data['solar_zenith']['data'].mean(),
          l2.data['solar_azimuth']['data'].mean())
    exit()

    lon_2d, lat_2d, ref_2d = grid_modis_by_extent(l1b.data['lon']['data'],
                                                  l1b.data['lat']['data'],
                                                  l1b.data['ref']['data'][0,
                                                                          ...],
                                                  extent=extent)

    a0 = np.median(ref_2d)
    threshold = a0 * 2.0

    xx_2stream = np.linspace(0.0, 200.0, 10000)
    yy_2stream = cal_r_twostream(
        xx_2stream,
        a=a0,
        mu=np.cos(np.deg2rad(l2.data['solar_zenith']['data'].mean())))

    indices = np.where(ref_2d > threshold)
    indices_x = indices[0]
    indices_y = indices[1]

    cot_2d = np.zeros_like(ref_2d)
    cer_2d = np.zeros_like(ref_2d)
    cer_2d[...] = 1.0
    for i in range(indices_x.size):
        cot_2d[indices_x[i], indices_y[i]] = xx_2stream[np.argmin(
            np.abs(yy_2stream - ref_2d[indices_x[i], indices_y[i]]))]
        cer_2d[indices_x[i], indices_y[i]] = 12.0

    l1b.data['lon_2d'] = dict(name='Gridded longitude',
                              units='degrees',
                              data=lon_2d)
    l1b.data['lat_2d'] = dict(name='Gridded latitude',
                              units='degrees',
                              data=lat_2d)

    l1b.data['cot_2d'] = dict(name='Gridded cloud optical thickness',
                              units='N/A',
                              data=cot_2d)
    l1b.data['cer_2d'] = dict(name='Gridded cloud effective radius',
                              units='micro',
                              data=cer_2d)

    cld0 = cld_sat(sat_obj=l1b, fname=fname_cld, overwrite=True)
Example #11
0
def test_cld_modis_l2(fdir):

    atm0 = atm_atmmod(levels=np.arange(21))

    extent = [-112.0, -111.0, 29.4, 30.4]
    fname_l1b = 'data/MYD02QKM.A2017237.2035.061.2018034164525.hdf'
    fname_l2 = 'data/MYD06_L2.A2017237.2035.061.2018038220417.hdf'
    fname_cld = '%s/modis.pk' % fdir

    # l1b = modis_l1b(fname=fname_l1b, extent=extent)
    l2 = modis_l2(fname=fname_l2,
                  vnames=[
                      'Solar_Zenith', 'Solar_Azimuth', 'Sensor_Zenith',
                      'Sensor_Azimuth', 'Cloud_Top_Height'
                  ])

    # # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    # fig = plt.figure(figsize=(8, 6))
    # ax1 = fig.add_subplot(111)
    # ax1.hist(l2.data['cloud_top_height']['data']/1000.0, 100)
    # ax1.set_xlim((0.0, 12.0))
    # # ax1.set_ylim(())
    # # ax1.set_xlabel('')
    # # ax1.set_ylabel('')
    # # ax1.set_title('')
    # # ax1.legend(loc='upper right', fontsize=12, framealpha=0.4)
    # # plt.savefig('test.png')
    # plt.show()
    # exit()
    # # ---------------------------------------------------------------------

    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    fig = plt.figure(figsize=(12, 10))
    # logic = l2.data['pcl']['data']==1
    # ax1.scatter(l2.data['lon']['data'][logic], l2.data['lat']['data'][logic], c=l2.data['cot']['data'][logic], s=0.1)
    # ax1 = fig.add_subplot(122)
    # logic = l2.data['pcl']['data']==0
    ax1 = fig.add_subplot(221)
    cs1 = ax1.scatter(l2.data['lon_5km']['data'],
                      l2.data['lat_5km']['data'],
                      c=l2.data['solar_zenith']['data'],
                      s=0.1)
    plt.colorbar(cs1)
    ax1.set_title('Solar Zenith Angle')

    ax2 = fig.add_subplot(222)
    angle = l2.data['solar_azimuth']['data']
    angle[angle < 0.0] += 360.0
    cs2 = ax2.scatter(l2.data['lon_5km']['data'],
                      l2.data['lat_5km']['data'],
                      c=angle,
                      s=0.1)
    plt.colorbar(cs2)
    ax2.set_title('Solar Azimuth Angle')

    ax3 = fig.add_subplot(223)
    cs3 = ax3.scatter(l2.data['lon_5km']['data'],
                      l2.data['lat_5km']['data'],
                      c=l2.data['sensor_zenith']['data'],
                      s=0.1)
    plt.colorbar(cs3)
    ax3.set_title('Sensor Zenith Angle')

    ax4 = fig.add_subplot(224)
    angle = l2.data['sensor_azimuth']['data']
    # angle[angle<0.0] += 360.0
    # ax4.hist(angle.ravel(), 100)
    cs4 = ax4.scatter(l2.data['lon_5km']['data'],
                      l2.data['lat_5km']['data'],
                      c=angle,
                      s=0.1)
    plt.colorbar(cs4)
    ax4.set_title('Sensor Azimuth Angle')
    plt.savefig('geometry.png', bbox_inches='tight')
    plt.show()
    exit()
    # ---------------------------------------------------------------------

    print(l2.data)
    exit()

    # cld0 = cld_mod(fname_h4=fname_h4, fname=fname_modis, extent=np.array([8.0, 10.0, -18.5, -13.5]), coarsing=[1, 1, 1], overwrite=True)
    # cld0 = cld_mod(fname_h4=fname_h4, fname=fname_modis, extent=None, coarsing=[1, 1, 1], overwrite=True)

    # for key in cld0.lay.keys():
    #     print(key, cld0.lay[key])
    #     print()

    ext_2d = np.sum(cld0.lay['extinction']['data'], axis=-1)
    print(ext_2d.shape)
    # print(ext_2d.min())
    # print(ext_2d.max())

    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    fig = plt.figure(figsize=(8, 6))
    ax1 = fig.add_subplot(111)
    ax1.imshow(np.transpose(ext_2d))
    # ax1.set_xlim(())
    # ax1.set_ylim(())
    # ax1.set_xlabel('')
    # ax1.set_ylabel('')
    # ax1.set_title('')
    # ax1.legend(loc='upper right', fontsize=12, framealpha=0.4)
    # plt.savefig('test.png')
    plt.show()
    exit()