Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
import_format.py 1.61 KiB
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


import rasterio
import numpy as np
import netCDF4 as nc
import pandas as pd

def import_tif(path):
    
    """
        Import a geotiff file
        
        Input:
            path: Path to the tif file to open, string
            missing_value = no data value of the 
    """

    raster = rasterio.open(path, 'r')
    data = raster.read()[0, :, :]

    if np.dtype(data[0, 0]) == 'uint8':
        data = np.int32(data)

    bounds = raster.bounds
    x = np.linspace(bounds[0], bounds[2], np.shape(data)[1])
    y = np.linspace(bounds[1], bounds[3], np.shape(data)[0])
    crs = raster.crs

    if y[0] < y[-1]:
        y = np.flip(y)

    return data, x, y, crs


def import_nc(path):
    
    """
        Import a netCDF4 file and contained metadata
        
        Input:
            path: Path to the netCDF4 file to open, string
    """

    ds = nc.Dataset(path)
    x = ds['Longitude'][:]
    y = ds['Latitude'][:]

    if 'Result' in ds.variables.keys():
        data = ds['Result'][:][:]
        data = np.float64(data)
        data = data.data
    else:
        data = None

    if 'Time' in ds.variables.keys():
        data = ds['Result'][:][:]
        data = data.data

    if hasattr(ds.variables['Longitude'], 'units'):
        crs = ds['Longitude'].units
    else:
        crs = None

    x = x.data
    y = y.data

    if y[0] < y[-1]:
        y = np.flip(y)

    return data, x, y, crs


def import_cvs(path):
    
    """
        Import a csv file
        
        Input:
            path: Path to the csv file to open, string
    """

    df = pd.read_csv(path)

    return df