Re: ncl

From: Mary Haley <haley_at_nyahnyahspammersnyahnyah>
Date: Wed Oct 31 2012 - 10:02:11 MDT

Hi Clotilde,

I'm not familiar enough with the CDO to know what format they expect the file to be in order to recognize 2D lat/lon arrays.

In your message below, you said, ". . .but the lon/lat information are define as variables, …"

That's because you wrote the "lat2d" and "lon2d" arrays as variables on the file, with the following code:

> fout->lat2d=lat2d
> fout->lon2d=lon2d

With 2D lat/lon arrays, you can't store them in any other way on the file, so what you are doing should be fine. However, you didn't predefine "lat2d" or "lon2d" with a call to "filevardef", so this will possibly make your code a little slower.

What error are the CDO giving you? Perhaps they expect the lat2d/lon2d arrays to be in a particular format or have a particular name, or, maybe you need to include a "coordinates" attribute when you write out "t2m_cru".

Maybe you need something like this:

  filevardef(fout, "time" ,typeof(time),getvardims(time))
; NEW CODE
  filevardef(fout,"latitude",typeof(lat2d),getvardims(lat2d))
  filevardef(fout,"longitude",typeof(lon2d),getvardims(lon2d))
  filevardef(fout, "t2m_cru" ,"float",(/"time","y","x"/))
. . .
   filevarattdef(fout,"time" ,time) ; copy time attributes
; NEW CODE
   t2m_atts = true ; create some t2m_cru attributes
   t2m_atts@coordinates = "latitude longitude"
   t2m_atts@….. ; add more attributes if desired
   filevarattdef(fout,"t2m_cru",t2m_atts) ; Define the attributes for t2m_cru

And then, instead of:

   fout->lat2d=lat2d
   fout->lon2d=lon2d
   fout->time=time

you would have:

   fout->latitude=lat2d
   fout->longitude=lon2d
   fout->time=time

I believe the key is to find out what format the CDO want for the 2D lat/lon grids. If you still have trouble with this, can you also include the CDO error message you're getting?

Thanks,

--Mary

On Oct 31, 2012, at 9:18 AM, DUBOIS Clotilde wrote:

>
>
> Hello NCL,
>
> I'm trying to write a new netcdf file after regriding data onto a regional 2-D grid. The file is created but the lon/lat information are define as variables, which cause me some trouble when i am doing some operations afterwards especially when using CDO.
> Below is what I'm trying to do by creating a lat2d /lon2d grid. I am not sure what is went wrong but any help or direction would be greatly appreciated.
>
> Best regards,
>
> Clotilde
>
>
>
> 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 file and read in data
> ;*******************************************
>
> ;f = addfile ("/home/dubois/LATMOS/tas_MED-44_ECMWF-ERAINT_evaluation_r1i1p1_CNRM-ALADIN52_v1_mon_201101-201112_mean.nc", "r")
> f = addfile ("/home/dubois/LATMOS/tas_MED-11_ECMWF-ERAINT_evaluation_r1i1p1_CNRM-ALADIN52_v1_mon_201101-201112_mean.nc","r")
>
> field=f->tas
> lat2d= f->lat
> lon2d= f->lon
>
> dimp = dimsizes(field)
> nlat = dimp(2)
> nlon = dimp(3)
>
> f7 = addfile("/sxeac1/data0/dubois/VALIDATION/DATA/CRU/cru_ts_3_00.1901.2006.tmp.nc","r")
> lat_cru = f7->lat(200:320)
> lon_cru = f7->lon(300:470)
> TIME= f7->time
>
> precip = f7->tmp
>
> dimp1 = dimsizes(precip)
> ntim = dimp1(0)
>
> t2m_cru = new((/ntim,nlat,nlon/),"float")
>
> i=-1
>
> do year=1901,2006
> do month=1,12
> i=i+1
> champ = f7->tmp(i,200:320,300:470)*0.1
> t2m_cru(i,:,:)=rgrid2rcm(lat_cru,lon_cru,champ,lat2d,lon2d,1)
> end do
> end do
>
> DATE = cd_calendar(TIME, 0)
> new_time_units = "hours since 1901-01-01 00:00"
> time = time_to_newtime(TIME, new_time_units)
> printVarSummary(TIME)
>
> ;==================================================================
> ;==================================================================
>
> system("/bin/rm -f foo2.nc") ; remove any pre-existing file
>
> fout= addfile("foo2.nc","c")
>
> ;===================================================================
> ; explicitly declare file definition mode. Improve efficiency.
> ;==================================================================
> setfileoption(fout,"DefineMode",True)
>
> ;===================================================================
> ; create global attributes of the file
> ;===================================================================
> fAtt = True ; assign file attributes
> fAtt@title = "NCL Efficient Approach to netCDF Creation"
> fAtt@source_file = "original-file.nc"
> fAtt@Conventions = "None"
> fAtt@creation_date = systemfunc ("date")
> fileattdef( fout, fAtt ) ; copy file attributes
>
> ;===================================================================
> ; predefine the coordinate variables and their dimensionality
> ; Note: to get an UNLIMITED record dimension, we set the dimensionality
> ; to -1 (or the actual size) and set the dimension name to True.
> ;===================================================================
> dimNames = (/"x","y","time"/)
> dimSizes = (/ nlon, nlat, -1 /)
> dimUnlim = (/ False , False, True/)
> filedimdef(fout,dimNames,dimSizes,dimUnlim)
>
> ;===================================================================
> ; predefine the the dimensionality of the variables to be written out
> ;===================================================================
> ; Here we are using NCL functions to facilitate defining
> ; each variable's dimension name(s) and type.
> ; The following could be replaced with explicit, user defined dimension
> ; names different from those associated with the variable in memory.
> ; Say, PS(time,lat,lon) in the NCL script. They could be redefined for the file via:
> ; filevardef(fout, "PS" ,typeof(PS) ,(/"TIME","latitude","longitude"/))
> ;===================================================================
> filevardef(fout, "time" ,typeof(time),getvardims(time))
> filevardef(fout, "t2m_cru" ,"float",(/"time","y","x"/))
>
> ;===================================================================
> ; Copy attributes associated with each variable to the file
> ; All attributes associated with each variable will be copied.
> ;====================================================================
> filevarattdef(fout,"time" ,time) ; copy time attributes
>
>
>
> t2m_cru@units="degrees"
> fout->t2m_cru=t2m_cru
> ;t2m_cru@lat2d=lat2d
> ;t2m_cru@lon2d=lon2d
>
> fout->lat2d=lat2d
> fout->lon2d=lon2d
> fout->time=time
>
>
> 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 Wed Oct 31 10:02:21 2012

This archive was generated by hypermail 2.1.8 : Tue Nov 06 2012 - 15:05:49 MST