示例#1
0
def distance2coast(dst_domain, distance_src=None):
    """ Estimate distance to the nearest coast (in km) for each pixcel in the
    domain of interest. The method utilizes NASA's OBPG group Distance to the Nearest Coast
    product: https://oceancolor.gsfc.nasa.gov/docs/distfromcoast/. The product is stored in GeoTiff
    format with pixcelsize of 0.01x0.01 degree.

    Parameters
    -----------
    dst_domain : Domain
        destination domain
    distance_src : str
        path to the NASA Distance to the Nearest coast GeoTIFF product

    Returns
    --------
    distance : Nansat object with distance to the coast mask in current projection

    See Also
    ---------
    `<https://oceancolor.gsfc.nasa.gov/docs/distfromcoast/>`_
    `<http://nansat.readthedocs.io/en/latest/source/features.html#differentiating-between-land-and-water>`

    """
    # Get path to the auxilary dataset predefined in enviromental variable
    if distance_src is None:
        distance_src = os.getenv('DIST2COAST')
    # If path to the distance data source was not specified or directly provided raise an error
    if distance_src is None or not os.path.exists(distance_src):
        raise IOError('Distance to the nearest coast product does not exist - see Nansat '
                      'documentation to get it (the path is % s)' % distance_src)
    distance = Nansat(distance_src)
    # Reproject the source file on the domain of interest
    distance.reproject(dst_domain, addmask=False)
    return distance
示例#2
0
    def _get_masked_windspeed(self, landmask=True, icemask=True, windspeedBand="windspeed"):
        try:
            sar_windspeed = self[windspeedBand]
        except:
            raise ValueError("SAR wind has not been calculated, " "execute calculate_wind(wind_direction) first.")

        sar_windspeed[sar_windspeed < 0] = 0
        palette = jet

        if landmask:
            try:  # Land mask
                sar_windspeed = np.ma.masked_where(self.watermask()[1] == 2, sar_windspeed)
                palette.set_bad([0.3, 0.3, 0.3], 1.0)  # Land is masked (bad)
            except:
                print "Land mask not available"

        if icemask:
            try:  # Ice mask
                try:  # first try local file
                    ice = Nansat(
                        "metno_local_hires_seaice_" + self.SAR_image_time.strftime("%Y%m%d"),
                        mapperName="metno_local_hires_seaice",
                    )
                except:  # otherwise Thredds
                    ice = Nansat("metno_hires_seaice:" + self.SAR_image_time.strftime("%Y%m%d"))
                ice.reproject(self)
                iceBandNo = ice._get_band_number({"standard_name": "sea_ice_area_fraction"})
                sar_windspeed[ice[iceBandNo] > 0] = -1
                palette.set_under("w", 1.0)  # Ice is 'under' (-1)
            except:
                print "Ice mask not available"

        return sar_windspeed, palette
 def test_reproject_ecmwf_to_SAR(self):
     sar = Nansat(self.s1aEW)
     wind = Nansat(self.test_file_ecmwf, netcdf_dim={'time':
         np.datetime64(sar.time_coverage_start)},
         bands=['y_wind','x_wind'])
     self.assertTrue(wind['x_wind_10m'].any())
     self.assertTrue(wind['y_wind_10m'].any())
     wind.reproject(sar, addmask=False)
     self.assertTrue(wind['x_wind_10m'].any())
     self.assertTrue(wind['y_wind_10m'].any())
 def test_reproject_ecmwf_to_SAR(self):
     sar = Nansat(self.s1aEW)
     wind = Nansat(
         self.test_file_ecmwf,
         netcdf_dim={'time': np.datetime64(sar.time_coverage_start)},
         bands=['y_wind', 'x_wind'])
     self.assertTrue(wind['x_wind_10m'].any())
     self.assertTrue(wind['y_wind_10m'].any())
     wind.reproject(sar, addmask=False)
     self.assertTrue(wind['x_wind_10m'].any())
     self.assertTrue(wind['y_wind_10m'].any())
示例#5
0
 def test_nansat_reproject(self):
     if len(self.test_data.asar)==0:
         raise IOError('No ASAR data - try adding some as ' \
                 'described in templates/openwind_local_archive.py' )
     asar = Nansat(self.test_data.asar[0])
     asar.resize(pixelsize=500, eResampleAlg=1)
     mw = Nansat(self.test_data.ncep4asar[0])
     mw.reproject(asar, eResampleAlg=1)
     if sys.version_info < (2, 7):
         type(mw[1]) == np.ndarray
     else:
         self.assertIsInstance(mw[1], np.ndarray)
