|
|
# how to create and write
|
|
|
```
|
|
|
file_name = 'brazilian_biomes.nc'
|
|
|
|
|
|
file_netcdf = Dataset(file_name,'w', format='NETCDF3_CLASSIC') #'w' stands for write
|
|
|
|
|
|
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()
|
|
|
```
|
|
|
|
|
|
# how to read
|
|
|
```
|
|
|
nc = Dataset("/landuse/users/brunel/chalumeau_tillage/run_set_01/nf_001/vegc.nc", mode='r')
|
|
|
years = nc.variables['time'][:]
|
|
|
latitudes = nc.variables['latitude'][:]
|
|
|
longitudes = nc.variables['longitude'][:]
|
|
|
variables = list(nc.variables.keys())
|
|
|
variables = nc.variables.keys()
|
|
|
for variable in variables :
|
|
|
print(nc.variables[variable][:])
|
|
|
nc.close()
|
|
|
``` |
|
|
\ No newline at end of file |