示例#1
0
# In[11]:

plt.figure()
plt.plot(star['tau'])


# In[11]:

plt.figure()
plt.plot(star['utc'],star['tau'])


# In[13]:

star['ref'] = Sp.smooth(star['ref'],20)


# In[14]:


plt.figure()
plt.plot(star['utc'],star['ref'],'x-')



# ## Load the important MODIS files

# In[12]:

myd06_file = fp+'MODIS\\MYD06_L2.A2013050.1725.006.2014260074007.hdf'
iwat = np.argmin(abs(ssfr.tmhrs-twat))


# In[36]:

alb = ssfr.nspectra1/ssfr.zspectra1
alb[alb<=0.0] = 0.0
alb[alb>=1.0] = 1.0
alb[np.isnan(alb)] = 0.0


# In[37]:

plt.figure()
plt.plot(wvl,alb[iwat,:],'b.')
plt.plot(wvl,Sp.smooth(alb[iwat,:],6),'r')

plt.xlim([350,1700])
plt.ylim([0,1])
plt.ylabel('Albedo')
plt.xlabel('Wavelength [nm]')
plt.title('Surface albedo, above water UTC: %.4f' % ssfr.tmhrs[iwat])
plt.savefig(fp+'dc8/'+daystr+'_surface_albedo_ice.png',dpi=600,transparent=True)


# In[38]:

plt.figure()
plt.plot(wvl,alb[iice,:],'b.')
plt.plot(wvl,Sp.smooth(alb[iice,:],6),'r')
plt.xlim([350,1700])
cba.set_label('UTC [h]')
plt.ylim(0,1)
plt.xlim(340,1750)
plt.title('SASZe normalized spectra, Ascension Island, 2016-08-14')
plt.xlabel('Wavelength [nm]')
plt.ylabel('Normalized Radiance')
plt.tight_layout()
#plt.plot(lut.wvl,lut.norm[0,:,0,8,20],'k-',lw=3,
#         label='Modeled, $\\tau$={:2.1f}, r$_{{eff}}$={:2.0f}$\\mu$m'.format(lut.tau[20],lut.ref[8]))
plt.plot(lut.wvl,lut.norm[0,:,0,4,16],'-',color='k',lw=3,alpha=0.8,
         label='Modeled, $\\tau$={:2.1f}, r$_{{eff}}$={:2.0f}$\\mu$m'.format(lut.tau[16],lut.ref[4]))
plt.plot(lut.wvl,lut.norm[0,:,0,4,1],'-',color='lightgrey',lw=3,alpha=0.8,
         label='Modeled, $\\tau$={:2.1f}, r$_{{eff}}$={:2.0f}$\\mu$m'.format(lut.tau[1],lut.ref[4]))
plt.legend(handletextpad=0.08,labelspacing=0.05,frameon=True)
plt.savefig(fp+'data/SASZe_sample_spectra_with_model.png',dpi=600,transparent=True)


# In[184]:


fig = sp.plt_zenrad(sr)
plt.ylim(0,0.5)
#plt.figure()


# In[253]:


sp.plt_norm_zenrad(sr)