示例#6
0
 def test_nansat_reproject(self):
     if len(self.test_data.asar) == 0:
         raise IOError('No ASAR data - try adding some as ' \
                 'described in templates/openwind_local_archive.py' )
     asar = Nansat(self.test_data.asar['agulhas'])
     asar.resize(pixelsize=500, eResampleAlg=1)
     mw = Nansat(self.test_data.ncep4asar['agulhas'])
     mw.reproject(asar, eResampleAlg=1)
     if sys.version_info < (2, 7):
         type(mw[1]) == np.ndarray
     else:
         self.assertIsInstance(mw[1], np.ndarray)
示例#7
0
    def _get_masked_windspeed(self,
                              landmask=True,
                              icemask=True,
                              windspeedBand='windspeed'):
        try:
            sar_windspeed = self[windspeedBand]
        except:
            raise ValueError('SAR wind has not been calculated, ' \
                'execute calculate_wind(wind_direction) first.')

        sar_windspeed[sar_windspeed < 0] = 0
        palette = jet

        if landmask:
            try:  # Land mask
                sar_windspeed = np.ma.masked_where(
                    self.watermask(tps=True)[1] == 2, sar_windspeed)
                palette.set_bad([.3, .3, .3], 1.0)  # Land is masked (bad)
            except:
                print 'Land mask not available'

        if icemask:
            try:  # Ice mask
                try:  # first try local file
                    ice = Nansat('metno_local_hires_seaice_' +
                                 self.SAR_image_time.strftime('%Y%m%d'),
                                 mapperName='metno_local_hires_seaice')
                except:  # otherwise Thredds
                    ice = Nansat('metno_hires_seaice:' +
                                 self.SAR_image_time.strftime('%Y%m%d'))
                ice.reproject(self, tps=True)
                iceBandNo = ice._get_band_number(
                    {'standard_name': 'sea_ice_area_fraction'})
                sar_windspeed[ice[iceBandNo] > 0] = -1
                palette.set_under('w', 1.0)  # Ice is 'under' (-1)
            except:
                print 'Ice mask not available'

        return sar_windspeed, palette
示例#8
0
    def _get_masked_windspeed(self, landmask=True, icemask=True,
            windspeedBand='windspeed'):
        try:
            sar_windspeed = self[windspeedBand]
        except:
            raise ValueError('SAR wind has not been calculated, ' \
                'execute calculate_wind(wind_direction) first.')

        sar_windspeed[sar_windspeed<0] = 0
        palette = jet

        if landmask:
            try: # Land mask
                sar_windspeed = np.ma.masked_where(
                                    self.watermask()[1]==2, sar_windspeed)
                palette.set_bad([.3, .3, .3], 1.0) # Land is masked (bad)
            except:
                print 'Land mask not available'

        if icemask:
            try: # Ice mask
                try: # first try local file
                    ice = Nansat('metno_local_hires_seaice_' +
                            self.SAR_image_time.strftime('%Y%m%d'),
                            mapperName='metno_local_hires_seaice')
                except: # otherwise Thredds
                    ice = Nansat('metno_hires_seaice:' +
                            self.SAR_image_time.strftime('%Y%m%d'))
                ice.reproject(self)
                iceBandNo = ice._get_band_number(
                    {'standard_name': 'sea_ice_area_fraction'})
                sar_windspeed[ice[iceBandNo]>0] = -1
                palette.set_under('w', 1.0) # Ice is 'under' (-1)
            except:
                print 'Ice mask not available'

        return sar_windspeed, palette
def mean_gc_geostrophic(datetime_start=timezone.datetime(2010,1,1,
    tzinfo=timezone.utc), datetime_end=timezone.datetime(2010,2,1,
    tzinfo=timezone.utc), domain=Domain(NSR().wkt, 
        '-te 10 -44 40 -30 -tr 0.05 0.05')):
    #gc_datasets = Dataset.objects.filter(entry_title__contains='globcurrent',
    #                time_coverage_start__range=[datetime_start,
    #                datetime_end])
    shapeD = domain.shape()
    U = np.zeros((shapeD[0], shapeD[1], 1))
    fn = 'http://tds0.ifremer.fr/thredds/dodsC/CLS-L4-CURGEO_0M-ALT_OI_025-V02.0_FULL_TIME_SERIE'
    dt = datetime_start
    while dt <= datetime_end:
        expFn = '/vagrant/shared/test_data/globcurrent/eastward_geostrophic_current_velocity_%d-%02d-%02d.nc'%(dt.year, dt.month, dt.day)
        #n = Nansat(
        #    fn, date='%d-%02d-%02d'%(dt.year, dt.month, dt.day),
        #    bands=['eastward_geostrophic_current_velocity'])
        #n.export(expFn)
        n = Nansat(expFn, mapper='generic')
        n.reproject(domain, addmask=False)
        u = n['eastward_geostrophic_current_velocity']
        # OK:
        #plt.imshow(u)
        #plt.colorbar()
        #plt.show()
        if np.sum(np.isnan(u))==u.size:
            continue
        else:
            U = np.append(U, np.expand_dims(u, axis=2), axis=2)
        dt = dt + timezone.timedelta(days=1)
    meanU = np.nanmean(U, axis=2)
    nu = Nansat(array=meanU, domain=domain)
    nmap=Nansatmap(nu, resolution='h')
    nmap.pcolormesh(nu[1], vmin=-1.5, vmax=1.5, cmap='jet') #bwr
    nmap.add_colorbar()
    nmap.draw_continents()
    nmap.fig.savefig('/vagrant/shared/u_gc.png', bbox_inches='tight')
