Skip to content

Instantly share code, notes, and snippets.

View jgomezdans's full-sized avatar

José Gómez-Dans jgomezdans

View GitHub Profile
@jgomezdans
jgomezdans / import_geonames.sql
Created July 1, 2010 13:14
Importing the geonames DB into Spatialite
CREATE TABLE geonames ( geoname_id int, name text, asciiname text, alternatenames text, latitude real, longitude real, feature_class text, feature_code text, country_code text, cc2 text, admin1 text, admin2 text, admin3 text, admin4 text, population integer, elevation integer, gtopo30 integer, timezone integer, modification_date text) ;
.separator \t
.import allCountries.txt geonames
ALTER TABLE geonames ADD COLUMN geom blob ;
UPDATE geonames SET geom = PointFromText ( 'POINT(' || longitude || ' ' || latitude || ')', 4326 ) ;
# -*- coding: utf-8 -*-
from drivers import fuel_drivers, spitfire_drivers
import numpy as np
f = open ( "selected_SA_spitfire_cells.txt", 'r' )
f.readline() # Skip header
pft_names = np.array( ["TrBLEvgn", "TrBLRgn", \
"TeNLEvgn", "TeBLEvgn", "TeBLSgn", \
"BoNLEvgn", "BoNLSgn", \
"C3", "C4" ] )
@jgomezdans
jgomezdans / csv_python.py
Created November 1, 2010 10:17
Pissing about with MdK CSV file
"""
The idea is:
1. Load it into a numpy array:
2. Loop over the years (1998 in your case)
3. Sort by DoY (maybe not needed, but just in case)
4. Do a cumsum on the field you want (assuming there's no resetting and stuff, but easy
to add that in)
"""
# Start by storing model names in an array. This keeps order, but prolly not important
models = [ 'casa', 'lpj', 'jules', 'middle_aged_glamour_model']
# The year list that you are going to loop over
years = np.arange ( 1990, 2011 )
# Model variables for each model
# You need to keep each list in the order they appear in the output. That's fairly important.
model_var = { 'casa':[ 'doy','NEE', 'NPP'], 'lpj':['year', 'doy','NEE', "GPP"], \
'doy',middle_aged_glamour_model']=[ 'doy','you','dont','wanna_know'] }
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.basemap import Basemap
from matplotlib import colors
from matplotlib.colorbar import ColorbarBase
# cylindrical projection
d = np.loadtxt("GFED3.1.C.GFED3.1.C.04-09.dat")
d = np.ma.array ( d, mask=d==0.0)
@jgomezdans
jgomezdans / using_sets.py
Created November 19, 2010 11:16
Using sets to line up arrays
import numpy as np
m_date = np.array(['1998-01-01 00:00:00', '1999-01-01 00:00:00', \
'2000-01-01 00:00:00', '2005-01-01 00:00:00'])
o_date = np.array(['1998-01-01 00:00:00', '1999-01-01 00:00:00', \
'2000-01-01 00:00:00'])
mm = np.array([ 3.5732, 4.5761, 4.0994, 3.9031])
oo = np.array([ 5.84, 5.66, 5.83])
# Create a dictionary, with dates as keys
M = dict ( zip ( m_date, mm))
O = dict ( zip ( o_date, oo ))
from osgeo import ogr
# First, define our point. Using WKT, but could use a OGR source or something
my_point = ogr.CreateGeometryFromWkt( \
"POINT( 224021.425476915, 4114664.1465754998)" )
dataset = ogr.Open ( FILENAME )
layer = dataset.GetLayer ( 0 ) # Or whatever it is you require
@jgomezdans
jgomezdans / sun_position.py
Created December 8, 2010 19:12
Finding the position of the sun
def sun_position ( date, hour, longitude, latitude ):
"""
Calculates the position of the sun given a position and time.
Basically, all you need to know is here:
http://answers.google.com/answers/threadview/id/782886.html
"""
import time
from math import sin, cos, degrees, radians, acos
doy = int ( time.strftime( "%j", time.strptime( date, "%Y-%m-%d" ) ) )
#!/usr/bin/env python
#buffer.py
import sys
import os
from osgeo import ogr
from osgeo import osr
def buffer(infile,outfile,buffdist):
try:
import os
import fnmatch
def locate( pattern, root=os.curdir ):
"""
Locate all files matching supplied filename pattern in and below
supplied root directory.
"""
for path, dirs, files in os.walk( os.path.abspath( root ) ):
for filename in fnmatch.filter( files, pattern ):