def viirs_sst(infile, outdir, vmin=None, vmax=None, contrast='relative',
              ngcps=(41, 32), denoise_kernel='boxcar', denoise_width=27,
              open_iterations=1, nprocs=1,
              pngkml=False, write_netcdf=False, file_range=None):
    """
    """
    dic = None

    # Check file containing ranges
    if file_range is not None:
        if not os.path.isfile(file_range):
            raise Exception('file_range {} not found'.format(file_range))
        # Read a txt file which contains three columns: yearday,vmin,vmax
        with open(file_range, 'r') as f:
            dic = {}
            for line in f:
                (fdoy, fmin, fmax) = line.split(',')
                dic[int(fdoy)] = (float(fmin), float(fmax))

    if contrast == 'med':
        listbox = [[-6., 35., 2.75, 42.48],
                   [2.74, 30, 42.2, 47.00]]
    elif contrast == 'cwe':
        listbox = [[-23., 35.2, -5.5, 42.88],
                   [-23., 42.8, 2.20, 51.]]
    elif contrast == 'nwe':
        listbox = [[-23., 50.8, 32.7, 68.]]
    elif contrast == 'gom':
        listbox = [[-98., 18.0, -80.5, 30.5]]
    elif contrast == 'agulhas':
        listbox = [[10.8437, -45.7404, 39.9799, -25.3019]]
    elif contrast == 'gs':
        listbox = [[-81.52, 20, -30, 45]]
    else:
        listbox = None
    # Read/Process data
    print 'Read/Process data'
    dataset = Dataset(infile)
    start_time = datetime.strptime(dataset.start_time, '%Y%m%dT%H%M%SZ')
    print start_time.day
    print start_time.month
    stop_time = datetime.strptime(dataset.stop_time, '%Y%m%dT%H%M%SZ')
    lon = dataset.variables['lon'][:, :]
    lat = dataset.variables['lat'][:, :]
    sst = np.ma.array(dataset.variables['sea_surface_temperature'][0, :, :])
    _bt11= dataset.variables['brightness_temperature_11um'][0, :, :]
    bt11 = np.ma.array(_bt11)
    quality_level = np.ma.array(dataset.variables['quality_level'][0, :, :])
    '''
    if file_shape is not None:
        with open(file_shape, 'r') as fshape:
            shape = shapely.wkt.load(fshape)
        box = shape.bounds
        index_in = np.where((lon >= box[0]) & (lat >= box[1])
                            & (lon <= box[2]) & (lat <= box[3]))
        index_out = np.where((lon < box[0]) | (lat < box[1])
                             | (lon > box[2]) | (lat > box[3]))
        sst[index_out] = np.nan
        print(np.shape(index_in))
        sys.exit(1)
        for i, j in zip(index_in[0], index_in[1]):
            p = Point(lon[i, j], lat[i, j])
            if p.within(shape) is False:
                sst[i, j] = np.nan
    '''
    if listbox is not None:
        mask_box = np.zeros(np.shape(sst))
        for i in range(np.shape(listbox)[0]):
            index_in = np.where((lon >= listbox[i][0]) & (lat >= listbox[i][1])
                             & (lon <= listbox[i][2]) & (lat <= listbox[i][3]))
            mask_box[index_in] = 1
        mask = ma.getmaskarray(sst) | ma.getmaskarray(bt11) | \
               (quality_level.data < 4) | (mask_box == 0)
    else:
        mask = ma.getmaskarray(sst) | ma.getmaskarray(bt11) | \
               (quality_level.data < 4)
    if mask.all():
        print 'No data'
        sys.exit(0)
    # GCPs for resampling and geotiff georeference
    scansize = 16
    dtime0 = datetime.utcnow()
    gcps = resample.get_gcps_from_bowtie(lon, lat, scansize, ngcps=ngcps)
    dtime = datetime.utcnow() - dtime0
    print 'Get GCPs from bowtie swath : {}'.format(dtime)
    gcplon, gcplat, gcpnpixel, gcpnline = gcps
    rspysize = lon.shape[0]
    geod = pyproj.Geod(ellps='WGS84')
    mid = abs(gcpnline[:, 0] - 0.5).argmin()
    xdists = geod.inv(gcplon[mid, :-1], gcplat[mid, :-1],
                      gcplon[mid, 1:], gcplat[mid, 1:])[2]
    xdist = np.sum(xdists) / abs(gcpnpixel[mid, -1] - gcpnpixel[mid, 0])
    rspxsize = np.round(xdist / 750.).astype('int') + 1
    gcpline = gcpnline * rspysize
    gcppixel = gcpnpixel * rspxsize

    # Resample with LinearNDInterpolator in output space
    dtime0 = datetime.utcnow()
    pix, lin = resample.get_points_from_gcps(gcplon, gcplat, gcppixel,
                                             gcpline, rspxsize, rspysize,
                                             1, lon, lat, nprocs=nprocs) - 0.5
    dtime = datetime.utcnow() - dtime0
    print 'Get input coordinates in new grid : {}'.format(dtime)
    # Test input grid in output space
    # import matplotlib.pyplot as plt
    # for iscan in range(lon.shape[0] / scansize):
    #     pixscan = pix[iscan * scansize: (iscan+1) * scansize, :]
    #     linscan = lin[iscan * scansize: (iscan+1) * scansize, :]
    #     # maskscan = mask[iscan * scansize: (iscan+1) * scansize, :]
    #     # pixscan = pixscan[~maskscan]
    #     # linscan = linscan[~maskscan]
    #     plt.plot(pixscan.flatten(), linscan.flatten(), '+')
    # plt.show()
    # import pdb ; pdb.set_trace()
    # \Test input grid in output space
    dtime0 = datetime.utcnow()
    sst.data[mask] = np.nan
    bt11.data[mask] = np.nan
    val = np.dstack((sst.data, bt11.data))
    rspval = resample.resample_bowtie_linear(pix, lin, val, scansize,
                                             rspxsize, rspysize, show=False)
    rspsst = rspval[:, :, 0]
    rspbt11 = rspval[:, :, 1]
    rspmask = ma.getmaskarray(rspsst) | ma.getmaskarray(rspbt11)
    dtime = datetime.utcnow() - dtime0
    print 'Interpolate in new grid : {}'.format(dtime)

    # Denoise sst and open mask
    rspsst.mask = rspmask
    rspbt11.mask = rspmask
    finalsst = denoise_sst(rspsst, rspbt11, kernel=denoise_kernel,
                           width=denoise_width, show=False)
    finalmask = ~binary_opening(~rspmask, structure=np.ones((3, 3)),
                                iterations=open_iterations)
    finalsst.mask = finalmask

    # Contrast
    if vmin == None:
        if contrast == 'relative':
            vmin = np.percentile(finalsst.compressed(), 0.5)
        #elif contrast == 'agulhas':
        #    dayofyear = float(start_time.timetuple().tm_yday)
        #    vmin = 273.15 + 2. * np.cos((dayofyear - 45.) * 2. * np.pi / 365.) + 20. - 9.
        #    #par = [277.94999694824219, 42, 2.5500030517578125, -219]
        #    par = [278.09999084472656, 0.62831853071795862,
        #           2.4000091552734375, 0.1570796326794896]
        #    vmin = par[0] + par[2] * np.cos(par[3] * dayofyear - par[1])
        #if a specific txt file is provided for the range
        elif dic is not None:
            dayofyear = float(start_time.timetuple().tm_yday)
            extrema = dic.get(dayofyear, dic[min(dic.keys(),
                           key=lambda k:abs(k - dayofyear))])
            vmin = extrema[0]
        else:
            raise Exception('Unknown contrast : {}'.format(contrast))
    if vmax == None:
        if contrast == 'relative':
            vmax = np.percentile(finalsst.compressed(), 99.5)
        #elif contrast == 'agulhas':
        #    dayofyear = float(start_time.timetuple().tm_yday)
        #    vmax = 273.15 + 2. * np.cos((dayofyear - 45.) * 2. * np.pi / 365.) + 20. + 4.
        #    #par = [300.59999084472656, 21, 2.8499908447265625, -191]
        #    par = [300.59999084472656, 0.29919930034188508,
        #           2.8499908447265625, 0.14959965017094254]
        #    vmax = par[0] + par[2] * np.cos(par[3] * dayofyear - par[1])
        #if a specific text file is provided for the range
        elif dic is not None:
            dayofyear = float(start_time.timetuple().tm_yday)
            extrema = dic.get(dayofyear, dic[min(dic.keys(),
                           key=lambda k:abs(k - dayofyear))])
            vmax = extrema[1]
        else:
            raise Exception('Unknown contrast : {}'.format(contrast))

    # Flip (geotiff in "swath sense")
    finalsst = finalsst[::-1, ::-1]
    gcppixel = rspxsize - gcppixel
    gcpline = rspysize - gcpline

    # Construct metadata/geolocation/band(s)
    print 'Construct metadata/geolocation/band(s)'
    metadata = {}
    (dtime, time_range) = stfmt.format_time_and_range(start_time, stop_time,
                                                      units='ms')
    metadata['product_name'] = 'SST_VIIRS_denoised'
    if contrast == 'relative':
        metadata['name'] = os.path.splitext(os.path.basename(infile))[0]
    else:
        metadata['name'] = '{}_{}'.format(os.path.splitext(os.path.basename(infile))[0], contrast)
    metadata['datetime'] = dtime
    metadata['time_range'] = time_range
    metadata['source_URI'] = infile
    metadata['source_provider'] = 'NOAA'
    metadata['processing_center'] = 'OceanDataLab'
    metadata['conversion_software'] = 'Syntool'
    metadata['conversion_version'] = '0.0.0'
    metadata['conversion_datetime'] = stfmt.format_time(datetime.utcnow())
    metadata['parameter'] = 'sea surface temperature'
    metadata['type'] = 'remote sensing'
    metadata['sensor_type'] = 'radiometer'
    metadata['sensor_name'] = 'VIIRS'
    metadata['sensor_platform'] = 'Suomi-NPP'
    #metadata['sensor_pass'] =
    geolocation = {}
    geolocation['projection'] = stfmt.format_gdalprojection()
    gcpheight = np.zeros(gcppixel.shape)
    geolocation['gcps'] = stfmt.format_gdalgcps(gcplon, gcplat, gcpheight,
                                                gcppixel, gcpline)
    band = []
    indndv = np.where(ma.getmaskarray(finalsst) == True)
    offset, scale = vmin, (vmax-vmin)/254.
    np.clip(finalsst.data, vmin, vmax, out=finalsst.data)
    array = np.round((finalsst.data - offset) / scale).astype('uint8')
    array[indndv] = 255
    colortable = stfmt.format_colortable('cerbere_medspiration',
                                         vmax=vmax, vmax_pal=vmax,
                                         vmin=vmin, vmin_pal=vmin)
    band.append({'array':array, 'scale':scale, 'offset':offset,
                 'description':'sea surface temperature', 'unittype':'K',
                 'nodatavalue':255, 'parameter_range':[vmin, vmax],
                 'colortable':colortable})

    if write_netcdf == False:
        # Write geotiff
        print 'Write geotiff'
        tifffile = stfmt.format_tifffilename(outdir, metadata, create_dir=True)
        stfmt.write_geotiff(tifffile, metadata, geolocation, band)
        # Write projected png/kml
        if pngkml == True:
            print 'Write projected png/kml'
            stfmt.write_pngkml_proj(tifffile)
    elif write_netcdf == True:
        print 'Write netcdf'
        ncfile = stfmt.format_ncfilename(outdir, metadata, create_dir=True)
        band[0]['name'] = 'denoised_sst'
        band[0]['long_name'] = 'denoised sea surface temperature'
        band[0]['standard_name'] = 'sea_surface_temperature'
        # ymid = abs(gcpline[:, 0] - rspysize / 2.).argmin()
        # xdists = geod.inv(gcplon[ymid, :-1], gcplat[ymid, :-1],
        #                   gcplon[ymid, 1:], gcplat[ymid, 1:])[2] / \
        #                   np.abs(gcppixel[ymid, 1:] - gcppixel[ymid, :-1])
        # xmid = abs(gcppixel[0, :] - rspxsize / 2.).argmin()
        # ydists = geod.inv(gcplon[:-1, xmid], gcplat[:-1, xmid],
        #                   gcplon[1:, xmid], gcplat[1:, xmid])[2] / \
        #                   np.abs(gcpline[1:, xmid] - gcpline[:-1, xmid])
        # print xdists.min(), xdists.max(), xdists.mean()
        # # e.g. 749.905437495 749.905892002 749.905827652
        # print ydists.min(), ydists.max(), ydists.mean()
        # # e.g. 737.638084996 741.195663083 739.157662785
        metadata['spatial_resolution'] = 750.
        stfmt.write_netcdf(ncfile, metadata, geolocation, band, 'swath',
                           ngcps=gcplon.shape)