示例#4
0
def run_retrieval(meas, model, subp=range(15)):
    """ 
    Name:

        run_retrieval
    
    Purpose:

        A function that uses the Sp class to run through each utc point in meas, 
        and finds the closest model values, 
        uses only the subset of parameters defined by subp, which defaults to first 15 parameters
    
    Calling Sequence:

        (ta,re,ph,ki) = run_retrieval(meas,model,subp=range(15))
    
    Input: 
  
        meas : Sp object (from Sp_parameters) of measurement spectra
        model: Sp object (from Sp_paramters) of modeled spectra, also considered the look-up-table (lut) object
        subp: (optional) array of parameters to use for retrieval
     
    Output:

        ta: array of cloud optical thickness
        re: array of cloud particle effective radius
        ph: array of cloud thermodynamic phase (ph=0 for liquid, ph=1 for ice)
        ki: array of minimal ki^2 values 
    
    Keywords: 

        none
    
    Dependencies:

        Sp_parameters
        numpy
        gc: for clearing the garbage
        run_kisq_retrieval (this file)
        pdb: for debugging when there is an error
    
    Required files:
   
        none
    
    Example:

        ...
        
    Modification History:
    
        Written (v1.0): Samuel LeBlanc, 2014-11-08, NASA Ames

    """
    import Sp_parameters as Sp
    import numpy as np
    import run_kisq_retrieval as rk
    rk.assure_input(meas)
    rk.assure_input(model)
    # normalize the parameters
    pcoef = model.norm_par()
    meas.norm_par(pcoef=pcoef)

    # build the measurement error
    meas.build_std(
    )  #in future must reference a set of measurements to establishe the uncertainty, right now only white noise at 0.5%
    meas.stdparn = meas.stdpar / pcoef[
        'coef']  #for creating the normalized version of the stdpar

    # build the reshaped model for ice and liquid
    model.reshape_lut(phase='liq')
    model.reshape_lut(phase='ice')

    # build the normalized measurement parameters based on liquid or ice values
    meas.norm_par(pcoef=model.ice.pcoef, vartitle='parn_ice')
    meas.norm_par(pcoef=model.liq.pcoef, vartitle='parn_liq')

    # build the weights from the stdparn of measurements
    wg = np.sqrt(meas.stdpar) / pcoef['coef']

    #start loop over measurement
    Sp.startprogress('Retrieval progress over times')
    ph = np.zeros_like(meas.utc) * np.nan
    ki = np.zeros_like(meas.utc) * np.nan
    ta = np.zeros_like(meas.utc) * np.nan
    re = np.zeros_like(meas.utc) * np.nan
    #ki_2ph = np.zeros_like(meas.utc) #kisq with 2 phase
    for tt in meas.good:
        try:
            if np.all(np.isnan(meas.parn[tt, subp])):
                continue
        except:
            import pdb
            pdb.set_trace()
        #first get the phase in first method
        #import pdb; pdb.set_trace()
        ph[tt] = rk.phase(meas.parn[tt, :].ravel(), model, meas.stdparn)
        if ph[tt] == 2:  # undecided, must do the kisq
            ki_2ph = np.nansum(
                wg[subp] *
                (meas.parn[tt, subp] - model.parn[:, :, :, subp])**2,
                axis=3)
            ki_minin = np.unravel_index(np.nanargmin(ki_2ph), ki_2ph.shape)
            ph[tt] = ki_minin[0]
        if ph[tt] == 0:  #liquid
            goodpi = [
                i for i in subp
                if (meas.parn_liq[tt, i] + meas.stdparn[i] >= 0) and (
                    meas.parn_liq[tt, i] - meas.stdparn[i] <= 1)
            ]
            if len(goodpi) < 4:
                continue
            ki_arr = np.nansum(
                wg[goodpi] *
                (meas.parn_liq[tt, goodpi] - model.liq.parn[:, :, goodpi])**2,
                axis=2)
            ki[tt] = np.nanmin(ki_arr)
            #print ki[tt]
            ki_minin = np.unravel_index(np.nanargmin(ki_arr), ki_arr.shape)
            (ta[tt], re[tt]) = (model.liq.tau[ki_minin[1]],
                                model.liq.ref[ki_minin[0]])
        elif ph[tt] == 1:  #ice
            goodpi = [
                i for i in subp
                if (meas.parn_ice[tt, i] + meas.stdparn[i] >= 0) and (
                    meas.parn_ice[tt, i] - meas.stdparn[i] <= 1)
            ]
            #       print goodpi
            if len(goodpi) < 4:
                continue
            ki_arr = np.nansum(
                wg[goodpi] *
                (meas.parn_ice[tt, goodpi] - model.ice.parn[:, :, goodpi])**2,
                axis=2)
            ki[tt] = np.nanmin(ki_arr)
            ki_minin = np.unravel_index(np.nanargmin(ki_arr), ki_arr.shape)
            (ta[tt], re[tt]) = (model.ice.tau[ki_minin[1]],
                                model.ice.ref[ki_minin[0]])
            #print ki[tt]
        else:
            print('Problem with phase!')
            return
        Sp.progress(float(tt) / len(meas.utc) * 100.0)
    Sp.endprogress()

    #save the file
    return (ta, re, ph, ki)
