Exemplo n.º 1
0
    def _download_geometric_features(self, componentName, objectType,
                                     featureNames):
        '''
        Determine a list of requested files and download the any that are
        missing from the repo

        Parameters
        ----------
        componentName : {'bedmap2', 'iceshelves', 'landice', 'natural_earth', 'ocean'}
            The component from which to retieve the geometric features

        objectType : {'point', 'transect', 'region'}
            The type of geometry to load, a point (0D), transect (1D) or region
            (2D)

        featureNames : list of str
            The names of geometric features to read

        Returns
        -------
        fileList : list of str
            File names of the features


        '''
        # Authors
        # -------
        # Xylar Asay-Davis
        try:
            os.makedirs(self.cacheLocation)
        except OSError:
            pass

        fileList = []
        filesToDownload = []
        for featureName in featureNames:
            relativePath = _get_file_name(componentName, objectType,
                                          featureName)
            fullPath = os.path.join(self.cacheLocation, relativePath)
            fileList.append(fullPath)
            if not os.path.exists(fullPath):
                filesToDownload.append(relativePath)

        if len(filesToDownload) > 0:
            baseURL = 'https://raw.githubusercontent.com/MPAS-Dev/' \
                'geometric_features/{}/geometric_data'.format(
                    self.remoteBranch)
            download_files(filesToDownload, baseURL, self.cacheLocation)

        return fileList
Exemplo n.º 2
0
def bedmap2_bin_to_netcdf(outFileName):

    if os.path.exists(outFileName):
        return

    fields = [
        'bed', 'surface', 'thickness', 'coverage', 'rockmask',
        'grounded_bed_uncertainty', 'icemask_grounded_and_shelves'
    ]

    allExist = True
    for field in fields:
        fileName = 'bedmap2/bedmap2_bin/bedmap2_{}.flt'.format(field)
        if not os.path.exists(fileName):
            allExist = False
            break

    if not allExist:
        # download
        baseURL = 'https://secure.antarctica.ac.uk/data/bedmap2'
        fileNames = ['bedmap2_bin.zip']

        download_files(fileNames, baseURL, 'bedmap2')

        print('Decompressing Bedmap2 data...')
        # unzip
        with zipfile.ZipFile('bedmap2/bedmap2_bin.zip', 'r') as f:
            f.extractall('bedmap2/')
        print('  Done.')

    print('Converting Bedmap2 to NetCDF...')
    ds = xarray.Dataset()
    x = numpy.linspace(-3333000., 3333000., 6667)
    y = x
    ds['x'] = ('x', x)
    ds.x.attrs['units'] = 'meters'
    ds['y'] = ('y', y)
    ds.y.attrs['units'] = 'meters'
    ds.attrs['Grid'] = "Datum = WGS84, earth_radius = 6378137., " \
                       "earth_eccentricity = 0.081819190842621, " \
                       "falseeasting = -3333000., " \
                       "falsenorthing = -3333000., " \
                       "standard_parallel = -71., central_meridien = 0, " \
                       "EPSG=3031"
    ds.attrs['proj'] = "+proj=stere +lat_0=-90 +lat_ts=-71 +lon_0=0 +k=1 " \
                       "+x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs"
    ds.attrs['proj4'] = "+init=epsg:3031"

    # Antarctic stereographic
    inProj = pyproj.Proj(init='epsg:3031')
    # lon/lat
    outProj = pyproj.Proj(init='epsg:4326')
    X, Y = numpy.meshgrid(x, y)
    Lon, Lat = pyproj.transform(inProj, outProj, X, Y)

    ds['lon'] = (('y', 'x'), Lon)
    ds.lon.attrs['units'] = 'degrees east'
    ds['lat'] = (('y', 'x'), Lat)
    ds.lat.attrs['units'] = 'degrees north'

    # add Bedmap2 data
    for fieldName in fields:
        fileName = 'bedmap2/bedmap2_bin/bedmap2_{}.flt'.format(fieldName)
        with open(fileName, 'r') as f:
            field = numpy.fromfile(f, dtype=numpy.float32).reshape(6667, 6667)
            # flip the y axis
            field = field[::-1, :]
            # switch invalid values to be NaN (as expected by xarray)
            field[field == -9999.] = numpy.nan
        if fieldName == 'rockmask':
            # rock mask is zero where rock and -9999 (now NaN) elsewhere
            field = numpy.array(numpy.isfinite(field), numpy.float32)
        if fieldName == 'icemask_grounded_and_shelves':
            # split into separate grounded and floating masks
            ds['icemask_grounded'] = \
                (('y', 'x'), numpy.array(field == 0, numpy.float32))
            ds['icemask_shelves'] = \
                (('y', 'x'), numpy.array(field == 1, numpy.float32))
            ds['open_ocean_mask'] = \
                (('y', 'x'), numpy.array(numpy.isnan(field), numpy.float32))
        else:
            ds[fieldName] = (('y', 'x'), field)

    ds.to_netcdf(outFileName)
    print('  Done.')