示例#2
0
def modis_sst(infileid,
              outdir,
              download_dir='/tmp',
              vmin=None,
              vmax=None,
              contrast='relative',
              ngcps=(21, 25),
              resample_radius=5000.,
              resample_sigma=2500.,
              denoise_kernel='boxcar',
              denoise_width=20,
              open_iterations=1,
              nprocs=1,
              pngkml=False,
              write_netcdf=False,
              file_range=None):
    """
    """
    dic = None

    # Check file containing ranges
    if file_range is not None:
        if not os.path.isfile(file_range):
            raise Exception('file_range {} not found'.format(file_range))
        # Read a txt file which contains three columns: yearday,vmin,vmax
        with open(file_range, 'r') as f:
            dic = {}
            for line in f:
                (fdoy, fmin, fmax) = line.split(',')
                dic[int(fdoy)] = (float(fmin), float(fmax))

    # modissstfname = '/mnt/data/sst/modis/MYD021KM.A2011338.1225/A2011338122500.L2_LAC_SST'
    # modis02fname = '/mnt/data/sst/modis/MYD021KM.A2011338.1225/MYD021KM.A2011338.1225.005.2011339235825.hdf'
    # modis03fname = '/mnt/data/sst/modis/MYD021KM.A2011338.1225/MYD03.A2011338.1225.005.2011339233301.hdf'
    # modis35l2fname = '/mnt/data/sst/modis/MYD021KM.A2011338.1225/MYD35_L2.A2011338.1225.005.2011340001234.hdf'
    if contrast == 'med':
        listbox = [[-6., 35., 2.75, 42.48], [2.74, 30, 42.2, 47.00]]
    elif contrast == 'cwe':
        listbox = [[-23., 35.2, -5.5, 42.88], [-23., 42.8, 2.20, 51.]]
    elif contrast == 'nwe':
        listbox = [[-23., 50.8, 32.7, 68.]]
    elif contrast == 'gom':
        listbox = [[-98., 18.0, -80.5, 30.5]]
    elif contrast == 'agulhas':
        listbox = [[10.8437, -45.7404, 39.9799, -25.3019]]
    elif contrast == 'gs':
        listbox = [[-81.52, 20, -30, 45]]
    else:
        listbox = None
    # Search/Download data
    print 'Search/Download data'
    if re.match(r'^[AT][0-9]{13}$', infileid) is None:
        raise Exception('Input for modis_sst is an ID '
                        '(e.g. A2011338122500 or T2014143234500)')
    platform = infileid[0]
    date = datetime.strptime(infileid[1:], '%Y%j%H%M%S')
    modissstid = {'A': 'MODISAL2SST', 'T': 'MODISTL2SST'}[platform]
    modissstfname = modis.search_and_download(modissstid, date, download_dir)
    modis02id = {'A': 'MYD021KM', 'T': 'MOD021KM'}[platform]
    modis02fname = modis.search_and_download(modis02id, date, download_dir)
    modis03id = {'A': 'MYD03', 'T': 'MOD03'}[platform]
    modis03fname = modis.search_and_download(modis03id, date, download_dir)
    modis35l2id = {'A': 'MYD35_L2', 'T': 'MOD35_L2'}[platform]
    modis35l2fname = modis.search_and_download(modis35l2id, date, download_dir)

    # Read/Process data
    print 'Read/Process data'
    # Read from SST file
    modissstfile = modis.MODISL2File(modissstfname)
    # lon = modissstfile.read_lon()
    # lat = modissstfile.read_lat()
    sst = modissstfile.read_sst() + 273.15
    attrs = modissstfile.read_attributes()
    modissstfile.close()
    # Read from radiances file
    modis02file = modis.MODIS02File(modis02fname)
    rad11 = modis02file.read_radiance(31)
    modis02file.close()
    bt11 = modis.modis_bright(rad11, 31, 1)
    # Read from geolocation file
    modis03file = modis.MODIS03File(modis03fname)
    lon = modis03file.read_lon()
    lat = modis03file.read_lat()
    modis03file.close()
    # Read from cloud mask file
    modis35l2file = modis.MODIS35L2File(modis35l2fname)
    cloudmask = modis35l2file.read_cloudmask(byte=0)
    modis35l2file.close()
    cloudy = (np.bitwise_and(cloudmask, 2) == 0) & \
             (np.bitwise_and(cloudmask, 4) == 0)
    land = np.bitwise_and(cloudmask, 128) == 128  # Desert or Land
    # land = (np.bitwise_and(cloudmask, 128) == 128) | \
    #        (np.bitwise_and(cloudmask, 64) == 64) # Desert or Land or Coastal
    if listbox is not None:
        mask_box = np.zeros(np.shape(sst))
        for i in range(np.shape(listbox)[0]):
            index_in = np.where((lon >= listbox[i][0]) & (lat >= listbox[i][1])
                                & (lon <= listbox[i][2])
                                & (lat <= listbox[i][3]))
            mask_box[index_in] = 1
        mask = cloudy | land | ma.getmaskarray(sst) | ma.getmaskarray(bt11) | (
            mask_box == 0)
    else:
        mask = cloudy | land | ma.getmaskarray(sst) | ma.getmaskarray(bt11)
    if mask.all():
        print 'No data'
        sys.exit(0)
    # GCPs for resampling and geotiff georeference
    scansize = 10
    dtime0 = datetime.utcnow()
    gcps = resample.get_gcps_from_bowtie(lon, lat, scansize, ngcps=ngcps)
    #gcps = resample.get_gcps_from_bowtie_old(lon, lat, scansize, ngcps=ngcps)
    dtime = datetime.utcnow() - dtime0
    print 'Get GCPs from bowtie swath : {}'.format(dtime)
    gcplon, gcplat, gcpnpixel, gcpnline = gcps
    rspysize = lon.shape[0]
    geod = pyproj.Geod(ellps='WGS84')
    mid = abs(gcpnline[:, 0] - 0.5).argmin()
    xdists = geod.inv(gcplon[mid, :-1], gcplat[mid, :-1], gcplon[mid, 1:],
                      gcplat[mid, 1:])[2]
    xdist = np.sum(xdists) / abs(gcpnpixel[mid, -1] - gcpnpixel[mid, 0])
    rspxsize = np.round(xdist / 1000.).astype('int') + 1
    gcpline = gcpnline * rspysize
    gcppixel = gcpnpixel * rspxsize

    # Resample with LinearNDInterpolator in output space
    dtime0 = datetime.utcnow()
    pix, lin = resample.get_points_from_gcps(gcplon,
                                             gcplat,
                                             gcppixel,
                                             gcpline,
                                             rspxsize,
                                             rspysize,
                                             1,
                                             lon,
                                             lat,
                                             nprocs=nprocs) - 0.5
    dtime = datetime.utcnow() - dtime0
    print 'Get input coordinates in new grid : {}'.format(dtime)
    # Test input grid in output space
    # import matplotlib.pyplot as plt
    # for iscan in range(lon.shape[0] / scansize):
    #     pixscan = pix[iscan * scansize: (iscan+1) * scansize, :]
    #     linscan = lin[iscan * scansize: (iscan+1) * scansize, :]
    #     # maskscan = mask[iscan * scansize: (iscan+1) * scansize, :]
    #     # pixscan = pixscan[~maskscan]
    #     # linscan = linscan[~maskscan]
    #     plt.plot(pixscan.flatten(), linscan.flatten(), '+')
    # plt.show()
    # import pdb ; pdb.set_trace()
    # \Test input grid in output space
    dtime0 = datetime.utcnow()
    sst.data[mask] = np.nan
    bt11.data[mask] = np.nan
    val = np.dstack((sst.data, bt11.data))
    rspval = resample.resample_bowtie_linear(pix,
                                             lin,
                                             val,
                                             scansize,
                                             rspxsize,
                                             rspysize,
                                             show=False)
    rspsst = rspval[:, :, 0]
    rspbt11 = rspval[:, :, 1]
    rspmask = ma.getmaskarray(rspsst) | ma.getmaskarray(rspbt11)
    dtime = datetime.utcnow() - dtime0
    print 'Interpolate in new grid : {}'.format(dtime)

    # Resample with pyresample in lon/lat space
    # rsplin, rsppix = np.mgrid[0:rspysize, 0:rspxsize] + 0.5
    # rsplon, rsplat = resample.get_points_from_gcps(gcplon, gcplat, gcppixel,
    #                                                gcpline, rspxsize, rspysize,
    #                                                0, rsppix, rsplin, nprocs=nprocs)
    # # Test resample grid
    # import matplotlib.pyplot as plt
    # plt.plot(lon.flatten(), lat.flatten(), '+b')
    # plt.plot(rsplon.flatten(), rsplat.flatten(), '+g')
    # plt.plot(gcplon.flatten(), gcplat.flatten(), 'xr')
    # plt.show()
    # import pdb ; pdb.set_trace()
    # # \Test resample grid
    # # Test radius / sigma
    # resample_radius = 5000.
    # resample_sigma = 2500.
    # sst.mask = False
    # #sst.mask = sst.mask | (sst.data < 273.15+5) | (sst.data > 273.15+30)
    # rspsst = resample.resample_gauss(lon, lat, sst, rsplon, rsplat,
    #                                  resample_radius, resample_sigma,
    #                                  nprocs=nprocs, show=True)
    # import pdb ; pdb.set_trace()
    # # \Test radius / sigma
    # valid = np.where(mask == False)
    # rspsst = resample.resample_gauss(lon[valid], lat[valid], sst[valid],
    #                                  rsplon, rsplat,
    #                                  resample_radius, resample_sigma,
    #                                  fill_value=None, nprocs=nprocs,
    #                                  show=False)
    # rspbt11 = resample.resample_gauss(lon[valid], lat[valid], bt11[valid],
    #                                   rsplon, rsplat,
    #                                   resample_radius, resample_sigma,
    #                                   fill_value=None, nprocs=nprocs,
    #                                   show=False)
    # rspmask = resample.resample_nearest(lon, lat, mask,
    #                                     rsplon, rsplat,
    #                                     resample_radius,
    #                                     fill_value=True, nprocs=nprocs,
    #                                     show=False)
    # rspmask = rspmask | ma.getmaskarray(rspsst) | ma.getmaskarray(rspbt11)

    # Denoise sst and open mask
    rspsst.mask = rspmask
    rspbt11.mask = rspmask
    finalsst = denoise_sst(rspsst,
                           rspbt11,
                           kernel=denoise_kernel,
                           width=denoise_width,
                           show=False)
    #finalsst = rspsst
    finalmask = ~binary_opening(
        ~rspmask, structure=np.ones((3, 3)), iterations=open_iterations)
    #finalmask = rspmask
    finalsst.mask = finalmask

    # Contrast
    if vmin == None:
        if contrast == 'relative':
            vmin = np.percentile(finalsst.compressed(), 0.5)
        #elif contrast == 'agulhas':
        #    dayofyear = float(attrs['start_time'].timetuple().tm_yday)
        #    vmin = 273.15 + 2. * np.cos((dayofyear - 45.) * 2. * np.pi / 365.) + 20. - 9.
        #    #par = [277.94999694824219, 42, 2.5500030517578125, -219]
        #    par = [278.09999084472656, 0.62831853071795862,
        #           2.4000091552734375, 0.1570796326794896]
        #    vmin = par[0] + par[2] * np.cos(par[3] * dayofyear - par[1])
        #if a specific txt file is provided for the range
        elif dic is not None:
            dayofyear = float(attrs['start_time'].timetuple().tm_yday)
            # Read a txt file which contains three columns: yearday,vmin,vmax
            extrema = dic.get(
                dayofyear, dic[min(dic.keys(),
                                   key=lambda k: abs(k - dayofyear))])
            vmin = extrema[0]
        else:
            raise Exception('Unknown contrast : {}'.format(contrast))
    if vmax == None:
        if contrast == 'relative':
            vmax = np.percentile(finalsst.compressed(), 99.5)
        #elif contrast == 'agulhas':
        #    dayofyear = float(attrs['start_time'].timetuple().tm_yday)
        #    vmax = 273.15 + 2. * np.cos((dayofyear - 45.) * 2. * np.pi / 365.) + 20. + 4.
        #    #par = [300.59999084472656, 21, 2.8499908447265625, -191]
        #    par = [300.59999084472656, 0.29919930034188508,
        #           2.8499908447265625, 0.14959965017094254]
        #    vmax = par[0] + par[2] * np.cos(par[3] * dayofyear - par[1])
        #if a specific text file is provided for the range
        elif dic is not None:
            dayofyear = float(attrs['start_time'].timetuple().tm_yday)
            extrema = dic.get(
                dayofyear, dic[min(dic.keys(),
                                   key=lambda k: abs(k - dayofyear))])
            vmax = extrema[1]
        else:
            raise Exception('Unknown contrast : {}'.format(contrast))

    # Flip (geotiff in "swath sense")
    finalsst = finalsst[::-1, ::-1]
    gcppixel = rspxsize - gcppixel
    gcpline = rspysize - gcpline

    # Construct metadata/geolocation/band(s)
    print 'Construct metadata/geolocation/band(s)'
    metadata = {}
    (dtime, time_range) = stfmt.format_time_and_range(attrs['start_time'],
                                                      attrs['stop_time'],
                                                      units='ms')
    metadata['product_name'] = 'SST_MODIS_denoised'
    if contrast == 'relative':
        metadata['name'] = os.path.splitext(os.path.basename(modissstfname))[0]
    else:
        metadata['name'] = '{}_{}'.format(
            os.path.splitext(os.path.basename(modissstfname))[0], contrast)
    metadata['datetime'] = dtime
    metadata['time_range'] = time_range
    metadata['source_URI'] = [
        modissstfname, modis02fname, modis03fname, modis35l2fname
    ]
    metadata['source_provider'] = 'NASA'
    metadata['processing_center'] = 'OceanDataLab'
    metadata['conversion_software'] = 'Syntool'
    metadata['conversion_version'] = '0.0.0'
    metadata['conversion_datetime'] = stfmt.format_time(datetime.utcnow())
    metadata['parameter'] = 'sea surface temperature'
    metadata['type'] = 'remote sensing'
    metadata['sensor_type'] = 'radiometer'
    metadata['sensor_name'] = 'MODIS'
    metadata['sensor_platform'] = attrs['platform']
    metadata['sensor_pass'] = attrs['pass']
    geolocation = {}
    geolocation['projection'] = stfmt.format_gdalprojection()
    gcpheight = np.zeros(gcppixel.shape)
    geolocation['gcps'] = stfmt.format_gdalgcps(gcplon, gcplat, gcpheight,
                                                gcppixel, gcpline)
    band = []
    indndv = np.where(ma.getmaskarray(finalsst) == True)
    offset, scale = vmin, (vmax - vmin) / 254.
    np.clip(finalsst.data, vmin, vmax, out=finalsst.data)
    array = np.round((finalsst.data - offset) / scale).astype('uint8')
    array[indndv] = 255
    colortable = stfmt.format_colortable('cerbere_medspiration',
                                         vmax=vmax,
                                         vmax_pal=vmax,
                                         vmin=vmin,
                                         vmin_pal=vmin)
    band.append({
        'array': array,
        'scale': scale,
        'offset': offset,
        'description': 'sea surface temperature',
        'unittype': 'K',
        'nodatavalue': 255,
        'parameter_range': [vmin, vmax],
        'colortable': colortable
    })

    # Write geotiff
    if write_netcdf == False:
        print 'Write geotiff'
        tifffile = stfmt.format_tifffilename(outdir, metadata, create_dir=True)
        stfmt.write_geotiff(tifffile, metadata, geolocation, band)
        # Write projected png/kml
        if pngkml == True:
            print 'Write projected png/kml'
            stfmt.write_pngkml_proj(tifffile)
    elif write_netcdf == True:
        print 'Write netcdf'
        ncfile = stfmt.format_ncfilename(outdir, metadata, create_dir=True)
        band[0]['name'] = 'denoised_sst'
        band[0]['long_name'] = 'denoised sea surface temperature'
        band[0]['standard_name'] = 'sea_surface_temperature'
        # ymid = abs(gcpline[:, 0] - rspysize / 2.).argmin()
        # xdists = geod.inv(gcplon[ymid, :-1], gcplat[ymid, :-1],
        #                   gcplon[ymid, 1:], gcplat[ymid, 1:])[2] / \
        #                   np.abs(gcppixel[ymid, 1:] - gcppixel[ymid, :-1])
        # xmid = abs(gcppixel[0, :] - rspxsize / 2.).argmin()
        # ydists = geod.inv(gcplon[:-1, xmid], gcplat[:-1, xmid],
        #                   gcplon[1:, xmid], gcplat[1:, xmid])[2] / \
        #                   np.abs(gcpline[1:, xmid] - gcpline[:-1, xmid])
        # print xdists.min(), xdists.max(), xdists.mean()
        # # e.g. 999.763079208 999.763084628 999.763082543
        # print ydists.min(), ydists.max(), ydists.mean()
        # # e.g. 1006.4149472 1008.60679776 1007.5888004
        metadata['spatial_resolution'] = 1000.
        stfmt.write_netcdf(ncfile,
                           metadata,
                           geolocation,
                           band,
                           'swath',
                           ngcps=gcplon.shape)
