amescap.Ncdf_wrapper

Ncdf_wrapper archives data into netCDF format. It serves as a wrapper for creating netCDF files.

Third-party Requirements:

  • numpy

  • amescap.FV3_utils

  • scipy.io

  • netCDF4

  • os

  • datetime

Module Contents

Classes

Fort

A class that generates an object from a fort.11 file. The new file

Ncdf

netCDF wrapper for archiving data in netCDF format. Alex Kling.

class Fort(filename=None, description_txt='')

Bases: object

A class that generates an object from a fort.11 file. The new file will have netCDF file attributes. Alex Kling.

EX:

f.variables.keys()
f.variables['var'].long_name
f.variables['var'].units
f.variables['var'].dimensions

Create a Fort object using the following:

f=Fort('/Users/akling/test/fort.11/fort.11_0684')

Public methods can be used to generate FV3-like netCDF files:

f.write_to_fixed()
f.write_to_average()
f.write_to_daily()
f.write_to_diurn()
Parameters:

object (_type_) – _description_

Returns:

_description_

Return type:

_type_

class Fort_var(input_vals, name_txt, long_name_txt, units_txt, dimensions_tuple)

Bases: numpy.ndarray

Sub-class that emulates a netCDF-like variable by adding the name, long_name, units, and dimensions attributes to a numpy array. Inner class for fortran_variables (Fort_var) that comprise the Fort file. Alex Kling

A useful resource on subclassing is available at: https://numpy.org/devdocs/reference/arrays.classes.html

Note

Because we use an existing numpy.ndarray to define the object, we do not call __array_finalize__(self, obj)

Parameters:

np.ndarray (_type_) – _description_

Returns:

_description_

Return type:

_type_

__abs__()
__add__(value)
__and__(value)
__array__(dtype=None)
__array_wrap__(obj)
classmethod __class_getitem__(value)
__contains__(key)
__copy__()
__deepcopy__(memo)
__divmod__(value)
__eq__(value)

Return self==value.

__float__()
__floordiv__()
__ge__(value)

Return self>=value.

__getitem__(key)
__gt__(value)

Return self>value.

__iadd__(value)
__iand__(value)
__ifloordiv__(value)
__ilshift__(value)
__imod__(value)
__imul__(value)
__int__()
__invert__()
__ior__(value)
__ipow__(value)
__irshift__(value)
__isub__(value)
__itruediv__(value)
__ixor__(value)
__le__(value)

Return self<=value.

__len__()
__lshift__(value)
__lt__(value)

Return self<value.

__matmul__(value)
__mod__(value)
__mul__(value)
__ne__(value)

Return self!=value.

__neg__()
__or__(value)
__pos__()
__pow__()
__repr__()

Return repr(self).

__rshift__()
__setitem__(key, value)
__str__()

Return str(self).

__sub__(value)
__truediv__(value)
__xor__(value)
all(axis=None, out=None, keepdims=False)
any(axis=None, out=None, keepdims=False)
argmax(axis=None, out=None)
argmin(axis=None, out=None)
argpartition(kth, axis=-1, kind='introselect', order=None)
argsort(axis=-1, kind='quicksort', order=None)
astype(dtype, order='K', casting='unsafe', subok=True, copy=True)
byteswap(inplace=False)
choose(choices, out=None, mode='raise')
clip(min=None, max=None, out=None)
compress(condition, axis=None, out=None)
conj()
conjugate()
copy(order='C')
cumprod(axis=None, dtype=None, out=None)
cumsum(axis=None, dtype=None, out=None)
diagonal(offset=0, axis1=0, axis2=1)
dot(b, out=None)
dump(file)
dumps()
fill(value)
flatten(order='C')
getfield(dtype, offset=0)
item(*args)
itemset(*args)
max(axis=None, out=None)
mean(axis=None, dtype=None, out=None, keepdims=False)
min(axis=None, out=None, keepdims=False)
newbyteorder(new_order='S')
nonzero()
partition(kth, axis=-1, kind='introselect', order=None)
prod(axis=None, dtype=None, out=None, keepdims=False)
ptp(axis=None, out=None)
put(indices, values, mode='raise')
ravel(order='C')
repeat(repeats, axis=None)
reshape(shape, order='C')
resize(new_shape, refcheck=True)
round(decimals=0, out=None)
searchsorted(v, side='left', sorter=None)
setfield(val, dtype, offset=0)
setflags(write=None, align=None, uic=None)
sort(axis=-1, kind='quicksort', order=None)
squeeze(axis=None)
std(axis=None, dtype=None, out=None, ddof=0, keepdims=False)
sum(axis=None, dtype=None, out=None, keepdims=False)
swapaxes(axis1, axis2)
take(indices, axis=None, out=None, mode='raise')
tobytes(order='C')
tofile(fid, sep='', format='%s')
tolist()
tostring(order='C')
trace(offset=0, axis1=0, axis2=1, dtype=None, out=None)
transpose(*axes)
var(axis=None, dtype=None, out=None, ddof=0, keepdims=False)
view(dtype=None, type=None)
close()
write_to_average(day_average=5)