示例#5
0
# <codecell>

map(lambda x:x*x,[-1,1,24])

# <codecell>

reload(Sp)
if 'meas' in locals():
    del meas
    import gc; gc.collect()

# <codecell>

# first convert measurements to Sp class, with inherent parameters defined
meas = Sp.Sp(m)
meas.params()

# <markdowncell>

# Plot the parameters for the specified time

# <codecell>

fig2,ax2 = plt.subplots(5,3,sharex=True,figsize=(15,8))
ax2 = ax2.ravel()
for i in range(meas.npar-1):
    ax2[i].plot(meas.utc,Sp.smooth(meas.par[:,i],3))
    ax2[i].set_title('Parameter '+str(i))
    ax2[i].grid()
    ax2[i].set_xlim([17,19])
    try:
        ccn[i] = recarray_to_dict(ccn[i])
    except:
        pass
    ccn[i]['alt'] = nearest_neighbor(hsk[i]['Start_UTC'],hsk[i]['GPS_Altitude'],ccn[i]['UTC_mid'],dist=1.0/3600.0)


# ## Check out AMS data

# In[185]:

plt.figure()
cs = ['r','g','b','k','c','y']
for i,d in enumerate(days):
    #plt.plot(ams[i]['UTC_mid'],ams[i]['Org_STP'],'x',color=cs[i],label=d,alpha=0.3)
    plt.plot(ams[i]['UTC_mid'],Sp.smooth(ams[i]['Org_STP'],16),'-',color=cs[i],alpha=0.6,label=d)
plt.legend(frameon=False)
plt.xlabel('UTC')
plt.ylabel('Org_STP')


# In[210]:

plt.figure()
plt.plot(ams[2]['UTC_mid'],Sp.smooth(ams[2]['Org_STP'],14))
plt.plot(ams[2]['UTC_mid'],Sp.smooth(ams[2]['SO4_STP'],14),'-g')
plt.plot(ams[2]['UTC_mid'],Sp.smooth(ams[2]['Chl_STP'],14),'-c')

plt.plot(star[2]['Start_UTC'],star[2]['COD']/200.0,'xk')
plt.plot(star[2]['Start_UTC'],star[2]['REF']/100.0,'+r')
plt.xlim(15,15.8)
def plot_vert_hist(fig,ax1,y,pos,ylim,color='grey',label=None,legend=False,onlyhist=True,loc=2,bins=30,alpha=0.5):
    """
    Purpose:
        function to plot a 'bean' like vertical histogram
    
    Input:
        fig: figure object for reference and plotting
        ax1: axis object to plot on
        y: values to be plotted in histogram fashion
        pos: position along axis to create the bean plot
        ylim: range of plotted axis [min,max]
        color: (default to grey) color of the plotted histogram
        label: (default to none) if included, the label for the color squares of this histogram
        legend: (default to False) If True adds a legend on the location 
        onlyhist: (default to True) only plots the histogram bean, not the mean and median lines
        loc: (default to 2) location of the legend
        bins: (default to 30) number of bins to plot
        alpha: (defautl to 0.5) value of transparency (0 to 1)
    
    Output:
        only the histogram plot on top of an existing plot
    
    Dependencies:
        - numpy
        - Sp_parameters
        - plotting_utils : this file
    
    Required files:
        None
    
    Modification History:
        Written: Samuel LeBlanc, NASA Ames
        Modified: Samuel LeBlanc, NASA Ames in Santa Cruz, 2015-12-09
                  - added comments
                  - added the bins keyword to be used
        Modified: Samuel LeBlanc, NASA Ames in Santa Cruz, 2015-12-12
                  - added alpha keyword
    """
    import Sp_parameters as Sp
    import numpy as np
    from plotting_utils import data2figpoints
    (ymask,iy) = Sp.nanmasked(y)
    ax = fig.add_axes(data2figpoints(pos,0.4,fig=fig,ax1=ax1),frameon=False,ylim=ylim)
    ax.tick_params(axis='both', which='both', labelleft='off', labelright='off',bottom='off',top='off',
               labelbottom='off',labeltop='off',right='off',left='off')
    ax.hist(ymask,orientation='horizontal',normed=True,color=color,edgecolor='None',bins=bins,alpha=alpha,label=label,range=ylim)
    if onlyhist:
        label_mean = None
        label_median = None
    else:
        label_mean = 'Mean'
        label_median = 'Median'
    ax.axhline(np.mean(ymask),color='red',linewidth=2,label=label_mean)
    ax.axhline(np.median(ymask),color='k',linewidth=2,linestyle='--',label=label_median)
    if legend:
        ax.legend(frameon=False,loc=loc)
    ax = fig.add_axes(data2figpoints(pos+0.01,-0.4,fig=fig,ax1=ax1),frameon=False,ylim=ylim)
    ax.tick_params(axis='both', which='both', labelleft='off', labelright='off',bottom='off',top='off',
                   labelbottom='off',labeltop='off',right='off',left='off')
    ax.hist(ymask,orientation='horizontal',normed=True,color=color,edgecolor='None',bins=bins,alpha=alpha,range=ylim)
    ax.axhline(np.mean(ymask),color='red',linewidth=2)
    ax.axhline(np.median(ymask),color='k',linewidth=2,linestyle='--')