示例#3
0
def sar_wind(infile,
             outdir,
             pngkml=False,
             valid_percent_min=1.,
             vmin=0.,
             vmax=25.4,
             vmin_pal=0.,
             vmax_pal=50 * 0.514):
    """
    """
    # Read/Process data
    print 'Read/Process data'
    sarwind = SAFEOCNNCFile(infile, product='WIND')
    mission = sarwind.read_global_attribute('missionName')
    if mission == 'S1A':
        sensor_name = 'Sentinel-1A'
        sensor_platform = 'Sentinel-1A'
        source_provider = 'ESA'
    elif mission == 'S1B':
        sensor_name = 'Sentinel-1B'
        sensor_platform = 'Sentinel-1B'
        source_provider = 'ESA'
    else:
        raise Exception('S1A/S1B missions expected.')
    start_time = sarwind.get_start_time()
    stop_time = sarwind.get_end_time()
    heading = sarwind.read_values('owiHeading')
    if np.sin((90 - heading[0, 0]) * np.pi / 180) > 0:
        sensor_pass = '******'
    else:
        sensor_pass = '******'
    safe_name = os.path.basename(os.path.dirname(os.path.dirname(infile)))
    sensor_mode = safe_name.split('_')[1]
    if sensor_mode not in ['S1', 'S2', 'S3', 'S4', 'S5', 'S6', 'IW', 'EW']:
        raise Exception('S[1-6]/IW/EW modes expected.')
    sensor_swath = os.path.basename(infile).split('-')[1].upper()
    sensor_polarisation = sarwind.read_global_attribute('polarisation')
    datagroup = safe_name.replace('.SAFE', '')
    pid = datagroup.split('_')[-1]
    dataname = os.path.splitext(os.path.basename(infile))[0] + '-' + pid
    windspeed = sarwind.read_values('owiWindSpeed')
    if windspeed.shape == (1, 1):
        raise Exception('owiRaSize and owiAzSize equals 1 !')
    winddirection = sarwind.read_values('owiWindDirection')
    landflag = sarwind.read_values('owiLandFlag')
    inversionquality = sarwind.read_values('owiInversionQuality')
    windquality = sarwind.read_values('owiWindQuality')
    #pbright = sarwind.read_values('owiPBright')
    lon = sarwind.read_values('lon')
    lat = sarwind.read_values('lat')
    if np.ma.is_masked(lon) or np.ma.is_masked(lat):
        raise Exception('Some lon and/or lat is masked.')
    if np.all(lon == 0) or np.all(lat == 0):
        raise Exception('All lon and/or lat set to 0.')
    ngcps = np.ceil(np.array(lon.shape) / 10.).astype('int') + 1
    pix = np.linspace(0, lon.shape[1] - 1,
                      num=ngcps[1]).round().astype('int32')
    lin = np.linspace(0, lon.shape[0] - 1,
                      num=ngcps[0]).round().astype('int32')
    pix2d, lin2d = np.meshgrid(pix, lin)
    gcplon = lon[lin2d, pix2d]
    gcplat = lat[lin2d, pix2d]
    gcppix = pix2d + 0.5
    gcplin = lin2d + 0.5
    gcphei = np.zeros(ngcps)
    ## Make sure lon are continuous (no jump because of IDL crossing)
    ## (if IDL crossing, by convention we make lon to be around 180deg)
    # if gcplon.min() < -135 and gcplon.max() > 135:
    #     gcplon[np.where(gcplon < 0)] += 360.
    gcplonmid = gcplon[ngcps[0] / 2, ngcps[1] / 2]
    gcplon = np.mod(gcplon - (gcplonmid - 180.), 360.) + gcplonmid - 180.
    gcplonmin = gcplon.min()
    gcplon = gcplon - np.floor((gcplonmin + 180.) / 360.) * 360.

    # Construct metadata/geolocation/band(s)
    print 'Construct metadata/geolocation/band(s)'
    metadata = {}
    (dtime, time_range) = stfmt.format_time_and_range(start_time,
                                                      stop_time,
                                                      units='ms')
    metadata['product_name'] = 'SAR_wind'
    metadata['name'] = dataname
    metadata['datetime'] = dtime
    metadata['time_range'] = time_range
    metadata['source_URI'] = infile
    metadata['source_provider'] = source_provider
    metadata['processing_center'] = ''
    metadata['conversion_software'] = 'Syntool'
    metadata['conversion_version'] = '0.0.0'
    metadata['conversion_datetime'] = stfmt.format_time(datetime.utcnow())
    metadata['parameter'] = ['wind speed', 'wind direction']
    metadata['type'] = 'remote sensing'
    metadata['sensor_type'] = 'SAR'
    metadata['sensor_name'] = sensor_name
    metadata['sensor_platform'] = sensor_platform
    metadata['sensor_mode'] = sensor_mode
    metadata['sensor_swath'] = sensor_swath
    metadata['sensor_polarisation'] = sensor_polarisation
    metadata['sensor_pass'] = sensor_pass
    metadata['datagroup'] = datagroup
    geolocation = {}
    geolocation['projection'] = stfmt.format_gdalprojection()
    geolocation['gcps'] = stfmt.format_gdalgcps(gcplon, gcplat, gcphei, gcppix,
                                                gcplin)
    band = []
    #mask = landflag != 0
    mask = (landflag != 0) | \
        ((windspeed == 0) & (winddirection == 180)) | \
        ((windspeed == 0) & (windquality == 3)) | \
        ((windspeed == 0) & (inversionquality == 2))
    mask = np.ma.getdata(mask)  # we don't want to sum on a masked mask
    valid_percent = np.sum(~mask) / float(mask.size) * 100
    if valid_percent <= valid_percent_min:
        raise Exception(
            'Not enough valid data: {:0.3f}%'.format(valid_percent))
    # if np.all(mask):
    #     raise Exception('Data is fully masked !')
    offset, scale = vmin, (vmax - vmin) / 254.
    np.clip(windspeed, vmin, vmax, out=windspeed)
    array = np.round((windspeed - offset) / scale).astype('uint8')
    array[mask] = 255
    colortable = stfmt.format_colortable('noaa_wind',
                                         vmax=vmax,
                                         vmax_pal=vmax_pal,
                                         vmin=vmin,
                                         vmin_pal=vmin_pal)
    band.append({
        'array': array,
        'scale': scale,
        'offset': offset,
        'description': 'wind speed',
        'unittype': 'm/s',
        'nodatavalue': 255,
        'parameter_range': [vmin, vmax],
        'colortable': colortable
    })
    winddirection = np.mod(90. - winddirection + 180., 360.)
    array = np.round(winddirection / 360. * 254.).astype('uint8')
    array[mask] = 255
    band.append({
        'array': array,
        'scale': 360. / 254.,
        'offset': 0.,
        'description': 'wind direction',
        'unittype': 'deg',
        'nodatavalue': 255,
        'parameter_range': [0, 360.]
    })

    # Write geotiff
    print 'Write geotiff'
    tifffile = stfmt.format_tifffilename(outdir, metadata, create_dir=True)
    stfmt.write_geotiff(tifffile, metadata, geolocation, band)
    # Write projected png/kml
    if pngkml == True:
        print 'Write projected png/kml'
        stfmt.write_pngkml_proj(tifffile)
