Re: Regridding from low to high resolution ?

From: Mary Haley <haley_at_nyahnyahspammersnyahnyah>
Date: Fri Jun 15 2012 - 08:25:07 MDT

Hi Madeleine,

Functions that you write yourself are the same as the other functions that you call, like grid2triple. You just need to make sure the function is defined before you call it.

Here's a very basic and complete example of defining a function, and then calling it:

undef("addten")
function addten(x)
begin
   y = x + 10
  return(y)
end

;----Main code
begin
    x = ispan(1,100,1)
    x10 = addten(x)
    print(x10)
end

------------------------------------------------------------------------------------

Here's what your partial script might look like, although I
don't understand what "cnew" is supposed to be.

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"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/shea_util.ncl"

;----------------------------------------------------------------------
; Define the regrid_cat function
;----------------------------------------------------------------------
undef("regrid_cat")
function regrid_cat(clat[*],clon[*],pctpft[*][*],tlat[*],tlon[*]) ; categorical grid and target grid
local testd, pctpftnew, cnew ; using "local" is not required, but recommended
begin
  testd = grid2triple(clon,clat,pctpft) ; testd(3,ld)
  pctpftnew = triple2grid(testd(0,:),testd(1,:),testd(2,:),tlon,tlat,False)

  copy_VarAtts(pctpft,pctpftnew) ;contributed.ncl
;
; I'm not sure what the "cnew" variable is,
; since it doesn't (and can't) get returned
; by this function
;
  cnew!0 = "lat"
  cnew!1 = "lon"
  cnew&lat =tlat
  cnew&lon =tlon

  return(pctpftnew)
end

;----------------------------------------------------------------------
; This is the main code that will read in the data and call regrid_cat
;----------------------------------------------------------------------
begin

  setfileoption("nc","SuppressClose",False)
  in1 = addfile("pft_file1.nc","r")

  pctpft = in1->PCT_PFT

  clat=90
  clon=144
  tlat=360
  tlon=720
  . . .

  xregrid = regrid_cat(clat,clon,pctpft,tlat,tlon)

end

On Jun 14, 2012, at 2:46 PM, Madeleine Patterson wrote:

> Hi Mary,
>
> Sorry about the cluenessness about ncl-talk etiquette!
>
> I have set up the function declaration at the start of the code, before the main script, but am unsure of what I need to do to implement the function and where, in the main body of the script (i.e. after I've open files and read variables into variable names, etc.)
>
> In your example below, I would -after opening files and reading in variables -define clon, clat, tlon and tlat, but what do I do then? use the regrid_cat function that I've already defined (and if so, how?), as well as the 'testd=grid2triple()...' and 'pctpftnew=triple2grid()...' commands?
>
> In grid2triple command, 'z' should be 2-d, but in my case it is 3-d (10 pfts, 91 lats, 144 lons)
>
> Is there an example script you could point me to that creates a function and implements it later in the script?
>
> Thanks,
> M
>
> ******
> You are missing a "begin" after the "function" declaration.
>
> In general, I would recommend not putting a function declaration right in the middle of your main program. It's cleaner to do it like this:
>
> 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"
> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/shea_util.ncl"
>
> undef("regrid_cat")
> function regrid_cat(clat[*],clon[*],pctpft[*][*],tlat[*],tlon[*]) ; categorical grid and target grid
> begin
> testd=grid2triple(clon,clat,pctpft) ; testd(3,ld)
> pctpftnew = triple2grid(testd(0,:),testd(1,:),testd(2,:),tlon,tlat,False)
>
> copy_VarAtts(pctpft,pctpftnew) ;contributed.ncl
> cnew!0 = "lat"
> cnew!1 = "lon"
> cnew&lat =tlat
> cnew&lon =tlon
>
> return(pctpftnew)
> end
>
>
> begin
>
> setfileoption("nc","SuppressClose",False)
>
> in1 = addfile("pft_file1.nc","r")
>
> pctpft = in1->PCT_PFT
>
> clat=90
> clon=144
> tlat=360
> tlon=720
> . . .
>
> end
>
>
>

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Fri Jun 15 08:25:15 2012

This archive was generated by hypermail 2.1.8 : Fri Jun 15 2012 - 14:51:31 MDT