Skip to content

Instantly share code, notes, and snippets.

@cboettig
cboettig / ibis_to_json.py
Created November 7, 2024 20:20
ibis / raw sql workarounds
import ibis
from ibis import _
con = ibis.duckdb.connect()
# example query, could be anything
subset = (gbif
.filter(_["class"] == "Aves")
.rename(hex = "h8")
.group_by(_.hex)
.agg(n = _.count())
@cboettig
cboettig / geoarrow-error.md
Created November 4, 2024 16:36
ibis geo error

To solve

ImportError: cannot import name 'types' from 'geoarrow' (unknown location), 

install geoarrow-types.

ibis may also need geoarrow-cto work with geoparquet now.

import os
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import ConfigurableField
from langchain_core.tools import tool
from langchain.agents import create_tool_calling_agent, AgentExecutor
@tool
def multiply(x: float, y: float) -> float:
"""Multiply 'x' times 'y'."""
return x * y
@cboettig
cboettig / python-access.py
Created October 24, 2024 16:56
forecast access
import ibis
con = ibis.duckdb.connect()
con.raw_sql(f'''
CREATE OR REPLACE SECRET secret (
TYPE S3,
ENDPOINT 'sdsc.osn.xsede.org',
URL_STYLE 'path'
);
@cboettig
cboettig / ibis_geo.py
Last active August 23, 2024 18:16
ibis geospatial I/O
import ibis
from ibis import _
con = ibis.duckdb.connect(extensions=["spatial"])
## Read a geospatial file other than parquet (via GDAL)
rivers_geojson = "https://data.source.coop/cboettig/us-rivers/nhd_flowline_national.geojson"
rivers = con.read_geo(rivers_geojson)
## Read geoparquet
@cboettig
cboettig / debug.py
Last active July 31, 2024 16:43
fishbase export in python
conn.sql('''
SELECT *
FROM information_schema.columns
WHERE table_name = 'families';
'''
)
@cboettig
cboettig / gurobi-setup.sh
Created April 1, 2024 02:42
gurobi-setup.sh
# Be sure to put a copy of the license file in "$HOME/gurobi.lic" for this to work!!
# Download the release
wget https://packages.gurobi.com/11.0/gurobi11.0.1_linux64.tar.gz # or path to most recent linux_64 download from https://www.gurobi.com/downloads/gurobi-software/
# extract:
tar -xvf gurobi*_linux64.tar.gz
# Set environmental variables
@cboettig
cboettig / xarray-via-gdal.py
Created December 21, 2023 18:08
xarray GDAL VSI vs fsspec
import xarray as xr
import rasterio
import rioxarray
import earthaccess
import os
from timebudget import timebudget
from pathlib import Path
# assumes we have a ~/.netrc created
cookies = os.path.expanduser("~/.urs_cookies")
## Using Forecasts data, ~ 300+ GB in many very small partitions!
uri <- "s3://anonymous@bio230014-bucket01/neon4cast-forecasts/parquet/aquatics?endpoint_override=sdsc.osn.xsede.org"
bench::bench_time(df <- duckdbfs::open_dataset(uri))
# process real
# 1.23m 4.91m
bench::bench_time( df <- arrow::open_dataset(uri))
# process real
# 4.21m 32.46m
@cboettig
cboettig / earthdata_examples.R
Last active January 23, 2024 18:55
Use earthdata tokens with GDAL to access data via https links
## examples
# requires GDAL >= 3.6
edl_set_token()
library(stars)
url <- "https://data.lpdaac.earthdatacloud.nasa.gov/lp-prod-protected/HLSL30.020/HLS.L30.T56JKT.2023246T235950.v2.0/HLS.L30.T56JKT.2023246T235950.v2.0.SAA.tif"
x <- read_stars(paste0("/vsicurl/",url))
plot(x)