[ncl-talk] Reading multiple hdf-eos files in an array

Mary Haley haley at ucar.edu
Fri Dec 11 09:13:57 MST 2015


There are a number of issues with your script.

[1]

By default, when you pull a variable off a set of files opened with
"addfiles", they are "concatenated" (this is known as "cat" mode in NCL)
across the leftmost dimension.

If you ​use the "join" mode, then a new leftmost dimension will be created
that will be the same size as the number of files (in your case, 20).

To get "join" mode, uncomment your ListSetType option above and change it
to:

  ListSetType (f, "join") ;join files

[2]

To get a 4 x 5 configuration rather than a 20 configuration, you can use
reshape to change the dimensions:

  npp_dims = filevardimsizes(f[0],"Npp_1km_MOD_Grid_MOD17A3")
  npp      = reshape(short2flt(f[:]->Npp_1km_
MOD_Grid_MOD17A3),(/4,5,npp_dims(0),npp_dims(1)/))


Please see the documentation for "addfiles" for more information:

http://www.ncl.ucar.edu/Document/Functions/Built-in/addfiles.shtml

[3]

An issue with using reshape is that you may lose attribute and coordinate
information, if any. You can use copy_VarAtts and copy_VarCoords to copy
over any information you need.

See:

http://www.ncl.ucar.edu/Document/Functions/Contributed/copy_VarAtts.shtml
http://www.ncl.ucar.edu/Document/Functions/Contributed/copy_VarCoords.shtml

[4]

You are reading two-dimensional latitude and longitude arrays off the file,
and then trying to treat them as coordinate arrays with:

npp!0="lat2d"
npp!1="lon2d"
npp&lat2d=lat2d
npp&lon2d=lon2d


By strict definition, coordinate arrays can only be one-dimensional, and of
the same size as whatever dimension they are associated with.  Since your
lat/lon arrays are 2D, you cannot assign them as coordinate arrays.

You can, however, write them separately to the file just like your "npp"
variable, and then create a "coordinates" attribute (that I see you have
already) that indicates what the name of the lat/lon arrays are on the new
file:

npp at coordinates="lon2d lat2d"


Note that the "coordinates" attribute generally doesn't have a comma in it.
Also, if your "npp" variable is 4D, and the other two dimensions have
variables associated with them, you'll want to include these in the
"coordinates" setting.  For example, if you had a 3D array, and the
leftmost dimension was represented by a variable on the file called "time",
then you would use:

npp at coordinates = "lon2d lat2d time"

For some reason, the coordinates are usually listed right-to-left, although
I don't think this is required.

[5]

You should use the same dimension names for your "npp" variable that you
have for the lat2d/lon2d variables:

npp!2 = "nlat"
npp!3 = "nlon"

Note that I changed these to dimensions 2 and 3, because dimensions 0 and 1
are associated with the "4 x 5" part of your array. You will need to name
these accordingly.


--Mary


On Wed, Dec 9, 2015 at 5:13 AM, Abdulla Sakalli <as at abdulla-sakalli.com>
wrote:

> Dear List,
>
> I downloaded 20 files from MODIS satellite for NPP (MOD17A3). I’d like to
> read the hdf files and merge it in 4x5 to get the NPP value for
> pan-European domain. I've been written the fallowing script for my aim.
> Unfortunately, I get the files merged in 1x20 (i.e. merged along latitude
> and not lonxlat direction).
> Can please anyone help me to read the npp from the 20 files and enlarge it
> in LonxLat direction.
>
> Thank you very much in advance
> Abu
>
> Script:
>
>
>
> load "$NCARG_ROOT/share/ncarg/nclscripts/csm/gsn_code.ncl"
> load "$NCARG_ROOT/share/ncarg/nclscripts/csm/gsn_csm.ncl"
> load "$NCARG_ROOT/share/ncarg/nclscripts/csm/contributed.ncl"
> load "$NCARG_ROOT/share/ncarg/nclscripts/csm/shea_util.ncl"
>
> begin
>
> ; 1km annual npp MOD17A3 on 1200x1200 tiles
>
> files = systemfunc("ls *.hdfeos")
>
> f = addfiles(files,"r")
> ;print(f)
> ;ListSetType (f, "cat") ;concatenate files
>
> lat2d=f[:]->GridLat_MOD_Grid_MOD17A3
> lon2d=f[:]->GridLon_MOD_Grid_MOD17A3
>
> npp=short2flt(f[:]->Npp_1km_MOD_Grid_MOD17A3)
>
> lat2d!0="nlat"
> lat2d!1="nlon"
> lon2d!0="nlat"
> lon2d!1="nlon"
>
> ;printVarSummary(lon2d)
>
> npp!0="lat2d"
> npp!1="lon2d"
> npp&lat2d=lat2d
> npp&lon2d=lon2d
> npp at coordinates="lat2d,lon2d"
> npp at projection="sinusoidal"
>
> printVarSummary(npp)
>
> system("rm test.nc")
> out=addfile("test.nc","c")
> out->npp=npp
> out->lat2d=lat2d
> out->lon2d=lon2d
>
> end
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> *print summary is Variable: npp Type: short Total Size: 57600000 bytes
>             28800000 values Number of Dimensions: 2 Dimensions and
> sizes:    [YDim_MOD_Grid_MOD17A3 | 24000] x [XDim_MOD_Grid_MOD17A3 | 1200]
> Coordinates: Number Of Attributes: 6   lon2d :    <ARRAY of 28800000
> elements>   lat2d :    <ARRAY of 28800000 elements>   coordinates :
> lat2d,lon2d   hdfeos_name :    Npp_1km   projection :    sinusoidal
> unsigned :    True *
>
> ---------------------------------------------------------------------------
> Asst. Prof. Abdulla Sakalli
>
> Iskenderun Technical University
> Faculty of Marine Sciences & Technology
> Dept. Water Resources Management and Organization
> Meydan Mah. 512 Sk
> Iskenderun-Hatay-Turkey
> as at abdulla-sakalli.com <http://www.abdulla-sakalli.com>
> asakalli at istek.edu.tr <asakalli at mku.edu.tr>
> +90 326 614 1693 (330)
> +90 536 506 9191
>
>
>
>
>
>
>
>
> _______________________________________________
> ncl-talk mailing list
> ncl-talk at ucar.edu
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20151211/a769f5f1/attachment.html 


More information about the ncl-talk mailing list