[ncl-talk] lonFlip function

HAVENER, KEVIN F GS-12 USAF AFWA 14 WS/WXED kevin.havener at us.af.mil
Fri Feb 20 08:55:09 MST 2015


Incomplete copy paste from my last email.  Here it is with the last few lines intact.  Sorry for the misdirection.

begin
;***********************************************
; get variable names from grib file
;***********************************************
   grib_in  = addfile("./U78559.grb","r")   
   names    = getfilevarnames(grib_in); extract all variable names 
;
;  get the longitudinal dimension name(s)
;
   dimnames = getvardims(grib_in)
   print(dimnames)
   londims = str_match_ic(dimnames,"lon")
;***********************************************
; create output netcdf file
;*********************************************** 
   system("rm out.nc") ; remove any pre-existing file
   ncdf_out = addfile("out.nc" ,"c")       ; create output netCDF file
;***********************************************
; loop through variables and output each to netcdf
;***********************************************
   do i = 0, dimsizes(names)-1  
     print (names(i))
     ;
     ; strings need to be converted to character if you want them in the output file
     ; 
     if (typeof(grib_in->$names(i)$) .eq. "string") then
       continue
     end if
     ;
     ; if the rightmost dimension is longitude then flip the data
     ; otherwise just copy the variable unaltered
     ;
     dims := getfilevardims(grib_in,names(i))
     rdim = dims(dimsizes(dims)-1)
     if (any(londims .eq. rdim)) then
        ncdf_out->$names(i)$ = lonFlip(grib_in->$names(i)$)
     else   
        ncdf_out->$names(i)$ = grib_in->$names(i)$
     end if
   end do   
end

Kevin Havener, DAFC, 14WS/WXED
Comm 828-271-4323; DSN 673-9044
NIPRNet kevin.havener at us.af.mil
SIPRNet kevin.f.havener.civ at mail.smil.mil


-----Original Message-----
From: ncl-talk-bounces at ucar.edu [mailto:ncl-talk-bounces at ucar.edu] On Behalf Of HAVENER, KEVIN F GS-12 USAF AFWA 14 WS/WXED
Sent: Friday, February 20, 2015 10:51 AM
To: Sunmin Park
Cc: ncl-talk at ucar.edu
Subject: Re: [ncl-talk] lonFlip function

Here is a script that I got from the ncl website somewhere, or maybe from the mailing list, because re-ordering 0-360 arrays is something I have to do frequently. It re-orders the array and re-calculates the longitudes.  I deal with grib1/2 files, so it reads a grib and writes out a netcdf, but it doesn't really matter what the input file type is as long as ncl likes it.

begin
;***********************************************
; get variable names from grib file
;***********************************************
   grib_in  = addfile("./U78559.grb","r")   
   names    = getfilevarnames(grib_in); extract all variable names 
;
;  get the longitudinal dimension name(s) ;
   dimnames = getvardims(grib_in)
   print(dimnames)
   londims = str_match_ic(dimnames,"lon")
;***********************************************
; create output netcdf file
;*********************************************** 
   system("rm out.nc") ; remove any pre-existing file
   ncdf_out = addfile("out.nc" ,"c")       ; create output netCDF file
;***********************************************
; loop through variables and output each to netcdf
;***********************************************
   do i = 0, dimsizes(names)-1  
     print (names(i))
     ;
     ; strings need to be converted to character if you want them in the output file
     ; 
     if (typeof(grib_in->$names(i)$) .eq. "string") then
       continue
     end if
     ;
     ; if the rightmost dimension is longitude then flip the data
     ; otherwise just copy the variable unaltered
     ;
     dims := getfilevardims(grib_in,names(i))
     rdim = dims(dimsizes(dims)-1)
     if (any(londims .eq. rdim)) then
        ncdf_out->$names(i)$ = lonFlip(grib_in->$names(i)$)
     else   
        ncdf_out->$names(i)$ = grib_in->$names(i)$

Kevin Havener

-----Original Message-----
From: ncl-talk-bounces at ucar.edu [mailto:ncl-talk-bounces at ucar.edu] On Behalf Of Sunmin Park
Sent: Friday, February 20, 2015 10:32 AM
To: Adam Phillips
Cc: ncl-talk at ucar.edu
Subject: Re: [ncl-talk] lonFlip function

