Skip to content

Instantly share code, notes, and snippets.

@monocongo
monocongo / detect_json_lines.py
Created August 9, 2024 18:42
Detect whether a JSON file is using JSON lines or a single JSON object
import json
# try to load the entire file as a single JSON object
file_path = '/home/james/tmp/api_data.json'
try:
with open(file_path, 'r') as f:
data = json.load(f)
print("The file is a single JSON object.")
except json.JSONDecodeError:
print("The file is not a single JSON object.")

Calculate SPI using monthly rainfall data in GeoTIFF format

These last few months, I have tried a lot of difference formulation to calculate Standardized Precipitation Index (SPI) based on rainfall data in netCDF format, check below files as a background:

The reason why I use rainfall in netCDF format in above files because the software to calculate SPI: climate-indices python package will only accept single netCDF as input, and the SPI script will read the netCDF input file based on time dimension.

Converting raster files into netCDF is easy using GDAL or other GIS software, but to make the time dimension enabled n

@monocongo
monocongo / mssql_df_upsert.py
Created September 26, 2023 15:04 — forked from gordthompson/mssql_df_upsert.py
Build a T-SQL MERGE statement and upsert a DataFrame
# Copyright 2023 Gordon D. Thompson, [email protected]
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
@monocongo
monocongo / xarray_stack_groupby_apply_unstack_spi.ipynb
Last active January 3, 2023 23:26
Use xarray for parallel computation with stack-groupby-apply-unstack
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@monocongo
monocongo / start_jupyter_pyspark.sh
Created July 29, 2022 01:06 — forked from BryanCutler/start_jupyter_pyspark.sh
How to start a Jupyter Notebook with PySpark Kernel
#!/usr/bin/env bash
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
@monocongo
monocongo / gist:853f49fec2705ab12cd94bfd20b38cf7
Created June 22, 2022 11:12
Below we demonstrate how to utilize [sbpy](https://sbpy.readthedocs.io/en/latest/index.html) to transform an unpacked designation to packed format
import pandas as pd
from sbpy.data import Names
# function to create the new packed format column
def pack_designation(
row: pd.Series,
) -> pd.Series:
return pd.Series(
data=[Names.to_packed(row.pdes)],
index=['Principal_desig'],
@monocongo
monocongo / compute_correlation_matrix.py
Created March 10, 2022 14:25 — forked from cameres/compute_correlation_matrix.py
Compute Pandas Correlation Matrix of a Spark Data Frame
from pyspark.mllib.stat import Statistics
import pandas as pd
# result can be used w/ seaborn's heatmap
def compute_correlation_matrix(df, method='pearson'):
# wrapper around
# https://forums.databricks.com/questions/3092/how-to-calculate-correlation-matrix-with-all-colum.html
df_rdd = df.rdd.map(lambda row: row[0:])
corr_mat = Statistics.corr(df_rdd, method=method)
corr_mat_df = pd.DataFrame(corr_mat,
@monocongo
monocongo / prefix_all_files_in_directory.py
Created January 13, 2020 16:49
Rename all files in a directory with a prefix
import os
directory = "/home/james/images"
extension = ".jpg"
prefix = "prefix_"
for filename in os.listdir(directory):
if not filename.endswith(extension):
continue
new_filename = prefix + filename
old_file_path = os.path.join(directory, filename)
new_file_path = os.path.join(directory, new_filename)
@monocongo
monocongo / ray_test.ipynb
Created December 18, 2019 15:37
Example of using ray for parallization, not working as expected (10x slower than comparable loop implementation)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@monocongo
monocongo / ray_example_numpy.ipynb
Last active November 30, 2019 18:18
Inability to assign values into shared memory objects using ray, i.e. it results in ValueError: assignment destination is read-only
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.