<jupyter_start><jupyter_text>Using NetCDF File To Create 3D TexturesDervived from tutorial at https://unidata.github.io/netcdf4-python/netCDF4/ Example NetCDF dataset is a synthetic tornado 'tornado3d.nc' taken from https://cgl.ethz.ch/research/visualization/data.phpNOTE: might consider Xarray   Read the NetCDF Dataset as NC object- Import requrired modules/packages- Scan for NC files in /data folder- Open a sample NC file & explore groups, dimensions, variables-<jupyter_code># Import required modules

from os import listdir
import datetime as dt  # Python standard library datetime  module
import numpy as np
from netCDF4 import Dataset  

# define config constant (to be parms in code)
data_path = "../data/"
netCDF_extension = "nc"
# Define function to list all files in directory with a specific extension 

def list_files(directory, extension):
    return (f for f in listdir(directory) if f.endswith('.' + extension))

# List each nc file with its creation data and variables
for f in list_files(data_path, netCDF_extension):
    print(f)
    nc = Dataset(data_path + f, "r")
    nc_dict = nc.__dict__
    cdate = nc_dict['creation_date']
    vars = 
    print(f"{f} created {cdate} with variables {vars}")

nc.keys()
# Open NC dataset as binary

nc = Dataset("../data/tornado3d.nc", "r")
nc
nc.__dict__
# List groups within dataset. Seems only one unnamed group 
nc.groups
# Get the filepath to the NC file
nc.filepath()
# Get the attributes from NC file; different than variables & dimensions
nc.ncattrs()
# uninteresting...
nc.grid
# interesting... dimensions for each of the nc variables
nc.dimensions
str(nc.dimensions['xdim'])
nc.dimensions['xdim']
nc.variables.keys()
# deinition for a nc variable
nc.variables['u']
nc.variables['xdim'][:]
# can view nc variable definitions as python dict types
print(nc.variables['u'].__dict__)
nc.variables['u'].__dict__
# found those wind velocity components u, v, w
u_values = nc.variables['u']
uvw_shape = u_values[:].shape
uvw_shape
# can work with nc variables like numpy
u_values[:3, :3, :3]
# can also explicitly convert nc variables into numpy; may not be needed
u = np.array(u_values)
print(type(u), u.shape)<jupyter_output><class 'numpy.ndarray'> (128, 128, 128)<jupyter_text>Find min-max of u-v-w velocities<jupyter_code>u = np.array(nc.variables['u'])
v = np.array(nc.variables['v'])
w = np.array(nc.variables['w'])
print(u.shape, v.shape, w.shape)
uvw_min = min((u.min(), v.min(), w.min()))
uvw_min
uvw_max = max((u.max(), v.max(), w.max()))
uvw_max<jupyter_output><empty_output><jupyter_text>Normalize and scale 255 for colorusing x_norm = 256*(X-uvw_min)/(uvw_max-uvw_min)<jupyter_code>u_norm = (255 * (u/(uvw_max-uvw_min) - (uvw_min/(uvw_max-uvw_min)))).astype(np.int)
v_norm = (255 * (v/(uvw_max-uvw_min) - (uvw_min/(uvw_max-uvw_min)))).astype(np.int)
w_norm = (255 * (w/(uvw_max-uvw_min) - (uvw_min/(uvw_max-uvw_min)))).astype(np.int)

print(u_norm[:2,:2,:2])
print(v_norm[:2,:2,:2])
print(w_norm[:2,:2,:2])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1378737541528239, 'fraction_numerical': 0.013953488372093023, 'mean_word_length': 5.0951417004048585, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '473141', 'license': 'MIT', 'n_tokens_mistral': 1122, 'n_tokens_neox': 1084, 'n_words': 300, 'path': 'notebooks/Try NetCDF-py.ipynb', 'repo_name': 'Hackathorn/NetCDP-3Dtexture', 'size': '3010'}