def param(sp,wvlin):
    " Calculates the parameters from a spectrum."
    from linfit import linfit
    from Sp_parameters import nanmasked, norm2max, smooth, deriv, find_closest
    npar = 16
    spc, mask = nanmasked(sp)
    wvl = wvlin[mask]
    try:
        norm = norm2max(spc)
    except ValueError:
        par = np.zeros(npar)*np.nan
        return par
    [i1000,i1077,i1493,i1600,i1200,i1300,i530,i610,
     i1565,i1634,i1193,i1198,i1236,i1248,i1270,i1644,
     i1050,i1040,i1065,i600,i870,i515] = find_closest(wvl,np.array([1000,1077,1493,1600,1200,1300,530,
                                                     610,1565,1634,1193,1198,1236,1248,
                                                     1270,1644,1050,1040,1065,600,870,515]))
    if np.isnan(spc[i1000]) or not spc[i1000]:
        par = np.zeros(npar)*np.nan
        return par
    norm2 = spc/spc[i1000]
    dsp = smooth(deriv(norm2,wvl/1000),2,nan=False)
    imaxwvl = np.argmax(spc)
    maxwvl = wvl[mask[imaxwvl]]
    # now calculate each parameter
    fit0 = np.polyfit(np.array([wvl[i1000],wvl[i1077]]),np.array([norm2[i1000],norm2[i1077]]),1)
    fit0_fn = np.poly1d(fit0)
    fit7 = np.polyfit(np.array([wvl[i1493],wvl[i1600]]),np.array([norm2[i1493],norm2[i1600]]),1)
    fit7_fn = np.poly1d(fit7)
    fit8,z = linfit(wvl[i1000:i1077],dsp[i1000:i1077])
    fit9,z = linfit(wvl[i1200:i1300],dsp[i1200:i1300])
    fit10,z = linfit(wvl[i530:i610]/1000,norm[i530:i610])
    fit14,z = linfit(wvl[i1565:i1634],spc[i1565:i1634]/norm[i1565])
    par = [sum(norm2[i1000:i1077]-fit0_fn(wvl[i1000:i1077])),   # 1 curvature of rad normed to 1000 nm for 1000 nm - 1077 nm
           dsp[i1198],                                          # 2 deriv of rad normed to 1000 nm at 1198 nm (!=IDL version)
           dsp[i1493],                                          # 3 deriv of rad normed to 1000 nm at 1493 nm
           norm[i1198]/norm[i1236],                             # 4 ratio of normalized rad of 1198 nm / 1236 nm
           np.nanmean(norm[i1248:i1270]),                       # 5 mean of normalized rad between 1248 nm - 1270 nm
           np.nanmean(norm[i1565:i1644]),                       # 6 mean of normalized rad between 1565 nm - 1644 nm
           np.nanmean(norm[i1000:i1050]),                       # 7 mean of normalized rad between 1000 nm - 1050 nm
           sum(norm2[i1493:i1600]-fit7_fn(wvl[i1493:i1600])),   # 8 curvature of rad normed to 1000 nm for 1493 nm - 1600 nm
           fit8[0],                                             # 9 slope of deriv of rad normed to 1000 nm, 1000 nm - 1077 nm
           fit9[0],                                             # 10 slope of deriv of rad normed to 1000 nm, 1200 nm - 1300 nm
           fit10[0],                                            # 11 slope of normalized radiance between 530 nm - 610 nm
           norm[i1040],                                         # 12 normalized radiance at 1040 nm
           norm[i1000]/norm[i1065],                             # 13 ratio of normalized radiance at 1000 nm / 1065 nm
           norm[i600]/norm[i870],                               # 14 ratio of normalized radiance at 600 nm / 870 nm
           np.nanmin([0.003,fit14[0]]),                         # 15 slope of radiance / rad at 1565 between 1565 nm - 1634 nm
           spc[i515]]                                           # 16 radiance at 515 nm
    # do a check for bad points
    if np.all(np.isnan(par[0:13])): 
        par[14] = np.nan
        par[15] = np.nan
    return par
def build_lut_2wvl(model,wvls,sza_norm):
    " function that builds the 2 wavelength Reflectance lut from the model Sp object (lut) "
    import numpy as np
    import math
    from Sp_parameters import find_closest
    iw = find_closest(model.wvl,np.array(wvls))
    if hasattr(model,'sza') & sza_norm:
        sza_factor = 1.0/math.cos(math.radians(model.sza))
    else:
        sza_factor = 1.0
    return np.reshape(model.reflect[:,iw,1,:,:],(2,2,model.ref.size,model.tau.size))*sza_factor #ph, wvl, z, ref, tau