print 'sp (wp, wvl, z, re, ta)'
# create custom key for sorting via wavelength
iwvls = np.argsort(s.zenlambda)
s.wv = np.sort(s.zenlambda)

# <codecell>

if 'Sp' in locals():
    reload(Sp)
if 'lut' in locals():
    del lut
    import gc; gc.collect()

# <codecell>

lut = Sp.Sp(s,irrad=True)
lut.params()
lut.param_hires()

# <codecell>

lut.sp_hires()

# <codecell>

print lut.tau.shape
print lut.ref.shape
print lut.sp.ndim
print lut.par.size
print lut.par.shape
print lut.ref
def run_retrieval(meas,model,subp=range(15)):
    """ 
    Name:

        run_retrieval
    
    Purpose:

        A function that uses the Sp class to run through each utc point in meas, 
        and finds the closest model values, 
        uses only the subset of parameters defined by subp, which defaults to first 15 parameters
    
    Calling Sequence:

        (ta,re,ph,ki) = run_retrieval(meas,model,subp=range(15))
    
    Input: 
  
        meas : Sp object (from Sp_parameters) of measurement spectra
        model: Sp object (from Sp_paramters) of modeled spectra, also considered the look-up-table (lut) object
        subp: (optional) array of parameters to use for retrieval
     
    Output:

        ta: array of cloud optical thickness
        re: array of cloud particle effective radius
        ph: array of cloud thermodynamic phase (ph=0 for liquid, ph=1 for ice)
        ki: array of minimal ki^2 values 
    
    Keywords: 

        none
    
    Dependencies:

        Sp_parameters
        numpy
        gc: for clearing the garbage
        run_kisq_retrieval (this file)
        pdb: for debugging when there is an error
    
    Required files:
   
        none
    
    Example:

        ...
        
    Modification History:
    
        Written (v1.0): Samuel LeBlanc, 2014-11-08, NASA Ames

    """
    import Sp_parameters as Sp
    import numpy as np
    import run_kisq_retrieval as rk
    rk.assure_input(meas)
    rk.assure_input(model)
    # normalize the parameters
    pcoef = model.norm_par()
    meas.norm_par(pcoef=pcoef)
    
    # build the measurement error
    meas.build_std() #in future must reference a set of measurements to establishe the uncertainty, right now only white noise at 0.5%
    meas.stdparn = meas.stdpar/pcoef['coef'] #for creating the normalized version of the stdpar
    
    # build the reshaped model for ice and liquid
    model.reshape_lut(phase='liq')
    model.reshape_lut(phase='ice')
    
    # build the normalized measurement parameters based on liquid or ice values
    meas.norm_par(pcoef=model.ice.pcoef,vartitle='parn_ice')
    meas.norm_par(pcoef=model.liq.pcoef,vartitle='parn_liq')
    
    # build the weights from the stdparn of measurements
    wg = np.sqrt(meas.stdpar)/pcoef['coef']
    
    #start loop over measurement
    Sp.startprogress('Retrieval progress over times')
    ph = np.zeros_like(meas.utc)*np.nan
    ki = np.zeros_like(meas.utc)*np.nan
    ta = np.zeros_like(meas.utc)*np.nan
    re = np.zeros_like(meas.utc)*np.nan
    #ki_2ph = np.zeros_like(meas.utc) #kisq with 2 phase
    for tt in meas.good:
        try:
            if np.all(np.isnan(meas.parn[tt,subp])):
                continue
        except:
            import pdb; pdb.set_trace()
        #first get the phase in first method
        #import pdb; pdb.set_trace()
        ph[tt] = rk.phase(meas.parn[tt,:].ravel(),model,meas.stdparn)
        if ph[tt] == 2: # undecided, must do the kisq
            ki_2ph = np.nansum(wg[subp]*(meas.parn[tt,subp]-model.parn[:,:,:,subp])**2,axis=3)
            ki_minin = np.unravel_index(np.nanargmin(ki_2ph),ki_2ph.shape)
            ph[tt] = ki_minin[0]
        if ph[tt] == 0: #liquid
            goodpi = [i for i in subp if (meas.parn_liq[tt,i]+meas.stdparn[i]>=0) and (meas.parn_liq[tt,i]-meas.stdparn[i]<=1)]
            if len(goodpi) < 4:
                continue
            ki_arr = np.nansum(wg[goodpi]*(meas.parn_liq[tt,goodpi]-model.liq.parn[:,:,goodpi])**2,axis=2)
            ki[tt] = np.nanmin(ki_arr)
            #print ki[tt]
            ki_minin = np.unravel_index(np.nanargmin(ki_arr),ki_arr.shape)
            (ta[tt],re[tt]) = (model.liq.tau[ki_minin[1]],model.liq.ref[ki_minin[0]])
        elif ph[tt] == 1: #ice
            goodpi = [i for i in subp if (meas.parn_ice[tt,i]+meas.stdparn[i]>=0) and (meas.parn_ice[tt,i]-meas.stdparn[i]<=1)]
     #       print goodpi
            if len(goodpi) < 4:
                continue
            ki_arr = np.nansum(wg[goodpi]*(meas.parn_ice[tt,goodpi]-model.ice.parn[:,:,goodpi])**2,axis=2)
            ki[tt] = np.nanmin(ki_arr)
            ki_minin = np.unravel_index(np.nanargmin(ki_arr),ki_arr.shape)
            (ta[tt],re[tt]) = (model.ice.tau[ki_minin[1]],model.ice.ref[ki_minin[0]])
            #print ki[tt]
        else:
            print('Problem with phase!')
            return
        Sp.progress(float(tt)/len(meas.utc)*100.0)
    Sp.endprogress()
    
    #save the file
    return (ta,re,ph,ki)
