Newer
Older
def writeNetCDF(cfg, device, fe, r, variables_1D):
# ncvars is a dictionary that store a netcdf variable for each physical parameter key
ncvars = {}
# variables and dimensions use for 1D and 2D variables
#variables_1D = ['TIME', 'LATITUDE', 'LONGITUDE']
dims_2D = ['TIME', 'depth']
fileName = "{}/OS_{}_{}.nc".format(cfg['global']
['netcdf'], cfg['cruise']['cycleMesure'], device)
if not os.path.exists(cfg['global']['ascii']):
print('writing netCDF file: {}'.format(fileName))
# n is number of profiles, m the max size of profiles
time = nc.createDimension("TIME", fe.n)
lat = nc.createDimension("LATITUDE", fe.n)
lon = nc.createDimension("LONGITUDE", fe.n)
depth = nc.createDimension('depth', fe.m)
logging.debug(" depth: {}, time: {}, lat: {}, lon: {}".format(
len(depth), len(time), len(lat), len(lon)))
# create variables
# add dimensions before variables list
for k in fe.keys:
variables.append(k)
# variables.extend(fe.keys())
for key in variables:
# for each variables get the attributes dictionary from Roscop
hash = r[key]
# _FillValue attribute must be set when variable is created
# (using fill_value keyword to createVariable)
if '_FillValue' in hash:
fillvalue = hash['_FillValue']
# remove from the dictionary
hash.pop('_FillValue')
else:
fillvalue = None
try:
# create variable whit same dimension name, as TIME(TIME)
ncvars[key] = nc.createVariable(
key, dtype(hash['types']).char, (key,), fill_value=fillvalue)
except:
# for BATH(TIME), it's a mess !
ncvars[key] = nc.createVariable(
key, dtype(hash['types']).char, 'TIME', fill_value=fillvalue)
key, dtype(hash['types']).char, dims_2D, fill_value=fillvalue)
# remove from the dictionary
hash.pop('types')
# create dynamically variable attributes
for k in hash.keys():
# add global attributes
nc.data_type = "OceanSITES profile data"
nc.Conventions = "CF-1.8"
nc.cycle_mesure = cfg['cruise']['cycleMesure']
nc.time_coverage_start = cfg['cruise']['beginDate']
nc.time_coverage_end = cfg['cruise']['endDate']
nc.timezone = cfg['cruise']['timezone']
nc.data_assembly_center = cfg['cruise']['institute']
nc.type_instrument = cfg[device.lower()]['typeInstrument']
nc.instrument_number = cfg[device.lower()]['instrumentNumber']
nc.date_update = datetime.today().strftime('%Y-%m-%dT%H:%M:%SZ')
nc.pi_name = cfg['cruise']['pi']
nc.processing_state = "1A"
nc.codification = "OOPC"
nc.format_version = "1.2"
nc.Netcdf_version = "3.6"
logging.debug(" var: {}, dims: {}, shape: {}, dtype: {}, ndim: {}".format(
key, ncvars[key].dimensions, ncvars[key].shape, ncvars[key].dtype, ncvars[key].ndim))
for key in variables:
if any(key in item for item in variables_1D):