Setup File Structure


In [ ]:
import netCDF4     
import numpy as np

try:
    ncfile.close()
except:
    pass
ncfile = netCDF4.Dataset("new-dvmdostem-output.nc", mode="w", format='NETCDF4')

# Dimensions for the file.
time_dim = ncfile.createDimension('time', None) # unlimited axis (can be appended to).
community_type = ncfile.createDimension('community_type', 10)
pft = ncfile.createDimension('pft', 10)
y = ncfile.createDimension('x', 10)
x = ncfile.createDimension('y', 10)

# Coordinate Variables
x = ncfile.createVariable('x', np.int, ('x',))  # x,y are pixel coords in 2D (spatial?) image
y = ncfile.createVariable('y', np.int, ('y',))
community_type = ncfile.createVariable('community_type', np.int, ('y','x'))

# Spatial Reference Variables?
lat = ncfile.createVariable('lat', np.float32, ('y', 'x',))
lon = ncfile.createVariable('lon', np.float32, ('y', 'x',))

# Add space/time variables...
grow_start = ncfile.createVariable('grow_start', np.int, ('time', 'y', 'x',)) # day of year
grow_end = ncfile.createVariable('grow_end', np.int, ('time', 'y', 'x'))

org_shlw_thickness = ncfile.createVariable('org_shlw_thickness', np.float32, ('time', 'y', 'x'))
# Need to add all these:
# 1                         //OSHLWDZ     - (23) shallow fibrous organic soil horizon thickness (m)
# 1                         //ODEEPDZ     - (24) deep amorphous organic soil horizon thickness (m)
# 1                         //MINEADZ     - (25) upper minearal soil horizon thickness (m)
# 1                         //MINEBDZ     - (26) middel mineral soil horizon thickness (m)
# 1                         //MINECDZ     - (27) lower mineral soil horizon thickness (m)
# 1                         //OSHLWC      - (28) SOM C in firbrous soil horizon  (gC/m2)
# 1                         //ODEEPC      - (29) SOM C in amorphous soil horizon  (gC/m2)
# 1                         //MINEAC      - (30) SOM C in upper mineral soil horizon  (gC/m2)
# 1                         //MINEBC      - (31) SOM C in middle mineral soil horizon  (gC/m2)
# 1                         //MINECC      - (32) SOM C in lower mineral soil horizon  (gC/m2)
# 1                         //ORGN        - (33) total soil organic N (gN/m2)
# 2                         //AVLN        - (35) total soil mineral N  (gN/m2)

# Add more complicated time/cmt type/PFT/Y/X variables...
veg_fraction = ncfile.createVariable('veg_fraction', np.float32, ('time','community_type','pft','y','x'))
vegc = ncfile.createVariable('vegc', np.float64, ('time','community_type','pft','y','x'))
# Need to add all these:
#     1                         //VEGFRAC     - (3) each pft's land coverage fraction (m2/m2)
#     1                         //VEGAGE      - (4) each pft's age (years)
#     2                         //LAI         - (5) each pft's LAI (m2/m2)
#     2                         //VEGC        - (6) each pft's total veg. biomass C (gC/m2)
#     2                         //LEAFC       - (7) each pft's leaf biomass C (gC/m2)
#     2                         //STEMC       - (8) each pft's stem biomass C (gC/m2)
#     2                         //ROOTC       - (9) each pft's root biomass C (gC/m2)
#     2                         //VEGN        - (10) each pft's total veg. biomass N  (gC/m2)
#     2                         //LABN        - (11) each pft's labile N (gN/m2)
#     2                         //LEAFN       - (12) each pft's leaf structural N (gN/m2)
#     2                         //STEMN       - (13) each pft's stem structural N (gN/m2)


# Add some random data to the vegC variable so we can check it
# out with ncview and see if the dimensions "make sense"
vegc[:,:,:,:,:] = np.reshape(np.random.uniform(0, 1, 40000), (4,10,10,10,10))
# vegc[time, cmt, pft, y, x]
print "NetCDF File Dimensions:"
for dim in ncfile.dimensions.items():
    print "   -->", dim

ncfile.close()

print ""

!ncdump -h new-dvmdostem-output.nc

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]: