[ncl-talk] regrid variable writing netcdf file
Mary Haley
haley at ucar.edu
Mon Mar 26 12:26:11 MDT 2018
Anil,
Your WRF grid is represented by two-dimensional latitude / longitude
arrays, so you cannot do this:
var_regrid&lat2d = dst_lat
var_regrid&lon2d = dst_lon
The "&" syntax is a special syntax that is reserved for attaching
*one-dimensional* coordinate arrays to a variable.
When your data is represented by two-dimensional lat/lon arrays, then in
order to associate the name of the 2D lat/lon arrays on the file with a
particular variable, you can use the "coordinates" attribute.
I know this is confusing, but this is how things are defined on NetCDF
files. You can read more about this in the CF convention document, chapter
5.2, "Two-Dimensional Latitude, Longitude, Coordinate Variables":
http://cfconventions.org/Data/cf-conventions/cf-conventions-1.7/cf-conventions.pdf
You want to do something like this:
var_regrid at coordinates = "XLONG_M XLAT_M" ; indicates name of lat/lon
coord arrays on the file
and then make sure you write the 2D latitude / longitude arrays to the file
using these same names. You've already read these as "dst_lat" and
"dst_lon", so to write them, you would use:
fnew->XLAT_M = dst_lat
fnew->XLONG_M = dst_lon
I've attached the modified script.
By the way, I tried to modify your script as little as possible so you
could easily see where I made changes.
However, I highly recommend that you clean up the indentation, because it's
very hard to read code when it is not consistently indented.
For example, you have:
lat1D = sfile->y
lon1D = sfile->x
var = sfile->zeta_max
dst_file = "geo_em.d01.nc"
dfile = addfile(dst_file,"r")
dst_lat = dfile->XLAT_M(0,:,:) ; Be sure to use
appropriate names
dst_lon = dfile->XLONG_M(0,:,:) ; here.
srcGridName = "source_grid_file.nc"
dstGridName = "destination_grid_file.nc"
wgtFileName = "unstruct_to_rect.nc"
This is hard to read because lines are indented somewhat randomly, and
there are extra line breaks and white space that you really don't need.
It helps if you can decide on a consistent indentation, say two or four
spaces for each line, and stick to it. It also helps if you line up the
equal signs, but this is a personal preference that you need to decide if
it works for you.
lat1D = sfile->y
lon1D = sfile->x
var = sfile->zeta_max
dst_file = "geo_em.d01.nc"
dfile = addfile(dst_file,"r")
dst_lat = dfile->XLAT_M(0,:,:) ; Be sure to use appropriate names
dst_lon = dfile->XLONG_M(0,:,:) ; here.
srcGridName = "source_grid_file.nc"
dstGridName = "destination_grid_file.nc"
wgtFileName = "unstruct_to_rect.nc"
--Mary
On Mon, Mar 26, 2018 at 11:39 AM, Anil Kumar - NOAA Affiliate <
anil.kumar at noaa.gov> wrote:
> Mary,
>
> Thanks for your email. After some proper plotting resources, I am able to
> plot figure (attached).
> My initial question is about writing regrid variable in lat x lon format .
> (ncl script attached)
>
> I am trying to write var_regrid variable in lat x long format i.e
> var_regrid(lat x lon). After esmf call var_regrid is in (x x y) point
> dimensions.
>
> I am trying to looking samples and scripts but no success so far.
>
> Thanks,
> Anil
>
>
>
>
>
> On Fri, Mar 23, 2018 at 6:09 PM, Mary Haley <haley at ucar.edu> wrote:
>
>> Hello,
>>
>> We have been looking into this offline.
>>
>> For starters, since both of your grids are regional, you want to set:
>>
>> Opt at DstRegional = True
>> Opt at SrcRegional = True
>>
>> Secondly, you already have the triangular mesh defined on your file,
>> which will help the regridding go much faster, because NCL doesn't have to
>> generate it for you:
>>
>>
>> Opt at SrcTriangularMesh = sfile->element-1
>> ; the minus one is important so indexes go from 0 to n-1
>>
>> For plotting purposes, I set these for both plots:
>>
>> res at cnLevelSelectionMode = "ManualLevels"
>> res at cnMinLevelValF = -6
>> res at cnMaxLevelValF = 6
>> res at cnLevelSpacingF = 0.25
>>
>> I removed the file writing part for now, because there's something weird
>> about an attribute type, but I'll include the original script
>> (test_regrid.ncl) and the modified one without the file writing
>> (test_regrid_mod.ncl) and hopefully you can add back in the file writing
>> part. I was in a hurry so I just removed it, and now I have to run out the
>> door.
>>
>> I'm not sure the regridding went well, but this could simply be the
>> nature of going from high-res to low-res, and also the fact that the two
>> grids don't have values in the same places. You will need to take a look
>> at the PNG image and see what you think.
>>
>> --Mary
>>
>>
>> On Thu, Mar 22, 2018 at 9:58 AM, Rick Brownrigg <brownrig at ucar.edu>
>> wrote:
>>
>>> Hi,
>>>
>>> Without the data, I can't run the script, but on visual inspection, it
>>> *looks* like you have all the right elements in place to write the NetCDF
>>> file. What's not happening for you -- any errors or odd results, or just
>>> plain nothing?
>>>
>>> Rick
>>>
>>> On Thu, Mar 22, 2018 at 6:58 AM, Anil Kumar - NOAA Affiliate <
>>> anil.kumar at noaa.gov> wrote:
>>>
>>>> Hi,
>>>>
>>>> I am trying to write regrid variable (unstructured data to WRF grid),
>>>> code creates var_regrid successfully at that point. I want to write
>>>> var_regird variable in separate netcdf file with lat & lon dimension in it.
>>>> Can anyone please help me on this. Thanks
>>>> The var_regrid print summmary
>>>> ********
>>>>
>>>> Variable: var_regrid
>>>>
>>>> Type: double
>>>>
>>>> Total Size: 1301864 bytes
>>>>
>>>> 162733 values
>>>>
>>>> Number of Dimensions: 2
>>>>
>>>> Dimensions and sizes: [353] x [461]
>>>>
>>>> Coordinates:
>>>>
>>>> Number Of Attributes: 10
>>>>
>>>> missing_value : -99999
>>>>
>>>> remap : remapped via ESMF_regrid_with_weights: Bilinear
>>>>
>>>> lon2d : <ARRAY of 162733 elements>
>>>>
>>>> lat2d : <ARRAY of 162733 elements>
>>>>
>>>> long_name : maximum water surface elevationabove geoid
>>>>
>>>> standard_name : maximum_sea_surface_height_above_geoid
>>>>
>>>> location : node
>>>>
>>>> mesh : adcirc_mesh
>>>>
>>>> units : m
>>>>
>>>> _FillValue : -99999
>>>>
>>>> ********
>>>> here is the code
>>>>
>>>> begin
>>>>
>>>>
>>>>
>>>>
>>>> src_file = "maxele.63.nc"
>>>>
>>>> sfile = addfile(src_file,"r")
>>>>
>>>>
>>>> ;printVarSummary(sfile)
>>>>
>>>> lat1D = sfile->y
>>>>
>>>> lon1D = sfile->x
>>>>
>>>> var = sfile->zeta_max
>>>>
>>>>
>>>> dst_file = "geo_em.d01.nc"
>>>>
>>>> dfile = addfile(dst_file,"r")
>>>>
>>>>
>>>>
>>>> ;---Set up regridding options
>>>>
>>>>
>>>> Opt = True
>>>>
>>>>
>>>> ;---"bilinear" is the default. "patch" and "conserve" are other options.
>>>>
>>>> Opt at InterpMethod = "bilinear" ;;---Change (maybe)
>>>>
>>>> Opt at WgtFileName = "unstruct_to_rect.nc"
>>>>
>>>>
>>>> Opt at SrcGridLat = lat1D
>>>>
>>>> Opt at SrcGridLon = lon1D
>>>>
>>>> Opt at SrcRegional = False ;;--Change (maybe)
>>>>
>>>> Opt at SrcInputFileName = src_file ; optional, but good idea
>>>>
>>>>
>>>> Opt at SrcMask2D = where(.not.ismissing(var),1,0) ; Necessary
>>>> if has
>>>>
>>>> ; missing
>>>> values.
>>>>
>>>>
>>>> dst_lat = dfile->XLAT_M(0,:,:) ; Be sure to use
>>>> appropriate names
>>>>
>>>> dst_lon = dfile->XLONG_M(0,:,:) ; here.
>>>>
>>>> Opt at DstGridLon = dst_lon
>>>>
>>>> Opt at DstGridLat = dst_lat
>>>>
>>>> Opt at DstRegional = True ;;--Change (maybe)
>>>>
>>>> ; Opt at DstMask2D = where(.not.ismissing(dst_lat).and.\
>>>>
>>>> ; .not.ismissing(dst_lon),1,0) ; Necessary if
>>>> lat/lon
>>>>
>>>> ; has missing
>>>> values.
>>>>
>>>> ; has missing
>>>> values.
>>>>
>>>> Opt at ForceOverwrite = True
>>>>
>>>> Opt at Debug = True
>>>>
>>>> Opt at PrintTimings = True
>>>>
>>>>
>>>> var_regrid = ESMF_regrid(var,Opt) ; Do the regridding
>>>>
>>>>
>>>> printVarSummary(var_regrid)
>>>>
>>>>
>>>>
>>>> ; Code for writing netcdf format file with lat and long cordinate
>>>>
>>>> setfileoption("nc", "Format", "NetCDF4")
>>>>
>>>> fon = "ZETA_MAX_D01.nc"
>>>>
>>>> system("/bin/rm -f " + fon) ; remove if exists
>>>>
>>>> fnew = addfile(fon, "c")
>>>>
>>>>
>>>>
>>>> ;===================================================================
>>>>
>>>> ; create global attributes of the file (optional)
>>>>
>>>> ;===================================================================
>>>>
>>>> fAtt = True ; assign file attributes
>>>>
>>>> fAtt at title = "NCL Simple Approach to netCDF Creation"
>>>>
>>>> fAtt at source_file = "original-file.nc"
>>>>
>>>> fAtt at Conventions = "None"
>>>>
>>>> fAtt at creation_date = systemfunc ("date")
>>>>
>>>> fileattdef( fnew, fAtt ) ; copy file attributes
>>>>
>>>>
>>>> filedimdef(fnew,(/"lat","lon"/),(/353,461/),(/False,False/))
>>>>
>>>> var_names2D = (/ "var_regrid" /)
>>>>
>>>> varvar_types2D = (/ "float" /)
>>>>
>>>>
>>>> filevardef( fnew, var_names2D, varvar_types2D, (/ "lat", "lon" /) )
>>>>
>>>>
>>>>
>>>> var_regrid!0 = "lat"
>>>>
>>>> var_regrid!1 = "lon"
>>>>
>>>>
>>>> var_regrid&lat = dst_lat
>>>>
>>>> var_regrid&lon = dst_lon
>>>>
>>>>
>>>> var_regrid at long_name = "maximum water surface elevationabove geoid"
>>>> ; assign attributes
>>>>
>>>> var_regrid at units = "m"
>>>>
>>>>
>>>> fnew->ZETA_MAX = var_regrid ; name on file different
>>>> from name in code
>>>>
>>>>
>>>>
>>>> end
>>>>
>>>>
>>>> --
>>>> Dr. Anil Kumar
>>>> NWS/NCEP/EMC, Office# 2875
>>>> NOAA Center for Weather and Climate Prediction (NCWCP)
>>>> 5830 UNIVERSITY RESEARCH CT
>>>> <https://maps.google.com/?q=5830+UNIVERSITY+RESEARCH+CT+College+Park,+MD+20740&entry=gmail&source=g>
>>>> College Park, MD 20740
>>>> <https://maps.google.com/?q=5830+UNIVERSITY+RESEARCH+CT+College+Park,+MD+20740&entry=gmail&source=g>
>>>> -3818
>>>> 301-683-0494 <(301)%20683-0494>
>>>> anil.kumar at noaa.gov
>>>>
>>>>
>>>> _______________________________________________
>>>> ncl-talk mailing list
>>>> ncl-talk at ucar.edu
>>>> List instructions, subscriber options, unsubscribe:
>>>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>>>
>>>>
>>>
>>> _______________________________________________
>>> ncl-talk mailing list
>>> ncl-talk at ucar.edu
>>> List instructions, subscriber options, unsubscribe:
>>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>>
>>>
>>
>
>
> --
> Dr. Anil Kumar
> NWS/NCEP/EMC, Office# 2875
> NOAA Center for Weather and Climate Prediction (NCWCP)
> 5830 UNIVERSITY RESEARCH CT
> <https://maps.google.com/?q=5830+UNIVERSITY+RESEARCH+CT+College+Park,+MD+20740&entry=gmail&source=g>
> College Park, MD 20740
> <https://maps.google.com/?q=5830+UNIVERSITY+RESEARCH+CT+College+Park,+MD+20740&entry=gmail&source=g>
> -3818
> 301-683-0494 <(301)%20683-0494>
> anil.kumar at noaa.gov
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20180326/548f13f2/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test_regrid_write_mod.ncl
Type: application/octet-stream
Size: 3508 bytes
Desc: not available
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20180326/548f13f2/attachment.obj>
More information about the ncl-talk
mailing list