# In[73]:


meas = Sp.Sp(m)
meas.params()


# # Now plot the measurements and luts

# ## Plot all measurements

# In[85]:


pz = Sp.plt_zenrad(meas,good_only=True)


# In[86]:


pn = Sp.plt_norm_zenrad(meas,good_only=True)


# In[87]:


pc = Sp.curtain_zenrad(meas)


# In[90]:
# In[ ]:


print 'Running the parameter calculations on measured spectra'
meas = Sp.Sp(mea,verbose=False)
meas.params()


# ### plot out the different spectra

# In[ ]:


if not noplot:
    print('Making plots...')
    fig1 = Sp.plt_zenrad(meas)
    fig1.savefig(fp_zencld_plot+'{datestr}_zenrad.png'.format(datestr=datestr),
                 dpi=600,transparent=True)
    print('zenrad...'),

    fig1n = Sp.plt_norm_zenrad(meas)
    fig1n.savefig(fp_zencld_plot+'{datestr}_norm_zenrad.png'.format(datestr=datestr),
                  dpi=600,transparent=True)
    print('norm zenrad...'),

    fig2 = Sp.curtain_zenrad(meas,utc=True)
    fig2.savefig(fp_zencld_plot+'{datestr}_curtain_utc_zenrad.png'.format(datestr=datestr),
                 dpi=600,transparent=True)
    print('utc curtain zenrad...'),

    fig2n = Sp.curtain_norm_zenrad(meas,utc=True)