示例#10
0
    def __init__(self, filename, doppler_file='', *args, **kwargs):
        # TODO: What is a reason?
        super(BayesianWind, self).__init__(filename, *args, **kwargs)
        # TODO: separate calculations between U and V

        # Set apriori (0 step) distribution of the wind field
        u_apriori, v_apriori = np.meshgrid(self.wind_speed_range,
                                           self.wind_speed_range)
        direction_apriori = 180. / np.pi * np.arctan2(
            u_apriori, v_apriori)  # 0 is wind towards North
        speed_apriori = np.sqrt(np.square(u_apriori) + np.square(v_apriori))

        # Get Nansat object of the model wind field
        # model_wind = self.get_source_wind(reprojected=False) # where did this
        # function go? anyway, the below should be equally fine..
        model_wind = Nansat(self.get_metadata('WIND_DIRECTION_SOURCE'))

        # TODO: Move to separate function
        if doppler_file:
            # Get Nansat object of the range Doppler shift
            dop = Nansat(doppler_file)
            # Estimate Doppler uncertainty
            fdg = dop['dop_coef_observed'] - dop['dop_coef_predicted'] - \
                  dop['range_bias_scene'] - dop['azibias']
            fdg[fdg > 100] = np.nan
            fdg[fdg < -100] = np.nan
            mask = np.isnan(fdg)
            fdg[mask] = np.interp(np.flatnonzero(mask), np.flatnonzero(~mask),
                                  fdg[~mask])
            err_fdg = grid_based_uncertainty(fdg, 2)
            err_fdg[err_fdg < self.doppler_err] = self.doppler_err
            dop.add_band(array=err_fdg, parameters={'name': 'err_fdg'})
            dop.reproject(self, eResampleAlg=self.resample_alg, tps=True)
            fdg = dop['dop_coef_observed'] - dop['dop_coef_predicted'] - \
                  dop['range_bias_scene'] - dop['azibias']

            err_fdg = dop['err_fdg']
            # fdg_err = dop['range_bias_std_scene'] - this is not the uncertainty...

        # Estimate sigma0 uncertainty
        s0 = self['sigma0_VV']
        err_s0 = self.s0_err_fac * s0
        #    #mask = np.isnan(fdg)
        #    #fdg[mask] = np.interp(np.flatnonzero(mask), np.flatnonzero(~mask),
        #    #        fdg[~mask])
        #err_s0 = grid_based_uncertainty(s0,2)
        #import ipdb
        #ipdb.set_trace()
        #err_s0[err_s0<self.s0_err_fac*s0] = self.s0_err_fac*s0[err_s0<self.s0_err_fac*s0]

        # Estimate model wind uncertainty (leads to adjustment near fronts)
        #model_px_resolution = int(np.round( 2 * model_wind.get_pixelsize_meters()[0] /
        #        self.get_pixelsize_meters()[0] ))
        uu = model_wind['U']
        vv = model_wind['V']
        err_u = grid_based_uncertainty(uu, 2)
        err_v = grid_based_uncertainty(vv, 2)

        model_wind.add_band(array=err_u, parameters={'name': 'err_u'})
        model_wind.add_band(array=err_v, parameters={'name': 'err_v'})

        # Reproject to SAR image
        model_wind.reproject(self, eResampleAlg=self.resample_alg, tps=True)

        # Get uncertainties in model wind
        err_u = model_wind['err_u']
        # Without the below, uncertainties are lower in uniform areas - this
        # should be quite reasonable...
        #err_u[err_u<self.model_err] = self.model_err
        err_v = model_wind['err_v']
        #err_v[err_v<self.model_err] = self.model_err

        # Assign shape of SAR image to variable imshape
        imshape = self.shape()

        # Initialize result arrays
        ub_modcmod = np.ones(imshape)
        vb_modcmod = np.ones(imshape)
        ub_all = np.ones(imshape)
        vb_all = np.ones(imshape)

        self.has_doppler = np.zeros(imshape)

        model_u = model_wind['U']
        model_v = model_wind['V']
        sar_look = self[self._get_band_number(
            {'standard_name': 'sensor_azimuth_angle'})]
        inci = self['incidence_angle']

        # TODO: speed up processing
        print('Applying Bayesian on one-by-one pixel')
        for i in range(imshape[0]):
            print 'Row %d of %d' % (i + 1, imshape[0])
            for j in range(imshape[1]):
                # There seems to be a problem with Radarsat-2 incidence angles
                # after resize (nan-values and erroneous resampling)
                # THIS IS NOT YET IN ANY GITHUB ISSUES...
                if np.isnan(inci[i, j]) or inci[i, j] < 0 or s0[i, j] == 0.0:
                    ub_modcmod[i, j] = np.nan
                    vb_modcmod[i, j] = np.nan
                    ub_all[i, j] = np.nan
                    vb_all[i, j] = np.nan
                    continue

                # Calculate model cost functions
                cost_model_u = cost_function(u_apriori, model_u[i, j],
                                             err_u[i, j])
                cost_model_v = cost_function(v_apriori, model_v[i, j],
                                             err_v[i, j])

                # Calculate sigma0 cost function
                cmod_s0 = cmod5n_forward(
                    speed_apriori, direction_apriori - sar_look[i, j],
                    np.ones(np.shape(speed_apriori)) * inci[i, j])
                cost_sigma0 = cost_function(cmod_s0, s0[i, j], err_s0[i, j])

                cost = cost_model_v + cost_model_u + cost_sigma0
                ind_min = np.where(cost == np.min(cost, axis=None))
                ub_modcmod[i, j] = u_apriori[ind_min]
                vb_modcmod[i, j] = v_apriori[ind_min]
                # TODO: Simplify comparison
                if (doppler_file and fdg[i, j] > -100 and fdg[i, j] < 100
                        and err_fdg[i, j] != 0
                        and not np.isnan(err_fdg[i, j])):
                    # Calculate Doppler cost function
                    self.has_doppler[i, j] = 1
                    cdop_fdg = cdop(
                        speed_apriori, sar_look[i, j] - direction_apriori,
                        np.ones(np.shape(speed_apriori)) * inci[i, j], 'VV')
                    cost_doppler = cost_function(cdop_fdg, fdg[i, j],
                                                 err_fdg[i, j])
                    cost += cost_doppler

                    ind_min = np.where(cost == np.min(cost, axis=None))
                    ub_all[i, j] = u_apriori[ind_min]
                    vb_all[i, j] = v_apriori[ind_min]

                # Should give uncertainties as well
                # self.rms_u[i,j] = err_u[i,j] + err_v[i,j] + ...

        self.add_band(
            array=np.sqrt(np.square(ub_modcmod) + np.square(vb_modcmod)),
            parameters={
                'wkv': 'wind_speed',
                'name': 'bspeed_modcmod',
                'long_name': 'Bayesian wind speed using model and cmod data'
            })

        self.add_band(array=np.mod(
            180. + 180. / np.pi * np.arctan2(ub_modcmod, vb_modcmod), 360),
                      parameters={
                          'wkv':
                          'wind_from_direction',
                          'name':
                          'bdir_modcmod',
                          'long_name':
                          'Bayesian wind direction using model and cmod data'
                      })

        if doppler_file:
            self.add_band(array=np.sqrt(np.square(ub_all) + np.square(vb_all)),
                          parameters={
                              'wkv': 'wind_speed',
                              'name': 'bspeed_all',
                              'long_name': 'Bayesian wind speed using all data'
                          })

            self.add_band(array=np.mod(
                180. + 180. / np.pi * np.arctan2(ub_all, vb_all), 360),
                          parameters={
                              'wkv': 'wind_from_direction',
                              'name': 'bdir_all',
                              'long_name':
                              'Bayesian wind direction using all data'
                          })
