Skip to content

Instantly share code, notes, and snippets.

View emileten's full-sized avatar

Emile Tenezakis emileten

  • Development Seed
  • Seoul, Korea
View GitHub Profile
import numpy as np
import xarray as xr
from xclim import sdba
def _datafactory(
x, start_time="1950-01-01", variable_name="fakevariable", lon=1.0, lat=1.0
):
"""Populate xr.Dataset with synthetic data for testing"""
start_time = str(start_time)
if x.ndim != 1:
@emileten
emileten / replace-low-values-by-sample-from-uniform.py
Created January 24, 2022 22:14
Replace xarray values conditionally by a set of values samples from the uniform distribution
def censor(x):
if x < 1:
return np.random.uniform(low=0.5, high=1)
else:
return x
vcensor = np.vectorize(censor)
ds_corrected = xr.apply_ufunc(vcensor, ds, dask='parallelized') # rather, should we load the data before hand ?
@emileten
emileten / impose-cell-specific-max.py
Created February 8, 2022 04:32
impose a cell specific temporal cap on an [time, lon, lat] xarray dataset.
import xarray as xr
import numpy as np
### Fake data ###
def spatio_temporal_gcm_factory(
x=np.random.rand(1, 361, 721),
start_date="1995-01-01",
lat=np.arange(-90, 90.5, 0.5),
lon=np.arange(-180, 180.5, 0.5),
@emileten
emileten / dict-to-and-from-yaml.py
Created February 25, 2022 01:24
dict-to-and-from-yaml
from yaml import load, dump, Loader
fp = '/Users/emile/Desktop/testyaml.yaml'
out = {
'a': 1
}
with open(fp, 'w') as f:
dump(out, f)
with open(fp, 'r') as f:
@emileten
emileten / xarray-openmfdataset-expand-assign-coords.py
Created April 21, 2022 23:11
Open multiple netcdf datasets with xarray, and automatically modify (add a dimension with coordinates based on file path) and then combine them.
### This works #####
FILEPATTERN = '...'
def func(ds):
var = next(var for var in ds)
fp = ds[var].encoding['source']
coordds = ds.assign_coords(path=fp)
dimds = coordds.expand_dims('path')
return dimds
fs = fsspec.filesystem('gs')
@emileten
emileten / some_notebook.ipynb
Last active March 10, 2023 00:38
some-notebook
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@emileten
emileten / stackstac_dupl_coords.ipynb
Created March 10, 2023 07:52
stackstac returns xarray data array with duplicated time coordinates
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@emileten
emileten / sentinel1-rtc-exploration.yaml
Created March 15, 2023 09:59
sentinel1-rtc-exploration-environment
name: explore-sentinel1-rtc
channels:
- conda-forge
- defaults
dependencies:
- python=3.10
- ipykernel
- pip
- pip:
- stackstac
@emileten
emileten / explore-sentinel1-rtc.ipynb
Created March 15, 2023 10:01
explore-sentinel1-rtc
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@emileten
emileten / cli_mfa_auth_aws.sh
Last active March 31, 2023 08:39
cli_mfa_auth_aws
# set ARN_MFA_DEVICE to the ARN of the MFA device associated with your SMCE account.
# and AWS_PROFILE to your AWS credentials profile associated with the SMCE MAAP account (in ~/.aws/credentials)
# first argument (positional) of this function is the MFA code that is displayed by your MFA app at the moment
# you run the function (e.g. Google Auth)
mfa_authentication_smce_maap () {
export AWS_PROFILE="YOUR_SMCE_MAAP_PROFILE"
export ARN_MFA_DEVICE="ARN_OF_MFA_DEVICE"
tokens=$(aws sts get-session-token --serial-number $ARN_MFA_DEVICE --token-code $1 --output json)