示例#4
0
def viirs_chlora(infileid,
                 outdir,
                 download_dir='/tmp',
                 vmin=None,
                 vmax=None,
                 contrast='relative',
                 ngcps=(26, 32),
                 open_iterations=1,
                 nprocs=1,
                 pngkml=False,
                 write_netcdf=False):
    """
    """
    if contrast == 'med':
        listbox = [[-6., 35., 2.75, 42.48], [2.74, 30, 42.2, 47.00]]
    elif contrast == 'cwe':
        listbox = [[-23., 35.2, -5.5, 42.88], [-23., 42.8, 2.20, 51.]]
    elif contrast == 'nwe':
        listbox = [[-23., 50.8, 32.7, 68.]]
    elif contrast == 'gom':
        listbox = [[-98., 18.0, -80.5, 30.5]]
    elif contrast == 'agulhas':
        listbox = [[10.8437, -45.7404, 39.9799, -25.3019]]
    elif contrast == 'gs':
        listbox = [[-81.52, 20, -30, 45]]
    else:
        listbox = None
    # Search/Download data
    print 'Search/Download data'
    if re.match(r'^V[0-9]{13}$', infileid) is None:
        raise Exception('Input for viirs_chlora is an ID '
                        '(e.g. V2014093110000)')
    product_id = 'VIIRSL2OC'
    date = datetime.strptime(infileid[1:], '%Y%j%H%M%S')
    viirsocfname = viirs.search_and_download(product_id, date, download_dir)

    # Read/Process data
    print 'Read/Process data'
    # Read from OC file
    viirsocfile = viirs.VIIRSL2File(viirsocfname)
    lon = viirsocfile.read_lon()
    lat = viirsocfile.read_lat()
    chlora = viirsocfile.read_chlora()
    attrs = viirsocfile.read_attributes()
    viirsocfile.close()
    if listbox is not None:
        mask_box = np.zeros(np.shape(chlora.data))
        for i in range(np.shape(listbox)[0]):
            index_in = np.where((lon >= listbox[i][0]) & (lat >= listbox[i][1])
                                & (lon <= listbox[i][2])
                                & (lat <= listbox[i][3]))
            mask_box[index_in] = 1
        mask = (mask_box == 0) | ma.getmaskarray(chlora)
    else:
        mask = ma.getmaskarray(chlora)
    if mask.all():
        print 'No data'
        sys.exit(0)
    # GCPs for resampling and geotiff georeference
    scansize = 16
    dtime0 = datetime.utcnow()
    gcps = resample.get_gcps_from_bowtie(lon, lat, scansize, ngcps=ngcps)
    dtime = datetime.utcnow() - dtime0
    print 'Get GCPs from bowtie swath : {}'.format(dtime)
    gcplon, gcplat, gcpnpixel, gcpnline = gcps
    rspysize = lon.shape[0]
    geod = pyproj.Geod(ellps='WGS84')
    mid = abs(gcpnline[:, 0] - 0.5).argmin()
    xdists = geod.inv(gcplon[mid, :-1], gcplat[mid, :-1], gcplon[mid, 1:],
                      gcplat[mid, 1:])[2]
    xdist = np.sum(xdists) / abs(gcpnpixel[mid, -1] - gcpnpixel[mid, 0])
    rspxsize = np.round(xdist / 750.).astype('int') + 1
    gcpline = gcpnline * rspysize
    gcppixel = gcpnpixel * rspxsize

    # Resample with LinearNDInterpolator in output space
    dtime0 = datetime.utcnow()
    pix, lin = resample.get_points_from_gcps(gcplon,
                                             gcplat,
                                             gcppixel,
                                             gcpline,
                                             rspxsize,
                                             rspysize,
                                             1,
                                             lon,
                                             lat,
                                             nprocs=nprocs) - 0.5
    dtime = datetime.utcnow() - dtime0
    print 'Get input coordinates in new grid : {}'.format(dtime)
    # Test input grid in output space
    # import matplotlib.pyplot as plt
    # for iscan in range(lon.shape[0] / scansize):
    #     pixscan = pix[iscan * scansize: (iscan+1) * scansize, :]
    #     linscan = lin[iscan * scansize: (iscan+1) * scansize, :]
    #     # maskscan = mask[iscan * scansize: (iscan+1) * scansize, :]
    #     # pixscan = pixscan[~maskscan]
    #     # linscan = linscan[~maskscan]
    #     plt.plot(pixscan.flatten(), linscan.flatten(), '+')
    # plt.show()
    # import pdb ; pdb.set_trace()
    # \Test input grid in output space
    dtime0 = datetime.utcnow()
    chlora.data[mask] = np.nan
    rspchlora = resample.resample_bowtie_linear(pix,
                                                lin,
                                                chlora.data,
                                                scansize,
                                                rspxsize,
                                                rspysize,
                                                show=False)
    rspmask = ma.getmaskarray(rspchlora)
    dtime = datetime.utcnow() - dtime0
    print 'Interpolate in new grid : {}'.format(dtime)

    # Take log and open mask
    finalchlora = ma.log(rspchlora)
    finalmask = ~binary_opening(
        ~rspmask, structure=np.ones((3, 3)), iterations=open_iterations)
    finalchlora.mask = finalmask

    # Contrast
    if vmin == None:
        if contrast == 'relative':
            vmin = np.percentile(finalchlora.compressed(), 0.5)
        elif contrast == 'agulhas':
            dayofyear = float(attrs['start_time'].timetuple().tm_yday)
            vmin = -0.5 * np.cos((dayofyear - 45.) * 2. * np.pi / 365.) - 3.
        elif contrast == 'med' or contrast == 'nwe' or contrast == 'cwe':
            vmin = np.percentile(finalchlora.compressed(), 2)
        else:
            raise Exception('Unknown contrast : {}'.format(contrast))
    else:
        if vmin != 0:
            vmin = math.log(vmin)
        else:
            vmin = np.percentile(finalchlora.compressed(), 0.5)
    if vmax == None:
        if contrast == 'relative':
            vmax = np.percentile(finalchlora.compressed(), 99.5)
        elif contrast == 'agulhas':
            dayofyear = float(attrs['start_time'].timetuple().tm_yday)
            vmax = 0.5 * np.cos((dayofyear - 45.) * 2. * np.pi / 365.) + 3.
        elif contrast == 'med':
            vmax = np.percentile(finalchlora.compressed(), 98)
        elif contrast == 'nwe':
            vmax = np.percentile(finalchlora.compressed(), 98)
        elif contrast == 'cwe':
            vmax = np.percentile(finalchlora.compressed(), 98)
        else:
            raise Exception('Unknown contrast : {}'.format(contrast))
    else:
        if vmax != 0:
            vmax = math.log(vmax)
        else:
            vmax = np.percentile(finalchlora.compressed(), 98)

    # Flip (geotiff in "swath sense")
    finalchlora = finalchlora[::-1, ::-1]
    gcppixel = rspxsize - gcppixel
    gcpline = rspysize - gcpline

    # Construct metadata/geolocation/band(s)
    print 'Construct metadata/geolocation/band(s)'
    metadata = {}
    (dtime, time_range) = stfmt.format_time_and_range(attrs['start_time'],
                                                      attrs['stop_time'],
                                                      units='ms')
    metadata['product_name'] = 'Chlorophyll_a_concentration_VIIRS'
    if contrast == 'relative':
        metadata['name'] = os.path.splitext(os.path.basename(viirsocfname))[0]
    else:
        metadata['name'] = '{}_{}'.format(
            os.path.splitext(os.path.basename(viirsocfname))[0], contrast)
    metadata['datetime'] = dtime
    metadata['time_range'] = time_range
    metadata['source_URI'] = viirsocfname
    metadata['source_provider'] = 'NOAA'
    metadata['processing_center'] = 'OceanDataLab'
    metadata['conversion_software'] = 'Syntool'
    metadata['conversion_version'] = '0.0.0'
    metadata['conversion_datetime'] = stfmt.format_time(datetime.utcnow())
    metadata['parameter'] = 'chlorophyll a concentration'
    metadata['type'] = 'remote sensing'
    metadata['sensor_type'] = 'radiometer'
    metadata['sensor_name'] = 'VIIRS'
    metadata['sensor_platform'] = 'Suomi-NPP'
    metadata['sensor_pass'] = attrs['pass']
    geolocation = {}
    geolocation['projection'] = stfmt.format_gdalprojection()
    gcpheight = np.zeros(gcppixel.shape)
    geolocation['gcps'] = stfmt.format_gdalgcps(gcplon, gcplat, gcpheight,
                                                gcppixel, gcpline)
    band = []
    indndv = np.where(ma.getmaskarray(finalchlora) == True)
    offset, scale = vmin, (vmax - vmin) / 254.
    np.clip(finalchlora.data, vmin, vmax, out=finalchlora.data)
    array = np.round((finalchlora.data - offset) / scale).astype('uint8')
    array[indndv] = 255
    colortable = stfmt.format_colortable('chla_jet',
                                         vmax=vmax,
                                         vmax_pal=vmax,
                                         vmin=vmin,
                                         vmin_pal=vmin)
    band.append({
        'array': array,
        'scale': scale,
        'offset': offset,
        'description': 'chlorophyll a concentration',
        'unittype': 'log(mg/m3)',
        'nodatavalue': 255,
        'parameter_range': [vmin, vmax],
        'colortable': colortable
    })

    if write_netcdf == False:
        # Write geotiff
        print 'Write geotiff'
        tifffile = stfmt.format_tifffilename(outdir, metadata, create_dir=True)
        stfmt.write_geotiff(tifffile, metadata, geolocation, band)
        # Write projected png/kml
        if pngkml == True:
            print 'Write projected png/kml'
            stfmt.write_pngkml_proj(tifffile)
    elif write_netcdf == True:
        print 'Write netcdf'
        ncfile = stfmt.format_ncfilename(outdir, metadata, create_dir=True)
        band[0]['name'] = 'chlor_a'
        band[0]['long_name'] = 'Chlorophyll Concentration, OCI Algorithm'
        band[0][
            'standard_name'] = 'mass_concentration_chlorophyll_concentration_in_sea_water'
        band[0]['unittype'] = 'mg m^-3 (log)'
        # ymid = abs(gcpline[:, 0] - rspysize / 2.).argmin()
        # xdists = geod.inv(gcplon[ymid, :-1], gcplat[ymid, :-1],
        #                   gcplon[ymid, 1:], gcplat[ymid, 1:])[2] / \
        #                   np.abs(gcppixel[ymid, 1:] - gcppixel[ymid, :-1])
        # xmid = abs(gcppixel[0, :] - rspxsize / 2.).argmin()
        # ydists = geod.inv(gcplon[:-1, xmid], gcplat[:-1, xmid],
        #                   gcplon[1:, xmid], gcplat[1:, xmid])[2] / \
        #                   np.abs(gcpline[1:, xmid] - gcpline[:-1, xmid])
        # print xdists.min(), xdists.max(), xdists.mean()
        # # e.g. 749.810419844 749.810438261 749.810429577
        # print ydists.min(), ydists.max(), ydists.mean()
        # # e.g. 737.874499629 739.856423757 738.87317625
        metadata['spatial_resolution'] = 750.
        stfmt.write_netcdf(ncfile,
                           metadata,
                           geolocation,
                           band,
                           'swath',
                           ngcps=gcplon.shape)
