undef("add_var_meta") procedure add_var_meta(x[*],time[*]:numeric,LongName[1]:string,Units[1]:string) ; cleaner code begin if (LongName.ne."") then x@long_name = LongName end if if (Units.ne."") then x@units = Units end if x!0 = "time" x&time = time end ;==================================================== ; MAIN ;==================================================== err = NhlGetErrorObjectId() setvalues err "errLevel" : "Fatal" ; only report Fatal errors end setvalues ;==================================================== diri = "/Users/shea/Data/ASCII/" fili = systemfunc("cd "+diri+" ; ls AOT_550_2*.txt") print(fili) nfili = dimsizes(fili) pthi = diri+fili ncol = 7 lskip = 1 ; skip the 1st line units = "days since 2005-01-01 00:00:0.0" ; arbitrary do nf=0,nfili-1 ; loop over each file YYYY = toint(str_get_field(pthi(nf), 3, "_.")) ; extract YEAR (YYYY) from the file name ;print("YYYY="+YYYY) data := readAsciiTable(pthi(nf),ncol,"float",lskip) ; := means overwrite array ndat = dimsizes(data(:,0)) ;time := data(:,0)-1 ;time@long_name= "time" ;time@units = "days since "+yyyy+"-01-01 00:00:0.0" ;time@calendar = "standard" ;time!0 = "time" ;time&time = time ddd := toint(data(:,0)) ; day (ddd) of current year yyyyddd := YYYY*1000 + ddd yyyymmdd := yyyyddd_to_yyyymmdd( yyyyddd ) yyyy := yyyymmdd/10000 mmdd := yyyymmdd-(yyyy*10000) mm := mmdd/100 dd := mmdd-mm*100 hh := conform_dims( ndat, 0, -1) mn := conform_dims( ndat, 0, -1) sc := conform_dims( ndat, 0, -1) time := cd_inv_calendar(yyyy,mm,dd,hh,mn,sc,units, 0) time!0 = "time" printVarSummary(time) ;print(yyyyddd+" "+yyyy+" "+sprinti("%0.2i", mm)+" "+sprinti("%0.2i", dd) \ ; +" "+sprinti("%0.2i", hh)+" "+sprinti("%0.2i", mn) \ ; +" "+sprinti("%0.2i", sc)+" "+time) add_var_meta(yyyyddd ,time, "", "year and day-of-year") add_var_meta(yyyymmdd,time, "", "year-month-day") AOT_675 := data(:,1) add_var_meta(AOT_675,time, "???", "???") AOT_500 := data(:,2) add_var_meta(AOT_500,time, "???", "???") ;=================================================================== ; http://www.ncl.ucar.edu/Applications/method_1.shtml ;=================================================================== ncDir = "./" ncFil = str_get_field(fili(nf),1,".")+".nc" ncPth = ncDir+ncFil system("/bin/rm -f "+ncPth) ; remove any pre-existing file ncdf = addfile(ncPth ,"c") ; open output netCDF file ;=================================================================== ; create global attributes of the file (optional) ;=================================================================== fAtt = True ; assign file attributes fAtt@title = "AOT" ;fAtt@source_file = fili(nf) fAtt@Conventions = "None" fAtt@creation_date = systemfunc ("date") fileattdef( ncdf, fAtt ) ; copy file attributes ;=================================================================== ; make time an UNLIMITED dimension ;=================================================================== filedimdef(ncdf,"time",-1,True) ;=================================================================== ; output variables directly; NCL will call appropriate functions ; to write the meta data associated with each variable ;=================================================================== ncdf->time = time ncdf->yyyyddd = yyyyddd ncdf->yyyymmdd= yyyymmdd ncdf->AOT_675 = AOT_675 ncdf->AOT_500 = AOT_500 end do ; nf