Thank you Adam

I am trying to do regredding the data using the function linint2_Wrap. 
I got warning
"warning:linint2: xi, yi, xo, and yo must be monotonically increasing"
therefore I think it is due to LON values. 
How I can remove the warning? 


  diri    = "/Users/spark/vertical/00_DATA/"
  fili    = "precipmonmean.nc"
  f       = addfile (diri+fili+".nc", "r")
  varname = "precip"
  data = f->$varname$

  LON     = f->lon ({0:360})  ;
  LAT     = f->lat ;({-90:90})  ;
;  LON     = lonFlip(LON1)

  printVarSummary(data)
  data1 = lonFlip(data)
  printVarSummary(data1)

  lat = fspan(-85,85,85)
  lon = fspan(-180,180,72)
  lat at units = "degrees_east"
  lon at units = "degrees_north"
  precipreg = linint2_Wrap(LON,LAT,data1,True,lon,lat,0)
printVarSummary(precipreg)

On Feb 20, 2015, at 7:17 AM, Adam Phillips <asphilli at ucar.edu> wrote:


	Hi Sunmin,
	lonFlip flips the attached longitude coordinate variable, and not the array itself. Try:
	printVarSummary(data)
	data = lonFlip(data) 
	printVarSummary(data)
	If you have any further questions please respond to ncl-talk.
	Adam

	On Fri, Feb 20, 2015 at 8:04 AM, Sunmin Park <mireiyue at gmail.com> wrote:
	

		Dear NCL users
		
		I am trying to change longitude 0,360 to -180,180. I used the function lonFlip to change longitude but it has wrong values. When I use printVarSummary the coordinate looks fine but when I print it out (print) it doesn't change LON (the file that I am using is GPCP monthly mean precipitation from NCAR) How can I get correct LON (-180,180)?
		
		Thank you in advance,
		Sun-
		
		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
		
		;==============================================================
		; Open the file:
		;================================================================
		
		  diri    = "/Users/spark/vertical/00_DATA/"
		  fili    = "precipmonmean.nc <http://precipmonmean.nc/> "
		  f       = addfile (diri+fili+".nc", "r")
		  varname = "precip"
		  data = f->$varname$
		
		  LON1     = f->lon ;({-180:180})  ;
		  LAT     = f->lat ;({-90:90})  ;
		  LON     = lonFlip(LON1)
		
		  printVarSummary(data)
		  printVarSummary(LON)
		  print(LON)
		return
		
		
		Variable: LON
		Type: float
		Total Size: 576 bytes
		            144 values
		Number of Dimensions: 1
		Dimensions and sizes:   [lon | 144]
		Coordinates:
		            lon: [-178.75..178.75]
		Number Of Attributes: 6
		  units :       degrees_east
		  long_name :   Longitude
		  actual_range :        ( 1.25, 358.75 )
		  standard_name :       longitude
		  axis :        X
		  lonFlip :     longitude coordinate variable has been reordered via lonFlip
		
		(0)     181.25
		(1)     183.75
		(2)     186.25
		(3)     188.75
		(4)     191.25
		(5)     193.75
		(6)     196.25
		(7)     198.75
		(8)     201.25
		(9)     203.75
		(10)    206.25
		.
		.
		.
		(135)   158.75
		(136)   161.25
		(137)   163.75
		(138)   166.25
		(139)   168.75
		(140)   171.25
		(141)   173.75
		(142)   176.25
		(143)   178.75
		_______________________________________________
		ncl-talk mailing list
		List instructions, subscriber options, unsubscribe:
		http://mailman.ucar.edu/mailman/listinfo/ncl-talk
		




	-- 
	
	Adam Phillips 
	
	Associate Scientist,  Climate and Global Dynamics Division, NCAR
	
	www.cgd.ucar.edu/staff/asphilli/   303-497-1726 
	
	
	
	<http://www.cgd.ucar.edu/staff/asphilli> 


_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk


More information about the ncl-talk mailing list