Re: sorting out the files

From: Rick Brownrigg <brownrig_at_nyahnyahspammersnyahnyah>
Date: Tue Apr 27 2010 - 20:56:03 MDT

I agree if there are a small number of files, fixing the names by hand
is the easiest. But if you have a production
environment, here's a solution similar to Jonathan's, but it creates
an *index-table* into the list of files,
keyed by the month, then sorts the table, and uses it to index into
the array of files to get the names in order:

; list of files, hardcoded here, but you'd use systemfunc as before
files = (/ "3B42.090609.0.6A.HDF", "3B42.090609.12.6A.HDF",
"3B42.090609.15.6A.HDF", "3B42.090609.18.6A.HDF", \
         "3B42.090609.21.6A.HDF", "3B42.090609.3.6A.HDF",
"3B42.090609.6.6A.HDF" /)
numFiles = dimsizes(files)

; our index-table: for each "row" the second element holds the month,
while the
; first element holds the index into the "files" array where that
month occurs. Note we parse the
; file name as Jonathan had done to get month.
index = new((/ numFiles, 2 /), integer)
index(:,0) = ispan(0, numFiles-1, 1)
index(:,1) = stringtoint(str_get_field(files, 3, "."))

; Now sort the index-table based upon month
sIndex = dim_pqsort_n(index, 2, 0)

; Now get the filenames in the preferred ordering:
do i=0, numFiles-1
   print(files(sIndex(i,1)))
end do

FWIW,
Rick

On Tue, 27 Apr 2010 16:36:00 -0600
  Jonathan Vigh <jvigh@ucar.edu> wrote:
> Hi Prabhakar,
> If you have a small number of files, then David Brown's solution
>is the easiest But if you have many files, or you are not able to
>physically rename them (for example, if they are on a remote server
>or you don't have write permission), then you have to be clever.
>
> What you can do is to read it the filenames, parse them into their
>separate parts, rewrite the file names in a way that can be sorted by
>NCL's string sort function (sqsort). If you first attach the original
>filenames as an attribute array to the list of new filenames, then
>you sort on the rehashed filenames and the attached attribute array
>of old names will be sorted into the correct order. You can then use
>that list of the old names sorted in the correct order.
>
> This might sounds hard, but it's really not too bad. I've attached a
>script that will do this for you.
>
> Best regards,
> Jonathan
>
>
>
>
>
>
>
>
> p s wrote:
>> Hi,
>> I am trying to read multipled 3B42 hdf 3hourly files and write it
>>out as a single netcdf file. The problem I am coming across is due to
>>the way the HDF filenames are:
>>
>>
>> 3B42.090609.0.6A.HDF
>> 3B42.090609.12.6A.HDF
>> 3B42.090609.15.6A.HDF
>> 3B42.090609.18.6A.HDF
>> 3B42.090609.21.6A.HDF
>> 3B42.090609.3.6A.HDF
>> 3B42.090609.6.6A.HDF
>>
>>
>>
>> When I use systemfunc to list the files and read in, they do not
>>appear in sequential time order, like 0, 3, 6, 15, 18, 21 because of
>>the size of hr array in filename
>> so the data is stored in netcdf in different time order (0, 12, 15,
>>18, 21, 3, 6), which I want to correct.
>>
>> Could you please kindly help me to sort out this issue.
>>
>> Regards,
>> Prabhakar
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> 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 Tue Apr 27 20:56:11 2010

This archive was generated by hypermail 2.1.8 : Thu Apr 29 2010 - 08:05:27 MDT