Re: syntax for creating array for data fordata from ascii file

From: Adam Phillips <asphilli_at_nyahnyahspammersnyahnyah>
Date: Mon, 08 Dec 2008 12:54:56 -0700

Hi Erik,
Reading self-describing file types (.nc,.hdf, GRIB) is usually quite
easy in NCL as you know.

Assuming you have the necessary information and the files aren't
ridiculously complex, reading binary or ascii data isn't bad.
But you still have to go thru the motions of assigning coordinate
variables and such for both of these file types. And specifically with
ascii files, it really depends on how the data was written to the file.
In your case, with multiple time steps and one record per line, it takes
a bit of coding.

All of which is why I principally use netCDF files when I can..
Adam

Erik Noble wrote:
> Hi Adam.
> Thank you for the help. I will try this. I didn't that something as simple
> as reading files would involve "counters" and such....at least from the NCL
> getting started examples.
> Sincerely,
> Erik
>
> On 12/8/08 2:24 PM, "Adam Phillips" <asphilli_at_cgd.ucar.edu> wrote:
>
>> Hi Erik,
>> It matters how standard/regular the data within the ascii file is.
>>
>> When I said "These are just guesses on my part" I was referring to the
>> fact that I don't know every detail about your ascii file. You do. I
>> didn't know how many longitudes are in the file, nor the number of
>> timesteps (although you just answered that), nor how regular the data
>> within the file is.
>>
>> Anyway, if you have 630 rows, and thirty days in the file, then that
>> leads to 21 records/longitudes per timestep. This is of course assuming
>> that each timestep has the same number of longitude records in it, again
>> getting back to what I stated above about the regularity of data within
>> the file.
>>
>> So, if each timestep does have 21 records, ordered the exact same way
>> for each timestep, and there are 30 days/timesteps within the single
>> ascii file, then the coding could look like this. Same idea though as
>> what I sent as before, except I am not using coordinate subscripting to
>> fill the new data array this time as that's not needed.
>>
>> data = asciiread("data.asc",(/630,5/),"float")
>> time = data(0::21,2) ; grab every 21st day record, should be 1,2,3,4
>> lon = data(:20,3) ; grab first 21 lon records, as they are repeated
>> file_data = data(:,4)
>>
>> print(time) ; just checking, should be 1,2,3,4,5....30
>>
>> arr = new((/dimsizes(time),dimsizes(lon)/),typeof(file_data))
>> arr!0 = "time"
>> arr&time = time
>> arr!1 = "lon"
>> arr&lon = lon
>>
>> cntr = 0
>> do gg = 0,29 ; 30 days in September
>> arr(gg,:) = file_data(cntr:cntr+20)
>> cntr = cntr+21
>> end do
>> delete(file_data)
>>
>> Adam
>>
>> Erik Noble wrote:
>>> There is only one file. The first column is contains timesteps of an entire
>>> month. What if I don't want to specify the lon array or the time array as
>>> you have here....I just want NCL to read it straight from the file so that
>>> there is no guessing?
>>>
>>>
>>> On 12/8/08 1:28 PM, "Adam Phillips" <asphilli_at_cgd.ucar.edu> wrote:
>>>
>>>> Hi Erik,
>>>> I think you're getting the coordinate variable assignments and the data
>>>> array entry confused. First, set up coordinate arrays for time and
>>>> longitude:
>>>>
>>>> (These are just guesses on my part..)
>>>> time = ispan(0,10,1)
>>>> lon = fspan(-20,294.5,630) ; go from 20W->294.5E by 0.5
>>>>
>>>> arr = new((/dimsizes(time),dimsizes(lon)/),"float",-999.)
>>>> arr!0 = "time"
>>>> arr&time = time
>>>> arr!1 = "lon"
>>>> arr&lon = lon
>>>>
>>>> Next, you need to read in the ascii data into the array correctly. Note
>>>> that the syntax of your time (2006:09:01) will be read in to asciiread
>>>> as three different entries. Thus, your data should be read in as 5
>>>> columns and 630 rows.
>>>>
>>>> data = asciiread("data.asc",(/630,5/),"float")
>>>> file_year = data(:,0)
>>>> file_month = data(:,1)
>>>> file_day = data(:,2)
>>>> file_lon = data(:,3)
>>>> file_data = data(:,4)
>>>> delete(data)
>>>>
>>>> Here is were it can get tricky. If each ascii file contains one
>>>> timestep, then it's easy:
>>>>
>>>> do gg = 0,dimsizes(file_data)-1
>>>> arr(0,{file_lon(gg)})=(/file_data(gg)/) ; data entry for 0th timestep
>>>> end do
>>>>
>>>> Notice that I am using coordinate subscripting to enter the data into
>>>> the correct longitude slot.
>>>>
>>>> If each ascii file has multiple timesteps, you will have to design the
>>>> do loop to recognize that when the time index has changed and put the
>>>> data into the correct time slot of the arr data array.
>>>>
>>>> This should at least get you started..
>>>> Adam
>>>>
>>>> Erik Noble wrote:
>>>>> Hi. Could I have some help here with a simple question? I just want to
>>>>> make sure that I am getting the syntax right for using data from an
>>>>> ascii file to create and array. I keep running into problems.
>>>>> I have an ascii file that is three columns -" time", "longitude", and
>>>>> "rainfall amount (mm)", and 630 rows.
>>>>> 2006:09:01 -20.000 11.3280
>>>>> 2006:09:01 -19.500 12.2291
>>>>> . . .
>>>>> . . .
>>>>> . . .
>>>>>
>>>>> I am trying to create a time vs. longitude plot (hovmoller) which
>>>>> requires that data be 2 dimensional with longitude as the the right-most
>>>>> dimension.
>>>>> To correctly read in this data into an array "rain" would be the
>>>>> following?
>>>>> rain = new((/2/630/),float)
>>>>> rain(0,:)=data(:,0) ; to read in time
>>>>> rain(1,:)=data(:,1) ; to read in lon
>>>>>
>>>>> The next step would be to just plot the data
>>>>>
>>>>> plot = gsn_csm_hov(wks, rain, res )
>>>>>
>>>>>
>>>>> Where do the actual "rainfall amount" rainfall amount values go if I
>>>>> have already "filled" in the dimensions for time and longitude? Just
>>>>> trying to understand the concept and what else I need to do.
>>>>> Thank you for your time.
>>>>> -Erik
>>>>>
>>>>>
>>>>> ------------------------------------------------------------------------
>>>>>
>>>>> _______________________________________________
>>>>> 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
>
>

-- 
--------------------------------------------------------------
Adam Phillips			             asphilli_at_ucar.edu
National Center for Atmospheric Research   tel: (303) 497-1726
ESSL/CGD/CAS                               fax: (303) 497-1333
P.O. Box 3000				
Boulder, CO 80307-3000	  http://www.cgd.ucar.edu/cas/asphilli
_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Mon Dec 08 2008 - 12:54:56 MST

This archive was generated by hypermail 2.2.0 : Thu Dec 11 2008 - 03:51:46 MST