Re: About reading .bin files in NCL

From: Waqar Younas <vickyqau_at_nyahnyahspammersnyahnyah>
Date: Thu Dec 27 2012 - 17:01:54 MST

Hi
Thanks for valuable suggestions.
First i download the data for a particular model (e.g. ECMWF), using
openDap from ENSEMBLES website using NCL as
begin

;*********************remotely access************************
  url= "
http://ensembles.ecmwf.int/thredds/dodsC/ensembles/stream2/seasonal/atmospheric/daily/
"
  filename = url + "ua"
  exists = isfilepresent(filename)
  if(.not.exists) then
    print("OPeNDAP isfilepresent test unsuccessful.")
    print("Either file doesn't exist, or NCL does not have OPeNDAP
capabilities on this system")
  else
    print("OPeNDAP isfilepresent test successful.")
    fin= addfile(filename,"r")
    vars = getfilevarnames(fin)
    print(vars) ; should be (in any order): "l","j","i","cross","aloan",
                  ; "shot","order","bears"
  end if

;***************************output**************************

  ;*****ECMWF output*****
  it=0
  do iyear=0,45
    var=fin->ua(it:it+209,0:8,0,:,:) ;;;;; 0 is for u850. The whole
data contains 4 levels, 850,500,200,50
    file_out="ua"+".feb"+".ECMWF"+".ENSEMBLES.bin"
    fbindirwrite(file_out,(/var/))
    it=it+210*3+420
  end do
  delete(var)

  it=210
  do iyear=0,45
    var=fin->ua(it:it+209,0:8,0,:,:)
    file_out="ua"+".may"+".ECMWF"+".ENSEMBLES.bin"
    fbindirwrite(file_out,(/var/))
    it=it+210*3+420
  end do
  delete(var)

  it=210*2
  do iyear=0,45
    var=fin->ua(it:it+209,0:8,0,:,:)
    file_out="ua"+".aug"+".ECMWF"+".ENSEMBLES.bin"
    fbindirwrite(file_out,(/var/))
    it=it+210*3+420
  end do
  delete(var)

  it=210*3
  do iyear=0,45
    var=fin->ua(it:it+419,0:8,0,:,:)
    file_out="ua"+".nov"+".ECMWF"+".ENSEMBLES.bin"
    fbindirwrite(file_out,(/var/))
    it=it+210*3+420
  end do
 delete(var)

The code is downloading the ECMWF data for feb, may, aug and nov initial
condition. Here in my code i am trying to read the nov initial condition.
I read the file using
v = fbindirread("ua.nov.ECMWF.ENSEMBLES.bin", 0, (/144*73*9*420*46/),
"float")

This only works for NCL version 5.2.0 beta and is not working with NCL
6.1.0 (strange). Even fbinrecread, cbinread are not working.

Now 144 are lon, 73 are lats, 9 are ensemble members, 420 are lead time
(days) and 46 are years from 1960-2005.

using MATLAB, i can read the same file with same sequence for example, in
MATLAB i read the following above file as
fid = fopen('ua.feb.ECMWF.ENSEMBLES.bin','r');
A = fread(fid,'float32');
A2=reshape(A,144,73,9,420,46);

The variable "v" above does not have any data. When i print(v(0:100)), it
is giving zeros at each point.

I know to convert .grb file to .nc but my file is simple binary instead of
grid binary.
Is there any way to convert .bin to .grb?
Because i need to convert the file to nc in anyway because in .bin file,
there are no attributes. I tried to attach attribute using
Noel Aloysius script but it is not working.
Thanks.

On Thu, Dec 27, 2012 at 8:58 AM, Noel Aloysius <noel.aloysius@gmail.com>wrote:

> Here is how to read a flat binary file.
>
> Precipitation data are stored as a 14x1200x180x360 array
> (/ensembles,time,lat,lon/).
>
> Pclim = fbindirread("~/pr.bin", 0, (/14,1200,180,360/), "float",
> 1e+20) ;
>
> ; define attributes
> Pclim!0 = "ensemble"
> Pclim!1 = "time"
> Pclim!2 = "lat"
> Pclim!3 = "lon"
> Pclim&ensemble =
> (/"model-1","model-2","model-3","model-4","model-5","model-6",\
>
> "model-7","model-8","model-9","model-10","model-11","model-12", \
> "model-13","model-14"/)
> Pclim&time = yyyymm_time(1901,2000,"integer")
>
> Pclim&lat = lat ; already defined, ex. see
> http://www.ncl.ucar.edu/Document/Functions/Contributed/latGlobeFo.shtml
> Pclim&lon = lon ; already defined, see
> http://www.ncl.ucar.edu/Document/Functions/Contributed/lonGlobeFo.shtml
>
> Once the attributes are assigned, most of the ncl functions can be used.
>
>
>
>
> Noel Aloysius
>
>
> On Thu, Dec 27, 2012 at 4:13 AM, Waqar Younas <vickyqau@gmail.com> wrote:
>
>> Hi
>> I am using NCL version 6.1.0 beta version. I am wondering what is correct
>> way of reading .bin files in NCL. I am trying to read ENSEMBLES project
>> stream 2, uwind data at 850mb.
>> The data dimensions are
>> 144,73,9,420,46 where these numbers are representing lon, lat, ensemble
>> member, lead time and number of years respectively.
>> I am using the following to read the data but i am facing errors.
>>
>> 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"
>> v = fbindirread("ua.nov.ECMWF.ENSEMBLES.bin", 5, (/144,73,9,420,46/),
>> "float")
>> But i am facing error like
>> fatal:fbindirread: The size implied by the dimension array and record
>> number is greater that the size of the file, can't continue
>>
>> Is there any way to convert .bin file into .nc using NCL because even if
>> i read the .bin file, the geographical coordinates like lat, lon are not
>> assigned to the data. Using .bin file, we can not get information like
>> ncdump.
>>
>> Any help would be highly appreciated.
>>
>> --
>> Best Regards
>>
>> Waqar Younas
>> PhD Candidate
>> Natural Resources and Environmental Studies (NRES)
>> University of Northern British Columbia (UNBC)
>> Canada
>>
>> _______________________________________________
>> ncl-talk mailing list
>> List instructions, subscriber options, unsubscribe:
>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>
>>
>

-- 
Best Regards
Waqar Younas
PhD Candidate
Natural Resources and Environmental Studies (NRES)
University of Northern British Columbia (UNBC)
Canada

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Thu Dec 27 17:02:15 2012

This archive was generated by hypermail 2.1.8 : Fri Jan 04 2013 - 15:32:29 MST