<jupyter_start><jupyter_text>CatalogUse the [Descartes Labs Catalog](https://docs.descarteslabs.com/guides/catalog_v2.html) to discover existing raster products, search the images contained in them and manage your own products and images. The Catalog Python client is mainly for discovering data and for managing data. For data analysis and rastering use, [Scenes](https://docs.descarteslabs.com/guides/scenes.html).You can run the following cells using `Shift-Enter`. __Note__: The Catalog Python object-oriented client provides the functionality previously covered by the more low-level, now deprecated Metadata and Catalog Python clients. Concepts The Descartes Labs Catalog is a repository for georeferenced images. Commonly these images are either acquired by Earth observation platforms like a satellite or they are derived from other georeferenced images. The catalog is modeled on the following core concepts, each of which is represented by its own class in the API. ImagesAn image (represented by class `Image` in the API) contains data for a shape on earth, as specified by its georeferencing. An image references one or more files (commonly TIFF or JPEG files) that contain the binary data conforming to the band declaration of its product. BandsA band (represented by class `Band`) is a 2-dimensional slice of raster data in an image. A product must have at least one band and all images in the product must conform to the declared band structure. For example, an optical sensor will commonly have bands that correspond to the red, blue and green visible light spectrum, which you could raster together to create an RGB image. ProductsA product (represented by class `Product`) is a collection of images that share the same band structure. Images in a product can generally be used jointly in a data analysis, as they are expected to have been uniformly processed with respect to data correction, georegistration and so on. For example, you can composite multiple images from a product to run an algorithm over a large geographic region.  Searching the catalogAll objects support the same search interface. Let’s look at two of the most commonly searched for types of objects: products and images. Finding products`Product.search()` is the entry point for searching products. It returns a query builder that you can use to refine your search and can iterate over to retrieve search results. Count all products with some data before 2016 using `filter()`:<jupyter_code>from descarteslabs.catalog import Product, properties as p
search = Product.search().filter(p.start_datetime < "2016-01-01")
search.count()<jupyter_output><empty_output><jupyter_text>You can apply multiple filters. To restrict this search to products with data after 2000:<jupyter_code>search = search.filter(p.end_datetime > "2000-01-01")
search.count()<jupyter_output><empty_output><jupyter_text>Of these, get the 3 products with the oldest data, using `sort()` and `limit()`. The search is not executed until you start retrieving results by iterating over it:<jupyter_code>oldest_search = search.sort("start_datetime").limit(3)
for result in oldest_search:
    print(result.id)<jupyter_output>e7e4fc5b0afd45d32c623697cb5a4437d3f09ade:daily-weather:cfsr-v1
fcd57a7bf668c5b65d49c5e32130a2c0a5281322:daily-weather:cfsr-v1:dev-v0
daily-weather:gsod-interpolated:v0<jupyter_text>All attributes are documented in the Product API reference, which also spells out which ones can be used to filter or sort.  Lookup by id and object relationshipsIf you know a product’s id, look it up directly with `Product.get()`: Wherever there are relationships between objects expect methods such as `bands()` to find related objects. This shows the first four bands of the Landsat 8 product we looked up:<jupyter_code>landsat8_collection1 = Product.get("landsat:LC08:01:RT:TOAR")
landsat8_collection1
list(landsat8_collection1.bands().limit(4))<jupyter_output><empty_output><jupyter_text>`bands()` returns a search object that can be further refined. This shows all class bands of this Landsat 8 product, sorted by name:<jupyter_code>from descarteslabs.catalog import BandType
list(landsat8_collection1.bands().filter(p.type == BandType.CLASS).sort("name"))<jupyter_output><empty_output><jupyter_text>Finding images Image filtersSearch images by the most common attributes - by product, intersecting with a geometry and by a date range:<jupyter_code>from descarteslabs.catalog import Image, properties as p
geometry = {
     "type": "Polygon",
     "coordinates": [[
         [2.915496826171875, 42.044193618165224],
         [2.838592529296875, 41.92475971933975],
         [3.043212890625, 41.929868314485795],
         [2.915496826171875, 42.044193618165224]
     ]]
 }

search = Product.get("landsat:LC08:01:RT:TOAR").images()
search = search.intersects(geometry)
search = search.filter((p.acquired > "2017-01-01") & (p.acquired < "2018-01-01"))
search.count()<jupyter_output><empty_output><jupyter_text>There are other attributes useful to filter by, documented in the API reference for Image. For example exclude images with too much cloud cover:<jupyter_code>search = search.filter(p.cloud_fraction < 0.2)
search.count()<jupyter_output><empty_output><jupyter_text>Filtering by `cloud_fraction` is only reasonable when the product sets this attribute on images. Images that don’t set the attribute are excluded from the filter.The created timestamp is added to all objects in the catalog when they are created and is immutable. Restrict the search to results created before some time in the past, to make sure that the image results are stable:<jupyter_code>from datetime import datetime
search = search.filter(p.created < datetime(2019, 1, 1))
search.count()<jupyter_output><empty_output><jupyter_text>Note that for all timestamps we can use datetime instances or strings that can reasonably be parsed as a timestamp. If a timestamp has no explicit timezone, it’s assumed to be in UTC.  Image summariesAny queries for images support a summary via the `summary()` method, returning a `SummaryResult` with aggregate statistics beyond just the number of results:<jupyter_code>from descarteslabs.catalog import Image, properties as p
search = Image.search().filter(p.product_id == "landsat:LC08:01:T1:TOAR")
search.summary()<jupyter_output><empty_output><jupyter_text>These summaries can also be bucketed by time intervals with `summary_interval()` to create a time series:<jupyter_code>search.summary_interval(interval="month", start_datetime="2017-01-01", end_datetime="2017-06-01")<jupyter_output><empty_output><jupyter_text>Managing productsCreating and updating a productBefore uploading images to the catalog, you need to create a product and declare its bands. The only required attributes are a unique id, passed in the constructor, and a name:<jupyter_code>from descarteslabs.catalog import Product
import random 

# We append a random number to the product ID so users who run this example multiple times do not get a "Product with that ID exists" error
product_id = "guide-example-product_" + str(random.randint(1000,9999))
product = Product(id=product_id)
product.name = "Example product"
product.save()
product.id
product.created<jupyter_output><empty_output><jupyter_text>`save()` saves the product to the catalog in the cloud. Note that you get to choose an id for your product but it must be unique within your organization (you get an exception if it’s not). This code example is assuming the user is in the “descarteslabs” organization. The id is prefixed with the organization id on save to enforce global uniqueness and uniqueness within an organization. If you are not part of an organization the prefix will be your unique user id.Every object has a read-only `created` attribute with the timestamp from when it was first saved.There are a few more attributes that you can set (see the [Product API reference](https://docs.descarteslabs.com/descarteslabs/catalog/readme.html)). You can update the product to define the timespan that it covers. This is as simple as assigning attributes and then saving again:<jupyter_code>product.start_datetime = "2012-01-01"
product.end_datetime = "2015-01-01"
product.save()
product.start_datetime
product.modified<jupyter_output><empty_output><jupyter_text>A read-only modified attribute exists on all objects and is updated on every save.Note that all timestamp attributes are represented as datetime instances in UTC. You may assign strings to timestamp attributes if they can be reasonably parsed as timestamps. Once the object is saved the attributes will appear as parsed datetime instances. If a timestamp has no explicit timezone, it’s assumed to be in UTC. Creating bandsBefore adding any images to a product you should create bands that declare the structure of the data shared among all images in a product.<jupyter_code>from descarteslabs.catalog import SpectralBand, DataType, Resolution, ResolutionUnit
band = SpectralBand(name="blue", product=product)
band.data_type = DataType.UINT16
band.data_range = (0, 10000)
band.display_range = (0, 4000)
band.resolution = Resolution(unit=ResolutionUnit.METERS, value=60)
band.band_index = 0
band.save()
band.id<jupyter_output><empty_output><jupyter_text>A band is uniquely identified by its name and product. The full id of the band is composed of the product id and the name.The band defines where its data is found in the files attached to images in the product: In this example, band_index = 0 indicates that blue is the first band in the image file, and that first band is expected to be represented by unsigned 16-bit integers (DataType.UINT16).This band is specifically a `SpectralBand`, with pixel values representing measurements somewhere in the visible/NIR/SWIR electro-optical wavelength spectrum, so you can also set additional attributes to locate it on the spectrum:<jupyter_code># These values are in nanometers (nm)
band.wavelength_nm_min = 452
band.wavelength_nm_max = 512
band.save()<jupyter_output><empty_output><jupyter_text>Bands are created and updated in the same way was as products and all other Catalog objects.  Band types It’s common for many products to have an alpha band, which masks pixels in the image that don’t have valid data:<jupyter_code>from descarteslabs.catalog import MaskBand
alpha = MaskBand(name="alpha", product=product)
alpha.is_alpha = True
alpha.data_type = DataType.UINT16
alpha.resolution = band.resolution
alpha.band_index = 1
alpha.save()<jupyter_output><empty_output><jupyter_text>Here the `alpha` band is created as a `MaskBand` which is by definition a binary band with a data range from 0 to 1, so there is no need to set the data_range and display_range attribute.Setting `is_alpha` to `True` enables special behavior for this band during rastering. If this band appears as the last band in a raster operation (such as `SceneCollection.mosaic` or `SceneCollection.stack` in the scenes client) pixels with a value of 0 in this band will be treated as transparent.There are five band types which may have some attributes specific to them. The type of a band does not necessarily affect how it is rastered, it mainly conveys useful information about the data it contains.* `SpectralBand`: A band that lies somewhere on the visible/NIR/SWIR electro-optical wavelength spectrum. Specific attributes: wavelength_nm_center, wavelength_nm_min, wavelength_nm_max, wavelength_nm_fwhm* `MicrowaveBand`: A band that lies in the microwave spectrum, often from SAR or passive radar sensors. Specific attributes: frequency, bandwidth* `MaskBand`: A binary band where by convention a 0 means masked and 1 means non-masked. The data_range and display_range for masks is implicitly [0, 1]. Specific attributes: is_alpha* `ClassBand`: A band that maps a finite set of values that may not be continuous to classification categories (e.g. a land use classification). A visualization with straight pixel values is typically not useful, so commonly a colormap is used. Specific attributes: colormap, colormap_name, class_labels* `GenericBand`: A generic type for bands that are not represented by the other band types, e.g., mapping physical values like temperature or angles. Specific attributes: colormap, colormap_name, physical_range, physical_range_unit  Managing imagesApart from searching and discovering data available to you, the main use case of the catalog is to let you upload new images. In the following example, we will upload data with a single band representing the blue light spectrum. In addition to the example below, users can [upload image](https://docs.descarteslabs.com/descarteslabs/catalog/readme.html) files in geoTiff format through the catalog.  Uploading ndarraysOften, when creating derived products - for example, running a classification model on existing data - you’ll have a NumPy array (often referred to as “ndarrays”) in memory instead of a file written to disk. In that case, you can use ```Image.upload_ndarray```. This method behaves like ```Image.upload```, with one key difference: you must provide georeferencing attributes for the ndarray.Georeferencing attributes are used to map between geospatial coordinates (such as latitude and longitude) and their corresponding pixel coordinates in the array. The required attributes are:An affine geotransform in GDAL format (the ```geotrans``` attribute)A coordinate reference system definition, preferrably as an EPSG code (the ```cs_code``` attribute) or alternatively as a string in PROJ.4 or WKT format (the ```projection``` attribute)If the ndarray you’re uploading was rastered through the the platform, this information is easy to get. When rastering you also receive a dictionary of metadata that includes both of these parameters. Using the ```Scene.ndarray```, you have to set ```raster_info=True```; with ```Raster.ndarray```, it’s always returned.The following example puts these pieces together. This extracts the ```blue``` band from a Landsat 8 scene at a lower resolution and uploads it to our product:<jupyter_code>from descarteslabs.catalog import OverviewResampler
from descarteslabs.scenes import Scene
scene, geoctx = Scene.from_id("landsat:LC08:01:T1:TOAR:meta_LC08_L1TP_163068_20181025_20181025_01_T1_v1")
ndarray, raster_meta = scene.ndarray(
     "blue",
     geoctx.assign(resolution=60),
     # return georeferencing info we need to re-upload
     raster_info=True
 )

image2 = Image(product=product, name="scene2")
image2.acquired = "2012-01-02"
upload2 = image2.upload_ndarray(
    ndarray,
    raster_meta=raster_meta,
    # create overviews for 120m and 240m resolution
    overviews=[2, 4],
    overview_resampler=OverviewResampler.AVERAGE
)
    
upload2.wait_for_completion()
upload2.status<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0659392789373814, 'fraction_numerical': 0.026701002981837897, 'mean_word_length': 5.647297297297297, 'pattern_counts': {'":': 2, '<': 68, '<?xml version=': 0, '>': 66, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '281461', 'license': 'Apache-2.0', 'n_tokens_mistral': 4254, 'n_tokens_neox': 3894, 'n_words': 1798, 'path': 'data_management_with_catalog.ipynb', 'repo_name': 'descarteslabs/tutorials', 'size': '14756'}