Skip to content

Instantly share code, notes, and snippets.

@csaybar
Last active September 22, 2022 12:08
Show Gist options
  • Save csaybar/c48245c8fe1087b4ef3de0a175a02041 to your computer and use it in GitHub Desktop.
Save csaybar/c48245c8fe1087b4ef3de0a175a02041 to your computer and use it in GitHub Desktop.
STARCOP IRIS jsonfile
{
"name": "main",
"authentication_required": true,
"images": {
"path": {
"R": "{id}/input/AR.tif",
"G": "{id}/input/AG.tif",
"B": "{id}/input/AB.tif",
"mag1c": "{id}/input/mag1c.tif",
"mag1c_scaled": "{id}/input/mag1c_scaled.tif"
},
"shape": [612, 612],
"metadata": "{id}/metadata.json"
},
"segmentation": {
"path": "{id}/target/manual.png",
"mask_encoding": "rgb",
"mask_area": [50, 50, 562, 562],
"score": "f1",
"pending_threshold": 1,
"test_images": null
},
"classes": [
{
"name": "Clear",
"description": "All clear pixels, i.e. without methane contamination.",
"colour": [255, 255, 255, 0],
"user_colour": [0, 255, 255, 70]
},
{
"name": "Contaminated",
"description": "All pixels with methane contamination.",
"colour": [255, 255, 0, 100]
}
],
"views": {
"Mag1c": {
"description": "Mag1c methane predictions.",
"type": "image",
"data": "$mag1c.B1",
"cmap": "jet"
},
"Mag1c_scaled": {
"description": "Mag1c methane predictions scaled to [200-5000].",
"type": "image",
"data": "$mag1c_scaled.B1",
"cmap": "jet"
},
"RGB": {
"description": "Normal RGB image.",
"type": "image",
"data": ["$R.B1", "$G.B1", "$B.B1"]
},
"black": {
"description": "A black background to finetune results.",
"type": "image",
"data": ["$R.B1*0", "$G.B1*0", "$B.B1*0"]
}
},
"view_groups": {
"default": ["Mag1c", "RGB", "black"]
}
}
@csaybar
Copy link
Author

csaybar commented Sep 22, 2022

import rasterio as rio
import numpy as np
import pathlib
import requests
import json


def saveGeoTiff(infile, outfile, addprofile=True):
    with rio.open(infile) as src:
        band_640 = src.read()
        band_640_padded = np.pad(band_640, ((0,0), (50,50), (50,50)), mode="constant", constant_values=0)
        base_profile = src.profile
        #profile = src.profile
        #profile.update(width=512 + 50*2)
        #profile.update(height=512 + 50*2)
        #profile.update(transform=src.transform * src.transform.translation(50, 50))
        
        # basic profile - simple affine
        profile = {
            'driver': 'GTiff',
            'dtype': 'float32',
            'nodata': None,
            'width': 512 + 50*2,
            'height': 512 + 50*2,
            'count': 1
        }               
        with rio.open(outfile, "w", **profile) as dst:
            dst.write(band_640_padded)
    
    if addprofile:
        base_profile.update(transform=list(src.transform)[:6])
        base_profile.update(crs=str(src.crs))        
        # base profile to dict
        with open(outfile.with_suffix(".json"), "w") as f:                                       
            json.dump(dict(base_profile), f)
    return True


def starcop_migration(infolder, outfolder, id):
    
    # 1. Convert to pathlib objects
    outfolder = pathlib.Path(outfolder)
    infolder = pathlib.Path(infolder)
    
    # Check if the outfolder exist
    if not outfolder.exists():
        outfolder.mkdir(parents=True, exist_ok=True)
        
    # Check if global metadatajson exists
    if not pathlib.Path(outfolder / "metadata.json").exists():
        metadata = "https://gist.githubusercontent.com/csaybar/c48245c8fe1087b4ef3de0a175a02041/raw/5678fd0db73c8051e5124d3a898b95f37c86f142/metadata.json"
        # read/write json from internet        
        r = requests.get(metadata)        
        with open(outfolder / "metadata.json", "w") as f:
            f.write(r.text)                            
    
    # 2. Create ID image paths
    id_path = pathlib.Path(outfolder) / id
    
    id_path_input = (id_path / "input/")
    id_path_input.mkdir(parents=True, exist_ok=True)
    
    id_path_target = (id_path / "target/")
    id_path_target.mkdir(parents=True, exist_ok=True)
    
    
    # 3. Add a buffer of (50, 50, 50, 50) to each image
    
    # RGB AVIRIS ------------------------------------
    
    ## RED 640    
    saveGeoTiff(
        infile = infolder/ id / "TOA_AVIRIS_640nm.tif",
        outfile = id_path_input / "AR.tif",
        addprofile=True
    )
    
    ## Green 550
    saveGeoTiff(
        infile = infolder/ id / "TOA_AVIRIS_550nm.tif",
        outfile = id_path_input / "AG.tif",
        addprofile=False
    )
    
    ## Blue
    saveGeoTiff(
        infile = infolder/ id / "TOA_AVIRIS_460nm.tif",
        outfile = id_path_input / "AB.tif",
        addprofile=False
    )
    
    # Mag1ck ------------------------------------
    saveGeoTiff(
        infile = infolder/ id / "mag1c.tif",
        outfile = id_path_input / "mag1c.tif",
        addprofile=False
    )
    
    # Mask -----------------------------
    ## Add labeling from binary mask1
    pathfolder = outfolder / "main.iris/segmentation/" / id
    pathfolder.mkdir(parents=True, exist_ok=True)
    
    with rio.open(infolder/ id / "labelbinary.tif") as src:
        bmask = src.read().astype(bool)
        # Get centroid from transform
        #bmask_padded = np.pad(bmask, ((0,0), (50,50), (50,50)), mode="constant", constant_values=0)[0]
    np.save("%s/2_user.npy" % pathfolder, bmask)
    
    bmask_copy = bmask.copy()[0]
    bmask_final = np.concatenate((~bmask_copy[:,:,None], bmask_copy[:,:,None]), axis=2)
    np.save("%s/2_final.npy" % pathfolder, bmask_final)
    
    # Create the image json file
    scene_metadata = dict(
        spacecraft_id = "Sentinel2/AVIRIS",
        scene_id = id,
        location = [0, 0],
        resolution = 5.0
    )
    
    with open(outfolder/ id / "metadata.json", "w") as f:
        json.dump(scene_metadata, f)

    return True

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment