Example #1
0
def make_reproject(path, final_path, file_name, show='off'):
    """
    Функция для перепроецирования снимков согласно параметрам указываемым в dom.
    После перепроецирования к файлу снимку генерируется и добавляется маска
    :param path: Путь до папки в которой лежит файл
    :param final_path: Путь до папки в которую нужно положить перепроецированный файл
    :param file_name: Имя файла (исходного)
    :param show: Флаг для отрисовки [2] канала. По умолчанию 'off', чтобы включить show='on'
    :return: Перепроецированный файл с иходным file_name
    """
    print path + file_name
    nansat_obj = Nansat(path + file_name)
    #  Для маленького конечного куска
    #dom = Domain('+proj=latlong +datum=WGS84 +ellps=WGS84 +no_defs', '-lle -86.20 45.10 -86.10 45.20 -ts 300 300')
    #   Для всего района
    dom = Domain('+proj=latlong +datum=WGS84 +ellps=WGS84 +no_defs', '-lle -86.3 44.6 -85.2 45.3 -ts 300 200')
    nansat_obj.reproject(dom)
    nansat_obj = create_mask(nansat_obj)

    if show == 'on':
        plt.imshow(nansat_obj[2])
        plt.colorbar()
        plt.show()

    nansat_obj.export(final_path + file_name + '.reproject.nc')
Example #2
0
def update_icemap_mosaic(inp_filename, inp_data, out_filename, out_domain,
                         out_metadata):
    if os.path.exists(out_filename):
        mos_array = Nansat(out_filename)[1]
    else:
        mos_array = np.zeros(out_domain.shape(), np.uint8) + 255

    # read classification data and reproject onto mosaic domain
    n = Nansat(inp_filename)
    if inp_data is None:
        n.reproject_gcps()
        n.reproject(out_domain)
        inp_data = dict(arr=n[1], mask=n[2])

    # put data into mosaic array
    gpi = (inp_data['mask'] == 1) * (inp_data['arr'] < 255)
    mos_array[gpi] = inp_data['arr'][gpi]

    # export
    n_out = Nansat.from_domain(out_domain)
    n_out.add_band(array=mos_array, parameters={'name': 'classification'})
    n_out.set_metadata(n.get_metadata())
    n_out.set_metadata(out_metadata)

    n_out = add_colortable(n_out)
    n_out.export(out_filename, driver='GTiff', options=['COMPRESS=LZW'])

    return inp_data
Example #3
0
    def test_reproject_domain_if_tps_is_given(self):
        n = Nansat(self.test_file_gcps,
                   log_level=40,
                   mapper=self.default_mapper)
        d = Domain(4326, "-te 27 70 30 72 -ts 500 500")
        n.reproject(d, tps=False)
        tmpfilename = os.path.join(self.tmp_data_path,
                                   'nansat_reproject_domain.png')
        n.write_figure(tmpfilename, 2, clim='hist')

        self.assertEqual(n.shape(), (500, 500))
        self.assertEqual(type(n[1]), np.ndarray)
        self.assertTrue(n.has_band('swathmask'))

        n = Nansat(self.test_file_gcps,
                   log_level=40,
                   mapper=self.default_mapper)
        d = Domain(4326, "-te 27 70 30 72 -ts 500 500")
        n.reproject(d, tps=True)
        tmpfilename = os.path.join(self.tmp_data_path,
                                   'nansat_reproject_domain.png')
        n.write_figure(tmpfilename, 2, clim='hist')

        self.assertEqual(n.shape(), (500, 500))
        self.assertEqual(type(n[1]), np.ndarray)
        self.assertTrue(n.has_band('swathmask'))
Example #4
0
def boreali_processing(obj,  final_path):
    wavelen = [412, 443, 469, 488, 531, 547, 555, 645, 667, 678]
    cpa_limits = [0.01, 2,
                  0.01, 1,
                  0.01, 1, 10]
    b = Boreali('michigan', wavelen)

    n = Nansat(obj)
    dom = Domain('+proj=latlong +datum=WGS84 +ellps=WGS84 +no_defs', '-lle -86.3 44.6 -85.2 45.3 -ts 300 200')
    n.reproject(dom)
    theta = numpy.zeros_like(n[2])
    custom_n = Nansat(domain=n)
    band_rrs_numbers = list(map(lambda x: n._get_band_number('Rrs_' + str(x)),
                                wavelen))

    for index in range(0, len(wavelen)):
        # Преобразуем в Rrsw
        rrsw = n[band_rrs_numbers[index]] / (0.52 + 1.7 * n[band_rrs_numbers[index]])
        custom_n.add_band(rrsw, parameters={'name': 'Rrsw_' + str(wavelen[index]),
                                            'units': 'sr-1',
                                            'wavelength': wavelen[index]})

    custom_n = create_mask(custom_n)
    cpa = b.process(custom_n, cpa_limits, mask=custom_n['mask'], theta=theta, threads=4)

    custom_n.add_band(array=cpa[0], parameters={'name': 'chl',
                                                'long_name': 'Chlorophyl-a',
                                                'units': 'mg m-3'})
    custom_n.add_band(array=cpa[1], parameters={'name': 'tsm',
                                                'long_name': 'Total suspended matter',
                                                'units': 'g m-3'})
    custom_n.add_band(array=cpa[2], parameters={'name': 'doc',
                                                'long_name': 'Dissolved organic carbon',
                                                'units': 'gC m-3'})
    custom_n.add_band(array=cpa[3], parameters={'name': 'mse',
                                                'long_name': 'Root Mean Square Error',
                                                'units': 'sr-1'})
    custom_n.add_band(array=cpa[4], parameters={'name': 'mask',
                                                'long_name': 'L2 Boreali mask',
                                                'units': '1'})

    custom_n.export(final_path + obj.split('/')[-1] + 'cpa_deep.nc')

    fig_params = {'legend': True,
                  'LEGEND_HEIGHT': 0.5,
                  'NAME_LOCATION_Y': 0,
                  'mask_array': cpa[4],
                  'mask_lut': {1: [255, 255, 255], 2: [128, 128, 128], 4: [200, 200, 255]}}
    custom_n.write_figure(final_path + obj.split('/')[-1] + 'chl_deep.png', 'chl', clim=[0, 1.], **fig_params)
    custom_n.write_figure(final_path + obj.split('/')[-1] + 'tsm_deep.png', 'tsm', clim=[0, 1.], **fig_params)
    custom_n.write_figure(final_path + obj.split('/')[-1] + 'doc_deep.png', 'doc', clim=[0, .2], **fig_params)
    custom_n.write_figure(final_path + obj.split('/')[-1] + 'mse_deep.png', 'mse', clim=[1e-5, 1e-2], logarithm=True, **fig_params)
    n.write_figure(final_path + obj.split('/')[-1] + 'rgb_deep.png',
                   [16, 14, 6],
                   clim=[[0, 0, 0], [0.006, 0.04, 0.024]],
                   mask_array=cpa[4],
                   mask_lut={2: [128, 128, 128]})
Example #5
0
    def test_reproject_no_addmask(self):
        """ Should not add swath mask and return 0 in areas out of swath """
        n = Nansat(self.test_file_gcps, log_level=40, mapper=self.default_mapper)
        d = Domain(4326, '-te -92.08 26.85 -92.00 26.91 -ts 200 200')
        n.reproject(d, addmask=False)
        b = n[1]

        self.assertTrue(not n.has_band('swathmask'))
        self.assertTrue(np.isfinite(b[0, 0]))
        self.assertTrue(np.isfinite(b[100, 100]))