def run_2wvl_retrieval(meas,model,wvls=[500.0,1600.0],sza_norm=True):
    """ 
    Name:

        run_2wvl_retrieval
    
    Purpose:

        Retrieves cloud optical properties from reflectance 2 wavelength method (Nakajima & King, 1990)
        A function that uses the Sp class to run through each utc point in meas, 
        and finds the closest model values.
        If sza is present then the reflectance is calculated with sza_factor (1/cos(sza))
    
    Calling Sequence:

        (ta,re,ki) = run_2wvl_retrieval(meas,model,wvls=[500.0,1600.0],sza_norm=True)
    
    Input: 
  
        meas : object with keys of utc (time in fractional hours), Rvis (reflectance in vis), Rnir (reflectance in nir)
        model: Sp object with irradiance values set
     
    Output:

        ta: 2d array of cloud optical thickness, for [:,0] = liquid, for [:,1] = ice
        re: 2d array of cloud particle effective radius, for [:,0] = liquid, for [:,1] = ice
        ki: 2d array of minimal ki^2 values, for [:,0] = liquid, for [:,1] = ice
    
    Keywords: 

        wvls: (optional) array of two values with the vis wavlength in first, and nir wavelength in second [nm]
        sza_norm: (optional) if set to True (default) then searches for sza in meas and model. 
                 If there, normalizes meas and model reflectances by 1/cos(sza) 
    
    Dependencies:

        Sp_parameters
        numpy
        warnings
        gc: for clearing the garbage
        run_2wvl_retrieval (this file)
        pdb: for debugging when there is an error
        math
    
    Required files:
   
        none
    
    Example:

        ...
        
    Modification History:
    
        Written (v1.0): Samuel LeBlanc, 2014-12-08, NASA Ames

    """
    import Sp_parameters as Sp
    import numpy as np
    import run_2wvl_retrieval as rw
    import warnings
    import math
    if not model.irrad:
        warnings.warn('model lut does not have irradiances set! please rerun with irradiances')
        return
    if sza_norm & hasattr(meas,'sza'):
        meas.sza_factor = 1.0/np.cos(np.radians(meas.sza))
    else:
        meas.sza_factor = 1.0
    meas.Rvis = meas.Rvis*meas.sza_factor
    meas.Rnir = meas.Rnir*meas.sza_factor
    model.lut_2wvl = rw.build_lut_2wvl(model,wvls,sza_norm)    
    #start loop over measurement
    Sp.startprogress('Retrieval progress over times')
    ki = np.ndarray((len(meas.utc),2))*np.nan
    ta = np.ndarray((len(meas.utc),2))*np.nan
    re = np.ndarray((len(meas.utc),2))*np.nan
    #make good filter
    meas.good = np.where((np.isfinite(meas.Rvis)) & (meas.Rvis > 0) & (np.isfinite(meas.Rnir)) & (meas.Rnir > 0))[0]
    for tt in meas.good:
        for ph in [0,1]:
            ki_ref_tau = ((meas.Rvis[tt]-model.lut_2wvl[ph,0,:,:])/meas.Rvis[tt])**2+((meas.Rnir[tt]-model.lut_2wvl[ph,1,:,:])/meas.Rnir[tt])**2
            try:
                ki_minin = np.unravel_index(np.nanargmin(ki_ref_tau),ki_ref_tau.shape)
            except:
                import pdb; pdb.set_trace()
            ki[tt,ph] = np.nanmin(ki_ref_tau)
            (ta[tt,ph],re[tt,ph]) = (model.tau[ki_minin[1]],model.ref[ki_minin[0]])
         #   if (ph == 1) & (meas.utc[tt] > 18.4):
         #       import pdb; pdb.set_trace()
        Sp.progress(float(tt)/len(meas.utc)*100.0)
    Sp.endprogress()
    return (ta,re,ki)