def sar_doppler_exp(infile,
                    outdir,
                    pngkml=False,
                    vmin=-2.5,
                    vmax=2.5,
                    vmin_pal=-2.5,
                    vmax_pal=2.5):
    """
    """
    # Read/Process data
    print 'Read/Process data'
    sardop = Dataset(infile)
    mission = sardop.MISSIONNAME
    if mission == 'S1A':
        sensor_name = 'Sentinel-1A'
        sensor_platform = 'Sentinel-1A'
        source_provider = 'ESA'
    else:
        raise Exception('S1A mission expected.')
    doptime = sardop.variables['rvlZeroDopplerTime'][:]
    start_time = datetime.strptime(''.join(list(doptime[0, 0, :])),
                                   '%Y-%m-%dT%H:%M:%S.%f')
    stop_time = datetime.strptime(''.join(list(doptime[-1, -1, :])),
                                  '%Y-%m-%dT%H:%M:%S.%f')
    heading = sardop.variables['rvlHeading'][:]
    if np.sin((90 - heading.mean()) * np.pi / 180) > 0:
        sensor_pass = '******'
    else:
        sensor_pass = '******'
    # safe_name = os.path.basename(os.path.dirname(os.path.dirname(infile)))
    # sensor_mode = safe_name.split('_')[1]
    # if sensor_mode not in ['S1', 'S2', 'S3', 'S4', 'S5', 'S6', 'IW', 'EW']:
    #     raise Exception('S[1-6]/IW/EW modes expected.')
    # sensor_swath = os.path.basename(infile).split('-')[1].upper()
    # sensor_polarisation = sardop.read_global_attribute('polarisation')
    # datagroup = safe_name.replace('.SAFE', '')
    # pid = datagroup.split('_')[-1]
    # dataname = os.path.splitext(os.path.basename(infile))[0] + '-' + pid
    dataname = os.path.splitext(os.path.basename(infile))[0]
    sensor_mode = dataname.split('_')[1]
    sensor_swath = sensor_mode
    sensor_polarisation = sardop.POLARISATION
    radvel = sardop.variables['rvlRadVel'][:]
    sweepangle = sardop.variables['rvlSweepAngle'][:]
    radvel = descalloping(radvel, sweepangle)
    radvel = smooth(radvel)
    inc = sardop.variables['rvlIncidenceAngle'][:]
    radvel /= np.sin(np.deg2rad(inc))
    #landflag = sardop.variables['rvlLandFlag'][:]
    lon = sardop.variables['rvlLon'][:]
    lat = sardop.variables['rvlLat'][:]
    if sensor_pass == 'Ascending':
        radvel *= -1
    ngcps = np.ceil(np.array(lon.shape) / 10.) + 1
    pix = np.linspace(0, lon.shape[1] - 1,
                      num=ngcps[1]).round().astype('int32')
    lin = np.linspace(0, lon.shape[0] - 1,
                      num=ngcps[0]).round().astype('int32')
    pix2d, lin2d = np.meshgrid(pix, lin)
    gcplon = lon[lin2d, pix2d]
    gcplat = lat[lin2d, pix2d]
    gcppix = pix2d + 0.5
    gcplin = lin2d + 0.5
    gcphei = np.zeros(ngcps)
    if gcplon.min() < -135 and gcplon.max() > 135:
        gcplon[np.where(gcplon < 0)] += 360.

    # Construct metadata/geolocation/band(s)
    print 'Construct metadata/geolocation/band(s)'
    metadata = {}
    (dtime, time_range) = stfmt.format_time_and_range(start_time,
                                                      stop_time,
                                                      units='ms')
    metadata['product_name'] = 'SAR_doppler_exp'
    metadata['name'] = dataname
    metadata['datetime'] = dtime
    metadata['time_range'] = time_range
    metadata['source_URI'] = infile
    metadata['source_provider'] = source_provider
    metadata['processing_center'] = ''
    metadata['conversion_software'] = 'Syntool'
    metadata['conversion_version'] = '0.0.0'
    metadata['conversion_datetime'] = stfmt.format_time(datetime.utcnow())
    metadata['parameter'] = 'radial horizontal velocities'
    metadata['type'] = 'remote sensing'
    metadata['sensor_type'] = 'SAR'
    metadata['sensor_name'] = sensor_name
    metadata['sensor_platform'] = sensor_platform
    metadata['sensor_mode'] = sensor_mode
    metadata['sensor_swath'] = sensor_swath
    metadata['sensor_polarisation'] = sensor_polarisation
    metadata['sensor_pass'] = sensor_pass
    # metadata['datagroup'] = datagroup
    geolocation = {}
    geolocation['projection'] = stfmt.format_gdalprojection()
    geolocation['gcps'] = stfmt.format_gdalgcps(gcplon, gcplat, gcphei, gcppix,
                                                gcplin)
    band = []
    #indndv = np.where(landflag != 0)
    offset, scale = vmin, (vmax - vmin) / 254.
    np.clip(radvel, vmin, vmax, out=radvel)
    array = np.round((radvel - offset) / scale).astype('uint8')
    #array[indndv] = 255
    colortable = stfmt.format_colortable('doppler',
                                         vmax=vmax,
                                         vmax_pal=vmax_pal,
                                         vmin=vmin,
                                         vmin_pal=vmin_pal)
    band.append({
        'array': array,
        'scale': scale,
        'offset': offset,
        'description': 'radial horizontal velocities',
        'unittype': 'm/s',
        'nodatavalue': 255,
        'parameter_range': [vmin, vmax],
        'colortable': colortable
    })
    # Write geotiff
    print 'Write geotiff'
    tifffile = stfmt.format_tifffilename(outdir, metadata, create_dir=True)
    stfmt.write_geotiff(tifffile, metadata, geolocation, band)
    # Write projected png/kml
    if pngkml == True:
        print 'Write projected png/kml'
        stfmt.write_pngkml_proj(tifffile)
