示例#1
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
示例#2
0
    def _get_aux_wind_from_str(self, aux_wind_source, *args, **kwargs):
        try:
            wdir = int(aux_wind_source)
            wdir, wdir_time, wspeed = self._get_aux_wind_from_int(wdir)
        except ValueError:
            import nansat.nansat
            mnames = [
                key.replace('mapper_', '')
                for key in nansat.nansat.nansatMappers
            ]
            # check if aux_wind_source is like 'ncep_wind_online', i.e. only
            # mapper name is given. By adding the SAR image time stamp, we
            # can then get the data online
            if aux_wind_source in mnames:
                aux_wind_source = aux_wind_source + \
                        datetime.strftime(self.SAR_image_time, ':%Y%m%d%H%M')
            aux = Nansat(
                aux_wind_source,
                netcdf_dim={
                    'time': np.datetime64(self.SAR_image_time),
                    'height2': '10',  # height dimension used in AROME arctic
                    # datasets
                    'height3': '10',
                },
                bands=[  # CF standard names of desired bands
                    'x_wind',
                    'y_wind',  # or..:
                    'eastward_wind',
                    'northward_wind',
                ])
            # Set filename of source wind in metadata
            try:
                wind_u_bandNo = aux._get_band_number({
                    'standard_name':
                    'eastward_wind',
                })
            except OptionError:
                wind_u_bandNo = aux._get_band_number({
                    'standard_name': 'x_wind',
                })
            self.set_metadata(
                'WIND_DIRECTION_SOURCE',
                aux.get_metadata(bandID=wind_u_bandNo)['SourceFilename'])
            wdir, wdir_time, wspeed = self._get_wind_direction_array(
                aux, *args, **kwargs)

        return wdir, wdir_time, wspeed
示例#3
0
 def export_band(self, rsfile):
     sys.stderr.write("\nexport_band:" + rsfile + "\n")
     orig = Nansat(rsfile)
     ncfile = "test.nc"
     orig.export(ncfile, bands=[orig._get_band_number("incidence_angle")])
     copy = Nansat(ncfile)
     inc0 = orig["incidence_angle"]
     inc1 = copy["incidence_angle"]
     np.testing.assert_allclose(inc0, inc1)
     os.unlink(ncfile)
示例#4
0
 def export_band(self, rsfile):
     sys.stderr.write('\nexport_band:'+rsfile+'\n')
     orig = Nansat(rsfile)
     ncfile = 'test.nc'
     orig.export(ncfile, bands=[orig._get_band_number('incidence_angle')])
     copy = Nansat(ncfile)
     inc0 = orig['incidence_angle']
     inc1 = copy['incidence_angle']
     np.testing.assert_allclose(inc0, inc1)
     os.unlink(ncfile)
示例#5
0
    def _get_aux_wind_from_str(self, aux_wind_source, *args, **kwargs):
        try:
            wdir = int(aux_wind_source)
            wdir, wdir_time, wspeed = self._get_aux_wind_from_int(wdir)
        except ValueError:
            import nansat.nansat
            mnames = [key.replace('mapper_','') for key in
                    nansat.nansat.nansatMappers]
            # check if aux_wind_source is like 'ncep_wind_online', i.e. only
            # mapper name is given. By adding the SAR image time stamp, we
            # can then get the data online
            if aux_wind_source in mnames:
                aux_wind_source = aux_wind_source + \
                        datetime.strftime(self.SAR_image_time, ':%Y%m%d%H%M')
            aux = Nansat(aux_wind_source, netcdf_dim={
                    'time': np.datetime64(self.SAR_image_time),
                    'height2': '10', # height dimension used in AROME arctic
                                     # datasets
                    'height3': '10',
                },
                bands = [ # CF standard names of desired bands
                    'x_wind',
                    'y_wind', # or..:
                    'eastward_wind',
                    'northward_wind',
                ])
            # Set filename of source wind in metadata
            try:
                wind_u_bandNo = aux._get_band_number({
                            'standard_name': 'eastward_wind',
                        })
            except OptionError:
                wind_u_bandNo = aux._get_band_number({
                            'standard_name': 'x_wind',
                        })
            self.set_metadata('WIND_DIRECTION_SOURCE',
                   aux.get_metadata(bandID=wind_u_bandNo)['SourceFilename'])
            wdir, wdir_time, wspeed = self._get_wind_direction_array(aux,
                                        *args, **kwargs)

        return wdir, wdir_time, wspeed
示例#6
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
示例#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()[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 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