Ejemplo n.º 1
0
def get_proj_params(modis_file):
    """
    given a path to a Modis level1b file with a standard
    'CoreMetadata.0' atrribute, return proj4 parameters
    for use by cartopy or pyresample, assuming a laea projection
    and WGS84 datum
    
    Parameters
    ----------
    
    modis_file:  Path or str with path to hdf file
    
    Returns
    -------
    
    proj_params: dict
        dict with parameters for proj4
        
    """
    modis_dict = parseMeta(modis_file)
    import cartopy.crs as ccrs
    globe_w = ccrs.Globe(datum="WGS84", ellipse="WGS84")
    projection_w = ccrs.LambertAzimuthalEqualArea(
        central_latitude=modis_dict['lat_0'],
        central_longitude=modis_dict['lon_0'],
        globe=globe_w)
    proj_params = projection_w.proj4_params
    return proj_params
Ejemplo n.º 2
0
def main():
    """
    run the test
    """
    for the_root in ['wv_ir', 'wv_nearir_hr', 'wv_nearir_lr']:
        the_file = a301.map_dir / Path(f'wv_maps/{the_root}.json')
        with open(the_file, 'r') as f:
            metadata_dict = json.load(f)
        print(f"found ({metadata_dict['area_def']['name']})")
    print('\ndata looks good, ready to go')

    generic_m2 = a301.data_dir / Path("myd02_2018_10_10.hdf")
    modis_metadata = parseMeta(generic_m2)
    print(f"\nfound {generic_m2}, which is a copy of\n"
          f"{modis_metadata['filename']}\n"
          f"its filetype is {modis_metadata['type']['SHORTNAME']['VALUE']}\n")
    print('\ndata looks good, ready to go')
Ejemplo n.º 3
0
def main():
    """
    run the test
    """
    generic_m3 = a301.data_dir / Path("m3_file_2018_10_1.hdf")
    if not generic_m3.exists():
        raise ValueError(f"couldn't find {generic_m3}")
    modis_meta = parseMeta(generic_m3)
    print(f"working on {generic_m3}, originally was { modis_meta['filename']}")
    m3_file = SD(str(generic_m3), SDC.READ)
    lats = m3_file.select('Latitude').get()
    lons = m3_file.select('Longitude').get()
    stars = '*' * 40
    print(
        f"{stars}\nlats.shape, lons.shape: {lats.shape},{lons.shape}\n{stars}")
    m3_file.end()

    generic_rad = a301.data_dir / Path("rad_file_2018_10_1.hdf")
    if not generic_rad.exists():
        raise ValueError(f"couldn't find {generic_rad}")

    rad_file = SD(str(generic_rad), SDC.READ)
    ch30 = rad_file.select('ch30').get()
    print(f"working on {generic_rad}, originally was {rad_file.filename}")
    print(f"{stars}\narray shape is: {ch30.shape}\n{stars}")
    rad_file.end()

    print(f'reading {generic_m3}')

    from pyresample import SwathDefinition
    proj_params = get_proj_params(generic_m3)
    swath_def = SwathDefinition(lons, lats)
    area_def = swath_def.compute_optimal_bb_area(proj_dict=proj_params)

    fill_value = -9999.
    area_name = 'modis swath 5min granule'
    image_30 = kd_tree.resample_nearest(swath_def,
                                        ch30.ravel(),
                                        area_def,
                                        radius_of_influence=5000,
                                        nprocs=2,
                                        fill_value=fill_value)
    print(f'\ndump area definition:\n{area_def}\n')
    print((f'\nx and y pixel dimensions in meters:'
           f'\n{area_def.pixel_size_x}\n{area_def.pixel_size_y}\n'))
