Skip to content

Instantly share code, notes, and snippets.

View Thomas-Moore-Creative's full-sized avatar

Thomas Moore Thomas-Moore-Creative

View GitHub Profile
@Thomas-Moore-Creative
Thomas-Moore-Creative / python-quiver-plot.py
Created June 8, 2023 02:39
simple python quiver plot of ocean currents
import matplotlib.pyplot as plt
# Define the x and y coordinates
x = [0, 1, 2, 3]
y = [0, 1, 2, 3]
# Define the u and v components of the currents
u = [1, 2, 3, 4]
v = [0, 1, 0, -1]
@Thomas-Moore-Creative
Thomas-Moore-Creative / open_OHC_nc_and_plot.py
Created August 23, 2021 04:27
Open OHC netCDF and plot
import xarray as xr
file = 'path/to/file.nc'
data = xr.open_dataset(file)
data.OHC_0_200_emean.isel(time=0).plot()
@Thomas-Moore-Creative
Thomas-Moore-Creative / pp-load-ACCESS-S2-RA.py
Created August 4, 2021 00:28
Preprocess step for loading ACCESS-S2 Reanalysis data with inconsistent NetCDF file structure
# define preprocess drop function - this unfortunately removes useful information like areau that's missing from 2015 files
def drop_not_in_2015(ds):
if 'latu_bounds' in ds.data_vars:
ds = ds.drop(['latu_bounds'])
if 'lonu_bounds' in ds.data_vars:
ds = ds.drop(['lonu_bounds'])
if 'latv_bounds' in ds.data_vars:
ds = ds.drop(['latv_bounds'])
if 'lonv_bounds' in ds.data_vars:
ds = ds.drop(['lonv_bounds'])
def calc_EKE(u, v):
'''
Author1 = {"name": "Thomas Moore", "affiliation": "CSIRO", "email": "thomas.moore@csiro.au", "orcid": "0000-0003-3930-1946"}
u,v are x and y currents as an xarray data array
'''
u_mean = u.mean('time')
v_mean = v.mean('time')
MKE = 0.5*(u_mean**2 + v_mean**2).rename('MKE') # currents
EKE = ( 0.5 * ((u-u_mean)**2 + (v-v_mean)**2) ).rename('EKE') # eddies
return EKE, MKE
@Thomas-Moore-Creative
Thomas-Moore-Creative / regex_file_list.py
Created June 9, 2021 07:36
generate file list using regex
import re
file_list =[]
# get regex range from web generator - https://3widgets.com/
# find range of files from filename_1981.nc to filename_2018.nc
regexp = re.compile('filename_(198[1-9]|199[0-9]|200[0-9]|201[0-8]).nc')
ROOT_DIR ='/root/path/to/files/'
for root, dirs, files in os.walk(ROOT_DIR):
for file in files:
if regexp.search(file):
file_list.append(os.path.join(root, file))
@Thomas-Moore-Creative
Thomas-Moore-Creative / load_specific_CCiA_data.py
Last active May 13, 2021 01:46
simple code for loading CCiA CMIP5 data
# this is specific to needs outlined in this repo:
# https://github.com/Thomas-Moore-Creative/NCI-CCiA-ARD
import xarray as xr
import numpy as np
import datetime
import intake
NCI_catalog = intake.open_catalog('/g/data/hh5/public/apps/nci-intake-catalogue/catalogue.yaml')
# build CCiA catalog subset
CCiA_catalog = NCI_catalog.esgf.cmip5.search(variable=['psl','pr'],
realm=['atmos'],
@Thomas-Moore-Creative
Thomas-Moore-Creative / load_CMIP5_using_intake.py
Last active May 13, 2021 01:52
CMIP5 intake-esm loading example
import intake
import xarray as xr
NCI_catalog = intake.open_catalog('/g/data/hh5/public/apps/nci-intake-catalogue/catalogue.yaml')
CCiA_catalog = NCI_catalog.esgf.cmip5.search(variable=['psl','pr'],
realm=['atmos'],
time_frequency='mon',
experiment=['rcp85','historical'],
ensemble=['r1i1p1'],
model=['ACCESS1.0',
'CESM1(CAM5)',