Skip to content

GitLab

  • Menu
Projects Groups Snippets
    • Loading...
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • Python_ToolBox Python_ToolBox
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Issues 0
    • Issues 0
    • List
    • Boards
    • Service Desk
    • Milestones
  • Monitor
    • Monitor
    • Metrics
    • Incidents
  • Packages & Registries
    • Packages & Registries
    • Package Registry
    • Container Registry
    • Infrastructure Registry
  • Analytics
    • Analytics
    • Value stream
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Create a new issue
  • Issue Boards
Collapse sidebar
  • Marie Brunel
  • Python_ToolBoxPython_ToolBox
  • Wiki
  • NetCDF format and file

Last edited by Marie Brunel Jan 11, 2022
Page history

NetCDF format and file

how to create and write

from datetime import date
today = date.today()

file_name = 'brazilian_biomes.nc'

file_netcdf = Dataset(file_name,'w', format='NETCDF3_CLASSIC') #'w' stands for write

#define latitudes and longitudes 
latitudes = np.arange(-32.25,5.75,0.5)
longitudes = np.arange(-73.75,-34.75,0.5)

file_netcdf.createDimension('lat', len(latitudes)) #create dimensions
file_netcdf.createDimension('lon', len(longitudes))

latitude = file_netcdf.createVariable('latitude', 'f4', 'lat')    #create variables - 1 arg:name; 2 arg:format (i4 or f4); 3 arg:format 
longitude = file_netcdf.createVariable('longitude', 'f4', 'lon')
biome = file_netcdf.createVariable('biome', 'i4', ('lat','lon'))

latitude[:] = latitudes
print(latitude)
longitude[:] = longitudes
biome[:,:] = 0

#Add global attributes
file_netcdf.description = "Brazilian biomes according to Mapbiomas\n"+str(biome_list)
file_netcdf.history = "Created " + today.strftime("%d/%m/%y")

#Add local attributes to variable instances
longitude.units = 'degrees east'
latitude.units = 'degrees north'
biome.units = 'biome id'
biome.warning = '1:amazon, 2:caatinga, 3:cerrado, 4:mata atlantica, 5:pampas, 6:pantanal'

file_netcdf.close()

string and char

with 'NETCDF4'

file_netcdf = Dataset(file_name,'w', format='NETCDF4'

landuse_name = np.array(landuses, dtype='object')
biome_name = np.array(biomes, dtype='object')

file_netcdf.createDimension('landuse', len(landuse_name))
file_netcdf.createDimension('biome', len(biome_name))

landuse = file_netcdf.createVariable('landuse', str, 'landuse')
biome = file_netcdf.createVariable('biome', str, 'biome')

landuse[:] = landuse_name
biome[:] = biome_name

with 'NETCDF3' or 'NETCDF4_CLASSIC'

landuse_name = stringtochar(np.array(landuses, 'S33'))
biome_name = stringtochar(np.array(biomes, 'S13'))
#need to be check

how to read

nc = Dataset("/landuse/users/brunel/chalumeau_tillage/run_set_01/nf_001/vegc.nc", mode='r')
variables = nc.variables.keys()
for variable in variables :
    print(nc.variables[variable][:])
nc.close()

how to read char

chartostring()

Clone repository
  • Binary file
  • Brazil
  • C balance abnormally high in LPJmL 5 running @LAM
  • Cluster
  • Color and ColorMap
  • Data treatment
  • Link shelf
  • Morocco
  • NetCDF format and file
  • Numpy array
  • Plot graph and map
  • Python script
  • Run CNRM and HADgem LPJmL5 (30 minutes)
  • Home