Commit ba143996 authored by Antoine Berchet's avatar Antoine Berchet
Browse files

Adding Empa NetCDF footprints

parent 0596f010
...@@ -3,7 +3,7 @@ from logging import debug ...@@ -3,7 +3,7 @@ from logging import debug
import numpy as np import numpy as np
import pandas as pd import pandas as pd
from .read import read_flexpart_grid from ...utils.read import read_flexpart_grid
def flux_contribution(self, mode, subdir, dataobs, def flux_contribution(self, mode, subdir, dataobs,
......
...@@ -3,7 +3,7 @@ import numpy as np ...@@ -3,7 +3,7 @@ import numpy as np
import pandas as pd import pandas as pd
from logging import debug from logging import debug
from .read import read_flexpart_gridinit from ...utils.read import read_flexpart_gridinit
def inicond_contribution(self, mode, subdir, dataobs, def inicond_contribution(self, mode, subdir, dataobs,
......
...@@ -7,7 +7,7 @@ import xarray as xr ...@@ -7,7 +7,7 @@ import xarray as xr
from logging import info, debug from logging import info, debug
from ..utils.flexpart_header import read_header from ..utils.flexpart_header import read_header
from pycif.plugins.models.flexpart.io.inputs.read import read_flexpart_grid, read_flexpart_gridinit from ..utils.read import read_flexpart_grid, read_flexpart_gridinit
def native2inputs_adj( def native2inputs_adj(
......
...@@ -8,6 +8,7 @@ import numpy as np ...@@ -8,6 +8,7 @@ import numpy as np
from scipy.io import FortranFile from scipy.io import FortranFile
import datetime import datetime
from logging import info from logging import info
from netCDF4 import Dataset
def read_flexpart_grid(subdir, file_name, fp_header, **kwargs): def read_flexpart_grid(subdir, file_name, fp_header, **kwargs):
...@@ -33,6 +34,23 @@ def read_flexpart_grid(subdir, file_name, fp_header, **kwargs): ...@@ -33,6 +34,23 @@ def read_flexpart_grid(subdir, file_name, fp_header, **kwargs):
scaleconc = 1.e12 scaleconc = 1.e12
if "nc" in path_file:
grid_fp, ngrid, gtime_dt = \
read_grid_nc(path_file, file_dates, fp_header)
else:
grid_fp, ngrid, gtime_dt = \
read_grid(path_file, file_dates, fp_header, scaleconc)
# Convert from ppt to ppmv or ppbv
# Convert s.m3/kg to s.m2/kg and apply numerical scaling
grid_fp = grid_fp / (fp_header.outheight[0] * numscale)
return grid_fp, gtime_dt, ngrid
def read_grid(path_file, file_dates, fp_header, scaleconc):
days = [] days = []
times = [] times = []
counts_i = [] counts_i = []
...@@ -87,31 +105,36 @@ def read_flexpart_grid(subdir, file_name, fp_header, **kwargs): ...@@ -87,31 +105,36 @@ def read_flexpart_grid(subdir, file_name, fp_header, **kwargs):
jt = ngrid - jt - 1 jt = ngrid - jt - 1
grid_fp[jx, jy, jt] = np.abs(sparse_r) * scaleconc grid_fp[jx, jy, jt] = np.abs(sparse_r) * scaleconc
# grid_fp, ngrid, gtime = mod_flexpart.read_grid( return grid_fp, ngrid, gtime_dt
# path_file, file_dates, fp_header.numx, fp_header.numy,
# fp_header.maxngrid, fp_header.xshift, fp_header.ndgrid, fp_header.trajdays)
# Convert from ppt to ppmv or ppbv
# Convert s.m3/kg to s.m2/kg and apply numerical scaling def read_grid_nc(path_file, file_dates, fp_header):
grid_fp = grid_fp / (fp_header.outheight[0] * numscale) with Dataset(path_file) as nc:
# spec001 has dimension nageclass, numpoint, time, level, lat, lon
# Assume that there is 1 numpoint/station per file
shape = nc['spec001'].shape
if shape[0] > 1 or shape[1] > 1:
info('WARNING: There are more than 1 station in the flexpart file.')
if shape[3] > 1:
info('INFO: Select the bottom layer of the grid')
# # Convert grid times to datetime format grid = nc['spec001'][0, 0, :, 0, :, :] # time, lat, lon
# gtime_dt = [] # swap axes to get it to lon, lat, time
# for i in range(len(gtime)): grid_fp = np.swapaxes(grid, 0, -1)
# # gtime_dt[i] = flexpart_header.Flexpartheader.j2d(gtime[i])
# if gtime[i] == 0.:
# break
#
# # gtime_dt.append(fp_header.j2d(gtime[i]))
# gtime_dt.append(j2d(gtime[i]))
return grid_fp, gtime_dt, ngrid times = np.sort(nc['time'][:])
enddate = datetime.strptime(nc.getncattr('iedate'), '%Y%m%d')
gtime = []
for t in times:
gtime.append(enddate + datetime.timedelta(seconds=int(t)))
grid_fp *= 1e12 # This is applied in mod_flexpart
return grid_fp, len(gtime), gtime
def read_flexpart_gridinit(subdir, filename, fp_header, def read_flexpart_gridinit(subdir, filename, fp_header,
scaleconc=1, **kwargs): scaleconc=1, **kwargs):
path_file = os.path.join(subdir, filename) path_file = os.path.join(subdir, filename)
with FortranFile(path_file, 'r') as f: with FortranFile(path_file, 'r') as f:
yyyymmdd = f.read_ints('i4')[0].astype(str) yyyymmdd = f.read_ints('i4')[0].astype(str)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment