title: netCDF grid file: Extracting information from 1D array using 2D values
body: <p>I am trying to work in Python 3 with topography/bathymetry-information (basically a grid containing x [longitude in decimal degrees], y [latitude in decimal degrees] and z [meter]). </p>
  
  <p>The grid file has the extension .nc and is therefore a netCDF-file. Normally I would use it in mapping tools like Generic Mapping Tools and don't have to bother with how a netCDF file works, but I need to extract specific information in a Python script. Right now this is only limiting the dataset to certain longitude/latitude ranges. </p>
  
  <p>However, right now I am a bit lost on how to get to the z-information for specific x and y values. Here's what I know about the data so far</p>
  
  <pre><code>import netCDF4
  
  #----------------------
  # Load netCDF file
  #----------------------
  bathymetry_file = 'C:/Users/te279/Matlab/data/gebco_08.nc'
  fh = netCDF4.Dataset(bathymetry_file, mode='r')
  
  #----------------------
  # Getting information about the file
  #----------------------
  print(fh.file_format)
  </code></pre>
  
  <blockquote>
    <p>NETCDF3_CLASSIC</p>
  </blockquote>
  
  <pre><code>print(fh)
  </code></pre>
  
  <blockquote>
    <p>
    root group (NETCDF3_CLASSIC data model, file format NETCDF3):
        title: GEBCO_08 Grid
        source: 20100927
        dimensions(sizes): side(2), xysize(933120000)
        variables(dimensions): float64 x_range(side), float64 y_range(side), int16 z_range(side), float64 spacing(side), int32 dimension(side), int16 z(xysize)
        groups: </p>
  </blockquote>
  
  <pre><code>print(fh.dimensions.keys()) 
  </code></pre>
  
  <blockquote>
    <p>odict_keys(['side', 'xysize'])</p>
  </blockquote>
  
  <pre><code>print(fh.dimensions['side'])
  </code></pre>
  
  <blockquote>
    <p>: name = 'side', size = 2</p>
  </blockquote>
  
  <pre><code>print(fh.dimensions['xysize'])
  </code></pre>
  
  <blockquote>
    <p>: name = 'xysize', size = 933120000</p>
  </blockquote>
  
  <pre><code>#----------------------
  # Variables
  #----------------------
  print(fh.variables.keys()) # returns all available variable keys
  </code></pre>
  
  <blockquote>
    <p>odict_keys(['x_range', 'y_range', 'z_range', 'spacing', 'dimension', 'z'])</p>
  </blockquote>
  
  <pre><code>xrange = fh.variables['x_range'][:] 
  print(xrange)
  </code></pre>
  
  <blockquote>
    <p>[-180.  180.] # contains the values -180 to 180 for the longitude of the whole world</p>
  </blockquote>
  
  <pre><code>yrange = fh.variables['y_range'][:]
  print(yrange) 
  </code></pre>
  
  <blockquote>
    <p>[-90.  90.] # contains the values -90 to 90 for the latitude of the whole world</p>
  </blockquote>
  
  <pre><code>zrange = fh.variables['z_range'][:]
  </code></pre>
  
  <blockquote>
    <p>[-10977   8685] # contains the depths/topography range for the world</p>
  </blockquote>
  
  <pre><code>spacing = fh.variables['spacing'][:] 
  </code></pre>
  
  <blockquote>
    <p>[ 0.00833333  0.00833333] # spacing in both x and y. Equals the dimension, if multiplied with x and y range</p>
  </blockquote>
  
  <pre><code>dimension = fh.variables['dimension'][:] 
  </code></pre>
  
  <blockquote>
    <p>[43200 21600] # corresponding to the shape of z if it was the 2D array I would've hoped for (it's currently an 1D array of 9333120000 - which is 43200*21600) </p>
  </blockquote>
  
  <pre><code>z = fh.variables['z'][:] # currently an 1D array of the depth/topography/z information I want
  fh.close
  </code></pre>
  
  <p>Based on this information I still don't know how to access z for specific x/y (longitude/latitude) values. I think basically I need to convert the 1D array of z into a 2D array corresponding to longitude/latitude values. I just have not a clue how to do that. I saw in some posts where people tried to convert a 1D into a 2D array, but I have no means to know in what corner of the world they start and how they progress.</p>
  
  <p>I know there is a 3 year old <a href="https://stackoverflow.com/questions/25750090/convert-netcdf-land-variable-to-latitude-and-longitude-with-python">similar post</a>, however, I don't know how to find an analogue "index of the flattened array" for my problem - or how to exactly work with that. Can somebody help?</p>