Ejemplo n.º 1
0
def make_monolith(layer):
    """ Build a monolith from a netcdf. """
    logging.info('Building monolith for {}'.format(layer))
    # Get paths
    monolith_path = utils.get_monolith_path(layer)
    netcdf_path = utils.get_netcdf_path(layer)
    bathy_path = utils.get_bathymetry_path(layer)
    projection = utils.get_bathymetry_srs(bathy_path)
    #if projection is not None: 
    #    projection = int(projection)
    logging.info('Monolith projection is {}'.format(projection))
    # Create monolith
    try:
        monolith = rasters.Monolith(path=monolith_path, compression='DEFLATE')
        if monolith.has_data():
            logging.info('Monolith has data for {}'.format(layer))
            return
        monolith.add(quads.get_dataset(path=netcdf_path, projection=projection))
    except rasters.LockError:
        logging.info('Monolith busy for {}'.format(layer))
        return
    logging.info('Monolith completed for {}'.format(layer))
Ejemplo n.º 2
0
def command(sourcepath, targetpath, timestep=None):
    """ Do something spectacular. """
    quaddataset = quads.get_dataset(sourcepath)
    ascdriver = gdal.GetDriverByName(b'aaigrid')
    ascdriver.CreateCopy(b'arjen.asc', quaddataset)

    quaddata = quaddataset.ReadAsArray()

    if timestep is None:
        logger.debug('Calculating maximum flow velocity.')
        with Dataset(sourcepath) as dataset:
            fvx = dataset.variables['ucx'][:].max(0)
            fvy = dataset.variables['ucy'][:].max(0)
    else:
        with Dataset(sourcepath) as dataset:
            logger.debug('Calculating flow velocity for time = {}.'.format(
                dataset.variables['time'][timestep],
            ))
            fvx = dataset.variables['ucx'][:][timestep]
            fvy = dataset.variables['ucy'][:][timestep]

    # Create linear array
    fv = np.ma.array(
        np.empty(fvx.size + 1),
        mask=True,
    )

    # Fill with flowvelocity from netcdf
    fv[0:-1] = np.sqrt(fvx ** 2 + fvy ** 2)

    # Create mem dataset
    mem_driver = gdal.GetDriverByName(b'mem')
    fvdataset = mem_driver.Create(
        b'',
        quaddataset.RasterXSize,
        quaddataset.RasterYSize,
        1,
        gdal.GDT_Float32,
    )
    fvdataset.SetGeoTransform(quaddataset.GetGeoTransform())
    fvband = fvdataset.GetRasterBand(1)
    fvband.Fill(-999)
    fvband.SetNoDataValue(-999)
    fvband.WriteArray(fv[quaddata].filled(-999))

    # Create asciifile
    asc_driver = gdal.GetDriverByName(b'aaigrid')
    asc_driver.CreateCopy(
        targetpath,
        fvdataset,
        options=[b'DECIMAL_PRECISION=3']
    )

    # Creating asciifile via vsimem to show capability
    # Note that we need to
    vsipath = '/vsimem/' + targetpath

    from arjan.monitor import Monitor
    mon = Monitor()

    for i in range(500):
        vsipath = '/vsimem/' + targetpath + str(i)
        asc_driver.CreateCopy(
            vsipath,
            fvdataset,
            options=[b'DECIMAL_PRECISION=3']
        )
    mon.check('')
    a = np.ones(10000000)
    mon.check('ones')

    vsifile = gdal.VSIFOpenL(vsipath, b'r')
    with open(targetpath + '.demo', 'w') as demofile:
        demofile.write(gdal.VSIFReadL(
            gdal.VSIStatL(vsipath).size,  # Size
            1,                            # Count
            vsifile
        ))
    gdal.VSIFCloseL(vsifile)