Skip to content

Instantly share code, notes, and snippets.

View Zeitsperre's full-sized avatar
🗺️
Keeping on keepin' on.

Trevor James Smith Zeitsperre

🗺️
Keeping on keepin' on.
View GitHub Profile
@tlogan2000
tlogan2000 / gist:e7402790a4752273e6ef060c0fa9c3e6
Last active April 26, 2019 18:12
Example of masking netcdf using shapefiles (xarray & geopandas)
from shapely.geometry import Point
import xarray as xr
import geopandas as gpd
from geopandas.tools import sjoin
import numpy as np
import pandas as pd
def create_mask_from_shape(ds, poly):
ds = ds.drop(ds.data_vars)
@emmanuelnk
emmanuelnk / pygrib_eccodes_python3.6_install.md
Last active February 3, 2022 19:56
How to install PyGrib with ECCodes (Python 3.6.5, Ubuntu 18, 16, RHEL)

How to install PyGrib with ECCodes (Python 3.6.5, Ubuntu 19, 18, 16, RHEL, Lambda, Linux AMI, Docker)

[ last_updated: 07-JAN-2020 ]

Recently I've had to work with Meterological data of the GRIB format. Needless to say, if you're reading this, you probably know how hard it is to get a library that can read them and even harder, setting up those libraries. Pygrib for python is by far the best library for reading grib files but the installation can leave you dizzy

In this tutorial I use Ubuntu 18 and python 3.6.5 virtualenv. Note: You don't have to use virtualenv Python. You can just as well use your system Python 3 however I personally find virtualenv a clean way to deal with these kind of complicated setups (prevents me mucking up my system's Python). This setup should also work with lower and higher Ubuntu versions (as someone below mentioned) and other Linux distros (like RHEL - AWS Linux AMI etc). The key to this installation is to build eccodes correctly (for your machine architecture) and succe

@danwild
danwild / subset_examples.py
Last active September 8, 2020 12:56
Crop a NetCDF file by XY, Z, and Time with CDO + python bindings
# setup
from cdo import *
cdo = Cdo()
cdo.debug = True
in_file = '/path/to/in_file.nc'
out_file = '/path/to/out_file.nc'
date1 = '2018-03-01T00:00:00'
date2 = '2018-03-01T02:00:00'
# my target is in meters
@dyerrington
dyerrington / auto_reload.py
Created December 6, 2017 04:19
This basic Python snippet is a basic pattern to automatically reload the contents of a file after it has changed. You can also adapt this example to extend the "watching" to include all files and directories within current context if you want to also trigger a reload on file modification / change.
import os, sys
from time import sleep
## We could update this to all files in all subdirectories
watching = [__file__]
watched_mtimes = [(f, os.path.getmtime(f)) for f in watching]
while True:
print("Idle...")
sleep(2) ## So not to kill our CPU cycles
@Hermanio
Hermanio / .thermal-daemon.sh
Last active July 6, 2021 03:24
Quick and dirty early throttling script using bash and intel p-state driver toggling. USE AT YOUR OWN RISK.
#!/bin/bash
targettemp=90
clockpct=70
while true
do
currenttemp=$(sensors -u | awk '/temp1_input/ {print $2; exit}' )
compare=$(echo $currenttemp'>'$targettemp | bc -l)
turbo=`cat /sys/devices/system/cpu/intel_pstate/no_turbo`
if [ "$turbo" -eq 0 ]
import asyncio
loop = asyncio.get_event_loop()
async def hello():
await asyncio.sleep(3)
print('Hello!')
if __name__ == '__main__':
loop.run_until_complete(hello())
@bdjackson
bdjackson / SetMidpoint.py
Last active September 15, 2021 23:55
Setting the midpoint of a matplotlib colormap
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
class MidpointNormalize(mpl.colors.Normalize):
"""
class to help renormalize the color scale
"""
def __init__(self, vmin=None, vmax=None, midpoint=None, clip=False):
self.midpoint = midpoint
@guziy
guziy / Converter.py
Last active June 30, 2022 04:59
A quick way to copy variables and metadata from one netcdf file to another using netcdf4-python
# -*- coding: utf-8 -*-
from netCDF4 import Dataset
#input file
dsin = Dataset("crop.nc")
#output file
dsout = Dataset("crop.nc3", "w", format="NETCDF3_CLASSIC")
#Copy dimensions
@jwass
jwass / convert.py
Created August 15, 2013 21:52
Simple Shapefile to GeoJSON converter. Using the shapefile from here: http://www.mass.gov/anf/research-and-tech/it-serv-and-support/application-serv/office-of-geographic-information-massgis/datalayers/senate2012.html it will result in an error "ValueError: Record's geometry type does not match collection schema's geometry type: 'Polygon' != 'Unk…
import fiona
import fiona.crs
def convert(f_in, f_out):
with fiona.open(f_in) as source:
with fiona.open(
f_out,
'w',
driver='GeoJSON',
crs = fiona.crs.from_epsg(4326),
@benbalter
benbalter / geojson-conversion.sh
Last active April 23, 2024 13:16
Bulk convert shapefiles to geojson using ogr2ogr
# Bulk convert shapefiles to geojson using ogr2ogr
# For more information, see http://ben.balter.com/2013/06/26/how-to-convert-shapefiles-to-geojson-for-use-on-github/
# Note: Assumes you're in a folder with one or more zip files containing shape files
# and Outputs as geojson with the crs:84 SRS (for use on GitHub or elsewhere)
#geojson conversion
function shp2geojson() {
ogr2ogr -f GeoJSON -t_srs crs:84 "$1.geojson" "$1.shp"
}