In [1]:
%matplotlib inline
import sys
import time
from datetime import datetime
sys.path.append('..')
from netCDF4 import Dataset
import pylab as pl
import numpy as np
import scipy.interpolate as interp
%matplotlib inline



def cosd(inn):
    return np.cos(inn*np.pi/180.)
def sind(inn):
    return np.sin(inn*np.pi/180.)
def acosd(inn):
    return np.arccos(inn)*180./np.pi
def asind(inn):
    return np.arcsin(inn)*180./np.pi

def azi2azid(sa,va):
    return acosd(cosd(sa)*cosd(va)+sind(sa)*sind(va))

def height2press(hh):
        return 1013.*(1.-(hh*0.0065/288.15))**5.2555
def histeq(im,nbr_bins=256):
    imhist,bins = np.histogram(im.flatten(),nbr_bins,normed=True)
    cdf = imhist.cumsum() #cumulative distribution function
    cdf = 255 * cdf / cdf[-1] #normalize
    im2 = np.interp(im.flatten(),bins[:-1],cdf)
    return im2.reshape(im.shape)




def get_altitude(ff,st=(1,1)):
    with Dataset('%s/geo_coordinates.nc'%ff) as ds:
        alt=ds.variables['altitude'][::st[0],::st[1]]*1.
    return alt

def interpolate_tie(inn,al,ac):
    npa=np.arange
    npl=np.linspace
    sh_in=inn.shape
    sh_ou=((sh_in[0]-1)*al+1,(sh_in[1]-1)*ac+1)
    ou=interp.RectBivariateSpline(
          npl(0.,sh_ou[0],sh_in[0])
        , npl(0.,sh_ou[1],sh_in[1])
        , inn, kx=1, ky=1)(npa(sh_ou[0])
                         , npa(sh_ou[1]))
    return ou

def get_pressure(ff,st=(1,1)):
    with Dataset('%s/tie_meteo.nc'%ff) as ds:
        lat=ds.variables['sea_level_pressure'][:]*1.
        ac=ds.ac_subsampling_factor
        al=ds.al_subsampling_factor
    return interpolate_tie(lat,al,ac)[::st[0],::st[1]]

def get_latitude(ff,st=(1,1)):
    ## assumes FR dataset
    with Dataset('%s/tie_geo_coordinates.nc'%ff) as ds:
        lat=ds.variables['latitude'][:]*1.
        ac=ds.ac_subsampling_factor
        al=ds.al_subsampling_factor
    return interpolate_tie(lat,al,ac)[::st[0],::st[1]]

def get_longitude(ff,st=(1,1)):
    ## assumes FR dataset
    with Dataset('%s/tie_geo_coordinates.nc'%ff) as ds:
        lon=ds.variables['longitude'][:]*1.
        ac=ds.ac_subsampling_factor
        al=ds.al_subsampling_factor
    re=np.sin(lon*np.pi/180.)
    im=np.cos(lon*np.pi/180.)
    re_out=interpolate_tie(re,al,ac)[::st[0],::st[1]]
    im_out=interpolate_tie(im,al,ac)[::st[0],::st[1]]
    return np.arctan2(re_out,im_out)*180./np.pi

def get_geometry(ff,st=(1,1)):
    out={}
    with Dataset('%s/tie_geometries.nc'%ff) as ds:
        ac=ds.ac_subsampling_factor
        al=ds.al_subsampling_factor
        out={k: ds.variables[k][:]*1. for k in ds.variables}
    for k in out:
        out[k]=interpolate_tie(out[k],al,ac)[::st[0],::st[1]]
    out['ADA']=azi2azid(out['SAA'],out['OAA'])    
    return out

def get_cwl(ff,bn,st=(1,1)):
    with Dataset('%s/instrument_data.nc'%ff) as ds:
        l0=(ds.variables['lambda0'][bn-1,:]*1.).squeeze()
        di=ds.variables['detector_index'][:]*1
    return l0[di][::st[0],::st[1]]  