Create average file (e.g., N-day averages [N=5 usually])

write_to_daily()

Create daily file (continuous time series)

write_to_diurn(day_average=5)

Create diurn file (variables organized by time of day & binned (typically 5-day bins)

write_to_fixed()

Create fixed file (all static variables)

class Ncdf(filename=None, description_txt='', action='w', ncformat='NETCDF4_CLASSIC')

Bases: object

netCDF wrapper for archiving data in netCDF format. Alex Kling.

Usage:

from netcdf_wrapper import Ncdf

Fgeo = 0.03 # W/m2, a constant
sfcT = np.ones((24,8)) # surface temperature

# Create file
filename = "/path/to/myfile.nc"
description = "results from new simulation, Alex 01-01-19"
Log = Ncdf(filename, description)

# Save the constant (``Fgeo``) to the file
Log.add_constant('Fgeo', Fgeo, "geothermal flux", "W/m2")

# Save the sfcT array to the file
Log.add_dimension('Nx', 8)
Log.add_dimension('time', 24)

Log.log_variable('sfcT', sfcT, ('time', 'Nx'),
                 'soil temperature', 'K')

Log.close()
Parameters:

object (_type_) – _description_

Returns:

netCDF file

add_constant(variable_name, value, longname_txt='', units_txt='')
add_dim_with_content(dimension_name, DATAin, longname_txt='', units_txt='', cart_txt='')

Function to define a dimension and add a variable at the same time. Equivalent to add_dimension() followed by log_axis1D():

lon_array = np.linspace(0, 360)

EX:

Log.add_dim_with_content("lon", lon_array, "longitudes",
                         "degree", "X")
add_dimension(dimension_name, length)
close()
copy_Ncaxis_with_content(Ncdim_var)

Copy a netCDF DIMENSION variable (e.g., Ncdim = f.variables["lon"]). If the dimension does not exist yet, it will be created

copy_Ncvar(Ncvar, swap_array=None)

Copy a netCDF variable from another file (e.g., Ncvar = f.variables["ucomp"]). All dimensions must already exist. If swap_array is provided, the original values are swapped with this array.

copy_all_dims_from_Ncfile(Ncfile_in, exclude_dim=[], time_unlimited=True)

Copy all variables, dimensions, and attributes from another netCDF file

copy_all_vars_from_Ncfile(Ncfile_in, exclude_var=[])
log_axis1D(variable_name, DATAin, dim_name, longname_txt='', units_txt='', cart_txt='')

EX:

Log.log_axis1D("areo", areo, "time", "degree", "T")
log_variable(variable_name, DATAin, dim_array, longname_txt='', units_txt='', datatype='f4')

EX:

Log.log_variable("sfcT", sfcT, ("time", "Nx"),
                 "soil temperature", "K")
merge_files_from_list(Ncfilename_list, exclude_var=[])
print_dimensions()
print_variables()