示例#11
0
import sys, os
home = os.path.expanduser("~")

import numpy as np
import matplotlib.pyplot as plt

from nansat.nansatmap import Nansatmap
from nansat.nansat import Nansat, Domain


n = Nansat(os.path.join(home,
        'conferences/ESABigData2014/demo_data/MER_RR__1PRACR20110222_020119_000025973099_00391_46957_0000.N1'))

d = Domain(4326, '-lle 118 28 132 40 -ts 1000 800')

n.reproject(d)

w = Nansat(os.path.join(home,
        'conferences/ESABigData2014/demo_data/gfs20110222/gfs.t00z.master.grbf03'))

w.reproject(d)

L_560 = n['L_560']

L_560[L_560>90] = np.nan

u = w['U']

v = w['V']

nMap = Nansatmap(n, resolution='f')
示例#12
0
    def __init__(self, filename, doppler_file='', *args, **kwargs):

        super(BayesianWind, self).__init__(filename, *args, **kwargs)

        [u_apriori, v_apriori] = np.meshgrid(self.wind_speed_range,
                self.wind_speed_range)
        direction_apriori = 180./np.pi*np.arctan2(u_apriori, v_apriori) # 0 is wind towards North
        speed_apriori = np.sqrt(np.square(u_apriori) + np.square(v_apriori))

        # Get Nansat object of the model wind field
        #model_wind = self.get_source_wind(reprojected=False) # where did this
        # function go? anyway, the below should be equally fine..
        model_wind = Nansat(self.get_metadata('WIND_DIRECTION_SOURCE'))

        if doppler_file:
            # Get Nansat object of the range Doppler shift
            dop = Nansat(doppler_file)
            # Estimate Doppler uncertainty
            fdg = dop['dop_coef_observed'] - dop['dop_coef_predicted'] - \
                    dop['range_bias_scene'] - dop['azibias']
            fdg[fdg>100] = np.nan
            fdg[fdg<-100] = np.nan
            mask = np.isnan(fdg)
            fdg[mask] = np.interp(np.flatnonzero(mask), np.flatnonzero(~mask),
                    fdg[~mask])
            err_fdg = grid_based_uncertainty(fdg,2)
            err_fdg[err_fdg<self.doppler_err]=self.doppler_err
            dop.add_band(array=err_fdg, parameters={'name':'err_fdg'})
            dop.reproject(self, eResampleAlg=self.resample_alg, tps=True)
            fdg = dop['dop_coef_observed'] - dop['dop_coef_predicted'] - \
                    dop['range_bias_scene'] - dop['azibias']
            err_fdg = dop['err_fdg']
            #fdg_err = dop['range_bias_std_scene'] - this is not the uncertainty...
        
        # Estimate sigma0 uncertainty
        s0 = self['sigma0_VV']
        err_s0 = self.s0_err_fac*s0
        #    #mask = np.isnan(fdg)
        #    #fdg[mask] = np.interp(np.flatnonzero(mask), np.flatnonzero(~mask),
        #    #        fdg[~mask])
        #err_s0 = grid_based_uncertainty(s0,2)
        #import ipdb
        #ipdb.set_trace()
        #err_s0[err_s0<self.s0_err_fac*s0] = self.s0_err_fac*s0[err_s0<self.s0_err_fac*s0]

        # Estimate model wind uncertainty (leads to adjustment near fronts)
        #model_px_resolution = int(np.round( 2 * model_wind.get_pixelsize_meters()[0] /
        #        self.get_pixelsize_meters()[0] ))
        uu = model_wind['U']
        vv = model_wind['V']
        err_u = grid_based_uncertainty(uu,2)
        err_v = grid_based_uncertainty(vv,2)

        model_wind.add_band(array=err_u, parameters={'name':'err_u'})
        model_wind.add_band(array=err_v, parameters={'name':'err_v'})

        # Reproject to SAR image
        model_wind.reproject(self, eResampleAlg=self.resample_alg, tps=True)

        # Get uncertainties in model wind  
        err_u = model_wind['err_u']
        # Without the below, uncertainties are lower in uniform areas - this
        # should be quite reasonable...
        #err_u[err_u<self.model_err] = self.model_err
        err_v = model_wind['err_v']
        #err_v[err_v<self.model_err] = self.model_err

        # Assign shape of SAR image to variable imshape
        imshape = self.shape()

        # Initialize result arrays
        ub_modcmod = np.ones(imshape)
        vb_modcmod = np.ones(imshape)
        ub_all = np.ones(imshape)
        vb_all = np.ones(imshape)

        self.has_doppler = np.zeros(imshape)

        model_u = model_wind['U']
        model_v = model_wind['V']
        sar_look = self[self._get_band_number({'standard_name':
                'sensor_azimuth_angle'})]
        inci = self['incidence_angle']
        print 'Applying Bayesian on one-by-one pixel'
        for i in range(imshape[0]):
            print 'Row %d of %d'%(i+1,imshape[0])
            for j in range(imshape[1]):
                # There seems to be a problem with Radarsat-2 incidence angles
                # after resize (nan-values and erroneous resampling)
                # THIS IS NOT YET IN ANY GITHUB ISSUES...
                if np.isnan(inci[i,j]) or inci[i,j]<0 or s0[i,j]==0.0:
                    ub_modcmod[i,j] = np.nan
                    vb_modcmod[i,j] = np.nan
                    ub_all[i,j] = np.nan
                    vb_all[i,j] = np.nan
                    continue

                # Calculate model cost functions
                cost_model_u = cost_function(u_apriori, model_u[i,j], err_u[i,j])
                cost_model_v = cost_function(v_apriori, model_v[i,j], err_v[i,j])

                # Calculate sigma0 cost function
                cmod_s0 = cmod5n_forward(speed_apriori,
                        direction_apriori-sar_look[i,j],
                        np.ones(np.shape(speed_apriori))*inci[i,j])
                cost_sigma0 = cost_function( cmod_s0, s0[i,j], err_s0[i,j] )

                cost = cost_model_v + cost_model_u + cost_sigma0
                ind_min = np.where(cost==np.min(cost,axis=None))
                ub_modcmod[i,j] = u_apriori[ind_min]
                vb_modcmod[i,j] = v_apriori[ind_min]

                if (doppler_file and 
                        fdg[i,j]>-100 and 
                        fdg[i,j]<100 and 
                        err_fdg[i,j]!=0 and
                        not np.isnan(err_fdg[i,j])):
                    # Calculate Doppler cost function
                    self.has_doppler[i,j] = 1
                    cdop_fdg = cdop(speed_apriori, 
                        sar_look[i,j]-direction_apriori,
                        np.ones(np.shape(speed_apriori))*inci[i,j], 'VV')
                    cost_doppler = cost_function(cdop_fdg, fdg[i,j], err_fdg[i,j])
                    cost += cost_doppler

                    ind_min = np.where(cost==np.min(cost,axis=None))
                    ub_all[i,j] = u_apriori[ind_min]
                    vb_all[i,j] = v_apriori[ind_min]


                # Should give uncertainties as well
                #self.rms_u[i,j] = err_u[i,j] + err_v[i,j] + ...

        self.add_band(
            array = np.sqrt(np.square(ub_modcmod) + np.square(vb_modcmod)),
            parameters={
                'wkv': 'wind_speed',
                'name':'bspeed_modcmod',
                'long_name': 'Bayesian wind speed using model and ' \
                        'cmod data'}
            )
        self.add_band(
            array = np.mod(180. + 180./np.pi*np.arctan2(ub_modcmod,
                vb_modcmod), 360),
            parameters = {
                'wkv': 'wind_from_direction',
                'name': 'bdir_modcmod',
                'long_name': 'Bayesian wind direction using model and ' \
                        'cmod data'}
            )
        if doppler_file:
            self.add_band(
                array = np.sqrt(np.square(ub_all) + np.square(vb_all)),
                parameters={
                    'wkv': 'wind_speed',
                    'name':'bspeed_all',
                    'long_name': 'Bayesian wind speed using all data'}
                )
            self.add_band(
                array = np.mod(180. + 180./np.pi*np.arctan2(ub_all,
                    vb_all), 360),
                parameters = {
                    'wkv': 'wind_from_direction',
                    'name': 'bdir_all',
                    'long_name': 'Bayesian wind direction using all data'}
                )
    def get_or_create(self, uri, reprocess=False, *args, **kwargs):
        # ingest file to db
        ds, created = super(DatasetManager,
                            self).get_or_create(uri, *args, **kwargs)

        # set Dataset entry_title
        ds.entry_title = 'SAR NRCS'
        ds.save()

        # Unless reprocess==True, we may not need to do the following... (see
        # managers.py in sar doppler processor)
        #visExists = ... # check if visualization(s) already created
        #if visExists and not reprocess:
        #    warnings.warn('NO VISUALISATIONS CREATED - update managers.py')
        #    return ds, created

        n = Nansat(nansat_filename(uri))
        n.reproject_GCPs()
        n.resize(pixelsize=500)
        lon, lat = n.get_corners()
        lat_max = min(lat.max(), 85)
        d = Domain(
            NSR(3857), '-lle %f %f %f %f -ts %d %d' %
            (lon.min(), lat.min(), lon.max(), lat_max, n.shape()[1],
             n.shape()[0]))
        # Get all NRCS bands
        s0bands = []
        pp = []
        for key, value in n.bands().iteritems():
            try:
                if value['standard_name'] == standard_name:
                    s0bands.append(key)
                    pp.append(value['polarization'])
            except KeyError:
                continue
        ''' Create data products
        '''
        mm = self.__module__.split('.')
        module = '%s.%s' % (mm[0], mm[1])
        mp = media_path(module, n.fileName)
        # ppath = product_path(module, n.fileName)

        # Create png's for each band
        num_products = 0
        for band in s0bands:
            print 'Visualize', band
            s0_tmp = n[band]
            n_tmp = Nansat(domain=n, array=s0_tmp)
            n_tmp.reproject_GCPs()
            n_tmp.reproject(d)

            s0 = n_tmp[1]
            n_tmp = None
            mask = np.ones(s0.shape, np.uint8)
            mask[np.isnan(s0) + (s0 <= 0)] = 0
            s0 = np.log10(s0) * 10.

            meta = n.bands()[band]
            product_filename = '%s_%s.png' % (meta['short_name'],
                                              meta['polarization'])

            nansatFigure(s0, mask, polarization_clims[meta['polarization']][0],
                         polarization_clims[meta['polarization']][1], mp,
                         product_filename)

            # Get DatasetParameter
            param = Parameter.objects.get(short_name=meta['short_name'])
            dsp, created = DatasetParameter.objects.get_or_create(
                dataset=ds, parameter=param)

            # Create Visualization
            geom, created = GeographicLocation.objects.get_or_create(
                geometry=WKTReader().read(n.get_border_wkt()))
            vv, created = Visualization.objects.get_or_create(
                uri='file://localhost%s/%s' % (mp, product_filename),
                title='%s %s polarization' %
                (param.standard_name, meta['polarization']),
                geographic_location=geom)

            # Create VisualizationParameter
            vp, created = VisualizationParameter.objects.get_or_create(
                visualization=vv, ds_parameter=dsp)

        return ds, True