def get_solar_flux(ff,bn,st=(1,1)):
    with Dataset('%s/instrument_data.nc'%ff) as ds:
        sf=(ds.variables['solar_flux'][bn-1,:]*1.).squeeze()
        di=ds.variables['detector_index'][:]*1
    return sf[di][::st[0],::st[1]]    

def get_albedo(lat,lon,doy):
    # this is meris white sky surface albedo of band 10 
    # from albedomap year 2005
    # better data e.g. from CAWA aerosol is welcome :)
    lat_idx=np.round((90. -lat)*20).astype(np.int).clip(0,3599)
    lon_idx=np.round((180.+lon)*20).astype(np.int).clip(0,7199)

    with Dataset('../luts/ws_alb_10_2005.nc') as nc:
        #get closest day of year
        doy_idx=np.abs(nc.variables['time'][:]-doy).argmin()
        alb = nc.variables['albedo'][doy_idx,:,:]
        #get closest albedo
        #nearest neighbour
    return alb[lat_idx,lon_idx].clip(0,1.)
def get_doy(ff):
    with Dataset('%s/time_coordinates.nc'%ff) as ds:
        doy=datetime.strptime(ds.start_time,"%Y-%m-%dT%H:%M:%S.%fZ").timetuple().tm_yday
    return doy
        


def get_band(ff,bn,st=(1,1)):
    variable='Oa%02i_radiance'%bn
    with Dataset('%s/%s.nc'%(ff,variable)) as ds:
        out=ds.variables[variable][:]*1.    
    return out[::st[0],::st[1]]
olci='S3A_OL_1_EFR____20160531T093141_20160531T093441_20160531T113517_0179_004_364_2160_MAR_O_NR_001.SEN3/'
#pl.imshow(get_band(olci,21))
#pl.imshow(get_cwl(olci,21))
#pl.imshow(get_longitude(olci,))
#pl.imshow(get_geometry(olci)['ADA'])
#pl.imshow(get_altitude(olci).clip(-2500,2500))
#pl.imshow(get_albedo(get_latitude(olci,(20,20)),get_longitude(olci,(20,20))))
#pl.imshow(get_longitude(olci))
#get_albedo(get_latitude(olci,(20,20)),get_longitude(olci,(20,20)))
#pl.imshow(np.fromfile('ws_albedo_10.img',dtype=np.float32).reshape(3600,7200).byteswap())
#pl.colorbar()
get_doy(olci)


Out[1]:
152

In [24]:
stride=(4,4)

rad={k:get_band(olci,k,stride)/get_solar_flux(olci,k,stride) for k in [2,4,6,12,13,14,15,]}
geo=get_geometry(olci,stride)
lam=get_cwl(olci,13,stride)
lon=get_longitude(olci,stride)
lat=get_latitude(olci,stride)
doy=get_doy(olci)
alb=get_albedo(lat,lon,doy)
prs=height2press(get_altitude(olci,stride))

In [25]:
for xxx in (lam,lon,lat,alb,prs):
    print xxx.shape,
print
for xxx in rad:
    print rad[xxx].shape,
print
for xxx in geo:
    print geo[xxx].shape,


(1023, 1217) (1023, 1217) (1023, 1217) (1023, 1217) (1023, 1217)
(1023, 1217) (1023, 1217) (1023, 1217) (1023, 1217) (1023, 1217) (1023, 1217) (1023, 1217)
(1023, 1217) (1023, 1217) (1023, 1217) (1023, 1217) (1023, 1217)

In [26]:
rgb=np.dstack((rad[6],rad[4],rad[2]))
rgb.shape
pl.imshow(rgb/0.3)

pl.imshow(alb)


Out[26]:
<matplotlib.image.AxesImage at 0x7f9171b37850>

In [27]:
import cloud_complete_core 
reload(cloud_complete_core)
cc=cloud_complete_core.cloud_core('../luts/cloud_complete_core_olci.nc4')#,used_ab='13')