Ejemplo n.º 4
0
def main():
    """
    run the test
    """
    stars = '*' * 50
    print(f'\n{stars}\nrunning test: {__file__}\n{stars}\n')
    generic_m3 = a301.data_dir / Path("m3_file_2018_10_1.hdf")
    if not generic_m3.exists():
        raise ValueError(f"couldn't find {generic_m3}")
    modis_meta = parseMeta(generic_m3)
    print(f"working on {generic_m3}, originally was { modis_meta['filename']}")
    m3_file = SD(str(generic_m3), SDC.READ)
    lats_1km = m3_file.select('Latitude').get()
    lons_1km = m3_file.select('Longitude').get()
    stars = '*' * 40
    print(
        f"{stars}\nlats_1km.shape, lons_1km.shape: {lats_1km.shape},{lons_1km.shape}\n{stars}"
    )
    m3_file.end()

    generic_m5 = a301.data_dir / Path("myd05_l2_10_7.hdf")
    if not generic_m5.exists():
        raise ValueError(f"couldn't find {generic_m5}")

    m5_file = SD(str(generic_m5), SDC.READ)
    vapor_ir = m5_file.select('Water_Vapor_Infrared').get()
    vapor_near_ir = m5_file.select('Water_Vapor_Near_Infrared').get()
    lats_5km = m5_file.select('Latitude').get()
    lons_5km = m5_file.select('Longitude').get()
    m5_file.end()
    print('through')
    m5_meta = parseMeta(generic_m5)
    print(f"working on {generic_m5}, originally was {m5_meta['filename']}")
    print(
        f"{stars}\nnearir vapor array shape is: {vapor_near_ir.shape}\n{stars}"
    )
    print(f"{stars}\nir vapor array shape is: {vapor_ir.shape}\n{stars}")
    print(f"{stars}\nlats_5km arrayshape is: {lats_5km.shape}\n{stars}")
    print(f"{stars}\nlons_5km arrayshape is: {lons_5km.shape}\n{stars}")
    # print(f'reading {generic_m3}')

    from pyresample import SwathDefinition
    proj_params = get_proj_params(generic_m3)
    swath_def = SwathDefinition(lons_1km, lats_1km)
    area_def = swath_def.compute_optimal_bb_area(proj_dict=proj_params)

    fill_value = -9999.
    area_name = 'modis swath 5min granule'
    image_nearir = kd_tree.resample_nearest(swath_def,
                                            vapor_near_ir.ravel(),
                                            area_def,
                                            radius_of_influence=5000,
                                            nprocs=2,
                                            fill_value=fill_value)
    print(
        f'was able to regrid the nearir image, xy shape is {image_nearir.shape}'
    )

    proj_params = get_proj_params(generic_m5)
    swath_def = SwathDefinition(lons_5km, lats_5km)
    area_def = swath_def.compute_optimal_bb_area(proj_dict=proj_params)

    fill_value = -9999.
    area_name = 'modis swath 5min granule'
    image_ir = kd_tree.resample_nearest(swath_def,
                                        vapor_ir.ravel(),
                                        area_def,
                                        radius_of_influence=5000,
                                        nprocs=2,
                                        fill_value=fill_value)
    print(f'was able to regrid the ir image, xy shape is {image_ir.shape}')

    print('data looks good, ready to go')
Ejemplo n.º 5
0
    m3_file = a301.data_dir / Path('MYD03.A2013222.2105.006.2013223155808.hdf')
    rad_file = a301.data_dir / Path('modis_chans_2018_9_24.hdf')
    #
    # do the copies  (could also do this by hand)
    #
    shutil.copy(m3_file, generic_m3)
    shutil.copy(rad_file, generic_rad)
    #
    # test to make sure we have the right files
    #
    rad_file = SD(str(generic_rad), SDC.READ)
    rad_filename = rad_file.filename
    rad_file.end()
    print(f"\nworking with radiance file {generic_rad}\n"
          f"with original data {rad_filename}\n")
    m3_metadata = parseMeta(generic_m3)
    print(f"\nworking with m3_file {generic_m3} \n"
          f"with original data {m3_metadata['filename']}")
except Exception as ex:
    ex_type, ex_val, tb = sys.exc_info()
    print(f'caught {ex_type}, {ex_val} but ignoring it')

# In[ ]:

# Read the lats and lons from the MYD03 file
generic_rad = a301.data_dir / Path('rad_file_2018_10_1.hdf')
generic_m3 = a301.data_dir / Path('m3_file_2018_10_1.hdf')
print(f'reading {generic_m3}')
m3_file = SD(str(generic_m3), SDC.READ)
lats = m3_file.select('Latitude').get()
lons = m3_file.select('Longitude').get()
Ejemplo n.º 6
0
#
# <img src="images/myd2105.jpg" width=400>
#
#

# %% [markdown]
# **The cell below uses

# %%
# %matplotlib inline
from a301.scripts.modismeta_read import parseMeta
import context

data_dir = Path(context.data_dir)
modis_file = list(data_dir.glob("MYD02*2105*hdf"))[0]
modis_dict = parseMeta(modis_file)

# %%
pprint.pprint(modis_dict)
projection = ccrs.LambertAzimuthalEqualArea(
    central_latitude=modis_dict["lat_0"],
    central_longitude=modis_dict["lon_0"],
    globe=globe,
)
center_point = projection.transform_point(modis_dict["lon_0"],
                                          modis_dict["lat_0"], geodetic)
