问题
I have a global meteorological dataset and I want to access the data for a certain grid (lat,lon). However, the data is compressed, i.e. the parameters of interest do not have the dimensions (lat, lon), but "land". "land" is a 1D array of integers.
I imported the file in python using
import scipy.io.netcdf as netcdf
path = '/path/.../ncfile.nc'
ncfile = netcdf.netcdf_file(path,'r')
Then I checked what variables there were and found that, e.g. the "Rainf" variable has the dimensions (tstep, land). I researched this on the internet and found the file landmask_gswp.nc (http://dods.ipsl.jussieu.fr/gswp/Fixed/landmask_gswp.nc), which is supposed to contain the information I need, that is, how to extract the information (lat, lon) from "land". This file contains the variables nav_lat, nav_lon and landmask. nav_lat and nav_lon relate, to my understanding, the coordinate variables x and y to latitude and longitude. "landmask" is a 2D array and contains the information ocean = 0 or land = 1. Indeed, the number of landpoints agrees with the length of my "land" 1D array. However, I cannot figure out how to extract the (lat, lon) information from it. Any help would be much appreciated.
I hope I made my problem somewhat understandable; I am not experienced with programming and/or using netcdf, so I hope that you can help out! Thanks in advance!
回答1:
Some others might find this helpful, so here's the answer to the problem. A friend figured it out for me. "land" is the index of the flattened array nav_lat, nav_lon, i.e. the first entry of "land" corresponds to the latitude: lat.flat[land[0]] and lon.flat[land[0]].
回答2:
You can extract variables from ncfile
using
lat = ncfile.variables['nav_lat'][:,:]
lon = ncfile.variables['nav_lon'][:,:]
This will create 2D numpy arrays lat
and lon
.
来源:https://stackoverflow.com/questions/25750090/convert-netcdf-land-variable-to-latitude-and-longitude-with-python