Skip to content

Instantly share code, notes, and snippets.

@jobel-openscience
jobel-openscience / FileTransfer.py
Created April 19, 2016 10:38 — forked from omz/FileTransfer.py
File Transfer script for Pythonista (iOS)
# File Transfer for Pythonista
# ============================
# This script allows you to transfer Python files from
# and to Pythonista via local Wifi.
# It starts a basic HTTP server that you can access
# as a web page from your browser.
# When you upload a file that already exists, it is
# renamed automatically.
# From Pythonista's settings, you can add this script
# to the actions menu of the editor for quick access.
@jobel-openscience
jobel-openscience / gist_add_new_column_to_pandas_df.py
Created October 7, 2015 07:28
add a new column to pandas data frame
data['LAT'] = pd.Series([str(round(value,9)) for value in data.lat], index=data.index)
@jobel-openscience
jobel-openscience / gist_reversing_key_values_from_dict.py
Last active October 1, 2015 11:42
reversing keys and values_from a dictionary (key <-> value) credits to: http://stackoverflow.com/a/19165996
# Reversing a dictionary, key <-> value
# but only this work when values are hashable
# and there are no duplicate values.
# credits to: http://stackoverflow.com/a/19165996
p = dict(zip(i.values(),i.keys()))
@jobel-openscience
jobel-openscience / remove_multiple_keys_by_value.py
Last active October 1, 2015 11:45
How to remove multiple keys from a dictionary by its value and bit of fun working with dates
from datetime import datetime
# Suppose you have a list of dates that you want to rearrange and group by year and month
date_list = ['2002-10-31', '2002-11-12', '2002-11-13', '2003-01-26', '2003-02-01', '2003-02-02', '2003-02-04', '2003-02-07',
'2003-02-09', '2003-02-16', '2003-02-18', '2003-02-27', '2003-03-02', '2003-03-04', '2003-03-05', '2003-03-10', '2003-03-12',
'2003-03-13', '2003-04-15', '2003-04-16', '2003-11-21', '2003-11-24', '2004-01-29', '2004-01-30', '2004-01-31', '2004-02-01',
'2004-02-02', '2004-02-03', '2004-02-04', '2004-02-05']
# You can convert it first as an isoDate
isoDates = [datetime.strptime(d, "%Y-%m-%d") for d in date_list]
@jobel-openscience
jobel-openscience / gist_to_parse_a_date_string.py
Last active September 28, 2015 11:06
Simple example on how to parse dates assuming pandas dataframe (df)
# Examples to parse a date
from datetime import datetime
def parser(string2parse):
return datetime.strptime(string2parse, "%d/%m/%Y")
H3 = pd.read_csv(os.path.join(dirname,filename),sep="\t")
# Alternative (1)
H3["isoDate"] = [parser(d) for d in H3.Date]
# or
# Alternative (2)
#
# /etc/sysctl.conf - Configuration file for setting system variables
# See /etc/sysctl.d/ for additional system variables.
# See sysctl.conf (5) for information.
#
#kernel.domainname = example.com
# Uncomment the following to stop low-level messages on console
#kernel.printk = 3 4 1 3
@jobel-openscience
jobel-openscience / gist_beam_batch_process_pin_file_creation.py
Created December 5, 2014 10:43
Python script that helps to batch process a table of sampling locations into BEAM-VISAT placemark files. Mostly for pixel extraction.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__credits__ = ["José M. Beltrán"]
__license__ = "GPL-3.0"
__created_on__ = ["2014-10-15"]
def create_pin_files(data_frame, outdir):
"""
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.
@jobel-openscience
jobel-openscience / gist_calculate_centroid_site_location.py
Created December 5, 2014 10:34
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.
"""