; NCL 6.4 0 required ;; If you do not have then uncomment the following. ;; load "./calculate_monthly_values.640.ncl" ;; ;---------------------------------------- ;---COMPUTE MONTHLY MEANS FROM DAILY DATA ;---------------------------------------- ;---File information (netCDF) diri = "./" ; input directory fili = systemfunc("cd "+diri+" ; ls AOT*20[0-1][1-9].nc") pthi = diri + fili nfili= dimsizes(fili) print(fili) print("---") print("nfili="+nfili) print("---") ;---Open and aggregate all files in specified path f = addfiles(pthi,"r") a5 = f[:]->AOT_500 a6 = f[:]->AOT_675 printVarSummary(a5) print("---") printVarSummary(a6) print("---") ;---Output convenience; make _FillValue nice a5@_FillValue = -999.0 a6@_FillValue = -999.0 ;---Calculate monthly values ;---https://www.ncl.ucar.edu/Document/Functions/Contributed/calculate_monthly_values.shtml opt = True opt@nval_crit = 10 ; require at least 10 values for the "avg" is calculated. ; arbitrary set by user a5MonAvg = calculate_monthly_values(a5, "avg", 0, opt) a6MonAvg = calculate_monthly_values(a6, "avg", 0, opt) ;---time for netCDF time = a5MonAvg&time time&time= time printVarSummary(time) print("---") ;---https://www.ncl.ucar.edu/Document/Functions/Built-in/cd_calendar.shtml yyyymm = cd_calendar(a5MonAvg&time,-1) yyyymm!0 = "time" yyyymm&time = time print(yyyymm) print("---") printVarSummary(a5MonAvg) print("---") printVarSummary(a6MonAvg) print("-------------------------------------") print(yyyymm+" "+a5MonAvg+" "+a6MonAvg) print("---") ;---Write to ascii ".txt" txt_data = yyyymm + sprintf("%12.5f",a5MonAvg) + sprintf("%12.5f",a6MonAvg) dtxt = "./" ftxt = "a5a6.2005-2014.txt" ptxt = dtxt+ftxt system("/bin/rm -f "+ptxt) ; rm pre-existing file (if present) asciiwrite(ptxt, txt_data) ;---Write to netCDF dnc = "./" fnc = "a5a6.2005-2014.nc" pnc = dnc +fnc system("/bin/rm -f "+pnc) ; rm pre-existing file (if present) ncdf = addfile(pnc ,"c") ; open output netCDF file ;=================================================================== ; create global attributes of the file (optional) ;=================================================================== fAtt = True ; assign file attributes fAtt@title = "AOT MONTHLY VALUES" 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->yyyymm = yyyymm ncdf->AOT_500_MonMean = a5MonAvg ncdf->AOT_675_MonMean = a6MonAvg