Skip to content

Instantly share code, notes, and snippets.

View geogradient's full-sized avatar

Jose Beltran geogradient

  • Stockholm University
  • Sweden
View GitHub Profile
@geogradient
geogradient / gist_column_rename_using_pandas.py
Created July 23, 2015 13:20
Example of using Pandas for renaming columns
# ...
# The nutrients dictionary holds a panda data frame with columns week, and "PO4_mgP_m3" or "DIN_mgN_m3".
# for simplicity I rename the columns holding the values as value.
for station in nutrients.keys():
for year in nutrients[station].keys():
nutrients[station][year]["PO4"].rename(columns={"PO4_mgP_m3":'value'}, inplace=True)
nutrients[station][year]["DIN"].rename(columns={"DIN_mgN_m3":'value'}, inplace=True)
# Example of use
@geogradient
geogradient / plt_added_tips.py
Last active August 29, 2015 14:20
Elements that can be useful in matplotlib. These are not secuential, just a copy&paste of bits and bytes of code used while learning matplotlib. Mostly for me to remember how I solved.
import matplotlib.pyplot as plt
from matplotlib import rc
rc('mathtext', default='regular')
%matplotlib inline
#
fig = plt.figure()
ax = fig.add_subplot(111)
# or
@geogradient
geogradient / create_N_colours_list.py
Created April 30, 2015 06:09
# Function to make an automatic colour list holding Hex values.
import colorsys
def get_N_HexCol(N=5):
HSV_tuples = [(x*1.0/N, 1, 1) for x in xrange(N)]
hex_out = []
for rgb in HSV_tuples:
rgb = map(lambda x: int(x*255),colorsys.hsv_to_rgb(*rgb))
hex_out.append("".join(map(lambda x: chr(x).encode('hex'),rgb)))
return ["#"+c_value for c_value in hex_out]
@geogradient
geogradient / gist_calculate_centroid_site_location.py
Created December 3, 2014 14:57
Calculates harmonized locations for sites where the sampling location (lat, lon) may have changed by boat drifting during the sampling date.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Modified from: http://stackoverflow.com/questions/14114610/finding-centre-of-a-polygon-using-limited-data
See also: http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon
NOTE: All the following geolocations calculations assumed that
the distance from each position is short, i.e. that can be considered to over a flat surface.
Otherwise, converting to spherical coordinates needs to be implemented.
"""
@geogradient
geogradient / gist_beam_utils.py
Last active August 29, 2015 14:07
Builds BEAM-VISAT pin files as a batch process. Outputs "pin_sampling_date_batched.placemark" files using an input table/pandas data frame with the details of the sampling locations.
def create_pin_files(data_frame, outdir = '/outdir/path/only/' ):
"""
Batch process the creation of BEAM-VISAT (http://www.brockmann-consult.de/cms/web/beam/) pin files.
Outputs a "pin_sampling_date_batched.placemark" file.
Uses pandas data_frame as input.
Expected column names in the input data frame are:
["IS_DATE"] YYYYMMDD format
["SITE"] Name of the Placemark, ie. station, sampling site or Cast ID
["Latitude"] Latitude of Placemark in decimal degrees using WGS84 datum
["Longitude"] Longitude of Placemark in decimal degrees using WGS84 datum
@geogradient
geogradient / gist: files_per_week_per_year.py
Last active August 29, 2015 14:04
get the number of images that per week per year loading from a json file in order to tabulate per week and year later on
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = "[José M. Beltrán](<beltran.data@gmail.com>)"
__credits__ = ["José M. Beltrán"]
__license__ = "GPL-3.0"
"""
The json file has the following scheme:
{"20020614":{"KEY1":{"KEY1.1":"path_to_file_01", ...},
@geogradient
geogradient / gist: date_to_week_number.py
Last active August 29, 2015 14:04
Function to get the week number based on a given date_string
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = "[José M. Beltrán](<beltran.data@gmail.com>)"
__credits__ = ["José M. Beltrán"]
__license__ = "GPL-3.0"
def date_to_week_number(date_string):
"""
:param date_string: input a date_string with format YYYYmmdd
:return: the week number for the given date_string