示例#14
0
    def process(self, uri, force=False, *args, **kwargs):
        fn = 'WIND_'+os.path.basename(uri)
        if DatasetURI.objects.filter(uri__contains=fn) and not force:
            wds = Dataset.objects.filter(dataseturi__uri__contains=fn)[0]
            return wds, False

        try:
            w = wind_from_sar_and_arome_forecast(uri)
        except (TooHighResolutionError, PolarizationError, ObjectDoesNotExist) as e:
            if type(e)==Dataset.DoesNotExist:
                warnings.warn(uri+': '+e.args[0])
            else:
                # ObjectDoesNotExist could happen if there is no overlap between SAR and model
                warnings.warn(e.file + ': ' + e.msg)
            return None, False

        metadata = w.get_metadata()

        # Direct reprojection fails - gdal can't read the bands if we do w.reproject...
        # Workaround: Export wind to temporary file
        #tmp_filename = os.path.join(settings.PRODUCTS_ROOT,'TMP_WIND_'+os.path.basename(uri))
        fd, tmp_filename = tempfile.mkstemp(suffix='.nc')
        os.close(fd) # Just in case - see https://www.logilab.org/blogentry/17873
        w.export(tmp_filename)

        # Read temporary file
        ww = Nansat(tmp_filename)

        # Reproject
        lon, lat = ww.get_geolocation_grids()
        #lon, lat = w.get_geolocation_grids()
        srs = '+proj=stere +datum=WGS84 +ellps=WGS84 +lat_0=%.2f +lon_0=%.2f +no_defs'%(np.mean(lat),np.mean(lon))
        xmin, xmax, ymin, ymax = -haversine(np.mean(lon),np.mean(lat),np.min(lon),np.mean(lat)), \
                    haversine(np.mean(lon),np.mean(lat),np.max(lon),np.mean(lat)), \
                    -haversine(np.mean(lon),np.mean(lat),np.mean(lon),np.min(lat)), \
                    haversine(np.mean(lon),np.mean(lat),np.mean(lon),np.max(lat))
        ext = '-te %d %d %d %d -tr 500 500' %(xmin-10000, ymin-10000, xmax+10000, ymax+10000)
        d = Domain(srs, ext)
        ww.reproject(d, tps=True)
        #w.reproject(d, tps=True)

        # Set global metadata
        metadata['data_center'] = json.dumps(pti.get_gcmd_provider(kwargs.pop('data_center', '')))
        metadata['naming_authority'] = kwargs.pop('naming_authority', '')
        metadata['project'] = 'SIOS InfraNor'
        metadata['entry_title'] = 'Wind field from '+os.path.basename(uri)
        metadata.pop('file_creation_date')
        metadata['history'] = metadata['history'] + ' ' + timezone.now().isoformat() + \
                '. Calculated wind field from NRCS and Arome Arctic forecast wind directions.'
        metadata.pop('institution')
        metadata['keywords'] += ', ['
        for key, value in pti.get_gcmd_science_keyword('U/V WIND COMPONENTS').items():
            if value:
                metadata['keywords'] += value + ', '
        metadata['keywords'] += ']'
        metadata.pop('LINE_SPACING')
        metadata.pop('PIXEL_SPACING')
        metadata['summary'] = 'Near surface (10m) wind from Arome Arctic forecast wind and ' + metadata['summary']
        metadata['title'] = 'Near surface wind from '+metadata['title']

	# Get or create data folder
        start_time = parse(metadata['time_coverage_start'])
        yfolder = os.path.join(settings.PRODUCTS_ROOT, '{:04d}'.format(start_time.year))
        mfolder = os.path.join(yfolder, '{:02d}'.format(start_time.month))
        dfolder = os.path.join(mfolder, '{:02d}'.format(start_time.day))
        if not os.path.isdir(yfolder):
            os.mkdir(yfolder)
        if not os.path.isdir(mfolder):
            os.mkdir(mfolder)
        if not os.path.isdir(dfolder):
            os.mkdir(dfolder)

        # Export
        thredds_fn = os.path.join(dfolder, fn)
        wind_uri = 'file://localhost' + thredds_fn
        ww.export2thredds(thredds_fn, mask_name='swathmask', metadata=metadata, no_mask_value=1)
        wds, cr = super(WindManager, self).get_or_create(wind_uri)
        
        os.unlink(tmp_filename)

        return wds, cr
    def get_or_create(self, uri, reprocess=False, *args, **kwargs):
        # ingest file to db
        ds, created = super(DatasetManager,
                            self).get_or_create(uri, *args, **kwargs)

        fn = nansat_filename(uri)

        n = Nansat(fn)

        # Reproject to leaflet projection
        xlon, xlat = n.get_corners()
        d = Domain(
            NSR(3857), '-lle %f %f %f %f -tr 1000 1000' %
            (xlon.min(), xlat.min(), xlon.max(), xlat.max()))
        n.reproject(d)

        # Get band numbers of required bands according to standard names
        speedBandNum = n._get_band_number({'standard_name': 'wind_speed'})
        dirBandNum = n._get_band_number(
            {'standard_name': 'wind_from_direction'})

        # Get numpy arrays of the bands
        speed = n[speedBandNum]
        dir = n[dirBandNum]

        ## It probably wont work with nansatmap...
        #nmap = Nansatmap(n, resolution='l')
        #nmap.pcolormesh(speed, vmax=18)
        #nmap.quiver(-speed*np.sin(dir), speed*np.cos(dir), step=10, scale=300,
        #        width=0.002)

        # Set paths - this code should be inherited but I think there is an
        # issue in generalising the first line that defines the current module
        mm = self.__module__.split('.')
        module = '%s.%s' % (mm[0], mm[1])
        mp = media_path(module, n.fileName)
        ppath = product_path(module, n.fileName)

        filename = os.path.basename(n.fileName).split('.')[0] + '.' + \
                os.path.basename(n.fileName).split('.')[1] + '.png'

        # check uniqueness of parameter
        param1 = Parameter.objects.get(standard_name=n.get_metadata(
            bandID=speedBandNum, key='standard_name'))
        param2 = Parameter.objects.get(standard_name=n.get_metadata(
            bandID=dirBandNum, key='standard_name'))

        n.write_figure(os.path.join(mp, filename),
                       bands=speedBandNum,
                       mask_array=n['swathmask'],
                       mask_lut={0: [128, 128, 128]},
                       transparency=[128, 128, 128])

        # Get DatasetParameter
        dsp1, created = DatasetParameter.objects.get_or_create(
            dataset=ds, parameter=param1)

        # Create Visualization
        geom, created = GeographicLocation.objects.get_or_create(
            geometry=WKTReader().read(n.get_border_wkt()))
        vv, created = Visualization.objects.get_or_create(
            uri='file://localhost%s/%s' % (mp, filename),
            title='%s' % (param1.standard_name),
            geographic_location=geom)

        # Create VisualizationParameter
        vp, created = VisualizationParameter.objects.get_or_create(
            visualization=vv, ds_parameter=dsp1)

        return ds, True
示例#16
0
n = Nansat(iFileName)

# List bands and georeference of the object
print n

# Write picture with map of the file location
n.write_map('map.png')

# Write indexed picture with data from the first band
n.write_figure('rgb.png', clim='hist')

# Reproject input image onto map of Norwegian Coast
# 1. Create domain describing the desired map
# 2. Transform the original satellite image
# 3. Write the transfromed image into RGB picture
dLatlong = Domain("+proj=latlong +datum=WGS84 +ellps=WGS84 +no_defs",
                          "-te 27 70.2 31 71.5 -ts 2000 2000")
n.reproject(dLatlong)
n.write_figure('pro.png', bands=[1,2,3], clim=[0, 100])

# Export projected satelite image into NetCDF format
n.export('gcps_projected.nc')

# Collect values from interactively drawn transect
# 1. draw transect interactively
# 2. plot the values
values, lonlat, pixlinCoord =n.get_transect()
plt.plot(lonlat['shape0']['longitude'], values['1:L_645']['shape0'], '.-');plt.show()