Example #6
0
    def test_reproject_of_complex(self):
        ''' Should return np.nan in areas out of swath '''
        n = Nansat(self.test_file_complex, logLevel=40)
        d = Domain(4326, '-te -92.08 26.85 -92.00 26.91 -ts 200 200')
        n.reproject(d)
        b = n[1]

        self.assertTrue(n.has_band('swathmask'))
        self.assertTrue(np.isnan(b[0, 0]))
        self.assertTrue(np.isfinite(b[100, 100]))
Example #7
0
    def test_reproject_gcps(self):
        n1 = Nansat(self.test_file_stere, log_level=40, mapper=self.default_mapper)
        n2 = Nansat(self.test_file_gcps, log_level=40, mapper=self.default_mapper)
        n1.reproject(n2)
        tmpfilename = os.path.join(self.tmp_data_path,
                                   'nansat_reproject_gcps.png')
        n1.write_figure(tmpfilename, 2, clim='hist')

        self.assertEqual(n1.shape(), n2.shape())
        self.assertEqual(type(n1[1]), np.ndarray)
Example #8
0
    def test_reproject_gcps(self):
        n1 = Nansat(self.test_file_stere, logLevel=40)
        n2 = Nansat(self.test_file_gcps, logLevel=40)
        n1.reproject(n2)
        tmpfilename = os.path.join(ntd.tmp_data_path,
                                   'nansat_reproject_gcps.png')
        n1.write_figure(tmpfilename, 2, clim='hist')

        self.assertEqual(n1.shape(), n2.shape())
        self.assertEqual(type(n1[1]), np.ndarray)
Example #9
0
    def test_reproject_domain(self):
        n = Nansat(self.test_file_gcps, logLevel=40)
        d = Domain(4326, "-te 27 70 30 72 -ts 500 500")
        n.reproject(d)
        tmpfilename = os.path.join(ntd.tmp_data_path,
                                   'nansat_reproject_domain.png')
        n.write_figure(tmpfilename, 2, clim='hist')

        self.assertEqual(n.shape(), (500, 500))
        self.assertEqual(type(n[1]), np.ndarray)
Example #10
0
    def test_reproject_no_addmask(self):
        ''' Should not add swath mask and return 0 in areas out of swath '''
        n = Nansat(self.test_file_complex, logLevel=40)
        d = Domain(4326, '-te -92.08 26.85 -92.00 26.91 -ts 200 200')
        n.reproject(d, addmask=False)
        b = n[1]

        self.assertTrue(not n.has_band('swathmask'))
        self.assertTrue(np.isfinite(b[0, 0]))
        self.assertTrue(np.isfinite(b[100, 100]))
Example #11
0
    def test_reproject_domain_if_source_and_destination_domain_span_entire_lons(self, mock_Nansat):
        n = Nansat(self.test_file_arctic, log_level=40, mapper=self.default_mapper)
        d = Domain(4326, "-te -180 180 60 90 -ts 500 500")
        n.reproject(d)
        tmpfilename = os.path.join(self.tmp_data_path, 'nansat_reproject_domain.png')
        n.write_figure(tmpfilename, 2, clim='hist')

        self.assertEqual(n.shape(), (500, 500))
        self.assertEqual(type(n[1]), np.ndarray)
        self.assertTrue(n.has_band('swathmask'))
Example #12
0
    def test_reproject_of_complex(self):
        """ Should return np.nan in areas out of swath """
        n = Nansat(self.test_file_complex, log_level=40, mapper=self.default_mapper)
        d = Domain(4326, '-te -92.08 26.85 -92.00 26.91 -ts 200 200')
        n.reproject(d)
        b = n[1]

        self.assertTrue(n.has_band('swathmask'))
        self.assertTrue(np.isnan(b[0, 0]))
        self.assertTrue(np.isfinite(b[100, 100]))
Example #13
0
    def test_reproject_domain(self):
        n = Nansat(self.test_file_gcps, logLevel=40)
        d = Domain(4326, "-te 27 70 30 72 -ts 500 500")
        n.reproject(d)
        tmpfilename = os.path.join(ntd.tmp_data_path,
                                   'nansat_reproject_domain.png')
        n.write_figure(tmpfilename, 2, clim='hist')

        self.assertEqual(n.shape(), (500, 500))
        self.assertEqual(type(n[1]), np.ndarray)
Example #14
0
    def test_reproject_no_addmask(self):
        ''' Should not add swath mask and return 0 in areas out of swath '''
        n = Nansat(self.test_file_complex, logLevel=40)
        d = Domain(4326, '-te -92.08 26.85 -92.00 26.91 -ts 200 200')
        n.reproject(d, addmask=False)
        b = n[1]

        self.assertTrue(not n.has_band('swathmask'))
        self.assertTrue(np.isfinite(b[0, 0]))
        self.assertTrue(np.isfinite(b[100, 100]))
Example #15
0
    def test_reproject_and_export_band(self):
        n1 = Nansat(self.test_file_gcps, logLevel=40)
        n2 = Nansat(self.test_file_stere, logLevel=40)
        n1.reproject(n2)
        tmpfilename = os.path.join(ntd.tmp_data_path,
                                   'nansat_reproject_export_band.nc')
        n1.export(tmpfilename, bands=[1])

        n = Nansat(tmpfilename, mapperName='generic')
        self.assertTrue(os.path.exists(tmpfilename))
        self.assertEqual(n.vrt.dataset.RasterCount, 1)
Example #16
0
    def test_reproject_gcps_on_repro_gcps(self):
        n1 = Nansat(self.test_file_stere, logLevel=40)
        n2 = Nansat(self.test_file_gcps, logLevel=40)
        n2.reproject_GCPs()
        n1.reproject(n2)
        tmpfilename = os.path.join(ntd.tmp_data_path,
                                   'nansat_reproject_gcps_on_repro_gcps.png')
        n1.write_figure(tmpfilename, 2, clim='hist')

        self.assertEqual(n1.shape(), n2.shape())
        self.assertEqual(type(n1[1]), np.ndarray)
Example #17
0
    def test_reproject_and_export_band(self):
        n1 = Nansat(self.test_file_gcps, logLevel=40)
        n2 = Nansat(self.test_file_stere, logLevel=40)
        n1.reproject(n2)
        tmpfilename = os.path.join(ntd.tmp_data_path,
                                   'nansat_reproject_export_band.nc')
        n1.export(tmpfilename, bands=[1])

        n = Nansat(tmpfilename, mapperName='generic')
        self.assertTrue(os.path.exists(tmpfilename))
        self.assertEqual(n.vrt.dataset.RasterCount, 1)
Example #18
0
def get_deph(h_max=-1):

    data = Nansat('/home/artemm/Documents/Work/MichiganLake/TetsData/michigan_lld.grd')
    dom = Domain('+proj=latlong +datum=WGS84 +ellps=WGS84 +no_defs', '-lle -86.3 44.6 -85.2 45.3 -ts 300 200')
    data.reproject(dom)
    h = numpy.copy(data[1])

    if h_max == -1:
        h[numpy.where(h > numpy.float32(0.0))] = numpy.nan
        h[numpy.where(h < numpy.float32(0.0))] *= numpy.float32(-1)

    return h
Example #19
0
    def test_get_mask(self):
        '''Mosaic.Layer should get mask from reprojected file '''
        n = Nansat(self.test_file_gcps)
        n.reproject(self.domain)
        swathmask = n['swathmask']

        l = Layer(self.test_file_gcps)
        l.make_nansat_object(self.domain)
        mask = l.get_mask_array()

        self.assertEqual(type(mask), np.ndarray)
        self.assertEqual(mask.shape, (650, 700))
        np.testing.assert_allclose(mask, swathmask*64)
Example #20
0
    def test_get_mask(self):
        '''Mosaic.Layer should get mask from reprojected file '''
        n = Nansat(self.test_file_gcps)
        n.reproject(self.domain)
        swathmask = n['swathmask']

        l = Layer(self.test_file_gcps)
        l.make_nansat_object(self.domain)
        mask = l.get_mask_array()

        self.assertEqual(type(mask), np.ndarray)
        self.assertEqual(mask.shape, (650, 700))
        np.testing.assert_allclose(mask, swathmask * 64)