Example #3
0
def nearest_neighbor(X, Y, Xnew, dist=1):
    """
    Purpose:
        To return a nearest neighbor linear interpolation, but with a limit on the possible distance between the two points
    
    Input:
        X: initial independent variable
        Y: initial dependant variable
        Xnew: new independant variable to interpolate over
        dist: max distance allowed
                       
    Output:
        Ynew: new dependent variable interpolate using nearest neighbor
    
    Keywords: 
        dist: (default 1) see above
        
    Dependencies:
        Numpy
        Sp_parameters
        
    Example:
        ...
        
    Modification History:
        Written: Samuel LeBlanc, NASA Ames, Santa Cruz, 2016-04-04
    """
    from Sp_parameters import find_closest
    import numpy as np
    i = find_closest(X, Xnew)
    Ynew = Y[i]
    i_bad = abs(X[i] - Xnew) > dist
    try:
        Ynew[i_bad] = np.nan
    except ValueError:
        YYnew = Ynew.astype('float64')
        YYnew[i_bad] = np.nan
        Ynew = YYnew
    return Ynew
def nearest_neighbor(X,Y,Xnew,dist=1):
    """
    Purpose:
        To return a nearest neighbor linear interpolation, but with a limit on the possible distance between the two points
    
    Input:
        X: initial independent variable
        Y: initial dependant variable
        Xnew: new independant variable to interpolate over
        dist: max distance allowed
                       
    Output:
        Ynew: new dependent variable interpolate using nearest neighbor
    
    Keywords: 
        dist: (default 1) see above
        
    Dependencies:
        Numpy
        Sp_parameters
        
    Example:
        ...
        
    Modification History:
        Written: Samuel LeBlanc, NASA Ames, Santa Cruz, 2016-04-04
    """
    from Sp_parameters import find_closest
    import numpy as np
    i = find_closest(X,Xnew)
    Ynew = Y[i]
    i_bad = abs(X[i]-Xnew) > dist
    try:
        Ynew[i_bad] = np.nan
    except ValueError:
        YYnew = Ynew.astype('float64')
        YYnew[i_bad] = np.nan
        Ynew = YYnew
    return Ynew   
star['good'] = np.where((star['utc']>20.0667) & 
                        (star['utc']<20.4667) & 
                        (star['Str'].flatten()!=0) & 
                        (star['sat_time'].flatten()==0) & 
                        (np.isfinite(star['tau_aero'][:,319])))[0]
len(star['good'])


# ## Plot time traces at select wavelengths

# In[7]:

print star['tau_aero'].shape
print star['w'].shape
iw = find_closest(star['w'][0],np.array([380,400,430,500,515,635,875,1000,1140,1200,1600])/1000.0)
print star['w'][0].shape
print iw


# In[8]:

plt.figure()
color.cycle_cmap(len(iw)+1,cmap=plt.cm.gist_ncar)
for i in iw:
    plt.plot(star['utc'][star['good']],star['tau_aero'][star['good'],i], label=str('%.3f $\mu$m' % star['w'][0,i]))
plt.xlabel('UTC [h]')
plt.ylabel('AOD')
plt.legend(bbox_to_anchor=[1,0.1],loc=3)
plt.savefig(fp+datestr+filesep+'AOD_time.png',dpi=600)
ax4[1].set_xlabel('Parameters')
ax4[1].legend()

# <markdowncell>

# Create the desired arrays which are used in creating the parameters

# <codecell>

s_0_5_6 = lut.sp[0,:,0,5,6]
s,n = nanmasked(s_0_5_6)
snorm = norm2max(s_0_5_6)
[i1000,i1077,i1493,i1600,i1200,i1300,i530,i610,
 i1565,i1634,i1193,i1198,i1236,i1248,i1270,i1644,
 i1050,i1040,i1065,i600,i870,i515] = find_closest(lut.wvl,np.array([1000,1077,1493,1600,1200,1300,530,
                                                     610,1565,1634,1193,1198,1236,1248,
                                                     1270,1644,1050,1040,1065,600,870,515]))
norm2 = s_0_5_6/s_0_5_6[i1000]
dsp = smooth(np.gradient(norm2,lut.wvl/1000.),2)

# <codecell>

norm2_uni = UnivariateSpline(lut.wvl/1000.0,norm2,k=5)
norm2_uni.set_smoothing_factor(1)
dnorm2 = norm2_uni.derivative()

# <codecell>

norm2_bspline = splrep(lut.wvl/1000.0,norm2,k=5)
norm2_b = splev(lut.wvl/1000.0,norm2_bspline,der=0)
dbnorm2 = splev(lut.wvl/1000.0,norm2_bspline,der=1)