[ncl-talk] *addfiles* function to span multiple files

Alan Brammer abrammer at albany.edu
Tue Aug 16 08:52:58 MDT 2016


Old thread and probably solved, but for the archive a simple alternative to Dennis’s function would be

Rather that sort the return of the below yourself
>  ls ./simulation*.nc
use :
>  ls -v ./simulation*.nc


probably depends on your  ls version so the function may be more generic. 

man ls 
...
    -v     natural sort  of  (version)  numbers  within  text 
...


e.g. 
 ~>ls -1 fil*
file1
file10
file2
file3
~>ls -1v fil*
file1
file2
file3
file10




##############################
Alan Brammer,
Post-Doc Researcher

Department of Atmospheric and Environmental Sciences,
University at Albany, State University of New York, Albany, NY, 12222
abrammer at albany.edu
##############################

> On 12 Aug 2016, at 08:52, Dennis Shea <shea at ucar.edu> wrote:
> 
> NCL uses the order returned by {u/li}nix 'ls' command.
> 
> %>  ls ./simulation*.nc
> 
> ---
> So, if the files are named as follows
> 
>     simulation_001.nc <http://simulation_001.nc/>
>     simulation_002.nc <http://simulation_002.nc/>
>     ...
>     simulation_100.nc <http://simulation_100.nc/>
> 
> you will get this order and you can use the:
>     
>     diri = "./"   ; input directory
>     fili   = systemfunc("cd "+diri+" ; ls simulation*nc")
>     f      = addfiles( diri+fili)
> 
> ==============
> 
> However, the files are named 
> 
>     simulation_1.nc <http://simulation_1.nc/>
>     simulation_2.nc <http://simulation_2.nc/>
>     ...
>     simulation_9.nc <http://simulation_9.nc/>
>     simulation_10.nc <http://simulation_10.nc/>
>     ...
>     simulation_20.nc <http://simulation_20.nc/>
>     ...
>     simulation_30.nc <http://simulation_30.nc/>
>     ...
>     ...
>     simulation_100.nc <http://simulation_100.nc/>
> 
> ====
> You will get
> 
> simulation_1.nc <http://simulation_1.nc/>
> simulation_10.nc <http://simulation_10.nc/>
> simulation_100.nc <http://simulation_100.nc/>
> simulation_2.nc <http://simulation_2.nc/>
> simulation_20.nc <http://simulation_20.nc/>
> simulation_30.nc <http://simulation_30.nc/>
> simulatiom_9.nc <http://simulatiom_9.nc/>
> 
> Likely, this is not chronological order.
> 
> ========================
> 
> I just wrote the attached function. If it is in a file named (say)  'myLib.ncl', It can be tested with:
> 
> load "./myLib.ncl"
> 
> fileName = (/ "simulation_1.nc <http://simulation_1.nc/>" ,"simulation_10.nc <http://simulation_10.nc/>" ,"simulation_100.nc <http://simulation_100.nc/>" \
>              , "simulation_2.nc <http://simulation_2.nc/>" ,"simulation_20.nc <http://simulation_20.nc/>" ,"simulation_30.nc <http://simulation_30.nc/>"  \
>              , "simulatiom_9.nc <http://simulatiom_9.nc/>" /)
>  print(fileName)
> print("============")
>  
>  filUp     =  filename_reorder_test(fileName, 2, "_.",  1)
>  filDown =  filename_reorder_test(fileName, 2, "_.", -1)
> 
>  print(fileName+"  "+ filUp+ "  "+filDown)
> 
> ++++++++++++++++++++++++++++++++++++++++++++++
> 
> In your usage case, something like:
> 
> load "./myLib.ncl"
> 
>    diri = "./"   ; input directory
>    fili  = systemfunc("cd "+diri+"  ;  ls simulation*nc")
>    
>    fili_new =  filename_reorder_test(fili, 2, "_.",  1)
>    print(fili_new)
>    print("=====")
> 
> ; read files in sorted order
> 
>    f = addfiles(diri+fili_new, "r")    
> 
>    temp = f[:]->TEMP
>    printVarSummary(temp)
>    printMinMax(temp,0)
> 
> 
>    Good luck
> 
> 
> 
> 
> 
> 
> On Fri, Aug 12, 2016 at 3:43 AM, Michele Petrini <mpetrini139 at yahoo.it <mailto:mpetrini139 at yahoo.it>> wrote:
> Dear NCL helpdesk,
> 
> I have a question re the addfiles function. I am using this function in order to read data from an ensemble of 101 NetCDF files - let's say from simulation000.nc to simulation100.nc. 
> 
> My question is, what is the order the files are read with? Can I assume is from 000 to 100 if I have 
> 
>  all_files = systemfunc <http://www.ncl.ucar.edu/Document/Functions/Built-in/systemfunc.shtml> ("ls ./simulation*.nc")
>      fall      = addfiles (all_files, "r") 
> I ask this because I had a missing value error for the last file - but simulation100.nc <http://simulation100.nc/> file seems to be ok. Do you have any clue why this could happen? 
> 
> Thank you for your help,
> 
> Best regards
> Michele Petrini
> -- 
> 
> ***
> Michele Petrini
> 
> Ph.D. student in Earth Science and Fluid Mechanics
> 
> Università degli studi di Trieste, 
> Dipartimento di Matematica e Geoscienze 
> Palazzina C - via Weiss 1, 34128 Trieste, Italy
> 
> Email: mpetrini139 at yahoo.it <mailto:mpetrini139 at yahoo.it>
> Skype: michele.petrins
> Mobile: +39 3398367372 <tel:%2B39%203398367372> 
> 
> _______________________________________________
> ncl-talk mailing list
> ncl-talk at ucar.edu <mailto:ncl-talk at ucar.edu>
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk <http://mailman.ucar.edu/mailman/listinfo/ncl-talk>
> 
> 
> <myLib.ncl><tst_reorder.ncl>_______________________________________________
> 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/20160816/5f58545a/attachment.html 


More information about the ncl-talk mailing list