top_right = projection.transform_point(modis_dict["max_lon"],
                                       modis_dict["max_lat"], geodetic)
bottom_left = projection.transform_point(modis_dict["min_lon"],
                                         modis_dict["min_lat"], geodetic)
extent = [bottom_left[0], top_right[0], bottom_left[1], top_right[1]]
help(hdf4ls)

# In[5]:

hdf4ls(hdf_files[0])

# # Read the CoreMetadata.0 attribute with parseMeta

# In[6]:

from a301.scripts.modismeta_read import parseMeta
help(parseMeta)

# In[7]:

parseMeta(hdf_files[0])

# # Plotting the lats and lons

# In[8]:

the_file = SD(str(hdf_files[0]), SDC.READ)
lat_data = the_file.select('Latitude')
lon_data = the_file.select('Longitude')

# In[9]:

fig, ax = plt.subplots(1, 1, figsize=(10, 14))
ax.plot(lon_data[900:940, 900:940], lat_data[900:940, 900:940], 'b+')
ax.set(xlabel='longitude (deg W)', ylabel='latitude (deg N)')
Ejemplo n.º 8
0
download(modis_file)
generic_m2 = a301.data_dir / Path(modis_file)
shutil.copy(Path(modis_file), generic_m2)

# %%
#
# replace with your file name if needed
#
modis_file = "MYD021KM.A2013222.2105.061.2018047235850.hdf"
generic_m2 = a301.data_dir / Path(modis_file)

# %%
#
# confirm that we can read this file
#
modis_meta = parseMeta(str(generic_m2))
print(f"opened and read {modis_meta['filename']}")
#
# this is my modis multichannel output file
#
generic_rad = a301.data_dir / Path('rad_file_2018_10_1.hdf')

# %% [markdown]
# # Reading modis data

# %% [markdown]
# ## Using pydf to get metadata
#
# I can convert the filename from a Path object to a string object and pass it to pyhdf.SD
# to find out how many datasets and attributes there are in the file
#
    """
    image_file = Path(foldername) / Path(image_array_name)
    out_dict = {image_array_name: image_array}
    np.savez(image_file, **out_dict)
    json_name = foldername / Path(image_array_name + '.json')
    with open(json_name, 'w') as f:
        json.dump(metadata_dict, f, indent=4)
    print(f"\ndumping {image_file}\n and {json_name}\n")


# ## Write out images, putting useful metadeta in metadata_dict

# In[ ]:

image_name = 'wv_nearir_lr'
metadata_dict = dict(modismeta=parseMeta(m5_file))
metadata_dict['area_def'] = area_def_to_dict(area_def_lr)
metadata_dict['image_name'] = image_name
metadata_dict[
    'description'] = 'modis near ir water vapor (cm) sampled at 5 km resolution'
metadata_dict['history'] = 'written by level2_cartopy_resample.ipynb'
map_dir = a301.data_dir.parent / Path('map_data/wv_maps')
map_dir.mkdir(parents=True, exist_ok=True)
dump_image(image_wv_nearir_lr, metadata_dict, map_dir, image_name)

image_name = 'wv_nearir_hr'
metadata_dict = dict(modismeta=parseMeta(m5_file))
metadata_dict['area_def'] = area_def_to_dict(area_def_hr)
metadata_dict['image_name'] = image_name
metadata_dict[
    'description'] = 'modis near ir water vapor (cm) sampled at 1 km resolution'
Ejemplo n.º 10
0
# ## Question 9.2
#
# Write out your wv_difference image and using dump_image

# In[7]:

from a301.geometry import dump_image, area_def_to_dict

image_name = 'wv_difference'
### BEGIN SOLUTION
metadata_dict = dict()
metadata_dict['area_def'] = area_def_to_dict(area_def_lr)
metadata_dict['image_name'] = image_name
metadata_dict[
    'description'] = 'column wv difference between nearir and ir retrievals'
metadata_dict['modis_metadata'] = parseMeta(a301.data_dir /
                                            Path('myd05_l2_10_7.hdf'))
metadata_dict['history'] = 'written by level2_cartopy_resample.ipynb'
out_folder = a301.map_dir / Path('wv_maps')
dump_image(wv_difference, metadata_dict, out_folder, image_name)
### END SOLUTION

# ### Here is the test that autograder will run
#
# Copy this code in a new cell and make sure it passes the test (i.e. doesn't complain)
# This is what autograder will run automatically

# In[9]:

from a301.geometry import get_image
#
# read back the image and the area_def