示例#6
0
def sar_roughness(infile,
                  outdir,
                  pngkml=False,
                  contrast=None,
                  vmin=None,
                  vmax=None,
                  landmaskpath=None,
                  write_netcdf=False,
                  gcp2height=0):
    """
    """
    # Read/Process data
    print 'Read/Process data'
    sarmp = SAFEGeoTiffFile(infile)
    sarim = SARImage(sarmp)
    mission = sarim.get_info('mission')
    if mission == 'S1A':
        sensor_name = 'Sentinel-1A'
        sensor_platform = 'Sentinel-1A'
        source_provider = 'ESA'
    elif mission == 'S1B':
        sensor_name = 'Sentinel-1B'
        sensor_platform = 'Sentinel-1B'
        source_provider = 'ESA'
    else:
        raise Exception('Unknown mission')
    timefmt = '%Y-%m-%dT%H:%M:%S.%f'
    start_time = datetime.strptime(sarim.get_info('start_time'), timefmt)
    stop_time = datetime.strptime(sarim.get_info('stop_time'), timefmt)
    sensor_pass = sarim.get_info('pass')
    sensor_mode = sarim.get_info('mode')
    sensor_swath = sarim.get_info('swath')
    sensor_polarisation = sarim.get_info('polarisation')
    product = sarim.get_info('product')
    if product == 'GRD':
        spacing = [2, 2]
    elif product == 'SLC':
        if sensor_mode == 'WV':
            mspacing = (15, 15)
        elif re.match(r'^S[1-6]$', sensor_mode) != None:
            mspacing = (15, 15)
        elif sensor_mode == 'IW':
            raise Exception('sar_roughness for IW SLC ?')
        elif sensor_mode == 'EW':
            raise Exception('sar_roughness for EW SLC ?')
        else:
            raise Exception('Unkown S1 mode : {}'.format(sensor_mode))
        spacing = np.round(sarim.meters2pixels(mspacing))
    else:
        raise Exception('Unkown S1 product : {}'.format(product))
    mspacing = sarim.pixels2meters(spacing)
    datagroup = sarim.get_info('safe_name').replace('.SAFE', '')
    pid = datagroup.split('_')[-1]
    dataname = os.path.splitext(os.path.basename(infile))[0] + '-' + pid
    ssr = np.sqrt(sarim.get_data('roughness', spacing=spacing))
    ########## TMP calibration constant ##########
    # if sensor_mode == 'WV':
    #     caldir = '/home/cercache/project/mpc-sentinel1/analysis/s1_data_analysis/L1/WV/S1A_WV_SLC__1S/cal_cste'
    #     if sensor_polarisation == 'HH':
    #         if sensor_swath == 'WV1':
    #             caltmp = (55.80+56.91)/2.
    #             calname = 'cal_cste_hh_wv1.pkl'
    #         elif sensor_swath == 'WV2':
    #             caltmp = (40.65+40.32)/2.
    #             calname = 'cal_cste_hh_wv2.pkl'
    #     elif sensor_polarisation == 'VV':
    #         if sensor_swath == 'WV1':
    #             caltmp = 58.24
    #             calname = 'cal_cste_vv_wv1.pkl'
    #         elif sensor_swath == 'WV2':
    #             caltmp = 49.02
    #             calname = 'cal_cste_vv_wv2.pkl'
    #     calpath = os.path.join(caldir, calname)
    #     if os.path.exists(calpath) == True:
    #         caltmp = get_caltmp(calpath, start_time)
    # elif re.match(r'^S[1-6]$', sensor_mode) != None:
    #     if start_time < datetime(2014, 7, 16, 0, 0, 0):
    #         if sensor_mode == 'S6':
    #             raise Exception('S6 calibration missing')
    #         sm2cal = {'S1':58., 'S2':56., 'S3':52., 'S4':52., 'S5':49.}
    #     else:
    #         # from commissioning phase report
    #         sm2cal = {'S1':3., 'S2':5., 'S3':-1.5, 'S4':4., 'S5':1., 'S6':4.75}
    #     caltmp = sm2cal[sensor_mode]
    # elif sensor_mode == 'IW':
    #     if start_time < datetime(2014, 7, 16, 0, 0, 0):
    #         caltmp = 109.
    #     else:
    #         caltmp = 3. # from commissioning phase report
    # elif sensor_mode == 'EW':
    #     if start_time < datetime(2014, 7, 16, 0, 0, 0):
    #         caltmp = 94.
    #     else:
    #         caltmp = -1. # <- -2. # from commissioning phase report
    # else:
    #     raise Exception('Which tmp calibration constant for this mode ?')
    # print '--> caltmp=%f' % caltmp
    # ssr *= np.sqrt(10 ** (caltmp / 10.))
    ########## /TMP calibration constant ##########
    dim = ssr.shape
    # Set contrast
    if vmin == None or vmax == None:
        if contrast == None:
            if sensor_mode == 'WV':
                contrast = 'relative'
            else:
                contrast = 'sea'
        if contrast == 'relative':
            if sensor_mode == 'WV':
                noborder = [
                    slice(int(dim[0] * .05), int(dim[0] * .95)),
                    slice(int(dim[1] * .05), int(dim[1] * .95))
                ]
            else:
                noborder = [
                    slice(int(dim[0] * .05), int(dim[0] * .95)),
                    slice(int(dim[1] * .1), int(dim[1] * .9))
                ]
            values = ssr[noborder]
            if landmaskpath != None and os.path.exists(landmaskpath):
                lmspacing = np.round(sarim.meters2pixels(111.32 / 120 * 1000))
                lmspacing -= np.mod(lmspacing, spacing)
                lon = sarim.get_data('lon', spacing=lmspacing)
                lat = sarim.get_data('lat', spacing=lmspacing)
                lmdim = (lon.shape[0] + 1, lon.shape[1] + 1)
                landmask = np.ones(lmdim, dtype=bool)
                landmask[:-1, :-1] = get_landmask(lon, lat, landmaskpath)
                lmfac = lmspacing / spacing
                landmask = np.repeat(landmask, lmfac[0], axis=0)
                landmask = np.repeat(landmask, lmfac[1], axis=1)
                seaindex = np.where(landmask[noborder] == False)
                if seaindex[0].size >= ssr.size * 0.01:
                    values = values[seaindex]
            if vmin == None:
                vmin = scoreatpercentile(values, 0.1)
            if vmax == None:
                vmax = scoreatpercentile(values, 99.9)
        elif contrast == 'sea':
            if sensor_polarisation in ['HH', 'VV']:
                if vmin == None:
                    vmin = 0.
                if vmax == None:
                    vmax = 2.
            else:
                if vmin == None:
                    vmin = 1.
                if vmax == None:
                    vmax = 3.
        elif contrast == 'ice':
            if sensor_polarisation in ['HH', 'VV']:
                if vmin == None:
                    vmin = 0.
                if vmax == None:
                    vmax = 3.5
            else:
                if vmin == None:
                    vmin = 1.
                if vmax == None:
                    vmax = 5.
        else:
            raise Exception('Unknown contrast name.')
    print '--> vmin=%f vmax=%f' % (vmin, vmax)
    ssr = ssr[::-1, :]  # keep SAR orientation for geotiff
    geoloc = sarim.get_info('geolocation_grid')
    gcplin = (dim[0] * spacing[0] - 1 - geoloc['line'] + 0.5) / spacing[0]
    gcppix = (geoloc['pixel'] + 0.5) / spacing[1]
    gcplon = geoloc['longitude']
    gcplat = geoloc['latitude']
    gcphei = geoloc['height']
    if gcp2height is not None:
        geod = pyproj.Geod(ellps='WGS84')
        gcpforw, gcpback, _ = geod.inv(gcplon[:, :-1], gcplat[:, :-1],
                                       gcplon[:, 1:], gcplat[:, 1:])
        gcpforw = np.hstack((gcpforw, gcpforw[:, [-1]]))
        gcpback = np.hstack((gcpback[:, [0]], gcpback))
        gcpinc = geoloc['incidence_angle']
        mvdist = (gcp2height - gcphei) / np.tan(np.deg2rad(gcpinc))
        mvforw = gcpforw
        indneg = np.where(mvdist < 0)
        mvdist[indneg] = -mvdist[indneg]
        mvforw[indneg] = gcpback[indneg]
        _gcplon, _gcplat, _ = geod.fwd(gcplon, gcplat, mvforw, mvdist)
        gcplon = _gcplon
        gcplat = _gcplat
        gcphei.fill(gcp2height)
    if gcplon.min() < -135 and gcplon.max() > 135:
        gcplon[np.where(gcplon < 0)] += 360.
    # Construct metadata/geolocation/band(s)
    print 'Construct metadata/geolocation/band(s)'
    metadata = {}
    (dtime, time_range) = stfmt.format_time_and_range(start_time,
                                                      stop_time,
                                                      units='ms')
    if sensor_polarisation in ['HH', 'VV']:
        metadata['product_name'] = 'SAR_roughness'
    else:
        metadata['product_name'] = 'SAR_roughness_crosspol'
    metadata['name'] = dataname
    metadata['datetime'] = dtime
    metadata['time_range'] = time_range
    metadata['source_URI'] = infile
    metadata['source_provider'] = source_provider
    metadata['processing_center'] = 'OceanDataLab'
    metadata['conversion_software'] = 'Syntool'
    metadata['conversion_version'] = '0.0.0'
    metadata['conversion_datetime'] = stfmt.format_time(datetime.utcnow())
    metadata['parameter'] = 'sea surface roughness'
    metadata['type'] = 'remote sensing'
    metadata['sensor_type'] = 'SAR'
    metadata['sensor_name'] = sensor_name
    metadata['sensor_platform'] = sensor_platform
    metadata['sensor_mode'] = sensor_mode
    metadata['sensor_swath'] = sensor_swath
    metadata['sensor_polarisation'] = sensor_polarisation
    metadata['sensor_pass'] = sensor_pass
    metadata['datagroup'] = datagroup
    geolocation = {}
    geolocation['projection'] = sarim._mapper._handler.GetGCPProjection()
    geolocation['gcps'] = stfmt.format_gdalgcps(gcplon, gcplat, gcphei, gcppix,
                                                gcplin)
    band = []
    scale = (vmax - vmin) / 254.
    offset = vmin
    indzero = np.where(ssr == 0)
    array = np.clip(np.round((ssr - offset) / scale), 0, 254).astype('uint8')
    array[indzero] = 255
    band.append({
        'array': array,
        'scale': scale,
        'offset': offset,
        'description': 'sea surface roughness',
        'unittype': '',
        'nodatavalue': 255,
        'parameter_range': [vmin, vmax]
    })
    # Write
    if write_netcdf == False:
        print 'Write geotiff'
        tifffile = stfmt.format_tifffilename(outdir, metadata, create_dir=True)
        stfmt.write_geotiff(tifffile, metadata, geolocation, band)
        # Write projected png/kml
        if pngkml == True:
            print 'Write projected png/kml'
            stfmt.write_pngkml_proj(tifffile)
    elif write_netcdf == True:
        print 'Write netcdf'
        ncfile = stfmt.format_ncfilename(outdir, metadata, create_dir=True)
        band[0]['name'] = 'sea_surface_roughness'
        band[0]['long_name'] = 'sea surface roughness'
        band[0]['unittype'] = '1'
        metadata['spatial_resolution'] = mspacing.min()
        stfmt.write_netcdf(ncfile,
                           metadata,
                           geolocation,
                           band,
                           'swath',
                           ngcps=gcplon.shape)
示例#7
0
def sar_wave(infile, outdir, pngkml=False):
    """
    """
    # Read/Process data
    print 'Read/Process data'
    sarwave = SAFEOCNNCFile(infile, product='WAVE')
    mission = sarwave.read_global_attribute('missionName')
    if mission == 'S1A':
        sensor_name = 'Sentinel-1A'
        sensor_platform = 'Sentinel-1A'
        source_provider = 'ESA'
    else:
        raise Exception('S1A mission expected.')
    # start_time = sarwave.get_start_time() # WARNING : whole SAFE for imagettes !
    # stop_time = sarwave.get_end_time() # WARNING : whole SAFE for imagettes !
    start_t = sarwave.read_global_attribute('firstMeasurementTime')
    if '.' in start_t:
        start_time = datetime.strptime(start_t, '%Y-%m-%dT%H:%M:%S.%fZ')
    else:
        start_time = datetime.strptime(start_t, '%Y-%m-%dT%H:%M:%SZ')
    stop_t = sarwave.read_global_attribute('lastMeasurementTime')
    if '.' in stop_t:
        stop_time = datetime.strptime(stop_t, '%Y-%m-%dT%H:%M:%S.%fZ')
    else:
        stop_time = datetime.strptime(stop_t, '%Y-%m-%dT%H:%M:%SZ')
    heading = sarwave.read_values('oswHeading')
    if np.sin((90 - heading[0, 0]) * np.pi / 180) > 0:
        sensor_pass = '******'
    else:
        sensor_pass = '******'
    safe_name = os.path.basename(os.path.dirname(os.path.dirname(infile)))
    sensor_mode = safe_name.split('_')[1]
    if sensor_mode not in ['WV', 'S1', 'S2', 'S3', 'S4', 'S5', 'S6']:
        raise Exception('WV/S[1-6] modes expected.')
    sensor_swath = os.path.basename(infile).split('-')[1].upper()
    sensor_polarisation = sarwave.read_global_attribute('polarisation')
    datagroup = safe_name.replace('.SAFE', '')
    pid = datagroup.split('_')[-1]
    dataname = os.path.splitext(os.path.basename(infile))[0] + '-' + pid

    # Make wave spectrum figure
    spec = sarwave.read_values('oswPolSpec')
    k = sarwave.read_values('oswK')
    phi = sarwave.read_values('oswPhi')
    npartitions = sarwave.get_dimsize('oswPartitions')
    partitions = sarwave.read_values('oswPartitions')
    # TMP Bug : there are now 3 partitions, numbered 0, 1 and 3 ...
    if npartitions == 3:
        indp2 = np.where(partitions == 2)
        indp3 = np.where(partitions == 3)
        if indp2[0].size == 0 and indp3[0].size != 0:
            partitions[indp3] = 2
    # /TMP
    hs = sarwave.read_values('oswHs')
    flag = sarwave.read_values('oswLandFlag')
    if sensor_mode == 'WV':
        imnum = int(
            os.path.splitext(os.path.basename(infile))[0].split('-')[-1])
    else:
        imnum = None
    spec_size = (512, 512)
    fontsize = 'small'
    cmap = getColorMap(rgbFile='wind.pal')
    fig = make_spec_fig(spec,
                        k,
                        phi,
                        heading,
                        npartitions,
                        partitions,
                        hs,
                        flag,
                        imnum=imnum,
                        spec_size=spec_size,
                        fontsize=fontsize,
                        cmap=cmap)
    rgb = fig2rgb(fig)
    plt.close(fig)

    # Make geolocation
    if sensor_mode == 'WV':
        lon = sarwave.read_values('lon')[0, 0]
        lat = sarwave.read_values('lat')[0, 0]
        grdrasize = sarwave.read_values('oswGroundRngSize')[0, 0]
        grdazsize = sarwave.read_values('oswAziSize')[0, 0]
        geod = pyproj.Geod(ellps='WGS84')
        lons = np.repeat(lon, 2)
        lats = np.repeat(lat, 2)
        az = heading[0, 0] + [0., 180.]
        dist = np.repeat(grdazsize / 2., 2)
        lonsmid, latsmid, dummy = geod.fwd(lons, lats, az, dist)
        lons = np.repeat(lonsmid, 2)
        lats = np.repeat(latsmid, 2)
        az = heading[0, 0] + [-90, 90., 90., -90.]
        dist = np.repeat(grdrasize / 2., 4)
        gcplon, gcplat, dummy = geod.fwd(lons, lats, az, dist)
        gcppix = np.array([0, spec_size[0], spec_size[0], 0])
        gcplin = np.array([0, 0, spec_size[1], spec_size[1]])
        if np.sin((90 - heading[0, 0]) * np.pi / 180) < 0:
            # descending pass
            gcppix = spec_size[0] - gcppix
            gcplin = spec_size[1] - gcplin
        gcphei = np.zeros(gcplin.size)
    else:
        gcplon = sarwave.read_values('lon')
        gcplat = sarwave.read_values('lat')
        gcphei = np.zeros(gcplon.shape)
        nra = sarwave.get_dimsize('oswRaSize')
        pix = np.arange(nra) * spec_size[0] + spec_size[0] / 2.
        naz = sarwave.get_dimsize('oswAzSize')
        lin = np.arange(naz - 1, -1, -1) * spec_size[1] + spec_size[1] / 2.
        gcppix, gcplin = np.meshgrid(pix, lin)
        if np.sin((90 - heading[0, 0]) * np.pi / 180) < 0:
            # descending pass
            gcppix = nra * spec_size[0] - gcppix
            gcplin = naz * spec_size[1] - gcplin
    if gcplon.min() < -135 and gcplon.max() > 135:
        gcplon[np.where(gcplon < 0)] += 360.

    # Construct metadata/geolocation/band(s)
    print 'Construct metadata/geolocation/band(s)'
    metadata = {}
    (dtime, time_range) = stfmt.format_time_and_range(start_time,
                                                      stop_time,
                                                      units='ms')
    metadata['product_name'] = 'SAR_wave'
    metadata['name'] = dataname
    metadata['datetime'] = dtime
    metadata['time_range'] = time_range
    metadata['source_URI'] = infile
    metadata['source_provider'] = source_provider
    metadata['processing_center'] = ''  #'OceanDataLab'
    metadata['conversion_software'] = 'Syntool'
    metadata['conversion_version'] = '0.0.0'
    metadata['conversion_datetime'] = stfmt.format_time(datetime.utcnow())
    metadata['parameter'] = ''
    metadata['type'] = 'remote sensing'
    metadata['sensor_type'] = 'SAR'
    metadata['sensor_name'] = sensor_name
    metadata['sensor_platform'] = sensor_platform
    metadata['sensor_mode'] = sensor_mode
    metadata['sensor_swath'] = sensor_swath
    metadata['sensor_polarisation'] = sensor_polarisation
    metadata['sensor_pass'] = sensor_pass
    metadata['datagroup'] = datagroup
    geolocation = {}
    geolocation['projection'] = stfmt.format_gdalprojection()
    geolocation['gcps'] = stfmt.format_gdalgcps(gcplon, gcplat, gcphei, gcppix,
                                                gcplin)
    band = []
    band.append({'array': rgb[:, :, 0]})
    band.append({'array': rgb[:, :, 1]})
    band.append({'array': rgb[:, :, 2]})

    # Write geotiff
    print 'Write geotiff'
    tifffile = stfmt.format_tifffilename(outdir, metadata, create_dir=True)
    stfmt.write_geotiff(tifffile, metadata, geolocation, band)
    # Write projected png/kml
    if pngkml == True:
        print 'Write projected png/kml'
        stfmt.write_pngkml_proj(tifffile)
def sar_doppler(infile, outdir, pngkml=False,
                vmin=-2.5, vmax=2.5, vmin_pal=-2.5, vmax_pal=2.5):
    """
    """
    # Read/Process data
    print 'Read/Process data'
    sardop = SAFEOCNNCFile(infile, product='DOPPLER')
    mission = sardop.read_global_attribute('missionName')
    if mission == 'S1A':
        sensor_name = 'Sentinel-1A'
        sensor_platform = 'Sentinel-1A'
        source_provider = 'ESA'
    else:
        raise Exception('S1A mission expected.')
    start_time = sardop.get_start_time()
    stop_time = sardop.get_end_time()
    heading = sardop.read_values('rvlHeading')
    if np.sin((90 - heading.mean()) * np.pi / 180) > 0:
        sensor_pass = '******'
    else:
        sensor_pass = '******'
    safe_name = os.path.basename(os.path.dirname(os.path.dirname(infile)))
    sensor_mode = safe_name.split('_')[1]
    if sensor_mode not in ['S1', 'S2', 'S3', 'S4', 'S5', 'S6', 'IW', 'EW']:
        raise Exception('S[1-6]/IW/EW modes expected.')
    sensor_swath = os.path.basename(infile).split('-')[1].upper()
    sensor_polarisation = sardop.read_global_attribute('polarisation')
    datagroup = safe_name.replace('.SAFE', '')
    pid = datagroup.split('_')[-1]
    dataname = os.path.splitext(os.path.basename(infile))[0] + '-' + pid
    if 'rvlSwath' in sardop.get_dimensions():
        nswath = sardop.get_dimsize('rvlSwath')
    else:
        nswath = 1
    for iswath in range(nswath):

        if nswath == 1:
            radvel = sardop.read_values('rvlRadVel')
            landflag = sardop.read_values('rvlLandFlag')
            lon = sardop.read_values('lon')
            lat = sardop.read_values('lat')
            name = dataname
        else:
            radvel = sardop.read_values('rvlRadVel')[:, :, iswath]
            landflag = sardop.read_values('rvlLandFlag')[:, :, iswath]
            lon = sardop.read_values('lon')[:, :, iswath]
            lat = sardop.read_values('lat')[:, :, iswath]
            valid = np.where((ma.getmaskarray(lon) == False) & \
                             (ma.getmaskarray(lat) == False))
            slices = [slice(valid[0].min(), valid[0].max() + 1),
                      slice(valid[1].min(), valid[1].max() + 1)]
            radvel = radvel[slices]
            landflag = landflag[slices]
            lon = lon[slices]
            lat = lat[slices]
            name = dataname + '-' + str(iswath+1)

        if sensor_pass == 'Ascending':
            radvel *= -1
        ngcps = np.ceil(np.array(lon.shape) / 10.) + 1
        pix = np.linspace(0, lon.shape[1] - 1, num=ngcps[1]).round().astype('int32')
        lin = np.linspace(0, lon.shape[0] - 1, num=ngcps[0]).round().astype('int32')
        pix2d, lin2d = np.meshgrid(pix, lin)
        gcplon = lon[lin2d, pix2d]
        gcplat = lat[lin2d, pix2d]
        gcppix = pix2d + 0.5
        gcplin = lin2d + 0.5
        gcphei = np.zeros(ngcps)
        if gcplon.min() < -135 and gcplon.max() > 135:
            gcplon[np.where(gcplon < 0)] += 360.

        # Construct metadata/geolocation/band(s)
        print 'Construct metadata/geolocation/band(s)'
        metadata = {}
        (dtime, time_range) = stfmt.format_time_and_range(start_time, stop_time,
                                                          units='ms')
        metadata['product_name'] = 'SAR_doppler'
        metadata['name'] = name
        metadata['datetime'] = dtime
        metadata['time_range'] = time_range
        metadata['source_URI'] = infile
        metadata['source_provider'] = source_provider
        metadata['processing_center'] = '' #'OceanDataLab'
        metadata['conversion_software'] = 'Syntool'
        metadata['conversion_version'] = '0.0.0'
        metadata['conversion_datetime'] = stfmt.format_time(datetime.utcnow())
        metadata['parameter'] = 'radial horizontal velocities'
        metadata['type'] = 'remote sensing'
        metadata['sensor_type'] = 'SAR'
        metadata['sensor_name'] = sensor_name
        metadata['sensor_platform'] = sensor_platform
        metadata['sensor_mode'] = sensor_mode
        metadata['sensor_swath'] = sensor_swath
        metadata['sensor_polarisation'] = sensor_polarisation
        metadata['sensor_pass'] = sensor_pass
        metadata['datagroup'] = datagroup
        geolocation = {}
        geolocation['projection'] = stfmt.format_gdalprojection()
        geolocation['gcps'] = stfmt.format_gdalgcps(gcplon, gcplat, gcphei,
                                                    gcppix, gcplin)
        band = []
        #indndv = np.where(landflag != 0)
        offset, scale = vmin, (vmax-vmin)/254.
        np.clip(radvel, vmin, vmax, out=radvel)
        array = np.round((radvel - offset) / scale).astype('uint8')
        #array[indndv] = 255
        colortable = stfmt.format_colortable('doppler', vmax=vmax, vmax_pal=vmax_pal,
                                             vmin=vmin, vmin_pal=vmin_pal)
        band.append({'array':array, 'scale':scale, 'offset':offset,
                     'description':'radial horizontal velocities', 'unittype':'m/s',
                     'nodatavalue':255, 'parameter_range':[vmin, vmax],
                     'colortable':colortable})
        # Write geotiff
        print 'Write geotiff'
        tifffile = stfmt.format_tifffilename(outdir, metadata, create_dir=True)
        stfmt.write_geotiff(tifffile, metadata, geolocation, band)
        # Write projected png/kml
        if pngkml == True:
            print 'Write projected png/kml'
            stfmt.write_pngkml_proj(tifffile)
