[ncl-talk] Dimension (nlon) of (var0) does not have an associated coordinate variable

Alan Brammer abrammer at albany.edu
Thu Feb 2 13:35:07 MST 2017


I’m not sure how those lines came to be.  Read the documentation and the examples on the documentation page. They are usually very helpful to understand what functions are doing and how to interact with them.

Here is a greatly reduced version of your script to focus on getind_latlon2d() function and output.  Make sure you understand what is happening before copy pasting into your original script. 

if in doubt run it interactively line by line, with extensive use of print() and printVarSummary() to understand the variable types and dimensions. 


Alan. 


;load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
;load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
;load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"

;; The above lines aren't necessary if you're using a current version of ncl. 

begin

;**************************************************
; Set file paths and plot specifics
;**************************************************

print("Setting File Paths")

;***** File Paths and Names *****

filen      = "b.e12.B1850C5CN.f09_g16.YD13ka.003.pop.SFWF.1_cat_840.nc"
file_loc   = "/glade/scratch/hughlett/csm/b.e12.B1850C5CN.f09_g16.YD13ka.003/ocn/tseries/cat/"
grid       = "b.e12.B1850C5CN.f09_g16.YD13ka.003.pop.h.0810.nc"
grid_loc   = "/glade/scratch/hughlett/csm/b.e12.B1850C5CN.f09_g16.YD13ka.003/ocn/hist/"
;**************************************************
; Read In and Process Data
;**************************************************

print("Reading in and processing data")
;***** Read in All Variables *****

fvar  = addfile(file_loc+filen,"r")
fgrid = addfile(grid_loc+grid,"r")

lat2d = fgrid->TLAT
lon2d = fgrid->TLONG

lat = 50
lon = 28


;***** Get indicies of the point to plot *****

ll_ind = getind_latlon2d( lat2d, lon2d, lat, lon)

print(ll_ind)   ; should be a 1x2 array or dimsizes(lat) x 2 

do k=0,dimsizes(lat)-1        ;; single iteration in this case.  k=0
  n = ll_ind(k,0)             ;; this is how you should retrieve the relevant index for lat
  m = ll_ind(k,1)             ;; and for lon
  print(lat2d(n,m)+"   "+lon2d(n,m))  ;;  check that n & m reference the right location. should be close to 50   28
end do


lat_index = ll_ind(0,0)
lon_index = ll_ind(0,1)

printVarSummary(var0(:,lat_index,lon_index) )


end