Example #21
0
    def test_add_band_and_reproject(self):
        """ Should add band and swath mask and return np.nan in areas out of swath """
        n = Nansat(self.test_file_gcps, log_level=40, mapper=self.default_mapper)
        d = Domain(4326, "-te 27 70 30 72 -ts 500 500")
        n.add_band(np.ones(n.shape(), np.uint8))
        n.reproject(d)
        b4 = n[4] # added, reprojected band
        b5 = n[5] # swathmask

        self.assertTrue(n.has_band('swathmask')) # the added band
        self.assertTrue(n.has_band('swathmask_0000')) # the actual swathmask
        self.assertTrue(b4[0, 0]==0)
        self.assertTrue(b4[300, 300] == 1)
        self.assertTrue(b5[0, 0]==0)
        self.assertTrue(b5[300, 300] == 1)
Example #22
0
    def test_add_band_and_reproject(self):
        """ Should add band and swath mask and return 0 in areas out of swath """
        n = Nansat(self.test_file_gcps, log_level=40, mapper=self.default_mapper)
        d = Domain(4326, "-te 27 70 30 72 -ts 500 500")
        n.add_band(np.ones(n.shape()))
        n.reproject(d)
        b1 = n[1]
        b4 = n[4]

        self.assertTrue(n.has_band('swathmask')) # the added band
        self.assertTrue(n.has_band('swathmask_0000')) # the actual swathmask
        self.assertTrue(b1[0, 0] == 0)
        self.assertTrue(b1[300, 300] > 0)
        self.assertTrue(np.isnan(b4[0, 0]))
        self.assertTrue(b4[300, 300] == 1.)
Example #23
0
    def test_add_band_and_reproject(self):
        ''' Should add band and swath mask
        and return 0 in areas out of swath '''
        n = Nansat(self.test_file_gcps, logLevel=40)
        d = Domain(4326, "-te 27 70 30 72 -ts 500 500")
        n.add_band(np.ones(n.shape()))
        n.reproject(d)
        b1 = n[1]
        b4 = n[4]

        self.assertTrue(n.has_band('swathmask'))
        self.assertTrue(b1[0, 0] == 0)
        self.assertTrue(b1[300, 300] > 0)
        self.assertTrue(np.isnan(b4[0, 0]))
        self.assertTrue(b4[300, 300] == 1.)
Example #24
0
    def test_add_band_and_reproject(self):
        ''' Should add band and swath mask
        and return 0 in areas out of swath '''
        n = Nansat(self.test_file_gcps, logLevel=40)
        d = Domain(4326, "-te 27 70 30 72 -ts 500 500")
        n.add_band(np.ones(n.shape()))
        n.reproject(d)
        b1 = n[1]
        b4 = n[4]

        self.assertTrue(n.has_band('swathmask'))
        self.assertTrue(b1[0, 0] == 0)
        self.assertTrue(b1[300, 300] > 0)
        self.assertTrue(np.isnan(b4[0, 0]))
        self.assertTrue(b4[300, 300] == 1.)
