Re: Regridding from low to high resolution ?

From: Mary Haley <haley_at_nyahnyahspammersnyahnyah>
Date: Mon Jun 18 2012 - 10:33:08 MDT

Hi Madeleine,

You will need to add a "do" loop and call grid2triple for each element of that third dimension. I'm not sure why grid2triple doesn't allow for multiple leftmost dimensions. I'll look into this.

Meanwhile, maybe something like this:

  ntim = dimsizes(pctpft(:,0,0))
  nlat = dimsizes(tlat)
  nlon = dimsizes(tlon)

  xregrid = new( (/ntim,nlat,nlon/), typeof(pctpft))

  do i=0,ntim-1
    xregrid(i,:,:) = regrid_cat(clon,clat,pctpft(i,:,:),tlat,tlon)
  end do

I don't know what "regrid_cat" looks like, so I'm not sure this is the correct syntax. Perhaps the creation of "xregrid" and the "do" loop should be moved into "regrid_cat".

--Mary

On Jun 15, 2012, at 10:23 AM, Madeleine Patterson wrote:

> Hi Mary,
>
> Thanks for your response - I am trying to get the regridding function I created to work; I've tried your code but I get an error message:
>
> fatal: number of dimensions in parameter (2) or (regrid_cat) is (3), (2) dimensions were expected
>
> So I tried putting the regrid function into a do loop so I can loop over all 10 pfts...
>
> do i=1,10
> xregrid = regrid_cat(clon,clat,pctpft(i,:,:),tlat,tlon) ;;;; (this may well be incorrect syntax)
> end do
>
> but then I get the error message :
>
> fatal: grid2triple : the last input array must be dimensioned ny x mx where ny is the length of y and mx is the length of x
>
> - so in my function definition testd=grid2triple(clon,clat,pctpft), it's saying pctpft must be 2 dimensional, not 3 dimensional (which it is).
>
> How to get around this? Would I have to read in each pft as one 2d variable, perform the regridding then reconstruct the 3d netcdf file, or is there an easier solution?
>
> thanks,
>
> M.P.
>
>
> On Sat, Jun 16, 2012 at 12:25 AM, Mary Haley <haley@ucar.edu> wrote:
> 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

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Mon Jun 18 12:05:00 2012

This archive was generated by hypermail 2.1.8 : Mon Jun 25 2012 - 09:57:23 MDT