nnn=rad[12].size
sha=rad[12].shape
oe_ctp=np.zeros(sha)*np.nan
oe_cot=np.zeros(sha)*np.nan
oe_exi=np.zeros(sha)*np.nan
oe_moi=np.zeros(sha)*np.nan
oe_cnv=np.zeros(sha).astype(np.bool)
oe_avk=np.zeros(sha)+1.
oe_cst=np.zeros(sha)+1.
print time.asctime()
a=time.time()
print nnn
jl=0
jw=0l
for i in range(0,nnn):
    inp={'suz':geo['SZA'].flat[i]
        ,'vie':geo['OZA'].flat[i]
        ,'azi':180. - geo['ADA'].flat[i]   # momo definition
        ,'prs':prs.flat[i]    # not yet used!!!!
        ,'dwl':lam.flat[i]-cc.cha['13']['cwvl']    # first abs-band
        ,'alb':alb.flat[i]    # last win-band
        ,'rtoa':{'12':rad[12].flat[i]
                ,'13':rad[13].flat[i]
                ,'14':rad[14].flat[i]
                ,'15':rad[15].flat[i]
                }
        }

    erg=cc.estimator(inp)
    oe_ctp.flat[i]=erg['ctp']
    oe_cot.flat[i]=erg['cot']
    oe_exi.flat[i]=erg['exi']
    oe_moi.flat[i]=erg['moi']
    oe_cnv.flat[i]=erg['res'].conv        
    oe_avk.flat[i]=erg['res'].a.trace()
    oe_cst.flat[i]=erg['res'].sr[0,0]
    jl+=1
            
    if i % 10000 ==0: print i,    
print ''
print time.asctime()


Thu Nov 10 16:59:52 2016
1244991
0 10000 20000 30000 40000 50000 60000 70000 80000 90000 100000 110000 120000 130000 140000 150000 160000 170000 180000 190000 200000 210000 220000 230000 240000 250000 260000 270000 280000 290000 300000 310000 320000 330000 340000 350000 360000 370000 380000 390000 400000 410000 420000 430000 440000 450000 460000 470000 480000 490000 500000 510000 520000 530000 540000 550000 560000 570000 580000 590000 600000 610000 620000 630000 640000 650000 660000 670000 680000 690000 700000 710000 720000 730000 740000 750000 760000 770000 780000 790000 800000 810000 820000 830000 840000 850000 860000 870000 880000 890000 900000 910000 920000 930000 940000 950000 960000 970000 980000 990000 1000000 1010000 1020000 1030000 1040000 1050000 1060000 1070000 1080000 1090000 1100000 1110000 1120000 1130000 1140000 1150000 1160000 1170000 1180000 1190000 1200000 1210000 1220000 1230000 1240000 
Thu Nov 10 17:20:50 2016

In [49]:
fac=0.5
si=32
oe_cot_t=10**oe_cot
ccot=0.9
pl.figure(figsize=(si*fac,si*fac*4))
pl.subplot(5,1,1)
pl.imshow(np.where(oe_cot>ccot,oe_ctp,np.nan))
pl.colorbar()

pl.subplot(5,1,2)
pl.imshow(np.where(oe_cot > ccot,oe_moi,np.nan))#,vmin=0.4,vmax=0.55)
pl.colorbar()

pl.subplot(5,1,3)
pl.imshow(np.where(oe_cot > ccot,oe_exi,np.nan))#,vmin=0.,vmax=0.2)
pl.colorbar()

pl.subplot(5,1,4)
pl.imshow(np.where(oe_cot > ccot,oe_cot,np.nan))#,vmin=0.,vmax=0.2)
pl.colorbar()


pl.subplot(5,1,5)
#rrr=np.dstack((histeq(rad[6]),rad[4],rad[2]))
pl.imshow(rad[12],cmap='gray')


-c:19: RuntimeWarning: invalid value encountered in greater
Out[49]:
<matplotlib.image.AxesImage at 0x7f91928377d0>