> On 2 Feb 2017, at 15:15, Hughlett, Taylor M <taylor.hughlett at uta.edu> wrote:
> 
> Hello again,
> 
> Thank you both for the info, and I seem to have fixed that issue, but now it appears I am back to square 1. The error reads:
> fatal:No coordinate variable exists for dimension (lat) in variable (ll_ind)
> 
> As always, full code is below the sig.
> 
> Thanks again,
> Taylor M Hughlett, Ph.D.
> UTA Earth and Environmental Sciences
> 500 Yates St.
> Geoscience Bldg Rm 117
> Arlington, TX 76013-0049
> 
> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
> 
> begin
> 
> ;**************************************************
> ; Set file paths and plot specifics
> ;**************************************************
> 
> print("Setting File Paths")
> 
> ;***** File Paths and Names *****
> 
> case       = "b.e12.B1850C5CN.f09_g16.YD13ka.003"
> filen      = "b.e12.B1850C5CN.f09_g16.YD13ka.003.pop.SFWF.1_cat_840.nc"
> file_loc   = "/glade/scratch/hughlett/csm/b.e12.B1850C5CN.f09_g16.YD13ka.003/ocn/tseries/cat/"
> grid       = "b.e12.B1850C5CN.f09_g16.YD13ka.003.pop.h.0810.nc"
> grid_loc   = "/glade/scratch/hughlett/csm/b.e12.B1850C5CN.f09_g16.YD13ka.003/ocn/hist/"
> plot_loc   = "/glade/scratch/hughlett/plots/b.e12.B1850C5CN.f09_g16.YD13ka.003/timeseries"
> 
> ;***** Variable to Plot *****
> 
> var        = "SFWF"
> 
> ;***** Text for Plot *****
> 
> CenterString = "Surface Freshwater Flux"
> YAxisString  = "SFWF (Sverdrups)"
> XAxisString  = "Years"
> 
> ;***** Start and End Time for Plot *****
> 
> plot_begin = 0001
> plot_end   = 840
> 
> ;***** Data Options (0 = no, 1 = yes) *****
> 
> smooth  = 0
> dtrd    = 0
> ncwrite = 0
> 
> ;**************************************************
> ; Read In and Process Data
> ;**************************************************
> 
> print("Reading in and processing data")
> ;***** Read in All Variables *****
> 
> fvar  = addfile(file_loc+filen,"r")
> fgrid = addfile(grid_loc+grid,"r")
> 
> var0  = fvar->SFWF(:,:,:)
> lat2d = fgrid->TLAT
> lon2d = fgrid->TLONG
> salt  = fgrid->SALT
> 
> lat = 50
> lon = 28
> 
> 
> ;***** Create a More Plottable Timeline *****
> 
> years = ispan(plot_begin,plot_end,1)
> nyrs = dimsizes(years)
> 
> ;***** Get indicies of the point to plot *****
> 
> ll_ind = getind_latlon2d( lat2d, lon2d, lat, lon)
> 
> ll_ind!0 = "lat"
> ll_ind!1 = "lon"
> 
> copy_VarCoords_n(salt,ll_ind,(/2,3/))
> 
> ;**************************************************
> ; Open workstation, set plot resources, and plot
> ;**************************************************
> 
> print("Creating Plot")
> 
> ;***** Create Plot Name *****
> 
> pname = var + "." + case + "_" + "timeseries_0m" + plot_begin + "-" + plot_end
> 
> ;***** Open workstation *****
> 
> wks = gsn_open_wks("ps",plot_loc+pname)
> 
> ;***** Set Resources *****
> 
> res                      = True
> res at gsnMaximize          = True
> res at gsnFrame             = False
> res at gsnDraw              = False
> res at vpHeightF            = .23
> res at vpWidthF             = .783
> res at tiXAxisFontHeightF   = .015
> res at tiYAxisFontHeightF   = .015
> res at tmYLLabelFontHeightF = .015
> res at tmXBLabelFontHeightF = .015
> res at gsnLeftString        = " "
> res at gsnRightString       = " "
> res at xyLineColor          = (/"black"/)
> res at gsnCenterString      = CenterString
> res at tiYAxisString        = YAxisString
> res at tiXAxisString        = XAxisString
> res at trYMinF              = 15
> res at trYMaxF              = 19
> 
> ;***** Create New Plot *****
> 
> plot      = new(1,graphic)
> plot(0)   = gsn_csm_xy (wks,years,var0(:,ll_ind&lat,ll_ind&lon),res)
> 
> print("Plot Created")
> 
> end
> 
> 
> From: Alan Brammer <abrammer at albany.edu <mailto:abrammer at albany.edu>>
> Date: Thursday, February 2, 2017 at 11:20 AM
> To: "Hughlett, Taylor M" <taylor.hughlett at uta.edu <mailto:taylor.hughlett at uta.edu>>
> Cc: "ncl-talk at ucar.edu <mailto:ncl-talk at ucar.edu>" <ncl-talk at ucar.edu <mailto:ncl-talk at ucar.edu>>
> Subject: Re: [ncl-talk] Dimension (nlon) of (var0) does not have an associated coordinate variable
> 
> Yeah, Guido is right.  
> 
> I didn’t test the code I wrote.  Do a printVarSummary(ll_ind) and read the docs and you’ll see getind_latlon2d always returns a 2-d array.  
> 
> for a single point it should be (0,0) and (0,1) for lat |  lon indices.  Examples in the docs show you how to double check that it is all working as desired.  
> 
> 
> Alan.
> 
> 
>> On 2 Feb 2017, at 12:17, Guido Cioni <guidocioni at gmail.com <mailto:guidocioni at gmail.com>> wrote:
>> 
>> I'm pretty sure NCL is referring to ll_ind, which is 2-Dimensional :)  (see https://www.ncl.ucar.edu/Document/Functions/Contributed/getind_latlon2d.shtml <https://www.ncl.ucar.edu/Document/Functions/Contributed/getind_latlon2d.shtml>).
>> 
>> Guido Cioni
>> http://guidocioni.altervista <http://guidocioni.altervista/>.org
>> 
>>> On 02 Feb 2017, at 18:14, Hughlett, Taylor M <taylor.hughlett at uta.edu <mailto:taylor.hughlett at uta.edu>> wrote:
>>> 
>>> Good afternoon,
>>> 
>>> Thank you very much for the suggestion! I have used the function that you recommended, but now I am receiving the following error:
>>> 
>>> Number of subscripts do not match number of dimensions of variable,(1) Subscripts used, (2) Subscripts expected
>>> 
>>> It occurs at the line where I actually create the plot:
>>> 
>>> plot(0)   = gsn_csm_xy (wks,years,var0(:,ll_ind(0),ll_ind(1)),res)
>>> 
>>> I have checked that var0 is three dimensional, and I have specified three subscripts required…
>>> 
>>> Have I missed something?
>>> 
>>> Taylor M Hughlett, Ph.D.
>>> UTA Earth and Environmental Sciences
>>> 500 Yates St.
>>> Geoscience Bldg Rm 117
>>> Arlington, TX 76013-0049
>>> 
>>> The Full Code:
>>> 
>>> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
>>> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
>>> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
>>> 
>>> begin
>>> 
>>> ;**************************************************
>>> ; Set file paths and plot specifics
>>> ;**************************************************
>>> 
>>> print("Setting File Paths")
>>> 
>>> ;***** File Paths and Names *****
>>> 
>>> case       = "b.e12.B1850C5CN.f09_g16.YD13ka.003"
>>> filen      = "b.e12.B1850C5CN.f09_g16.YD13ka.003.pop.SFWF.1_cat_840.nc"
>>> file_loc   = "/glade/scratch/hughlett/csm/b.e12.B1850C5CN.f09_g16.YD13ka.003/ocn/tseries/cat/"
>>> grid       = "b.e12.B1850C5CN.f09_g16.YD13ka.003.pop.h.0810.nc"
>>> grid_loc   = "/glade/scratch/hughlett/csm/b.e12.B1850C5CN.f09_g16.YD13ka.003/ocn/hist/"
>>> plot_loc   = "/glade/scratch/hughlett/plots/b.e12.B1850C5CN.f09_g16.YD13ka.003/timeseries"
>>> 
>>> ;***** Variable to Plot *****
>>> 
>>> var        = "SFWF"
>>> 
>>> ;***** Text for Plot *****
>>> 
>>> CenterString = "Surface Freshwater Flux"
>>> YAxisString  = "SFWF (Sverdrups)"
>>> XAxisString  = "Years"
>>> 
>>> ;***** Start and End Time for Plot *****
>>> 
>>> plot_begin = 0001
>>> plot_end   = 840
>>> 
>>> ;***** Data Options (0 = no, 1 = yes) *****
>>> 
>>> smooth  = 0
>>> dtrd    = 0
>>> ncwrite = 0
>>> 
>>> ;**************************************************
>>> ; Read In and Process Data
>>> ;**************************************************
>>> 
>>> print("Reading in and processing data")
>>> ;***** Read in All Variables *****
>>> 
>>> fvar  = addfile(file_loc+filen,"r")
>>> fgrid = addfile(grid_loc+grid,"r")
>>> 
>>> var0  = fvar->SFWF(:,:,:)
>>> lat2d = fgrid->TLAT
>>> lon2d = fgrid->TLONG
>>> 
>>> lat = 50
>>> lon = 28
>>> 
>>> ;***** Create a More Plottable Timeline *****
>>> 
>>> years = ispan(plot_begin,plot_end,1)
>>> nyrs = dimsizes(years)
>>> 
>>> ;***** Get indicies of the point to plot *****
>>> 
>>> ll_ind = getind_latlon2d( lat2d, lon2d, lat, lon)
>>> 
>>> ;**************************************************
>>> ; Open workstation, set plot resources, and plot
>>> ;**************************************************
>>> 
>>> print("Creating Plot")
>>> 
>>> ;***** Create Plot Name *****
>>> 
>>> pname = var + "." + case + "_" + "timeseries_0m" + plot_begin + "-" + plot_end
>>> 
>>> ;***** Open workstation *****
>>> 
>>> wks = gsn_open_wks("ps",plot_loc+pname)
>>> 
>>> ;***** Set Resources *****
>>> 
>>> res                      = True
>>> res at gsnMaximize          = True
>>> res at gsnFrame             = False
>>> res at gsnDraw              = False
>>> res at vpHeightF            = .23
>>> res at vpWidthF             = .783
>>> res at tiXAxisFontHeightF   = .015
>>> res at tiYAxisFontHeightF   = .015
>>> res at tmYLLabelFontHeightF = .015
>>> res at tmXBLabelFontHeightF = .015
>>> res at gsnLeftString        = " "
>>> res at gsnRightString       = " "
>>> res at xyLineColor          = (/"black"/)
>>> res at gsnCenterString      = CenterString
>>> res at tiYAxisString        = YAxisString
>>> res at tiXAxisString        = XAxisString
>>> res at trYMinF              = 15
>>> res at trYMaxF              = 19
>>> 
>>> ;***** Create New Plot *****
>>> 
>>> plot      = new(1,graphic)
>>> plot(0)   = gsn_csm_xy (wks,years,var0(:,ll_ind(0),ll_ind(1)),res)
>>> 
>>> print("Plot Created")
>>> 
>>> end
>>> 
>>> 
>>> 
>>> From: Alan Brammer <abrammer at albany.edu <mailto:abrammer at albany.edu>>
>>> Date: Wednesday, February 1, 2017 at 2:15 PM
>>> To: "Hughlett, Taylor M" <taylor.hughlett at uta.edu <mailto:taylor.hughlett at uta.edu>>
>>> Cc: "ncl-talk at ucar.edu <mailto:ncl-talk at ucar.edu>" <ncl-talk at ucar.edu <mailto:ncl-talk at ucar.edu>>
>>> Subject: Re: [ncl-talk] Dimension (nlon) of (var0) does not have an associated coordinate variable
>>> 
>>>  If you were plotting a map, like Barry Lynn's similar question today also on ncl-talk, you would want to do:
>>> 
>>> var0 at lon2d = lon2d
>>> var0 at lat2d = lat2d
>>> 
>>> The below lines are not needed. 
>>>     ; LON2D  = lon2d(::1,::1)        ;; I don’t think (::1,::1) does anything,  it will take all values with an increment of 1, i.e. the whole array. 
>>>     ; LAT2D  = lat2d(::1,::1)   ;   so this is just redundant characters which aren’t needed. 
>>> You don’t need to replicate a variable that already exists. 
>>> 
>>> To set coordinate variables named nlon and nlat, you want to use the & syntax not the @ syntax, but coordinate subscripting won’t work with 2d coordinates anyway. 
>>> 
>>> So instead get the index of the point you want using:
>>> https://www.ncl.ucar.edu/Document/Functions/Contributed/getind_latlon2d.shtml <https://www.ncl.ucar.edu/Document/Functions/Contributed/getind_latlon2d.shtml>
>>> e.g.
>>> 
>>> ll_ind = getind_latlon2d( lat2d, lon2d, 28,50)
>>>>>>>>> plot(0)   = gsn_csm_xy (wks, years, var0(:, ll_ind(0), ll_ind(1) ), res)
>>> 
>>> 
>>> 
>>> Good luck, 
>>> 
>>> Alan
>>> 
>>> 
>>> ##############################
>>> Alan Brammer,
>>> Post-Doc Researcher
>>> 
>>> Department of Atmospheric and Environmental Sciences,
>>> University at Albany, State University of New York, Albany, NY, 12222
>>> abrammer at albany.edu <mailto:abrammer at albany.edu>
>>> ##############################
>>> _______________________________________________
>>> ncl-talk mailing list
>>> ncl-talk at ucar.edu <mailto:ncl-talk at ucar.edu>
>>> List instructions, subscriber options, unsubscribe:
>>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk <http://mailman.ucar.edu/mailman/listinfo/ncl-talk>
>> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20170202/4a09e00e/attachment-0001.html 


More information about the ncl-talk mailing list