[Python] Which keyword to use for extracting data from netCDF file

Discussão em 'Python' iniciado por Stack, Outubro 2, 2024.

  1. Stack

    Stack Membro Participativo

    The header of a netCDF file reads:

    netcdf gpw_v4_population_density_rev11_2pt5_min {
    dimensions:
    longitude = 8640 ;
    latitude = 4320 ;
    raster = UNLIMITED ; // (20 currently)
    variables:
    double longitude(longitude) ;
    longitude:units = "degrees_east" ;
    longitude:long_name = "longitude" ;
    double latitude(latitude) ;
    latitude:units = "degrees_north" ;
    latitude:long_name = "latitude" ;
    int raster(raster) ;
    raster:units = "unknown" ;
    raster:long_name = "raster" ;
    float Population\ Density\,\ v4.11\ \(2000\,\ 2005\,\ 2010\,\ 2015\,\ 2020\)\:\ 2.5\ arc-minutes(raster, latitude, longitude) ;
    Population\ Density\,\ v4.11\ \(2000\,\ 2005\,\ 2010\,\ 2015\,\ 2020\)\:\ 2.5\ arc-minutes:units = "Persons per square kilometer" ;
    Population\ Density\,\ v4.11\ \(2000\,\ 2005\,\ 2010\,\ 2015\,\ 2020\)\:\ 2.5\ arc-minutes:_FillValue = -3.4e+38f ;
    Population\ Density\,\ v4.11\ \(2000\,\ 2005\,\ 2010\,\ 2015\,\ 2020\)\:\ 2.5\ arc-minutes:missing_value = -3.4e+38f ;
    Population\ Density\,\ v4.11\ \(2000\,\ 2005\,\ 2010\,\ 2015\,\ 2020\)\:\ 2.5\ arc-minutes:long_name = "Population Density, v4.11 (2000, 2005, 2010, 2015, 2020): 2.5 arc-minutes" ;
    Population\ Density\,\ v4.11\ \(2000\,\ 2005\,\ 2010\,\ 2015\,\ 2020\)\:\ 2.5\ arc-minutes:min = 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 4., 0., 0., 0., -9999., -9999., 0., 0., 0., 0. ;
    Population\ Density\,\ v4.11\ \(2000\,\ 2005\,\ 2010\,\ 2015\,\ 2020\)\:\ 2.5\ arc-minutes:max = 52542.421875, 53915.54296875, 59666.02734375, 78523.484375, 103340.84375, 207., 767642., 3., 21.5139427185059, 21.5139389038086, 32767., 6., 2015., 6., 6., 6., 2011., 2015., 4., 2016. ;

    // global attributes:
    :proj4 = "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0" ;
    :Conventions = "CF-1.4" ;
    :created_by = "R, packages ncdf4 and raster (version 2.8-4)" ;
    :date = "2018-11-16 10:27:52" ;


    I try reading it as usual with:

    import netCDF4 as nc
    import numpy as np
    import numpy.ma as ma

    sample_file = "/home/ubuntu/POP/gpw_v4_population_density_rev11_2pt5_min.nc"
    sample_dataset = nc.Dataset(sample_file)
    T_latitudes = sample_dataset.variables["latitude"][:]
    T_longitudes = sample_dataset.variables["longitude"][:]
    popu1 = sample_dataset.variables["Population"][:]


    This works for latitudes and longitudes (as expected) but I'm unsure which is the right syntax to extract the different populations. I have tried several syntaxes without success with the above one generating the error:

    KeyError: 'Population'

    Continue reading...

Compartilhe esta Página