Exemplo n.º 3
0
def get_longest_contour(contourValue, author):

    def stereo_to_lon_lat(x, y):
        return pyproj.transform(inProj, outProj, x, y)

    bathymetryFileName = 'IBCAO_V3_500m_SM.grd.gz'
    if not os.path.exists(bathymetryFileName):
        print('Downloading IBCAO bathymetry data...')
        # download
        baseURL = 'https://www.ngdc.noaa.gov/mgg/bathymetry/arctic/grids/version3_0'

        download_files([bathymetryFileName], baseURL, './')

    print('Decompressing and reading IBCAO bathymetry data...')
    infile = gzip.open(bathymetryFileName, 'rb')
    tmp = tempfile.NamedTemporaryFile(delete=False)
    shutil.copyfileobj(infile, tmp)
    infile.close()
    tmp.close()
    ds = xarray.open_dataset(tmp.name)
    os.unlink(tmp.name)

    x = ds.x.values
    y = ds.y.values
    z = ds.z.values
    z[(x==numpy.amin(x)) | (x==numpy.amax(x)) |
      (y==numpy.amin(y)) | (y==numpy.amax(y))] = 0.0
    # plot contours
    plt.figure()
    cs = plt.contour(x, y, z, (contourValue,))
    paths = cs.collections[0].get_paths()

    pathLengths = [len(paths[i]) for i in range(len(paths))]
    iLongest = numpy.argmax(pathLengths)

    p = paths[iLongest]
    v = p.vertices
    x = v[:, 0]
    y = v[:, 1]

    # Arctic stereographic
    inProj = pyproj.Proj(init='epsg:3995')
    # lon/lat
    outProj = pyproj.Proj(init='epsg:4326')
    lon, lat = pyproj.transform(inProj, outProj, x, y)

    poly = shapely.geometry.Polygon([(i[0], i[1]) for i in zip(x, y)])

    epsilon = 1e-14
    maxY = numpy.amax(y)
    wedge = shapely.geometry.Polygon([(epsilon, maxY),
                                      (epsilon**2, epsilon),
                                      (0, epsilon),
                                      (-epsilon**2, epsilon),
                                      (-epsilon, maxY),
                                      (epsilon, maxY)])

    difference = poly.difference(wedge)

    difference = shapely.ops.transform(stereo_to_lon_lat, difference)

    fc = FeatureCollection()

    geometry = shapely.geometry.mapping(difference)
    # get rid of the wedge again by rounding the coordinates
    geometry['coordinates'] = _round_coords(geometry['coordinates'])

    fc.add_feature(
        {"type": "Feature",
         "properties": {"name": "Contour {}".format(contourValue),
                        "author": author,
                        "object": 'region',
                        "component": 'ocean'},
         "geometry": geometry})

    return fc