# ### Reading the Modis level 2 cloudmask # # http://modis-atmos.gsfc.nasa.gov/MOD35_L2/ # In[1]: get_ipython().magic('matplotlib inline') import h5py import numpy as np from matplotlib import pyplot as plt from e582utils.data_read import download mask_name='MYD35_L2.A2014125.2135.006.2014125184012.h5' download(mask_name) with h5py.File(mask_name,'r') as infile: cloud_mask=infile['mod35/Data Fields/Cloud_Mask'][...] maskVals=cloud_mask[0,...].astype(np.uint8) #get the first byte # There are 6 bytes in the cloudmask: # " \n", # " Bit fields within each byte are numbered from the left: \n", # " 7, 6, 5, 4, 3, 2, 1, 0. \n", # " The left-most bit (bit 7) is the most significant bit. \n", # " The right-most bit (bit 0) is the least significant bit. \n", # " \n",
from e582utils.data_read import download import numpy as np import h5py import sys import warnings from matplotlib import pyplot as plt from IPython.display import Image from mpl_toolkits.basemap import Basemap from matplotlib.colors import Normalize import matplotlib import matplotlib.cm as cm import seaborn as sns get_ipython().magic('matplotlib inline') myd02file = "MYD021KM.A2016224.2100.006.2016225153002.h5" download(myd02file) # In[2]: Image('figures/MYBRGB.A2016224.2100.006.2016237025650.jpg') # In[3]: myd03file = "MYD03.A2016224.2100.006.2016225152335.h5" download(myd03file) # ### Calibrate and resample the channel 1 and channel 2 reflectivities # # # In[4]:
# ## First, get channel 1 and the lat/lon files as usual # In[1]: import h5py import numpy as np import pyproj import pyresample from pyresample import kd_tree, geometry from matplotlib import pyplot as plt from e582utils.modismeta_read import parseMeta from e582utils.data_read import download from mpl_toolkits.basemap import Basemap data_name = 'MYD021KM.A2016224.2100.006.2016225153002.h5' download(data_name) geom = 'MYD03.A2016224.2100.006.2016225152335.h5' download(geom) index = 0 my_name = 'EV_250_Aggr1km_RefSB' with h5py.File(data_name, 'r') as h5_file: chan1 = h5_file['MODIS_SWATH_Type_L1B']['Data Fields'][my_name][ index, :, :] scale = h5_file['MODIS_SWATH_Type_L1B']['Data Fields'][my_name].attrs[ 'reflectance_scales'][...] offset = h5_file['MODIS_SWATH_Type_L1B']['Data Fields'][my_name].attrs[ 'reflectance_offsets'][...] chan1_calibrated = (chan1 - offset[index]) * scale[index] with h5py.File(geom) as geo_file:
# In[13]: from netCDF4 import Dataset import numpy as np import matplotlib from matplotlib import pyplot as plt from mpl_toolkits.basemap import Basemap from e582utils.data_read import download import warnings warnings.filterwarnings("ignore") import pyproj from e582lib.map_slices import make_basemap_xy from rasterio.transform import from_bounds l3file='A2007008.L3m_DAY_PAR_par_9km.nc' download(l3file) # #### Extract the variables and the _FillValue and replace missing data with np.nan # # See http://unidata.github.io/netcdf4-python/ # In[2]: with Dataset(l3file,'r') as ncdat: ncdat.set_auto_mask(False) par=ncdat.variables['par'][...] lat=ncdat.variables['lat'][...] lon=ncdat.variables['lon'][...] fill_value=ncdat.variables['par']._FillValue hit= par == fill_value
# and # # [Python for Data Analysis](http://shop.oreilly.com/product/0636920023784.do) # In[1]: from e582utils.data_read import download from contexttimer import Timer import h5py import pandas as pd import datetime as dt import numpy as np import time chlor_file='A20101522010181.L3b_MO_CHL.h5' download(chlor_file) # ### dump the monthly level3 binned chlorophyll metadata # In[2]: from e582utils.h5dump import dumph5 outstring=dumph5(chlor_file) print(outstring) # ### Structured arrays # # The hdf file stores the array chlor_a as a vector with 11384896 values, each with two fields: chlor_a_sum and chlor_a_sum_sq #
# # 1) how to plot an image using imshow with a colorbar # # 2) how to find out which pixels are in a particular lon/lat grid cell so that we can # regrid the radiances onto a uniform grid # # In[14]: from e582utils.data_read import download import numpy as np import h5py import sys filename = 'MYD021KM.A2016136.2015.006.2016138123353.h5' download(filename) # Here is the corresponding red,green,blue color composite for the granule. # In[15]: from IPython.display import Image Image( url= 'http://clouds.eos.ubc.ca/~phil/courses/atsc301/downloads/aqua_136_2015.jpg', width=600) # In[16]: ### now use h5py to read some of the satellite channels
# ### Assignment -- for the following image, use [histogram2d](https://docs.scipy.org/doc/numpy/reference/generated/numpy.histogram2d.html) to make a 2-dimensional histogram # # Use the channel 1 reflectivity for your y-axis bins, and the channel 31 brightness temperature for your x-axis bins # In[6]: from IPython.display import Image from e582utils.data_read import download # In[5]: Image('MOBRGB.A2012240.0235.006.2015311151021.jpg',width=500) # In[9]: l1b_file='MOD021KM.A2012240.0235.006.2014220124853.h5' geom_file='MOD03.A2012240.0235.006.2012287184700.h5' mask_file='MOD35_L2.A2012240.0235.006.2015059110241.h5' cloud_file='MOD06_L2.A2012240.0235.006.2015062132158.h5' files=[l1b_file,geom_file,mask_file,cloud_file] for the_file in files: download(the_file) # In[ ]:
# unlike the file used in the calc_chlor and plot_chlor notebooks, downloads from the # https://oceancolor.gsfc.nasa.gov/cgi/l3 have a nc suffix but don't open as nc files. # Try as an h5 file instead. # # This notebook verifies that the data is the same format used in the calc_chlor notebook # In[10]: from e582utils.data_read import download from e582utils import h5dump import h5py # In[3]: infile = 'A2007010.L3b_DAY_PAR.nc' download(infile) # In[7]: out = h5dump.dumph5(infile) # In[25]: root_key = 'level-3_binned_data' with h5py.File(infile, 'r') as in_h5: print('root keys: ', list(in_h5.keys())) print('group keys: ', list(in_h5[root_key].keys())) par = in_h5[root_key]['par'][...] binlist = in_h5[root_key]['BinList'][...] # In[26]:
dset[...] = the_chan[...] dset.attrs['units'] = chan_dict[chan_name]['units'] dset.attrs['wavelength_um'] = chan_dict[chan_name]['wavelength_um'] dset.attrs['modis_chan'] = chan_name f.attrs['geotiff_args'] = geotiff_string f.attrs['basemap_args'] = basemap_string f.attrs['area_def_args'] = area_def_string f.attrs['fill_value'] = fill_value f.attrs['level1b_file'] = in_file return None if __name__ == "__main__": myd02_name = 'MYD021KM.A2016224.2100.006.2016225153002.h5' download(myd02_name) myd03_name = 'MYD03.A2016224.2100.006.2016225152335.h5' download(myd03_name) out_file = 'test.h5' chan_list = ['1', '4', '31'] project_channels = modisl1b_resample(myd02_name, myd03_name, chan_list) project_channels['out_file'] = 'test.h5' project_channels['in_file'] = myd02_name project_channels['chan_list'] = chan_list write_h5(**project_channels) from matplotlib import cm cmap = cm.autumn #see http://wiki.scipy.org/Cookbook/Matplotlib/Show_colormaps cmap.set_over('w') cmap.set_under('b', alpha=0.2)
from matplotlib import pyplot as plt import matplotlib.image as image import matplotlib import rasterio from e582lib.map_slices import get_basemap import numpy as np from mpl_toolkits.basemap import Basemap from e582utils.data_read import download import warnings warnings.filterwarnings("ignore") # In[2]: download('stretched_rgb.png') download('stretched_rgb.tif') # #### Read in the png 3-color image along with the tif version and save the projection and transform # In[3]: imfile = 'stretched_rgb.png' im=image.imread(imfile) # # find all black pixels and turn them light grey # hit = np.logical_and(im[:,:,0]==0, im[:,:,1]==0,im[:,:,2]==0) im[hit,:]=0.75 #