def sar_xspec(infile, outdir, pngkml=False, vmax_re=None, vmax_im=None,
              make_rgb=True, ncolors=74):
    """
    """
    # Read/Process data
    print 'Read/Process data'
    sarim = sarimage(infile)
    mission = sarim.get_info('mission')
    if mission == 'S1A':
        sensor_name = 'Sentinel-1A'
        sensor_platform = 'Sentinel-1A'
        source_provider = 'ESA'
    else:
        raise Exception('S1A mission expected.')
    product = sarim.get_info('product')
    if product != 'SLC':
        raise Exception('SLC expected.')
    timefmt = '%Y-%m-%dT%H:%M:%S.%f'
    start_time = datetime.strptime(sarim.get_info('start_time'), timefmt)
    stop_time = datetime.strptime(sarim.get_info('stop_time'), timefmt)
    sensor_pass = sarim.get_info('pass')
    sensor_mode = sarim.get_info('mode')
    sensor_swath = sarim.get_info('swath')
    sensor_polarisation = sarim.get_info('polarisation')
    datagroup = sarim.get_info('safe_name').replace('.SAFE', '')
    pid = datagroup.split('_')[-1]
    dataname = os.path.splitext(os.path.basename(infile))[0] + '-' + pid
    # Compute SAR Xspec and make figures
    if sensor_mode == 'WV':
        azi_periodo_size = 1024
        azi_dist, ran_dist = 20000., 20000. # ignored in WV case
        xspec_size = (512, 512)
        fontsize = 'small'
    elif re.match(r'^S[1-6]$', sensor_mode) != None:
        azi_periodo_size = 1024
        azi_dist, ran_dist = 10000., 10000.
        xspec_size = (512, 512) #(256, 256)
        fontsize = 'small' #'x-small'
    elif sensor_mode in ['IW', 'EW']:
        azi_periodo_size = 512
        azi_dist, ran_dist = 10000., 10000.
        xspec_size = (512, 512) #(256, 256)
        fontsize = 'small' #'x-small'
    else:
        raise Exception('Which settings for this mode ?')
    sarxspec = sarimage2sarxspec_loop(sarim, azi_dist=azi_dist, ran_dist=ran_dist,
                                      azi_periodo_size=azi_periodo_size)
    cmap_re, cmap_im = get_cmaps(ncolors=ncolors)
    fig_re = make_sarxspec_fig(sarxspec, part='real', tau=1, kmax=2*np.pi/75,
                               kmin=2*np.pi/400, xspec_size=xspec_size,
                               uniq_vmax=True, north_oriented=True,
                               klim=[2*np.pi/400, 2*np.pi/200, 2*np.pi/100],
                               north_arrow=False, index_pos=None, vmax_pos='tr',
                               nvar_pos=None, fontsize=fontsize,
                               vmax=vmax_re, cmap=cmap_re)
    if sensor_mode == 'WV':
        ax = fig_re.gca()
        imnum = sarim.get_info('image_number')
        imnumtxt = '#{:03d}'.format(imnum)
        ax.text(0.51, 0.99, imnumtxt, transform=ax.transAxes,
                ha='left', va='top', fontsize=fontsize)
    if make_rgb == True:
        rgb_re = fig2rgb(fig_re)
        #print nuniqcolors(rgb_re)
    else:
        img_re, pal_re = fig2imgpal(fig_re, cmap_re)
    plt.close(fig_re)
    fig_im = make_sarxspec_fig(sarxspec, part='imag', tau=1, kmax=2*np.pi/75,
                               kmin=2*np.pi/400, xspec_size=xspec_size,
                               uniq_vmax=True, north_oriented=True,
                               klim=[2*np.pi/400, 2*np.pi/200, 2*np.pi/100],
                               north_arrow=False, index_pos=None, vmax_pos='tr',
                               nvar_pos=None, fontsize=fontsize,
                               vmax=vmax_im, cmap=cmap_im)
    if sensor_mode == 'WV':
        ax = fig_im.gca()
        imnum = sarim.get_info('image_number')
        imnumtxt = '#{:03d}'.format(imnum)
        ax.text(0.51, 0.99, imnumtxt, transform=ax.transAxes,
                ha='left', va='top', fontsize=fontsize)
    if make_rgb == True:
        rgb_im = fig2rgb(fig_im)
        #print nuniqcolors(rgb_im)
    else:
        img_im, pal_im = fig2imgpal(fig_im, cmap_im)
    plt.close(fig_im)
    if make_rgb == True:
        nlin, npix = rgb_re.shape[0:2]
    else:
        nlin, npix = img_re.shape
    # Handle GCPS
    # geoloc = sarim.get_info('geolocation_grid')
    # pix = np.array([0, geoloc['npixels']-1, geoloc['npixels']-1, 0])
    # lin = np.array([0, 0, geoloc['nlines']-1, geoloc['nlines']-1])
    # gcplon = geoloc['longitude'][lin, pix]
    # gcplat = geoloc['latitude'][lin, pix]
    # gcphei = np.zeros(4)
    # gcppix = np.array([0, 512, 512, 0])
    # gcplin = np.array([0, 0, 512, 512])
    #############################################
    # geoloc = sarim.get_info('geolocation_grid')
    # gcplon = geoloc['longitude']
    # gcplat = geoloc['latitude']
    # gcphei = np.zeros(gcplon.shape)
    # geod = pyproj.Geod(ellps='WGS84')
    # nglin, ngpix = geoloc['nlines'], geoloc['npixels']
    # ra_geo_spacing = geod.inv(gcplon[nglin/2, 0:-1], gcplat[nglin/2, 0:-1],
    #                           gcplon[nglin/2, 1:], gcplat[nglin/2, 1:])[2]
    # ra_geo_dist = np.hstack((0., ra_geo_spacing.cumsum()))
    # ra_geo_ndist = ra_geo_dist/ra_geo_dist[-1]
    # gcppix = np.tile((ra_geo_ndist*npix).reshape((1, -1)), (nglin, 1))
    # az_geo_spacing = geod.inv(gcplon[0:-1, ngpix/2], gcplat[0:-1, ngpix/2],
    #                           gcplon[1:, ngpix/2], gcplat[1:, ngpix/2])[2]
    # az_geo_dist = np.hstack((0., az_geo_spacing.cumsum()))
    # az_geo_ndist = az_geo_dist/az_geo_dist[-1]
    # gcplin = np.tile((az_geo_ndist*nlin).reshape((-1, 1)), (1, ngpix))
    # import pdb ; pdb.set_trace()
    #############################################
    #import pdb ; pdb.set_trace()
    ext_min = sarxspec[0][0].get_info('extent')[0:2]
    ext_max = sarxspec[-1][-1].get_info('extent')[2:4]
    # geoloc = sarim.get_info('geolocation_grid')
    # nglin, ngpix = geoloc['nlines'], geoloc['npixels']
    nglin, ngpix = len(sarxspec)+1, len(sarxspec[0])+1
    pix = np.hstack((np.round(np.linspace(ext_min[1], ext_max[1], num=ngpix)),
                     np.ones(nglin)*ext_max[1],
                     np.round(np.linspace(ext_max[1], ext_min[1], num=ngpix)),
                     np.ones(nglin)*ext_min[1]))
    lin = np.hstack((np.ones(ngpix)*ext_min[0],
                     np.round(np.linspace(ext_min[0], ext_max[0], num=nglin)),
                     np.ones(ngpix)*ext_max[0],
                     np.round(np.linspace(ext_max[0], ext_min[0], num=nglin))))
    lon, lat = np.zeros(pix.size), np.zeros(pix.size)
    for ipt in range(pix.size):
        ext = [lin[ipt], pix[ipt], lin[ipt], pix[ipt]]
        lon[ipt] = sarim.get_data('lon', extent=ext, spacing=1)
        lat[ipt] = sarim.get_data('lat', extent=ext, spacing=1)
    ndist = np.zeros(pix.size)
    lim = [0, ngpix, ngpix+nglin, 2*ngpix+nglin, 2*ngpix+2*nglin]
    geod = pyproj.Geod(ellps='WGS84')
    for iside in range(4):
        pt0, pt1 = lim[iside], lim[iside+1]-1
        ddist = geod.inv(lon[pt0:pt1], lat[pt0:pt1], lon[pt0+1:pt1+1],
                         lat[pt0+1:pt1+1])[2]
        dist = ddist.cumsum()
        ndist[pt0:pt1+1] = np.hstack((0., dist))/dist.max()
    gcppix = np.hstack((ndist[lim[0]:lim[1]-1]*npix, np.ones(nglin-1)*npix,
                        (1-ndist[lim[2]:lim[3]-1])*npix, np.zeros(nglin-1)))
    gcplin = np.hstack((np.zeros(ngpix-1), ndist[lim[1]:lim[2]-1]*nlin,
                        np.ones(ngpix-1)*nlin, (1-ndist[lim[3]:lim[4]-1])*nlin))
    gcplon = np.hstack((lon[lim[0]:lim[1]-1], lon[lim[1]:lim[2]-1],
                        lon[lim[2]:lim[3]-1], lon[lim[3]:lim[4]-1]))
    gcplat = np.hstack((lat[lim[0]:lim[1]-1], lat[lim[1]:lim[2]-1],
                        lat[lim[2]:lim[3]-1], lat[lim[3]:lim[4]-1]))
    gcphei = np.zeros(gcplon.size)
    #import pdb ; pdb.set_trace()
    if gcplon.min() < -135 and gcplon.max() > 135:
        gcplon[np.where(gcplon < 0)] += 360.
    #############################################
    if sensor_pass == 'Descending':
        gcppix = npix-gcppix
        gcplin = nlin-gcplin
    gcplin = nlin-gcplin # because fig will be read and wrote from top to bottom
    # Loop on part and write
    for part in ['real', 'imag']:
        print part
        if part == 'real':
            product = 'SAR_cross-spectrum_real'
            nameext = '-xspec_re'
            if make_rgb == True:
                rgb = rgb_re
            else:
                img = img_re
                pal = pal_re
        elif part == 'imag':
            product = 'SAR_cross-spectrum_imaginary'
            nameext = '-xspec_im'
            if make_rgb == True:
                rgb = rgb_im
            else:
                img = img_im
                pal = pal_im
        # Construct metadata/geolocation/band(s)
        print 'Construct metadata/geolocation/band(s)'
        metadata = {}
        (dtime, time_range) = stfmt.format_time_and_range(start_time, stop_time,
                                                          units='ms')
        metadata['product_name'] = product
        metadata['name'] = dataname + nameext
        metadata['datetime'] = dtime
        metadata['time_range'] = time_range
        metadata['source_URI'] = infile
        metadata['source_provider'] = source_provider
        metadata['processing_center'] = 'OceanDataLab'
        metadata['conversion_software'] = 'Syntool'
        metadata['conversion_version'] = '0.0.0'
        metadata['conversion_datetime'] = stfmt.format_time(datetime.utcnow())
        metadata['parameter'] = ''
        metadata['type'] = 'remote sensing'
        metadata['sensor_type'] = 'SAR'
        metadata['sensor_name'] = sensor_name
        metadata['sensor_platform'] = sensor_platform
        metadata['sensor_mode'] = sensor_mode
        metadata['sensor_swath'] = sensor_swath
        metadata['sensor_polarisation'] = sensor_polarisation
        metadata['sensor_pass'] = sensor_pass
        metadata['datagroup'] = datagroup
        geolocation = {}
        geolocation['projection'] = sarim._mapper._handler.GetGCPProjection()
        geolocation['gcps'] = stfmt.format_gdalgcps(gcplon, gcplat, gcphei,
                                                    gcppix, gcplin)
        band = []
        if make_rgb == True:
            band.append({'array': rgb[:, :, 0]})
            band.append({'array': rgb[:, :, 1]})
            band.append({'array': rgb[:, :, 2]})
        else:
            band.append({'array': img, 'nodatavalue': 255,
                         'colortable': palette2colortable(pal)})
        # Write geotiff
        print 'Write geotiff'
        tifffile = stfmt.format_tifffilename(outdir, metadata, create_dir=True)
        stfmt.write_geotiff(tifffile, metadata, geolocation, band)
        # Write projected png/kml
        if pngkml == True:
            print 'Write projected png/kml'
            stfmt.write_pngkml_proj(tifffile)