Skip to content

Commit

Permalink
Merge pull request #11 from spectraphilic/python2-3
Browse files Browse the repository at this point in the history
Python2 3
  • Loading branch information
FrancescAlted committed Sep 15, 2016
2 parents df062b8 + b413478 commit 2d8ef6b
Show file tree
Hide file tree
Showing 14 changed files with 1,851 additions and 1,812 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ os:

python:
- 2.7
- 3.5

script:
- py.test reflexible -v --cov reflexible --cov-report term-missing
Expand Down
16 changes: 7 additions & 9 deletions reflexible/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,10 @@
This package follows creative commons usage.
"""
#print WELCOME

import os
import datetime as dt

from .version import __version__


this_dir = __path__[0]


Expand All @@ -70,6 +66,7 @@ def _get_hg_description(path_):
except subprocess.CalledProcessError: # not in hg repo
pass


_hg_description = _get_hg_description(this_dir)


Expand All @@ -94,18 +91,19 @@ def print_versions():
datasets = {
'Fwd1_V10.0': os.path.join(this_dir, "uio_examples/Fwd1_V10.0"),
'Bwd1_V9.02': os.path.join(this_dir, "uio_examples/Bwd1_V9.02/outputs"),
'Bwd2_V9.2beta': os.path.join(this_dir, "uio_examples/Bwd2_V9.2beta/outputs"),
'Bwd2_V9.2beta': os.path.join(this_dir,
"uio_examples/Bwd2_V9.2beta/outputs"),
'Fwd1_V9.02': os.path.join(this_dir, "uio_examples/Fwd1_V9.02/outputs"),
'Fwd2_V9.02': os.path.join(this_dir, "uio_examples/Fwd2_V9.02/outputs"),
'HelloWorld_V9.02': os.path.join(this_dir, "uio_examples/HelloWorld_V9.02/outputs"),
}

'HelloWorld_V9.02': os.path.join(this_dir,
"uio_examples/HelloWorld_V9.02/outputs"),
}

# Import the public functions here
from .tests.all import test
from . import conv2netcdf4
from .scripts.create_ncfile import create_ncfile
from .data_structures import (Header, Command, Release,
ReleasePoint, Ageclass)
ReleasePoint, Ageclass)

from .base_read import read_trajectories
88 changes: 44 additions & 44 deletions reflexible/base_read.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

"""
functions for reading fp data in reflexible.
Expand Down Expand Up @@ -36,20 +35,15 @@
import os
import datetime as dt


# Dependencies:
# Numpy
import numpy as np


from .data_structures import Trajectory




def read_trajectories(H, trajfile='trajectories.txt', \
ncluster=5, \
ageclasses=20):
def read_trajectories(H, trajfile='trajectories.txt', ncluster=5,
ageclasses=20):
"""
Reads the trajectories.txt file in a FLEXPART run output directory.
Expand Down Expand Up @@ -126,13 +120,12 @@ def read_trajectories(H, trajfile='trajectories.txt', \

if isinstance(H, str):
try:
alltraj = file(H, 'r').readlines()
alltraj = open(H, 'r').readlines()
except:
raise IOError('Could not open file: %s' % H)
else:
path = H.fp_path
alltraj = file(os.path.join(path, trajfile), 'r').readlines()

alltraj = open(os.path.join(path, trajfile), 'r').readlines()

try:
ibdate, ibtime, model, version = alltraj[0].strip().split()[:4]
Expand All @@ -142,33 +135,37 @@ def read_trajectories(H, trajfile='trajectories.txt', \
version = 'V.x'
tdelta = dt.datetime.strptime(ibdate + ibtime.zfill(6), '%Y%m%d%H%M%S')
numpoint = int(alltraj[2].strip())

RelTraj = Trajectory()
Trajectories = []
# format change?? Try block below because some trajectories.txt
# files have linebreaks, but seems more recent FP v10. does not.
try:
for i in range(3, 3 + (numpoint * 2), 3):
i1, i2, xp1, yp1, xp2 , = tuple([float(j) for j in alltraj[i].strip().split()])
yp2, zp1, zp2, k, npart , = tuple([float(j) for j in alltraj[i+1].strip().split()])
i1, i2, xp1, yp1, xp2, = tuple((float(j) for j in
alltraj[i].strip().split()))
yp2, zp1, zp2, k, npart, = tuple((float(j) for j in
alltraj[i + 1].strip().split()))
except:
for i in range(3, 3 + (numpoint * 2), 2):
i1, i2, xp1, yp1, xp2, yp2, zp1, zp2, k, npart , = \
tuple([float(j) for j in alltraj[i].strip().split()])
i1, i2, xp1, yp1, xp2, yp2, zp1, zp2, k, npart, = \
tuple([float(j) for j in alltraj[i].strip().split()])

itimerel1 = tdelta + dt.timedelta(seconds=i1)
itimerel2 = tdelta + dt.timedelta(seconds=i2)
Xp = (xp1 + xp2) / 2
Yp = (yp1 + yp2) / 2
Zp = (zp1 + zp2) / 2
RelTraj[alltraj[i + 1].strip()] = np.array((itimerel1, itimerel2, Xp, Yp, Zp, k, npart))
RelTraj[alltraj[i + 1].strip()] = np.array(
(itimerel1, itimerel2, Xp, Yp, Zp, k, npart))

for i in range(3 + (numpoint * 3), len(alltraj)):
raw = alltraj[i]
FMT = [0, 5, 8, 9, 9, 8, 8, 8, 8, 8, 8, 8, 8, 8, 6, 6, 6] + ncluster * [8, 8, 7, 6, 8]
FMT = [0, 5, 8, 9, 9, 8, 8, 8, 8, 8, 8, 8, 8, 8, 6, 6, 6] + \
ncluster * [8, 8, 7, 6, 8]

data = [raw[sum(FMT[:ii]):sum(FMT[:ii + 1])] for ii in range(1, len(FMT) - 1)] + \
[raw[sum(FMT[:-1]):]]
data = [raw[sum(FMT[:ii]):sum(FMT[:ii + 1])]
for ii in range(1, len(FMT) - 1)] + [raw[sum(FMT[:-1]):]]
### FIX ###
# ## To get rid of '******' that is now in trajectories.txt
data = [float(r.replace('********', 'NaN')) for r in data]
Expand All @@ -180,28 +177,31 @@ def read_trajectories(H, trajfile='trajectories.txt', \
RelTraj['date'] = tdelta
RelTraj['Trajectories'] = data
RelTraj['labels'] = \
['release number', 'seconds prior to release', 'lon', 'lat', 'height', 'mean topography', \
'mean mixing height', 'mean tropopause height', 'mean PV index', \
'rms distance', 'rms', 'zrms distance', 'zrms', \
'fraction mixing layer', 'fraction PV<2pvu', 'fraction in troposphere'] + \
ncluster * ['xcluster', 'ycluster', 'zcluster', 'fcluster', 'rmscluster']
['release number', 'seconds prior to release', 'lon', 'lat',
'height', 'mean topography',
'mean mixing height', 'mean tropopause height', 'mean PV index',
'rms distance', 'rms', 'zrms distance', 'zrms',
'fraction mixing layer', 'fraction PV<2pvu',
'fraction in troposphere'] + \
ncluster * ['xcluster', 'ycluster', 'zcluster', 'fcluster',
'rmscluster']
RelTraj['info'] = \
"""
Returns a dictionary:
R['Trajectories'] = array_of_floats(
releasenum,it1,xi,yi,zi,topoi,hmixi,tropoi,pvi,
rmsdisti,rmsi,zrmsdisti,zrmsi,hfri,pvfri,trfri,
(xclusti(k),yclusti(k),zclusti(k),fclusti(k),rmsclusti(k),k=1,5))
R['RELEASE_ID'] = (dt_i1,dt_i2,xp1,yp1,xp2,yp2,zp1,zp2,k,npart)
R['info'] = this message
To plot a trajectory timeseries:
RT = read_trajectories(H)
T = RT['Trajectories']
rel = 1
t = T[np.where(T[:,0]==rel),:][0]
plt.plot(t[:,1],t[:,14])
plt.savefig('trajectories.png')
"""
return RelTraj
"""
Returns a dictionary:
R['Trajectories'] = array_of_floats(
releasenum,it1,xi,yi,zi,topoi,hmixi,tropoi,pvi,
rmsdisti,rmsi,zrmsdisti,zrmsi,hfri,pvfri,trfri,
(xclusti(k),yclusti(k),zclusti(k),fclusti(k),rmsclusti(k),k=1,5))
R['RELEASE_ID'] = (dt_i1,dt_i2,xp1,yp1,xp2,yp2,zp1,zp2,k,npart)
R['info'] = this message
To plot a trajectory timeseries:
RT = read_trajectories(H)
T = RT['Trajectories']
rel = 1
t = T[np.where(T[:,0]==rel),:][0]
plt.plot(t[:,1],t[:,14])
plt.savefig('trajectories.png')
"""
return RelTraj
45 changes: 24 additions & 21 deletions reflexible/conv2netcdf4/flexpart_read.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#### Functions for reading FLEXPART output #####

from __future__ import print_function

import os
import re
import datetime
Expand Down Expand Up @@ -162,7 +164,7 @@ def read_command_old(path, headerrows):
================ =================================================================
"""
lines = file(path, 'r').readlines()
lines = open(path, 'r').readlines()
command_vals = [i.strip() for i in lines[headerrows:]] # clean line ends
COMMAND_KEYS = (
'SIM_DIR',
Expand Down Expand Up @@ -251,7 +253,7 @@ def getfile_lines(infile):
reverts to beginning of file."""

if isinstance(infile, str):
return file(infile, 'r').readlines()
return open(infile, 'r').readlines()
else:
infile.seek(0)
return infile.readlines()
Expand Down Expand Up @@ -375,20 +377,21 @@ def read_trajectories(H, trajfile='trajectories.txt',

if isinstance(H, str):
try:
alltraj = file(H, 'r').readlines()
alltraj = open(H, 'r').readlines()
except:
raise IOError('Could not open file: %s' % H)
else:
path = H.path
alltraj = file(os.path.join(path, trajfile), 'r').readlines()
alltraj = open(os.path.join(path, trajfile), 'r').readlines()

try:
ibdate, ibtime, model, version = alltraj[0].strip().split()[:4]
except:
ibdate, ibtime = alltraj[0].strip().split()[:2]
model = 'Flexpart'
version = 'V.x'
dt = datetime.datetime.strptime(ibdate + ibtime.zfill(6), '%Y%m%d%H%M%S')
dt = datetime.datetime.strptime(ibdate + ibtime.zfill(6),
'%Y%m%d%H%M%S')
numpoint = int(alltraj[2].strip())
# Fill a dictionary with the Release points and information keyed by name
# RelTraj['RelTraj_ID'] = (i1,i2,xp1,yp1,xp2,yp2,zp1,zp2,k,npart)
Expand Down Expand Up @@ -505,7 +508,7 @@ def curtain_agltoasl(H, curtain_agl, coords, below_gl=0.0):
xp = H.outheight - H.outheight[0]
casl = np.zeros((len(H.asl_axis), len(coords)))

for i in xrange(len(coords)):
for i in range(len(coords)):
casl[:, i] = np.interp(H.asl_axis, xp + gl[i],
curtain_agl[:, i], left=below_gl)
return casl
Expand Down Expand Up @@ -543,7 +546,7 @@ def read_agespectrum(filename, part=False, ndays=20):
"""

f = file(filename, 'r').readlines()
f = open(filename, 'r').readlines()

line1 = f[0].strip().split()
if part:
Expand Down Expand Up @@ -666,7 +669,7 @@ def gridarea(H):
ylata = outlat0 + (float(iy) + 0.5) * dyout
ylatp = ylata + 0.5 * dyout
ylatm = ylata - 0.5 * dyout
if (ylatm < 0 and ylatp > 0):
if ylatm < 0 and ylatp > 0:
hzone = dyout * r_earth * pih
else:
# cosfact = cosfunc(ylata)
Expand Down Expand Up @@ -700,12 +703,12 @@ def _get_header_version(bf):

ret = bf.tell()
bf.seek(12) # start of version string
version = bf.read('13S')
version = bf.read('13S').decode()
# Somewhere in version 9.2 beta, the version length changed to 29
# However, one *must* check which is the final size for this
if '9.2 b' in version:
bf.seek(12)
version = bf.read('29S')
version = bf.read('29S').decode()
bf.seek(ret)

return version
Expand Down Expand Up @@ -792,10 +795,10 @@ def read_header(pathname, **kwargs):
OPS.update(kwargs)

if OPS.verbose:
print "Reading Header with:\n"
print("Reading Header with:\n")

for o in OPS:
print "%s ==> %s" % (o, OPS[o])
print("%s ==> %s" % (o, OPS[o]))

# Define utility functions for reading binary file
skip = lambda n = 8: bf.seek(n, 1)
Expand Down Expand Up @@ -831,7 +834,7 @@ def read_header(pathname, **kwargs):
raise IOError("No DATEFILE: {0}".format(datefile))
else:
try:
fd = file(datefile, 'r').readlines()
fd = open(datefile, 'r').readlines()
except:
raise IOError("Could not read datefile: {0}".format(datefile))

Expand Down Expand Up @@ -872,7 +875,7 @@ def read_header(pathname, **kwargs):
h['jjjjmmdd'] = bf.read('i')
h['hhmmss'] = bf.read('i')
junk.append(bf.read('2i'))
h['nspec'] = bf.read('i') / 3 # why!?
h['nspec'] = bf.read('i') // 3 # why!?
h['numpointspec'] = bf.read('i')
junk.append(bf.read('2i'))

Expand All @@ -894,15 +897,15 @@ def read_header(pathname, **kwargs):
else:
junk.append(bf.read('i'))
h['wetdep'].append(
''.join([bf.read('c') for i in range(10)]).strip())
''.join([bf.read('c').decode() for i in range(10)]).strip())
junk.append(bf.read('2i'))
junk.append(bf.read('i'))
h['drydep'].append(
''.join([bf.read('c') for i in range(10)]).strip())
''.join([bf.read('c').decode() for i in range(10)]).strip())
junk.append(bf.read('2i'))
h['nz_list'].append(bf.read('i'))
h['species'].append(
''.join([bf.read('c') for i in range(10)]).strip())
''.join([bf.read('c').decode() for i in range(10)]).strip())
junk.append(bf.read('2i'))

if 'V6' in version:
Expand All @@ -920,7 +923,7 @@ def read_header(pathname, **kwargs):
I = {2: 'kindz', 3: 'xp1', 4: 'yp1', 5: 'xp2',
6: 'yp2', 7: 'zpoint1', 8: 'zpoint2', 9: 'npart', 10: 'mpart'}

for k, v in I.iteritems():
for k, v in I.items():
# create zero-filled lists in H dict
h[v] = np.zeros(h['numpoint'])

Expand Down Expand Up @@ -958,9 +961,9 @@ def read_header(pathname, **kwargs):
gt = bf.tell() + l # create 'goto' point
sp = ''
# collect the characters for the compoint
while re.search("\w", bf.read('c')):
while re.search(b"\w", bf.read('c')):
bf.seek(-1, 1)
sp = sp + bf.read('c')
sp = sp + bf.read('c').decode()

bf.seek(gt) # skip ahead to gt point

Expand Down Expand Up @@ -1180,7 +1183,7 @@ def _readV6(bf, h):

if bf:
# bf.read('i')
print h['numpoint']
print(h['numpoint'])
for i in range(h['numpoint']):
# r2=getbin('i')
i1 = getbin('i')
Expand Down
2 changes: 1 addition & 1 deletion reflexible/conv2netcdf4/fortflex/build_FortFlex.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
rm -f FortFlex.pyf
f2py -m FortFlex -h FortFlex.pyf FortFlex.f
f2py -c --fcompiler=gfortran FortFlex.pyf FortFlex.f
mv FortFlex.so ..
mv *.so ..

Loading

0 comments on commit 2d8ef6b

Please sign in to comment.