In [50]:
pl.figure(figsize=(32*1.5,24*1.5))
pl.imshow(np.where(oe_cot>0.7,oe_ctp,np.nan))#,aspect='auto')
#pl.imshow(oe_ctp ,aspect='auto')
#pl.colorbar()
pl.axis('off')
pl.savefig('/home/rene/Downloads/first_olci_ctp_noaxes.png',bbox_inches='tight', pad_inches=0)


-c:2: RuntimeWarning: invalid value encountered in greater
Out[50]:
(-0.5, 1216.5, 1022.5, -0.5)

In [51]:
import fubimage
reload(fubimage)
import matplotlib
cmap = matplotlib.cm.get_cmap()
cmap.set_bad('gray')
vmin=200.
vmax=1000.
cmap.set_over('grey')
cmap.set_under('pink',0.)
#pl.figure(figsize=(32/1.5,24/1.5))
fubimage.fubimage(oe_ctp,lon,lat,no_data_mask=oe_cot<0.8,cmap=cmap,vmin=vmin,vmax=vmax
                  ,colorbar=True,resolution='h',bartitle='ctp [hPa]', drawmapscale=True,dpi=350)
#pl.savefig('/home/rene/Downloads/olci_ctp_ctp_cb.png')


-c:11: RuntimeWarning: invalid value encountered in less

In [53]:
import fubimage
reload(fubimage)
import matplotlib
cmap = matplotlib.cm.get_cmap()
cmap.set_bad('gray')
vmin=100.
vmax=1000.
cmap.set_over('grey')
cmap.set_under('pink',0.)
pl.figure(figsize=(32/1.5,24/1.5))
fubimage.fubimage(oe_ctp,lon,lat,no_data_mask=oe_cot<0.8,cmap=cmap,vmin=vmin,vmax=vmax
                  ,colorbar=False,resolution='h',bartitle='log$_{10}$cot [1]', drawmapscale=True,dpi=350)
#pl.savefig('/home/rene/Downloads/olci_ctp_cot.png')



In [55]:
pl.figure(figsize=(32/1.5,24/1.5))
rrr=histeq(np.where(rad[6]>1,0.,rad[6]))
ggg=histeq(np.where(rad[4]>1,0.,rad[4]))
bbb=histeq(np.where(rad[2]>1,0.,rad[2]))
rgb=np.array((rrr,ggg,bbb))

#pl.imshow(rgb/0.1)
#pl.imshow(np.where(oe_cnv,oe_wsp,np.nan),vmax=15)
#pl.colorbar()
import fubimage
reload(fubimage)
fubimage.fubimage(rgb/rgb.max(),lon,lat,vmin=0,dpi=350)
#pl.savefig('/home/rene/Downloads/olci_ctp_rgb.png')



In [130]:
pl.figure(figsize=(32*1.5,24*1.5))
pl.imshow(rgb*3.,aspect='auto')
#pl.imshow(oe_ctp ,aspect='auto')
#pl.colorbar()

pl.savefig('/home/rene/Downloads/first_olci_rgb.png')



In [131]:
data={
    'lon'  :{'data':lon.astype(np.float32) ,
             'attributes':  {
                            'long_name':'longitude',
                            'standard_name': 'longitude',
                            'units':'degrees_east'
                            }
            },
    'lat'  :{'data':lat.astype(np.float32),
             'attributes':  {
                            'long_name':'latitude',
                            'standard_name': 'latitude',
                            'units':'degrees_north'
                            }
            },
    'ctp':{'data':oe_ctp.astype(np.float32),
             'attributes':  {
                            'long_name':'cloud top pressure',
                            'units':'hPa',
                            }
            },
     'cot':{'data':oe_cot.astype(np.float32),
             'attributes':  {
                            'long_name':'loh_10 of cloud optical thickness @750 nm',
                            'units':'1',
                            }
            }
     }       

from dict2beamncdf import dict2beamncdf as d2n
ret=d2n(data,'demo_olci_erg.nc')

In [11]:
10**0.8


Out[11]:
6.309573444801933

In [11]: