Commit 0aa946dd authored by Modellers Operational's avatar Modellers Operational
Browse files

Add file conversion for adrift

parent e40cccaa
#!/bin/sh
vars_str=$1
vars_list=$(echo $VARS | tr "," "\n" | tr ";" "\n")
for v in $vars_list; do
ncatted -a _FillValue,$v,o,f,"-32768" today_adrift.nc
done
ncatted -a _FillValue,h,o,f,"-32768" today_adrift.nc
import numpy as np
import netCDF4 as nc
import datetime as dt
import sys
copy_var = [l for s in [this_str.split(',') for this_str in sys.argv[1].split(';')] for l in s]
out_file = 'today_adrift.nc'
in_file = 'today_forecast.nc'
copy_var.extend(['h', 'mask', 'lon', 'lat'])
ref_date = dt.datetime(2000,1,1)
format='f4'
#copy_var = ['v', 'u', 'h', 'mask', 'lon', 'lat', 'temp', 'salinity', 'ww', 'zeta']
#copy_var = ['lon', 'lat', 'mask', 'h', 'Uwind', 'Vwind']
in_nc = nc.Dataset(in_file, 'r')
out_nc = nc.Dataset(out_file, 'w')
for dimension in in_nc.dimensions.keys():
out_nc.createDimension(dimension, in_nc.dimensions[dimension].size)
for attribute in in_nc.ncattrs():
setattr(out_nc, attribute, getattr(in_nc, attribute))
for var_name in copy_var:
in_var = in_nc.variables[var_name]
var = out_nc.createVariable(var_name, format, in_var.dimensions)
if in_var.ncattrs:
for attribute in in_var.ncattrs():
if attribute not in ['_FillValue']:
setattr(var, attribute, getattr(in_var, attribute))
var[:] = in_var[:]
# time
in_time_str_raw = in_nc.variables['Times'][:]
in_time_str = np.asarray([b''.join(this_str).decode('utf-8') for this_str in in_time_str_raw])
in_time_dt = np.asarray([dt.datetime.strptime(this_str, '%Y-%m-%dT%H:%M:%S.000000') for this_str in in_time_str])
time_sec = np.asarray([(this_dt - ref_date).total_seconds() for this_dt in in_time_dt])
atts = {'units': 'seconds since 2000-01-01 00:00:00',
'long_name': 'time',
'time_zone': 'UTC'}
var = out_nc.createVariable('time', format, ('time'))
for att_name, att_val in atts.items():
setattr(var, att_name, att_val)
var[:] = time_sec
in_nc.close()
out_nc.close()
[command]
default = src_file=/${OUTPUT_DIR}/${FORECAST_DAY}/${OUTPUT_FILENAME}.nc;
mkdir -p /${PORTAL_BASEDIR}/${FORECAST_YEAR}/${FORECAST_MONTH}/
cd /${PORTAL_BASEDIR}/today_forecast/
rm -f today_adrift.nc
python3 convert_time bash ${VARS};
bash ./add_FillValue.sh ${VARS};
Markdown is supported
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