Skip to content

Commit

Permalink
Added a new function to read a COMMAND file for V9 (and maybe older v…
Browse files Browse the repository at this point in the history
…ersions)
  • Loading branch information
Francesc Alted committed Dec 5, 2014
1 parent 2893359 commit 1b3562f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
2 changes: 1 addition & 1 deletion reflexible/conv2netcdf4/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Import the public functions here
from .data_structures import Header, Structure, BinaryFile, FDC
from .flexpart_read import read_header, read_trajectories, read_command
from .flexpart_read import read_header, read_trajectories, read_command, read_commandV9
from .grid_read import read_grid, get_slabs, fill_grids
51 changes: 51 additions & 0 deletions reflexible/conv2netcdf4/flexpart_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,57 @@
import reflexible.conv2netcdf4
from .helpers import closest

def read_commandV9(path, headerrows=7):
"""Quick and dirty approach for reading COMMAND file for FP V9"""
COMMAND_KEYS = (
'SIM_DIR',
'SIM_START',
'SIM_END',
'AVG_CNC_INT',
'AVG_CNC_TAVG',
'CNC_SAMP_TIME',
'T_PARTSPLIT',
'SYNC',
'CTL',
'IFINE',
'IOUT',
'IPOUT',
'LSUBGRID',
'LCONVECTION',
'LAGESPECTRA',
'IPIN',
'OUTPUTFOREACHRELEASE',
'IFLUX',
'MDOMAINFILL',
'IND_SOURCE',
'IND_RECEPTOR',
'MQUASILAG',
'NESTED_OUTPUT',
'LINIT_COND')
float_keys = ['CTL']
date_keys = ['SIM_START', 'SIM_END']

COMMAND = {}
ncommand = 0
with open(path, 'r') as comfile:
for i in range(headerrows):
comfile.readline()
while ncommand < len(COMMAND_KEYS):
comfile.readline()
val = comfile.readline().strip().split()
key = COMMAND_KEYS[ncommand]
if key in date_keys:
val = val[:2]
elif key in float_keys:
val = float(val[0])
else:
val = int(val[0])
COMMAND[key] = val
comfile.readline()
comfile.readline()
ncommand += 1
return COMMAND


def read_command(path, headerrows=7):
"""
Expand Down
16 changes: 11 additions & 5 deletions reflexible/scripts/create_ncfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
import os.path
import netCDF4 as nc

from reflexible.conv2netcdf4 import Header, read_grid
from reflexible.conv2netcdf4 import Header, read_grid, read_command, read_commandV9


def write_metadata(H, ncid):
def write_metadata(H, command, ncid):
# hes CF convention requires these attributes
ncid.Conventions = 'CF-1.6'
ncid.title = 'FLEXPART model output'
Expand Down Expand Up @@ -70,7 +70,7 @@ def write_metadata(H, ncid):
# ncid.surf_only = H.surf_only


def write_header(H, ncid):
def write_header(H, command, ncid):
nnx = H.numxgrid
nny = H.numygrid
nnz = H.numzgrid
Expand Down Expand Up @@ -138,6 +138,11 @@ def create_ncfile(fddir, nested, outfile=None):
fprefix = 'grid_conc_'
else:
fprefix = 'grid_time_'

command_path = os.path.join(os.path.dirname(fddir), "options/COMMAND")
# XXX This needs to be checked out, as I am not sure when the new format started
command = read_commandV9(command_path)

path = os.path.dirname(fddir)
fprefix = os.path.join(path, fprefix)
if outfile is None:
Expand All @@ -148,9 +153,10 @@ def create_ncfile(fddir, nested, outfile=None):
else:
ncfname = outfile
cache_size = 16 * H.numxgrid * H.numygrid * H.numzgrid

ncid = nc.Dataset(ncfname, 'w', chunk_cache=cache_size)
write_metadata(H, ncid)
write_header(H, ncid)
write_metadata(H, command, ncid)
write_header(H, command, ncid)
ncid.close()
return ncfname

Expand Down

0 comments on commit 1b3562f

Please sign in to comment.