Example #25
0
    def _get_masked_windspeed(self, landmask=True, icemask=True):
        try:
            sar_windspeed = self['windspeed']
        except:
            raise ValueError('SAR wind has not been calculated, ' \
                'execute calculate_wind(winddir) 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
Example #26
0
    def _get_masked_windspeed(self, landmask=True, icemask=True):
        try:
            sar_windspeed = self['windspeed']
        except:
            raise ValueError('SAR wind has not been calculated, ' \
                'execute calculate_wind(winddir) 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
Example #27
0
from nansat import Nansat, Domain
import matplotlib.pyplot as plt
import numpy as np


n_osw = Nansat('/home/artemm/Desktop/A2014301181500.L2_LAC_OC.x.nccpa_OSW.nc')
n_deep = Nansat('/home/artemm/Desktop/A2014301181500.L2_LAC_OC.x.nccpa_deep.nc')
bottom = Nansat('/home/artemm/Desktop/michigan_lld.grd')

dom = Domain('+proj=latlong +datum=WGS84 +ellps=WGS84 +no_defs', '-lle -86.3 44.6 -85.2 45.3 -ts 300 200')
bottom.reproject(dom)
h = np.copy(bottom[1])
h[np.where(h > np.float32(0.0))] = np.nan
h[np.where(h < np.float32(0.0))] *= np.float32(-1)
"""
test = np.copy(n_osw['chl'])
test[np.where(h <= -20)] = np.nan
plt.imshow(test)
plt.show()
pass
"""

osw_h_mean = []
deep_h_mean = []
for h_max in range(1, np.nanmax(h), 1):
    osw = np.copy(n_osw['chl'])
    deep = np.copy(n_deep['chl'])
    osw[np.where(h >= h_max)] = np.nan
    deep[np.where(h >= h_max)] = np.nan
    osw_h_mean.append(np.nanmean(osw))
    deep_h_mean.append(np.nanmean(deep))
Example #28
0
def boreali_osw_processing(obj, final_path):
    """
    Мой код в данной функции основан на tutorial.py который я нашел в репозитории boreali.
    :param obj: путь до изображения
    :param final_path: Путь для сохранения файлов
    :return:
    """
    wavelen = [412, 443, 469, 488, 531, 547, 555, 645, 667, 678]

    cpa_limits = [0.01, 2,
                  0.01, 1,
                  0.01, 1, 10]

    h = get_deph()  # Глубина исследуемого района по батиметрии

    b = Boreali('michigan', wavelen)
    n = Nansat(obj)
    dom = Domain('+proj=latlong +datum=WGS84 +ellps=WGS84 +no_defs', '-lle -86.3 44.6 -85.2 45.3 -ts 300 200')
    n.reproject(dom)
    theta = numpy.zeros_like(n[2])

    custom_n = Nansat(domain=n)
    band_rrs_numbers = list(map(lambda x: n._get_band_number('Rrs_' + str(x)),
                                wavelen))   # Получаем список номеров бандов в которых лежат значения Rrs

    # для корректной работы складываем в custom_n значения и Rrs и Rrsw
    for index in range(0, len(wavelen)):
        rrsw = n[band_rrs_numbers[index]] / (0.52 + 1.7 * n[band_rrs_numbers[index]])   # Пересчитываем Rrs в Rrsw
        custom_n.add_band(rrsw, parameters={'name': 'Rrsw_' + str(wavelen[index]),  # Складываем в новый объект Rrsw
                                            'units': 'sr-1',
                                            'wavelength': wavelen[index]})
        # Складываем в новый объект значения Rrs
        custom_n.add_band(n[band_rrs_numbers[index]], parameters={'name': 'Rrs_' + str(wavelen[index]),
                                                                  'units': 'sr-1',
                                                                  'wavelength': wavelen[index]})

    custom_n = create_mask(custom_n)
    cpa = b.process(custom_n, cpa_limits,  mask=custom_n['mask'], depth=h, theta=theta, threads=4)

    custom_n.add_band(array=cpa[0], parameters={'name': 'chl',
                                                'long_name': 'Chlorophyl-a',
                                                'units': 'mg m-3'})
    custom_n.add_band(array=cpa[1], parameters={'name': 'tsm',
                                                'long_name': 'Total suspended matter',
                                                'units': 'g m-3'})
    custom_n.add_band(array=cpa[2], parameters={'name': 'doc',
                                                'long_name': 'Dissolved organic carbon',
                                                'units': 'gC m-3'})
    custom_n.add_band(array=cpa[3], parameters={'name': 'mse',
                                                'long_name': 'Root Mean Square Error',
                                                'units': 'sr-1'})
    custom_n.add_band(array=cpa[4], parameters={'name': 'mask',
                                                'long_name': 'L2 Boreali mask',
                                                'units': '1'})

    custom_n.export(final_path + obj.split('/')[-1] + 'cpa_OSW.nc')

    fig_params = {'legend': True,
                  'LEGEND_HEIGHT': 0.5,
                  'NAME_LOCATION_Y': 0,
                  'mask_array': cpa[4],
                  'mask_lut': {1: [255, 255, 255],
                               2: [128, 128, 128],
                               4: [200, 200, 255]}}
    custom_n.write_figure(final_path + obj.split('/')[-1] + 'chl_OSW.png', 'chl', clim=[0, 1.], **fig_params)
    custom_n.write_figure(final_path + obj.split('/')[-1] + 'tsm_OSW.png', 'tsm', clim=[0, 1.], **fig_params)
    custom_n.write_figure(final_path + obj.split('/')[-1] + 'doc_OSW.png', 'doc', clim=[0, .2], **fig_params)
    custom_n.write_figure(final_path + obj.split('/')[-1] + 'mse_OSW.png', 'mse', clim=[1e-5, 1e-2],
                          logarithm=True, **fig_params)
    n.write_figure(final_path + obj.split('/')[-1] + 'rgb_OSW.png',
                   [16, 14, 6],
                   clim=[[0, 0, 0], [0.006, 0.04, 0.024]],
                   mask_array=cpa[4],
                   mask_lut={2: [128, 128, 128]})
Example #29
0
#~ sigma0 = n[3]

sigma0 = raw_counts**2.0 * sin(deg2rad(inc_angle))
sigma0 = 10*log10(sigma0)
n.add_band(bandID=4, array=sigma0)

# 1. Remove speckle noise (using Lee-Wiener filter)
speckle_filter('wiener', 7)

# Reprojected image into Lat/Lon WGS84 (Simple Cylindrical) projection
# 1. Cancel previous reprojection
# 2. Get corners of the image and the pixel resolution
# 3. Create Domain with stereographic projection, corner coordinates and resolution 1000m
# 4. Reproject
# 5. Write image
n.reproject() # 1.
lons, lats = n.get_corners() # 2.
pxlRes = distancelib.getPixelResolution(array(lats), array(lons), n.shape(), units="deg")
srsString = "+proj=latlong +datum=WGS84 +ellps=WGS84 +no_defs"
#~ extentString = '-lle %f %f %f %f -ts 3000 3000' % (min(lons), min(lats), max(lons), max(lats))
extentString = '-lle %f %f %f %f -tr %f %f' % (min(lons), min(lats), \
                max(lons), max(lats), pxlRes[1], pxlRes[0])
d = Domain(srs=srsString, ext=extentString) # 3.
n.reproject(d) # 4.

# get array with watermask (landmask) b 
# it must be done after reprojection!
# 1. Get Nansat object with watermask
# 2. Get array from Nansat object. 0 - land, 1 - water
#wm = n.watermask(mod44path='/media/magDesk/media/SOLabNFS/store/auxdata/coastline/mod44w/')
wm = n.watermask(mod44path='/media/data/data/auxdata/coastline/mod44w/')
Example #30
0
n.write_geotiffimage(oFileName + '05_geotiff.tif', bandID=1)

# create a NetCDF file with all bands
n.export(oFileName + '06a.nc')
n.export(oFileName + '06b.nc', bottomup=True)

# create a GTiff file with one band (default driver is NetCDF)
n.export_band(oFileName + '07.tif', bandID=1, driver='GTiff')

# get array with watermask (landmask)
# -- Get Nansat object with watermask
wm = n.watermask()[1]

# -- Reproject with cubic interpolation
d = Domain(4326, "-te 27 70.3 31 71.5 -ts 300 300")
n.reproject(d, 2)
# -- Write image
n.write_figure(oFileName + '08_pro.png', clim='hist')

# Get transect of the 1st and 2nd bands corresponding to the given points
values, lonlat, pixlinCoord = n.get_transect(
                                    points=((29.287, 71.153),
                                            (29.275, 71.145),
                                            (29.210, 71.154)),
                                    transect=False,
                                    bandList=[1, 2])
# print the results
print '1stBandVal  2ndBandVal       pix/lin         lon/lat '
for i in range (len(values[0])):
    print '%6d %10d %13.2f /%6.2f  %7.2f /%6.2f' % (values[0][i],
                                                    values[1][i],
Example #31
0
# List bands and georeference of the object
print n

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

# Write indexed picture with data from the first band
n.write_figure(oFileName + '.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 500 500")
n.reproject(dLatlong)
n.write_figure(oFileName + 'pro.png', bands=[1, 2, 3], clim=[0, 100])

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

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

# run tests of other nansat components
import test_domain
import test_nansat
import test_figure
Example #32
0
class SARWind(Nansat, object):
    '''
    A class for calculating wind speed from SAR images using CMOD
    '''
    def __init__(self, sar_image, winddir=None, pixelsize=500):
        '''
            Parameters
            -----------
            sar_image : string or Nansat object
                SAR image filename (original, raw file)
            winddir : int, string, Nansat, None
                Auxiliary wind field information needed to calculate
                SAR wind (must be or have wind direction)
        '''
        if isinstance(sar_image, str) or isinstance(sar_image, unicode):
            super(SARWind, self).__init__(sar_image)
        elif isinstance(sar_image, Nansat):
            super(SARWind, self).__init__(domain=sar_image)
            self.vrt = sar_image.vrt

        # Check that this is a SAR image with VV pol NRCS
        try:
            self.sigma0_bandNo = self._get_band_number({
                'standard_name':
                'surface_backwards_scattering_coefficient_of_radar_wave',
                'polarization': 'VV'
            })
        except:
            raise TypeError(self.fileName +
                            ' does not have SAR NRCS in VV polarization')

        self.SAR_image_time = self.get_time(
            self.sigma0_bandNo).replace(tzinfo=None)

        if pixelsize != 'fullres':
            print 'Resizing SAR image to ' + str(pixelsize) + ' m pixel size'
            self.resize(pixelsize=pixelsize)

        self.winddir = winddir
        if winddir is not None:
            self.calculate_wind()

    def calculate_wind(self, winddir=None, storeModelSpeed=True):
        # Calculate wind speed from SAR sigma0 in VV polarization

        if winddir:
            self.winddir = winddir
        if self.winddir is None or self.winddir == 'online':
            self.winddir = 'ncep_wind_online'  # default source

        if isinstance(self.winddir, int):
            # Constant wind direction is input
            print 'Using constant wind (from) direction: ' + str(self.winddir) + \
                    ' degrees clockwise from North'
            winddirArray = np.ones(self.shape()) * self.winddir
            winddir_time = None
            storeModelSpeed = False  # Not relevant if direction given as number
        else:
            # Nansat readable file
            if isinstance(self.winddir, str):
                try:
                    self.winddir = Nansat(self.winddir)
                except:
                    try:
                        self.winddir = Nansat(self.winddir + datetime.strftime(
                            self.SAR_image_time, ':%Y%m%d%H%M'))
                    except:
                        pass

            if not isinstance(self.winddir, Nansat):
                raise ValueError('Wind direction not available')

            winddir_time = self.winddir.get_time()[0]

            # Bi-linear interpolation onto SAR image
            self.winddir.reproject(self, eResampleAlg=1)

            # Check time difference between SAR image and wind direction object
            timediff = self.SAR_image_time - winddir_time
            hoursDiff = np.abs(timediff.total_seconds() / 3600.)
            print 'Time difference between SAR image and wind direction: ' \
                    + '%.2f' % hoursDiff + ' hours'
            print 'SAR image time: ' + str(self.SAR_image_time)
            print 'Wind dir time: ' + str(winddir_time)
            if hoursDiff > 3:
                print '#########################################'
                print 'WARNING: time difference exceeds 3 hours!'
                print '#########################################'

            wind_u_bandNo = self.winddir._get_band_number({
                'standard_name':
                'eastward_wind',
            })
            wind_v_bandNo = self.winddir._get_band_number({
                'standard_name':
                'northward_wind',
            })
            # Get wind direction
            u_array = self.winddir[wind_u_bandNo]
            v_array = self.winddir[wind_v_bandNo]
            winddirArray = np.degrees(np.arctan2(
                -u_array, -v_array))  # 0 from North, 90 from East

        # Calculate SAR wind with CMOD
        # TODO:
        # - add other CMOD versions than CMOD5
        print 'Calculating SAR wind with CMOD...'
        startTime = datetime.now()

        windspeed = cmod5n_inverse(
            self[self.sigma0_bandNo],
            np.mod(winddirArray - self['SAR_look_direction'], 360),
            self['incidence_angle'])
        print 'Calculation time: ' + str(datetime.now() - startTime)

        windspeed[np.where(np.isnan(windspeed))] = np.nan
        windspeed[np.where(np.isinf(windspeed))] = np.nan

        # Add wind speed and direction as bands
        # TODO: make it possible to update existing bands... See
        # https://github.com/nansencenter/nansat/issues/58
        self.add_band(array=windspeed,
                      parameters={
                          'wkv': 'wind_speed',
                          'name': 'windspeed',
                          'time': self.get_time(self.sigma0_bandNo),
                          'winddir_time': winddir_time
                      })
        self.add_band(array=winddirArray,
                      parameters={
                          'wkv': 'wind_from_direction',
                          'name': 'winddirection',
                          'time': winddir_time
                      })

        if storeModelSpeed:
            self.add_band(array=self.winddir['windspeed'],
                          parameters={
                              'wkv': 'wind_speed',
                              'name': 'model_windspeed',
                              'time': winddir_time,
                          })

        # TODO: Replace U and V bands with pixelfunctions
        u = -windspeed * np.sin((180.0 - winddirArray) * np.pi / 180.0)
        v = windspeed * np.cos((180.0 - winddirArray) * np.pi / 180.0)
        self.add_band(array=u, parameters={
            'wkv': 'eastward_wind',
        })
        self.add_band(array=v, parameters={
            'wkv': 'northward_wind',
        })

    def _get_masked_windspeed(self, landmask=True, icemask=True):
        try:
            sar_windspeed = self['windspeed']
        except:
            raise ValueError('SAR wind has not been calculated, ' \
                'execute calculate_wind(winddir) 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 write_geotiff(self, filename, landmask=True, icemask=True):

        sar_windspeed, palette = self._get_masked_windspeed(landmask, icemask)

        nansat_geotiff = Nansat(array=sar_windspeed,
                                domain=self,
                                parameters={
                                    'name': 'masked_windspeed',
                                    'minmax': '0 20'
                                })

        nansat_geotiff.write_geotiffimage(filename)

    def plot(self,
             filename=None,
             numVectorsX=16,
             show=True,
             landmask=True,
             icemask=True,
             flip=True,
             maskWindAbove=35):
        ''' Basic plotting function showing CMOD wind speed
        overlaid vectors in SAR image projection'''

        try:
            sar_windspeed, palette = self._get_masked_windspeed(
                landmask, icemask)
        except:
            raise ValueError('SAR wind has not been calculated, ' \
                'execute calculate_wind(winddir) before plotting.')
        sar_windspeed[sar_windspeed > maskWindAbove] = np.nan

        winddirReductionFactor = np.round(self.vrt.dataset.RasterXSize /
                                          numVectorsX)
        # model_winddir is direction from which wind is blowing
        winddir_relative_up = 360 - self['winddirection'] + \
                                    self.azimuth_up()
        indX = range(0, self.vrt.dataset.RasterXSize, winddirReductionFactor)
        indY = range(0, self.vrt.dataset.RasterYSize, winddirReductionFactor)
        X, Y = np.meshgrid(indX, indY)
        try:  # scaling of wind vector length, if model wind is available
            model_windspeed = self['model_windspeed']
            model_windspeed = model_windspeed[Y, X]
        except:
            model_windspeed = 8 * np.ones(X.shape)

        Ux = np.sin(np.radians(winddir_relative_up[Y, X])) * model_windspeed
        Vx = np.cos(np.radians(winddir_relative_up[Y, X])) * model_windspeed

        # Make sure North is up, and east is right
        if flip == True:
            lon, lat = self.get_corners()
            if lat[0] < lat[1]:
                sar_windspeed = np.flipud(sar_windspeed)
                Ux = -np.flipud(Ux)
                Vx = -np.flipud(Vx)

            if lon[0] > lon[2]:
                sar_windspeed = np.fliplr(sar_windspeed)
                Ux = np.fliplr(Ux)
                Vx = np.fliplr(Vx)

        # Plotting
        figSize = sar_windspeed.shape
        legendPixels = 60.0
        legendPadPixels = 5.0
        legendFraction = legendPixels / figSize[0]
        legendPadFraction = legendPadPixels / figSize[0]
        dpi = 100.0

        fig = plt.figure()
        fig.set_size_inches(
            (figSize[1] / dpi,
             (figSize[0] / dpi) * (1 + legendFraction + legendPadFraction)))
        ax = fig.add_axes([0, 0, 1, 1 + legendFraction])
        ax.set_axis_off()
        plt.imshow(sar_windspeed, cmap=palette, interpolation='nearest')
        plt.clim([0, 20])
        cbar = plt.colorbar(orientation='horizontal',
                            shrink=.80,
                            aspect=40,
                            fraction=legendFraction,
                            pad=legendPadFraction)
        cbar.ax.set_ylabel('[m/s]', rotation=0)
        cbar.ax.yaxis.set_label_position('right')
        ax.quiver(X,
                  Y,
                  Ux,
                  Vx,
                  angles='xy',
                  width=0.004,
                  scale=200,
                  scale_units='width',
                  color=[.0, .0, .0],
                  headaxislength=4)
        if filename is not None:
            fig.savefig(filename, pad_inches=0, dpi=dpi)
        if show:
            plt.show()
        return fig
Example #33
0
def main( argv=None ):

    year = '2012'
    useMask = False

    if argv is None:
        argv = sys.argv

    if argv is None:
        print ( "Please specify the path/year to the asar folder! \n")
        return

    # Parse arguments
    try:
        opts, args = getopt.getopt(argv,"hi:o:",["year=","oPath=","iPath=","useMask="])
    except getopt.GetoptError:
        print 'readASAR.py -year <year> ...'
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print 'readASAR.py -year <year> ...'
            sys.exit()
        elif opt in ("-year", "--year"):
            year = arg
        elif opt in ("-oPath", "--oPath"):
            oPath = arg
        elif opt in ("-iPath", "--iPath"):
            iPath = arg
        elif opt in ("-useMask", "--useMask"):
            useMask = arg

    oPath = '/media/SOLabNFS2/tmp/roughness/' + year + '/'
    iPath = '/media/SOLabNFS2/store/satellite/asar/' + year + '/'

    if not os.path.exists(oPath):
        os.makedirs(oPath)

    dirNames=os.listdir(iPath)
    for dirName in dirNames:
        fileNames=os.listdir(iPath+dirName)
        for fileName in fileNames:
            figureName = oPath + fileName[0:27] + '/' + fileName + '_proj.png'
            kmlName = oPath + fileName[0:27] + '/' + fileName + '.kml'
            if not os.path.exists(oPath + fileName[0:27] + '/'):
                os.makedirs(oPath + fileName[0:27] + '/')

            if os.path.isfile(kmlName):
                print "%s already processed" % (fileName)
                continue
            else:
                print "%s" % (fileName)

            # try to create Nansat object
            try:
                n = Nansat(iPath + dirName + '/' + fileName, mapperName='asar', logLevel=27)
            except Exception as e:
                print "Failed to create Nansat object:"
                print str(e)
                os.rmdir(oPath + fileName[0:27] + '/' )
                continue
                

            #~ Get the bands
            raw_counts = n[1]
            inc_angle = n[2]

            #~ NICE image (roughness)
            pol = n.bands()[3]['polarization']
            if pol == 'HH':
                ph = (2.20495, -14.3561e-2, 11.28e-4)
                sigma0_hh_ref = exp( ( ph[0]+inc_angle*ph[1]+inc_angle**2*ph[2])*log(10) )
                roughness = n[3]/sigma0_hh_ref
            elif pol == 'VV':
                pv = (2.29373, -15.393e-2, 15.1762e-4)
                sigma0_vv_ref = exp( ( pv[0]+inc_angle*pv[1]+inc_angle**2*pv[2])*log(10) )
                roughness = n[3]/sigma0_vv_ref

            #~ Create new band
            n.add_band(bandID=4, array=roughness, \
               parameters={'name':'roughness', \
               'wkv': 'surface_backwards_scattering_coefficient_of_radar_wave', \
               'dataType': 6})

            # Reproject image into Lat/Lon WGS84 (Simple Cylindrical) projection
            # 1. Cancel previous reprojection
            # 2. Get corners of the image and the pixel resolution
            # 3. Create Domain with stereographic projection, corner coordinates 1000m
            # 4. Reproject
            # 5. Write image
            n.reproject() # 1.
            lons, lats = n.get_corners() # 2.
            # Pixel resolution
            #~ pxlRes = distancelib.getPixelResolution(array(lats), array(lons), n.shape())
            #~ pxlRes = array(pxlRes)*360/40000 # great circle distance
            pxlRes = array(distancelib.getPixelResolution(array(lats), array(lons), n.shape(), 'deg'))
            
            
            ipdb.set_trace()
            
            
            if min(lats) >= 65 and max(lats) >= 75 and max(lats)-min(lats) >= 13:
               pxlRes = array([0.00065, 0.00065])*2 # make the resolution 150x150m
            #~ pxlRes = pxlRes*7 # make the resolution worser
            srsString = "+proj=latlong +datum=WGS84 +ellps=WGS84 +no_defs"
            #~ extentString = '-lle %f %f %f %f -ts 3000 3000' % (min(lons), min(lats), max(lons), max(lats))
            extentString = '-lle %f %f %f %f -tr %f %f' % (min(lons), min(lats), \
                            max(lons), max(lats), pxlRes[1], pxlRes[0])
            d = Domain(srs=srsString, ext=extentString) # 3.
            n.reproject(d) # 4.

            if useMask:
               # get array with watermask (landmask) b
               # it must be done after reprojection!
               # 1. Get Nansat object with watermask
               # 2. Get array from Nansat object. 0 - land, 1 - water
               #wm = n.watermask(mod44path='/media/magDesk/media/SOLabNFS/store/auxdata/coastline/mod44w/')
               wm = n.watermask(mod44path='/media/data/data/auxdata/coastline/mod44w/')
               wmArray = wm[1]

               #~ ОШИБКА numOfColor=255 не маскирует, потому что в figure.apply_mask: availIndeces = range(self.d['numOfColor'], 255 - 1)
               #~ n.write_figure(fileName=figureName, bands=[3], \
                            #~ numOfColor=255, mask_array=wmArray, mask_lut={0: 0},
                            #~ clim=[0,0.15], cmapName='gray', transparency=0) # 5.
               n.write_figure(fileName=figureName, bands=[4], \
                                 mask_array=wmArray, mask_lut={0: [0,0,0]},
                                 clim=[0,2], cmapName='gray', transparency=[0,0,0]) # 5.
            else:
               n.write_figure(fileName=figureName, bands=[1], \
                              clim=[0,2], cmapName='gray', transparency=[0,0,0]) # 5.

            # open the input image and convert to RGBA for further tiling with slbtiles
            input_img = Image.open(figureName)
            output_img = input_img.convert("RGBA")
            output_img.save(figureName)

            # make KML image
            n.write_kml_image(kmlFileName=kmlName, kmlFigureName=figureName)

            #~ Change the file permissions
            os.chmod(oPath, 0777)
            os.chmod(oPath + fileName[0:27] + '/', 0777)
            os.chmod(kmlName, 0777)
            os.chmod(figureName, 0777)

            #~ Change the owner and group
            #~ os.chown(oPath, 1111, 1111)
            #~ os.chown(oPath + fileName[0:27] + '/', 1111, 1111)
            #~ os.chown(kmlName, 1111, 1111)
            #~ os.chown(figureName, 1111, 1111)
            
            #~ garbage collection
            gc.collect()
Example #34
0
class SARWind(Nansat, object):
    '''
    A class for calculating wind speed from SAR images using CMOD
    '''

    def __init__(self, sar_image, winddir=None, pixelsize=500):
        '''
            Parameters
            -----------
            sar_image : string or Nansat object
                SAR image filename (original, raw file)
            winddir : int, string, Nansat, None
                Auxiliary wind field information needed to calculate
                SAR wind (must be or have wind direction)
        '''
        if isinstance(sar_image, str) or isinstance(sar_image, unicode):
            super(SARWind, self).__init__(sar_image)
        elif isinstance(sar_image, Nansat):
            super(SARWind, self).__init__(domain=sar_image)
            self.vrt = sar_image.vrt

        # Check that this is a SAR image with VV pol NRCS
        try:
            self.sigma0_bandNo = self._get_band_number(
                            {'standard_name': 
            'surface_backwards_scattering_coefficient_of_radar_wave', 
                            'polarization': 'VV'})
        except:
            raise TypeError(self.fileName + 
                ' does not have SAR NRCS in VV polarization')

        self.SAR_image_time = self.get_time(
                self.sigma0_bandNo).replace(tzinfo=None)

        if pixelsize != 'fullres':
            print 'Resizing SAR image to ' + str(pixelsize) + ' m pixel size'
            self.resize(pixelsize=pixelsize)

        self.winddir = winddir
        if winddir is not None:
            self.calculate_wind()


    def calculate_wind(self, winddir=None, storeModelSpeed=True):
        # Calculate wind speed from SAR sigma0 in VV polarization

        if winddir:
            self.winddir = winddir
        if self.winddir is None or self.winddir == 'online':
            self.winddir = 'ncep_wind_online' # default source

        if isinstance(self.winddir, int):
            # Constant wind direction is input
            print 'Using constant wind (from) direction: ' + str(self.winddir) + \
                    ' degrees clockwise from North'
            winddirArray = np.ones(self.shape())*self.winddir
            winddir_time = None
            storeModelSpeed = False # Not relevant if direction given as number
        else:
            # Nansat readable file
            if isinstance(self.winddir, str):
                try:
                    self.winddir = Nansat(self.winddir)
                except:
                    try:
                        self.winddir = Nansat(self.winddir + 
                                            datetime.strftime(
                                            self.SAR_image_time, ':%Y%m%d%H%M'))
                    except:
                        pass

            if not isinstance(self.winddir, Nansat):
                raise ValueError('Wind direction not available')

            winddir_time = self.winddir.get_time()[0]

            # Bi-linear interpolation onto SAR image
            self.winddir.reproject(self, eResampleAlg=1)

            # Check time difference between SAR image and wind direction object
            timediff = self.SAR_image_time - winddir_time
            hoursDiff = np.abs(timediff.total_seconds()/3600.)
            print 'Time difference between SAR image and wind direction: ' \
                    + '%.2f' % hoursDiff + ' hours'
            print 'SAR image time: ' + str(self.SAR_image_time)
            print 'Wind dir time: ' + str(winddir_time)
            if hoursDiff > 3:
                print '#########################################'
                print 'WARNING: time difference exceeds 3 hours!'
                print '#########################################'

            wind_u_bandNo = self.winddir._get_band_number({
                                'standard_name': 'eastward_wind',
                            })
            wind_v_bandNo = self.winddir._get_band_number({
                                'standard_name': 'northward_wind',
                            })
            # Get wind direction
            u_array = self.winddir[wind_u_bandNo]
            v_array = self.winddir[wind_v_bandNo]
            winddirArray = np.degrees(
                    np.arctan2(-u_array, -v_array)) # 0 from North, 90 from East


        # Calculate SAR wind with CMOD
        # TODO: 
        # - add other CMOD versions than CMOD5
        print 'Calculating SAR wind with CMOD...'
        startTime = datetime.now()

        windspeed = cmod5n_inverse(self[self.sigma0_bandNo], 
                        np.mod(winddirArray - self['SAR_look_direction'], 360), 
                        self['incidence_angle'])
        print 'Calculation time: ' + str(datetime.now() - startTime)

        windspeed[np.where(np.isnan(windspeed))] = np.nan
        windspeed[np.where(np.isinf(windspeed))] = np.nan

        # Add wind speed and direction as bands
        # TODO: make it possible to update existing bands... See
        # https://github.com/nansencenter/nansat/issues/58
        self.add_band(array=windspeed, parameters={
                        'wkv': 'wind_speed',
                        'name': 'windspeed',
                        'time': self.get_time(self.sigma0_bandNo),
                        'winddir_time': winddir_time
                })
        self.add_band(array=winddirArray, parameters={
                            'wkv': 'wind_from_direction',
                            'name': 'winddirection',
                            'time': winddir_time
                })

        if storeModelSpeed:
            self.add_band(array=self.winddir['windspeed'], parameters={
                            'wkv': 'wind_speed',
                            'name': 'model_windspeed',
                            'time': winddir_time,
            })

        # TODO: Replace U and V bands with pixelfunctions
        u = -windspeed*np.sin((180.0 - winddirArray)*np.pi/180.0)
        v = windspeed*np.cos((180.0 - winddirArray)*np.pi/180.0)
        self.add_band(array=u, parameters={
                            'wkv': 'eastward_wind',
        })
        self.add_band(array=v, parameters={
                            'wkv': 'northward_wind',
        })


    def _get_masked_windspeed(self, landmask=True, icemask=True):
        try:
            sar_windspeed = self['windspeed']
        except:
            raise ValueError('SAR wind has not been calculated, ' \
                'execute calculate_wind(winddir) 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 write_geotiff(self, filename, landmask=True, icemask=True):

        sar_windspeed, palette = self._get_masked_windspeed(landmask, icemask)

        nansat_geotiff = Nansat(array=sar_windspeed, domain=self,
                                parameters = {'name': 'masked_windspeed',
                                              'minmax': '0 20'})
                        
        nansat_geotiff.write_geotiffimage(filename)


    def plot(self, filename=None, numVectorsX = 16, show=True,
                landmask=True, icemask=True, flip=True, maskWindAbove=35):
        ''' Basic plotting function showing CMOD wind speed
        overlaid vectors in SAR image projection'''

        try:
            sar_windspeed, palette = self._get_masked_windspeed(landmask, icemask)
        except:
            raise ValueError('SAR wind has not been calculated, ' \
                'execute calculate_wind(winddir) before plotting.')
        sar_windspeed[sar_windspeed>maskWindAbove] = np.nan

        winddirReductionFactor = np.round(
                self.vrt.dataset.RasterXSize/numVectorsX)
        # model_winddir is direction from which wind is blowing
        winddir_relative_up = 360 - self['winddirection'] + \
                                    self.azimuth_up()
        indX = range(0, self.vrt.dataset.RasterXSize, winddirReductionFactor)
        indY = range(0, self.vrt.dataset.RasterYSize, winddirReductionFactor)
        X, Y = np.meshgrid(indX, indY)
        try: # scaling of wind vector length, if model wind is available
            model_windspeed = self['model_windspeed']
            model_windspeed = model_windspeed[Y, X]
        except:
            model_windspeed = 8*np.ones(X.shape)

        Ux = np.sin(np.radians(winddir_relative_up[Y, X]))*model_windspeed
        Vx = np.cos(np.radians(winddir_relative_up[Y, X]))*model_windspeed

        # Make sure North is up, and east is right
        if flip == True:
            lon, lat = self.get_corners()
            if lat[0] < lat[1]:
                sar_windspeed = np.flipud(sar_windspeed)
                Ux = -np.flipud(Ux)
                Vx = -np.flipud(Vx)

            if lon[0] > lon[2]:
                sar_windspeed = np.fliplr(sar_windspeed)
                Ux = np.fliplr(Ux)
                Vx = np.fliplr(Vx)

        # Plotting
        figSize = sar_windspeed.shape
        legendPixels = 60.0
        legendPadPixels = 5.0
        legendFraction = legendPixels/figSize[0]
        legendPadFraction = legendPadPixels/figSize[0]
        dpi=100.0

        fig = plt.figure()
        fig.set_size_inches((figSize[1]/dpi, (figSize[0]/dpi)*
                                (1+legendFraction+legendPadFraction)))
        ax = fig.add_axes([0,0,1,1+legendFraction])
        ax.set_axis_off()
        plt.imshow(sar_windspeed, cmap=palette, interpolation='nearest')
        plt.clim([0, 20])
        cbar = plt.colorbar(orientation='horizontal', shrink=.80,
                     aspect=40,
                     fraction=legendFraction, pad=legendPadFraction)
        cbar.ax.set_ylabel('[m/s]', rotation=0)
        cbar.ax.yaxis.set_label_position('right')
        ax.quiver(X, Y, Ux, Vx, angles='xy', width=0.004,
                    scale=200, scale_units='width',
                    color=[.0, .0, .0], headaxislength=4)
        if filename is not None:
            fig.savefig(filename, pad_inches=0, dpi=dpi)
        if show:
            plt.show()
        return fig
Example #35
0
n.write_kml(kmlFileName=oFileName + '_preview.kml')

# make image with map of the file location
n.write_map(oFileName + '_map.png')


# Make image reprojected onto map of Northern Europe
# 1. Create Domain object. It describes the desired grid of reprojected image:
# projection, resolution, size, etc. In this case it is geographic projection;
# -10 - 30 E, 50 - 70 W; 2000 x 2000 pixels
# 2. Reproject the Nansat object
# 3. Make simple image
dLatlong = Domain("+proj=latlong +datum=WGS84 +ellps=WGS84 +no_defs", "-te 25 70 35 72 -ts 2000 2000")
dLatlong.write_map(oFileName + '_latlong_map.png')
print 'Latlong Domain:', dLatlong
n.reproject(dLatlong)
n.write_figure(oFileName + '_pro_latlon.png')

# Reprojected image into stereographic projection
# 1. Cancel previous reprojection
# 2. Get corners of the image
# 3. Create Domain with stereographic projection, corner coordinates and resolution 1000m
# 4. Reproject with cubic interpolation
# 5. Write image
n.reproject() # 1.
lons, lats = n.get_corners() # 2.
meanLon = sum(lons, 0.0) / 4.
meanLat = sum(lats, 0.0) / 4.
srsString = "+proj=stere +lon_0=%f +lat_0=%f +k=1 +ellps=WGS84 +datum=WGS84 +no_defs" % (meanLon, meanLat)
extentString = '-lle %f %f %f %f -tr 100 100' % (min(lons), min(lats), max(lons), max(lats))
dStereo = Domain(srsString, extentString) # 3.
Example #36
0
oPath = '/home/mag/'
fileName = 'MER_FRS_1PNEPA20100401_154249_000001972088_00140_42278_0585.N1.nc'

# create Nansat object
n = Nansat(iPath + fileName)

# list bands and georeference of the object
print n

# Reprojected image into Lat/Lon WGS84 (Simple Cylindrical) projection
# 1. Cancel previous reprojection
# 2. Get corners of the image and the pixel resolution
# 3. Create Domain with stereographic projection, corner coordinates and resolution 1000m
# 4. Reproject
# 5. Write image
n.reproject() # 1.
lons, lats = n.get_corners() # 2.
pxlRes = distancelib.getPixelResolution(array(lats), array(lons), n[1])
pxlRes = array(pxlRes)*360/40000 # great circle distance
srsString = "+proj=latlong +datum=WGS84 +ellps=WGS84 +no_defs"
#~ extentString = '-lle %f %f %f %f -ts 3000 3000' % (min(lons), min(lats), max(lons), max(lats))
extentString = '-lle %f %f %f %f -tr %f %f' % (min(lons), min(lats), \
                max(lons), max(lats), pxlRes[1], pxlRes[0])
d = Domain(srs=srsString, ext=extentString) # 3.
print d
n.reproject(d) # 4.

# get array with watermask (landmask) b 
# it must be done after reprojection!
# 1. Get Nansat object with watermask
# 2. Get array from Nansat object. 0 - land, 1 - water
Example #37
0
def main(argv=None):

    year = '2012'
    useMask = False

    if argv is None:
        argv = sys.argv

    if argv is None:
        print("Please specify the path/year to the asar folder! \n")
        return

    # Parse arguments
    try:
        opts, args = getopt.getopt(argv, "hi:o:",
                                   ["year=", "oPath=", "iPath=", "useMask="])
    except getopt.GetoptError:
        print 'readASAR.py -year <year> ...'
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print 'readASAR.py -year <year> ...'
            sys.exit()
        elif opt in ("-year", "--year"):
            year = arg
        elif opt in ("-oPath", "--oPath"):
            oPath = arg
        elif opt in ("-iPath", "--iPath"):
            iPath = arg
        elif opt in ("-useMask", "--useMask"):
            useMask = arg

    oPath = '/media/SOLabNFS2/tmp/roughness/' + year + '/'
    iPath = '/media/SOLabNFS2/store/satellite/asar/' + year + '/'

    if not os.path.exists(oPath):
        os.makedirs(oPath)

    dirNames = os.listdir(iPath)
    for dirName in dirNames:
        fileNames = os.listdir(iPath + dirName)
        for fileName in fileNames:
            figureName = oPath + fileName[0:27] + '/' + fileName + '_proj.png'
            kmlName = oPath + fileName[0:27] + '/' + fileName + '.kml'
            if not os.path.exists(oPath + fileName[0:27] + '/'):
                os.makedirs(oPath + fileName[0:27] + '/')

            if os.path.isfile(kmlName):
                print "%s already processed" % (fileName)
                continue
            else:
                print "%s" % (fileName)

            # try to create Nansat object
            try:
                n = Nansat(iPath + dirName + '/' + fileName,
                           mapperName='asar',
                           logLevel=27)
            except Exception as e:
                print "Failed to create Nansat object:"
                print str(e)
                os.rmdir(oPath + fileName[0:27] + '/')
                continue

            #~ Get the bands
            raw_counts = n[1]
            inc_angle = n[2]

            #~ NICE image (roughness)
            pol = n.bands()[3]['polarization']
            if pol == 'HH':
                ph = (2.20495, -14.3561e-2, 11.28e-4)
                sigma0_hh_ref = exp(
                    (ph[0] + inc_angle * ph[1] + inc_angle**2 * ph[2]) *
                    log(10))
                roughness = n[3] / sigma0_hh_ref
            elif pol == 'VV':
                pv = (2.29373, -15.393e-2, 15.1762e-4)
                sigma0_vv_ref = exp(
                    (pv[0] + inc_angle * pv[1] + inc_angle**2 * pv[2]) *
                    log(10))
                roughness = n[3] / sigma0_vv_ref

            #~ Create new band
            n.add_band(bandID=4, array=roughness, \
               parameters={'name':'roughness', \
               'wkv': 'surface_backwards_scattering_coefficient_of_radar_wave', \
               'dataType': 6})

            # Reproject image into Lat/Lon WGS84 (Simple Cylindrical) projection
            # 1. Cancel previous reprojection
            # 2. Get corners of the image and the pixel resolution
            # 3. Create Domain with stereographic projection, corner coordinates 1000m
            # 4. Reproject
            # 5. Write image
            n.reproject()  # 1.
            lons, lats = n.get_corners()  # 2.
            # Pixel resolution
            #~ pxlRes = distancelib.getPixelResolution(array(lats), array(lons), n.shape())
            #~ pxlRes = array(pxlRes)*360/40000 # great circle distance
            pxlRes = array(
                distancelib.getPixelResolution(array(lats), array(lons),
                                               n.shape(), 'deg'))

            ipdb.set_trace()

            if min(lats) >= 65 and max(
                    lats) >= 75 and max(lats) - min(lats) >= 13:
                pxlRes = array([0.00065, 0.00065
                                ]) * 2  # make the resolution 150x150m
            #~ pxlRes = pxlRes*7 # make the resolution worser
            srsString = "+proj=latlong +datum=WGS84 +ellps=WGS84 +no_defs"
            #~ extentString = '-lle %f %f %f %f -ts 3000 3000' % (min(lons), min(lats), max(lons), max(lats))
            extentString = '-lle %f %f %f %f -tr %f %f' % (min(lons), min(lats), \
                            max(lons), max(lats), pxlRes[1], pxlRes[0])
            d = Domain(srs=srsString, ext=extentString)  # 3.
            n.reproject(d)  # 4.

            if useMask:
                # get array with watermask (landmask) b
                # it must be done after reprojection!
                # 1. Get Nansat object with watermask
                # 2. Get array from Nansat object. 0 - land, 1 - water
                #wm = n.watermask(mod44path='/media/magDesk/media/SOLabNFS/store/auxdata/coastline/mod44w/')
                wm = n.watermask(
                    mod44path='/media/data/data/auxdata/coastline/mod44w/')
                wmArray = wm[1]

                #~ ОШИБКА numOfColor=255 не маскирует, потому что в figure.apply_mask: availIndeces = range(self.d['numOfColor'], 255 - 1)
                #~ n.write_figure(fileName=figureName, bands=[3], \
                #~ numOfColor=255, mask_array=wmArray, mask_lut={0: 0},
                #~ clim=[0,0.15], cmapName='gray', transparency=0) # 5.
                n.write_figure(fileName=figureName, bands=[4], \
                                  mask_array=wmArray, mask_lut={0: [0,0,0]},
                                  clim=[0,2], cmapName='gray', transparency=[0,0,0]) # 5.
            else:
                n.write_figure(fileName=figureName, bands=[1], \
                               clim=[0,2], cmapName='gray', transparency=[0,0,0]) # 5.

            # open the input image and convert to RGBA for further tiling with slbtiles
            input_img = Image.open(figureName)
            output_img = input_img.convert("RGBA")
            output_img.save(figureName)

            # make KML image
            n.write_kml_image(kmlFileName=kmlName, kmlFigureName=figureName)

            #~ Change the file permissions
            os.chmod(oPath, 0777)
            os.chmod(oPath + fileName[0:27] + '/', 0777)
            os.chmod(kmlName, 0777)
            os.chmod(figureName, 0777)

            #~ Change the owner and group
            #~ os.chown(oPath, 1111, 1111)
            #~ os.chown(oPath + fileName[0:27] + '/', 1111, 1111)
            #~ os.chown(kmlName, 1111, 1111)
            #~ os.chown(figureName, 1111, 1111)

            #~ garbage collection
            gc.collect()