from scipy.io import * # Create a new netCDF File # Must have 'w' for creating new file and/or writing to it cdfFile = netcdf_file("example.cdf", "w") # cdfFile is a class # Create the dimension for netcdf variables cdfFile.createDimension("dim100", 100) # Create the 2 variables for x and f(x) # The "f" is for float # "i" would be for integer cdfFile.createVariable("x", "f", ["dim100"]) # list of dimensions cdfFile.createVariable("x_squared", "f", ["dim100"]) for i in range(100): cdfFile.variables["x"][i] = float(i) cdfFile.variables["x_squared"][i] = float(i**2) # for i in range(100): # print str(cdfFile.variables["x"][i]) + " ^2 = " + str(cdfFile.variables["x_Squared"][i]) # Now create a 2D variable 200 x 100 # Because it is 2D, the dimension list must have 2 dimensions cdfFile.createDimension("dim200", 200) q = cdfFile.createVariable("Q", "f", ["dim200","dim100"]) # list of dimensions for i in range(200): for j in range(100): q[i][j] = float(i + j) # Create an index variable for graphing convenience cdfFile.createVariable("Tindex", "f", ["dim200"]) for i in range(200): cdfFile.variables["Tindex"][i] = float(i) # Create a scalar variable for demonstration sc = cdfFile.createVariable("s", "i", ()) sc.assignValue(42) # Create a global attribute setattr(cdfFile, "history", "last week") # help(type(cdfFile)) # shows __setattr__(self, attr, value) # means can call setattr() directly # dir(cdfFile) shows all methods and variables cdfFile.diagnostic = "port A" # how does this work to create any attrib? # Create a variable attribute setattr(sc, "hello", "there") cdfFile.variables["Q"].units = "meters" # units will be shown in ElVis cdfFile.variables["Q"].tokamak = "NSTX" # Save all data and close file cdfFile.close() """ cdfFile.createDimension("dimTime", None) # create unlimited dimension cdfFile.variables["Q"][i][j] = float(i + j) # can access variables through dictionary can use numpy arrays to hold data and assign to netcdf variables cdfFile.flush() cdfFile.synch() """