GeoTIFF to NetCDF with time variable enabled - gdal

I have thousand GeoTIFF files (monthly precipitation from 1981) and would like to convert it into single NetCDF with time dimension enabled.
I have read various similar thread in StackOverflow and GIS StackExchange, but could not find the best solution for my case, either the script is too complicated (I don't have programming skill) or the solution didn't cover time dimension enabled.
What I have done is:
Convert all TIF in a folder to GRIB2 using GDAL: for i in *.tif; do gdal_translate -of GRIB $i ../GRIB/$i.grb2; done
Convert GRIB2 to NetCDF using CDO: for i in *.grb2; do cdo -r -f nc copy $i ../NetCDF/$i.nc; done
Merge all NC in a folder using NCO: ncrcat *.nc merge.nc
Then check the file: ncdump -h merge.nc I got this
netcdf merge {
dimensions:
time = UNLIMITED ; // (467 currently)
lon = 920 ;
lat = 339 ;
variables:
double time(time) ;
time:standard_name = "time" ;
time:units = "minutes since 1970-1-1 00:00:00" ;
time:calendar = "proleptic_gregorian" ;
time:axis = "T" ;
double lon(lon) ;
lon:standard_name = "longitude" ;
lon:long_name = "longitude" ;
lon:units = "degrees_east" ;
lon:axis = "X" ;
double lat(lat) ;
lat:standard_name = "latitude" ;
lat:long_name = "latitude" ;
lat:units = "degrees_north" ;
lat:axis = "Y" ;
float param255.255.0(time, lat, lon) ;
// global attributes:
:CDI = "Climate Data Interface version 1.9.8 (https://mpimet.mpg.de/cdi)" ;
:Conventions = "CF-1.6" ;
:history = "Tue Jul 7 09:49:30 2020: ncrcat idn_cli_chirps-v2.0.1981.01.tif.grb2.nc idn_cli_chirps-v2.0.1981.02.tif.grb2.nc idn_cli_chirps-v2.0.1981.03.tif.grb2.nc idn_cli_chirps-v2.0.1981.04.tif.grb2.nc idn_cli_chirps-v2.0.1981.05.tif.grb2.nc idn_cli_chirps-v2.0.1981.06.tif.grb2.nc idn_cli_chirps-v2.0.1981.07.tif.grb2.nc idn_cli_chirps-v2.0.1981.08.tif.grb2.nc idn_cli_chirps-v2.0.1981.09.tif.grb2.nc idn_cli_chirps-v2.0.1981.10.tif.grb2.nc idn_cli_chirps-v2.0.1981.11.tif.grb2.nc idn_cli_chirps-v2.0.1981.12.tif.grb2.nc idn_cli_chirps-v2.0.1982.01.tif.grb2.nc idn_cli_chirps-v2.0.1982.02.tif.grb2.nc idn_cli_chirps-v2.0.1982.03.tif.grb2.nc idn_cli_chirps-v2.0.1982.04.tif.grb2.nc idn_cli_chirps-v2.0.1982.05.tif.grb2.nc idn_cli_chirps-v2.0.1982.06.tif.grb2.nc idn_cli_chirps-v2.0.1982.07.tif.grb2.nc idn_cli_chirps-v2.0.1982.08.tif.grb2.nc idn_cli_chirps-v2.0.1982.09.tif.grb2.nc idn_cli_chirps-v2.0.1982.10.tif.grb2.nc idn_cli_chirps-v2.0.1982.11.tif.grb2.nc idn_cli_chirps-v2.0.1982.12.tif.grb2.nc idn_cli_chirps-v2.0.1983.01.tif.grb2.nc idn_cli_chirps-v2.0.1983.02.tif.grb2.nc idn_cli_chirps-v2.0.1983.03.tif.grb2.nc idn_cli_chirps-v2.0.1983.04.tif.grb2.nc idn_cli_chirps-v2.0.1983.05.tif.grb2.nc idn_cli_chirps-v2.0.1983.06.tif.grb2.nc idn_cli_chirps-v2.0.1983.07.tif.grb2.nc idn_cli_chirps-v2.0.1983.08.tif.grb2.nc idn_cli_chirps-v2.0.1983.09.tif.grb2.nc idn_cli_chirps-v2.0.1983.10.tif.grb2.nc idn_cli_chirps-v2.0.1983.11.tif.grb2.nc idn_cli_chirps-v2.0.1983.12.tif.grb2.nc idn_cli_chirps-v2.0.1984.01.tif.grb2.nc idn_cli_chirps-v2.0.1984.02.tif.grb2.nc idn_cli_chirps-v2.0.1984.03.tif.grb2.nc idn_cli_chirps-v2.0.1984.04.tif.grb2.nc idn_cli_chirps-v2.0.1984.05.tif.grb2.nc idn_cli_chirps-v2.0.1984.06.tif.grb2.nc idn_cli_chirps-v2.0.1984.07.tif.grb2.nc idn_cli_chirps-v2.0.1984.08.tif.grb2.nc idn_cli_chirps-v2.0.1984.09.tif.grb2.nc idn_cli_chirps-v2.0.1984.10.tif.grb2.nc idn_cli_chirps-v2.0.1984.11.tif.grb2.nc idn_cli_chirps-v2.0.1984.12.tif.grb2.nc idn_cli_chirps-v2.0.1985.01.tif.grb2.nc idn_cli_chirps-v2.0.1985.02.tif.grb2.nc idn_cli_chirps-v2.0.1985.03.tif.grb2.nc idn_cli_chirps-v2.0.1985.04.tif.grb2.nc idn_cli_chirps-v2.0.1985.05.tif.grb2.nc idn_cli_chirps-v2.0.1985.06.tif.grb2.nc idn_cli_chirps-v2.0.1985.07.tif.grb2.nc idn_cli_chirps-v2.0.1985.08.tif.grb2.nc idn_cli_chirps-v2.0.1985.09.tif.grb2.nc idn_cli_chirps-v2.0.1985.10.tif.grb2.nc idn_cli_chirps-v2.0.1985.11.tif.grb2.nc idn_cli_chirps-v2.0.1985.12.tif.grb2.nc idn_cli_chirps-v2.0.1986.01.tif.grb2.nc idn_cli_chirps-v2.0.1986.02.tif.grb2.nc idn_cli_chirps-v2.0.1986.03.tif.grb2.nc idn_cli_chirps-v2.0.1986.04.tif.grb2.nc idn_cli_chirps-v2.0.1986.05.tif.grb2.nc idn_cli_chirps-v2.0.1986.06.tif.grb2.nc idn_cli_chirps-v2.0.1986.07.tif.grb2.nc idn_cli_chirps-v2.0.1986.08.tif.grb2.nc idn_cli_chirps-v2.0.1986.09.tif.grb2.nc idn_cli_chirps-v2.0.1986.10.tif.grb2.nc idn_cli_chirps-v2.0.1986.11.tif.grb2.nc idn_cli_chirps-v2.0.1986.12.tif.grb2.nc idn_cli_chirps-v2.0.1987.01.tif.grb2.nc idn_cli_chirps-v2.0.1987.02.tif.grb2.nc idn_cli_chirps-v2.0.1987.03.tif.grb2.nc idn_cli_chirps-v2.0.1987.04.tif.grb2.nc idn_cli_chirps-v2.0.1987.05.tif.grb2.nc idn_cli_chirps-v2.0.1987.06.tif.grb2.nc idn_cli_chirps-v2.0.1987.07.tif.grb2.nc idn_cli_chirps-v2.0.1987.08.tif.grb2.nc idn_cli_chirps-v2.0.1987.09.tif.grb2.nc idn_cli_chirps-v2.0.1987.10.tif.grb2.nc idn_cli_chirps-v2.0.1987.11.tif.grb2.nc idn_cli_chirps-v2.0.1987.12.tif.grb2.nc idn_cli_chirps-v2.0.1988.01.tif.grb2.nc idn_cli_chirps-v2.0.1988.02.tif.grb2.nc idn_cli_chirps-v2.0.1988.03.tif.grb2.nc idn_cli_chirps-v2.0.1988.04.tif.grb2.nc idn_cli_chirps-v2.0.1988.05.tif.grb2.nc idn_cli_chirps-v2.0.1988.06.tif.grb2.nc idn_cli_chirps-v2.0.1988.07.tif.grb2.nc idn_cli_chirps-v2.0.1988.08.tif.grb2.nc idn_cli_chirps-v2.0.1988.09.tif.grb2.nc idn_cli_chirps-v2.0.1988.10.tif.grb2.nc idn_cli_chirps-v2.0.1988.11.tif.grb2.nc idn_cli_chirps-v2.0.1988.12.tif.grb2.nc idn_cli_chirps-v2.0.1989.01.tif.grb2.nc idn_cli_chirps-v2.0.1989.02.tif.grb2.nc idn_cli_chirps-v2.0.1989.03.tif.grb2.nc idn_cli_chirps-v2.0.1989.04.tif.grb2.nc idn_cli_chirps-v2.0.1989.05.tif.grb2.nc idn_cli_chirps-v2.0.1989.06.tif.grb2.nc idn_cli_chirps-v2.0.1989.07.tif.grb2.nc idn_cli_chirps-v2.0.1989.08.tif.grb2.nc idn_cli_chirps-v2.0.1989.09.tif.grb2.nc idn_cli_chirps-v2.0.1989.10.tif.grb2.nc idn_cli_chirps-v2.0.1989.11.tif.grb2.nc idn_cli_chirps-v2.0.1989.12.tif.grb2.nc idn_cli_chirps-v2.0.1990.01.tif.grb2.nc idn_cli_chirps-v2.0.1990.02.tif.grb2.nc idn_cli_chirps-v2.0.1990.03.tif.grb2.nc idn_cli_chirps-v2.0.1990.04.tif.grb2.nc idn_cli_chirps-v2.0.1990.05.tif.grb2.nc idn_cli_chirps-v2.0.1990.06.tif.grb2.nc idn_cli_chirps-v2.0.1990.07.tif.grb2.nc idn_cli_chirps-v2.0.1990.08.tif.grb2.nc idn_cli_chirps-v2.0.1990.09.tif.grb2.nc idn_cli_chirps-v2.0.1990.10.tif.grb2.nc idn_cli_chirps-v2.0.1990.11.tif.grb2.nc idn_cli_chirps-v2.0.1990.12.tif.grb2.nc idn_cli_chirps-v2.0.1991.01.tif.grb2.nc idn_cli_chirps-v2.0.1991.02.tif.grb2.nc idn_cli_chirps-v2.0.1991.03.tif.grb2.nc idn_cli_chirps-v2.0.1991.04.tif.grb2.nc idn_cli_chirps-v2.0.1991.05.tif.grb2.nc idn_cli_chirps-v2.0.1991.06.tif.grb2.nc idn_cli_chirps-v2.0.1991.07.tif.grb2.nc idn_cli_chirps-v2.0.1991.08.tif.grb2.nc idn_cli_chirps-v2.0.1991.09.tif.grb2.nc idn_cli_chirps-v2.0.1991.10.tif.grb2.nc idn_cli_chirps-v2.0.1991.11.tif.grb2.nc idn_cli_chirps-v2.0.1992.01.tif.grb2.nc idn_cli_chirps-v2.0.1992.02.tif.grb2.nc idn_cli_chirps-v2.0.1992.03.tif.grb2.nc idn_cli_chirps-v2.0.1992.04.tif.grb2.nc idn_cli_chirps-v2.0.1992.05.tif.grb2.nc idn_cli_chirps-v2.0.1992.06.tif.grb2.nc idn_cli_chirps-v2.0.1992.07.tif.grb2.nc idn_cli_chirps-v2.0.1992.08.tif.grb2.nc idn_cli_chirps-v2.0.1992.09.tif.grb2.nc idn_cli_chirps-v2.0.1992.10.tif.grb2.nc idn_cli_chirps-v2.0.1992.11.tif.grb2.nc idn_cli_chirps-v2.0.1992.12.tif.grb2.nc idn_cli_chirps-v2.0.1993.01.tif.grb2.nc idn_cli_chirps-v2.0.1993.02.tif.grb2.nc idn_cli_chirps-v2.0.1993.03.tif.grb2.nc idn_cli_chirps-v2.0.1993.04.tif.grb2.nc idn_cli_chirps-v2.0.1993.05.tif.grb2.nc idn_cli_chirps-v2.0.1993.06.tif.grb2.nc idn_cli_chirps-v2.0.1993.07.tif.grb2.nc idn_cli_chirps-v2.0.1993.08.tif.grb2.nc idn_cli_chirps-v2.0.1993.09.tif.grb2.nc idn_cli_chirps-v2.0.1993.10.tif.grb2.nc idn_cli_chirps-v2.0.1993.11.tif.grb2.nc idn_cli_chirps-v2.0.1993.12.tif.grb2.nc idn_cli_chirps-v2.0.1994.01.tif.grb2.nc idn_cli_chirps-v2.0.1994.02.tif.grb2.nc idn_cli_chirps-v2.0.1994.03.tif.grb2.nc idn_cli_chirps-v2.0.1994.04.tif.grb2.nc idn_cli_chirps-v2.0.1994.05.tif.grb2.nc idn_cli_chirps-v2.0.1994.06.tif.grb2.nc idn_cli_chirps-v2.0.1994.07.tif.grb2.nc idn_cli_chirps-v2.0.1994.08.tif.grb2.nc idn_cli_chirps-v2.0.1994.09.tif.grb2.nc idn_cli_chirps-v2.0.1994.10.tif.grb2.nc idn_cli_chirps-v2.0.1994.11.tif.grb2.nc idn_cli_chirps-v2.0.1994.12.tif.grb2.nc idn_cli_chirps-v2.0.1995.01.tif.grb2.nc idn_cli_chirps-v2.0.1995.02.tif.grb2.nc idn_cli_chirps-v2.0.1995.03.tif.grb2.nc idn_cli_chirps-v2.0.1995.04.tif.grb2.nc idn_cli_chirps-v2.0.1995.05.tif.grb2.nc idn_cli_chirps-v2.0.1995.06.tif.grb2.nc idn_cli_chirps-v2.0.1995.07.tif.grb2.nc idn_cli_chirps-v2.0.1995.08.tif.grb2.nc idn_cli_chirps-v2.0.1995.09.tif.grb2.nc idn_cli_chirps-v2.0.1995.10.tif.grb2.nc idn_cli_chirps-v2.0.1995.11.tif.grb2.nc idn_cli_chirps-v2.0.1995.12.tif.grb2.nc idn_cli_chirps-v2.0.1996.01.tif.grb2.nc idn_cli_chirps-v2.0.1996.02.tif.grb2.nc idn_cli_chirps-v2.0.1996.03.tif.grb2.nc idn_cli_chirps-v2.0.1996.04.tif.grb2.nc idn_cli_chirps-v2.0.1996.05.tif.grb2.nc idn_cli_chirps-v2.0.1996.06.tif.grb2.nc idn_cli_chirps-v2.0.1996.07.tif.grb2.nc idn_cli_chirps-v2.0.1996.08.tif.grb2.nc idn_cli_chirps-v2.0.1996.09.tif.grb2.nc idn_cli_chirps-v2.0.1996.10.tif.grb2.nc idn_cli_chirps-v2.0.1996.11.tif.grb2.nc idn_cli_chirps-v2.0.1996.12.tif.grb2.nc idn_cli_chirps-v2.0.1997.01.tif.grb2.nc idn_cli_chirps-v2.0.1997.02.tif.grb2.nc idn_cli_chirps-v2.0.1997.03.tif.grb2.nc idn_cli_chirps-v2.0.1997.04.tif.grb2.nc idn_cli_chirps-v2.0.1997.05.tif.grb2.nc idn_cli_chirps-v2.0.1997.06.tif.grb2.nc idn_cli_chirps-v2.0.1997.07.tif.grb2.nc idn_cli_chirps-v2.0.1997.08.tif.grb2.nc idn_cli_chirps-v2.0.1997.09.tif.grb2.nc idn_cli_chirps-v2.0.1997.10.tif.grb2.nc idn_cli_chirps-v2.0.1997.11.tif.grb2.nc idn_cli_chirps-v2.0.1997.12.tif.grb2.nc idn_cli_chirps-v2.0.1998.01.tif.grb2.nc idn_cli_chirps-v2.0.1998.02.tif.grb2.nc idn_cli_chirps-v2.0.1998.03.tif.grb2.nc idn_cli_chirps-v2.0.1998.04.tif.grb2.nc idn_cli_chirps-v2.0.1998.05.tif.grb2.nc idn_cli_chirps-v2.0.1998.06.tif.grb2.nc idn_cli_chirps-v2.0.1998.07.tif.grb2.nc idn_cli_chirps-v2.0.1998.08.tif.grb2.nc idn_cli_chirps-v2.0.1998.09.tif.grb2.nc idn_cli_chirps-v2.0.1998.10.tif.grb2.nc idn_cli_chirps-v2.0.1998.11.tif.grb2.nc idn_cli_chirps-v2.0.1998.12.tif.grb2.nc idn_cli_chirps-v2.0.1999.01.tif.grb2.nc idn_cli_chirps-v2.0.1999.02.tif.grb2.nc idn_cli_chirps-v2.0.1999.03.tif.grb2.nc idn_cli_chirps-v2.0.1999.04.tif.grb2.nc idn_cli_chirps-v2.0.1999.05.tif.grb2.nc idn_cli_chirps-v2.0.1999.06.tif.grb2.nc idn_cli_chirps-v2.0.1999.07.tif.grb2.nc idn_cli_chirps-v2.0.1999.08.tif.grb2.nc idn_cli_chirps-v2.0.1999.09.tif.grb2.nc idn_cli_chirps-v2.0.1999.10.tif.grb2.nc idn_cli_chirps-v2.0.1999.11.tif.grb2.nc idn_cli_chirps-v2.0.1999.12.tif.grb2.nc idn_cli_chirps-v2.0.2000.01.tif.grb2.nc idn_cli_chirps-v2.0.2000.02.tif.grb2.nc idn_cli_chirps-v2.0.2000.03.tif.grb2.nc idn_cli_chirps-v2.0.2000.04.tif.grb2.nc idn_cli_chirps-v2.0.2000.05.tif.grb2.nc idn_cli_chirps-v2.0.2000.06.tif.grb2.nc idn_cli_chirps-v2.0.2000.07.tif.grb2.nc idn_cli_chirps-v2.0.2000.08.tif.grb2.nc idn_cli_chirps-v2.0.2000.09.tif.grb2.nc idn_cli_chirps-v2.0.2000.10.tif.grb2.nc idn_cli_chirps-v2.0.2000.11.tif.grb2.nc idn_cli_chirps-v2.0.2000.12.tif.grb2.nc idn_cli_chirps-v2.0.2001.01.tif.grb2.nc idn_cli_chirps-v2.0.2001.02.tif.grb2.nc idn_cli_chirps-v2.0.2001.03.tif.grb2.nc idn_cli_chirps-v2.0.2001.04.tif.grb2.nc idn_cli_chirps-v2.0.2001.05.tif.grb2.nc idn_cli_chirps-v2.0.2001.06.tif.grb2.nc idn_cli_chirps-v2.0.2001.07.tif.grb2.nc idn_cli_chirps-v2.0.2001.08.tif.grb2.nc idn_cli_chirps-v2.0.2001.09.tif.grb2.nc idn_cli_chirps-v2.0.2001.10.tif.grb2.nc idn_cli_chirps-v2.0.2001.11.tif.grb2.nc idn_cli_chirps-v2.0.2001.12.tif.grb2.nc idn_cli_chirps-v2.0.2002.01.tif.grb2.nc idn_cli_chirps-v2.0.2002.02.tif.grb2.nc idn_cli_chirps-v2.0.2002.03.tif.grb2.nc idn_cli_chirps-v2.0.2002.04.tif.grb2.nc idn_cli_chirps-v2.0.2002.05.tif.grb2.nc idn_cli_chirps-v2.0.2002.06.tif.grb2.nc idn_cli_chirps-v2.0.2002.07.tif.grb2.nc idn_cli_chirps-v2.0.2002.08.tif.grb2.nc idn_cli_chirps-v2.0.2002.09.tif.grb2.nc idn_cli_chirps-v2.0.2002.10.tif.grb2.nc idn_cli_chirps-v2.0.2002.11.tif.grb2.nc idn_cli_chirps-v2.0.2002.12.tif.grb2.nc idn_cli_chirps-v2.0.2003.01.tif.grb2.nc idn_cli_chirps-v2.0.2003.02.tif.grb2.nc idn_cli_chirps-v2.0.2003.03.tif.grb2.nc idn_cli_chirps-v2.0.2003.04.tif.grb2.nc idn_cli_chirps-v2.0.2003.05.tif.grb2.nc idn_cli_chirps-v2.0.2003.06.tif.grb2.nc idn_cli_chirps-v2.0.2003.07.tif.grb2.nc idn_cli_chirps-v2.0.2003.08.tif.grb2.nc idn_cli_chirps-v2.0.2003.09.tif.grb2.nc idn_cli_chirps-v2.0.2003.10.tif.grb2.nc idn_cli_chirps-v2.0.2003.11.tif.grb2.nc idn_cli_chirps-v2.0.2003.12.tif.grb2.nc idn_cli_chirps-v2.0.2004.01.tif.grb2.nc idn_cli_chirps-v2.0.2004.02.tif.grb2.nc idn_cli_chirps-v2.0.2004.03.tif.grb2.nc idn_cli_chirps-v2.0.2004.04.tif.grb2.nc idn_cli_chirps-v2.0.2004.05.tif.grb2.nc idn_cli_chirps-v2.0.2004.06.tif.grb2.nc idn_cli_chirps-v2.0.2004.07.tif.grb2.nc idn_cli_chirps-v2.0.2004.08.tif.grb2.nc idn_cli_chirps-v2.0.2004.09.tif.grb2.nc idn_cli_chirps-v2.0.2004.10.tif.grb2.nc idn_cli_chirps-v2.0.2004.11.tif.grb2.nc idn_cli_chirps-v2.0.2004.12.tif.grb2.nc idn_cli_chirps-v2.0.2005.01.tif.grb2.nc idn_cli_chirps-v2.0.2005.02.tif.grb2.nc idn_cli_chirps-v2.0.2005.03.tif.grb2.nc idn_cli_chirps-v2.0.2005.04.tif.grb2.nc idn_cli_chirps-v2.0.2005.05.tif.grb2.nc idn_cli_chirps-v2.0.2005.06.tif.grb2.nc idn_cli_chirps-v2.0.2005.07.tif.grb2.nc idn_cli_chirps-v2.0.2005.08.tif.grb2.nc idn_cli_chirps-v2.0.2005.09.tif.grb2.nc idn_cli_chirps-v2.0.2005.10.tif.grb2.nc idn_cli_chirps-v2.0.2005.11.tif.grb2.nc idn_cli_chirps-v2.0.2005.12.tif.grb2.nc idn_cli_chirps-v2.0.2006.01.tif.grb2.nc idn_cli_chirps-v2.0.2006.02.tif.grb2.nc idn_cli_chirps-v2.0.2006.05.tif.grb2.nc idn_cli_chirps-v2.0.2006.06.tif.grb2.nc idn_cli_chirps-v2.0.2006.07.tif.grb2.nc idn_cli_chirps-v2.0.2006.08.tif.grb2.nc idn_cli_chirps-v2.0.2006.09.tif.grb2.nc idn_cli_chirps-v2.0.2006.10.tif.grb2.nc idn_cli_chirps-v2.0.2006.11.tif.grb2.nc idn_cli_chirps-v2.0.2006.12.tif.grb2.nc idn_cli_chirps-v2.0.2007.01.tif.grb2.nc idn_cli_chirps-v2.0.2007.02.tif.grb2.nc idn_cli_chirps-v2.0.2007.03.tif.grb2.nc idn_cli_chirps-v2.0.2007.04.tif.grb2.nc idn_cli_chirps-v2.0.2007.05.tif.grb2.nc idn_cli_chirps-v2.0.2007.06.tif.grb2.nc idn_cli_chirps-v2.0.2007.07.tif.grb2.nc idn_cli_chirps-v2.0.2007.08.tif.grb2.nc idn_cli_chirps-v2.0.2007.09.tif.grb2.nc idn_cli_chirps-v2.0.2007.10.tif.grb2.nc idn_cli_chirps-v2.0.2007.11.tif.grb2.nc idn_cli_chirps-v2.0.2007.12.tif.grb2.nc idn_cli_chirps-v2.0.2008.01.tif.grb2.nc idn_cli_chirps-v2.0.2008.02.tif.grb2.nc idn_cli_chirps-v2.0.2008.03.tif.grb2.nc idn_cli_chirps-v2.0.2008.04.tif.grb2.nc idn_cli_chirps-v2.0.2008.05.tif.grb2.nc idn_cli_chirps-v2.0.2008.06.tif.grb2.nc idn_cli_chirps-v2.0.2008.07.tif.grb2.nc idn_cli_chirps-v2.0.2008.08.tif.grb2.nc idn_cli_chirps-v2.0.2008.09.tif.grb2.nc idn_cli_chirps-v2.0.2008.10.tif.grb2.nc idn_cli_chirps-v2.0.2008.11.tif.grb2.nc idn_cli_chirps-v2.0.2008.12.tif.grb2.nc idn_cli_chirps-v2.0.2009.01.tif.grb2.nc idn_cli_chirps-v2.0.2009.02.tif.grb2.nc idn_cli_chirps-v2.0.2009.03.tif.grb2.nc idn_cli_chirps-v2.0.2009.04.tif.grb2.nc idn_cli_chirps-v2.0.2009.05.tif.grb2.nc idn_cli_chirps-v2.0.2009.06.tif.grb2.nc idn_cli_chirps-v2.0.2009.07.tif.grb2.nc idn_cli_chirps-v2.0.2009.08.tif.grb2.nc idn_cli_chirps-v2.0.2009.09.tif.grb2.nc idn_cli_chirps-v2.0.2009.10.tif.grb2.nc idn_cli_chirps-v2.0.2009.11.tif.grb2.nc idn_cli_chirps-v2.0.2009.12.tif.grb2.nc idn_cli_chirps-v2.0.2010.01.tif.grb2.nc idn_cli_chirps-v2.0.2010.02.tif.grb2.nc idn_cli_chirps-v2.0.2010.03.tif.grb2.nc idn_cli_chirps-v2.0.2010.04.tif.grb2.nc idn_cli_chirps-v2.0.2010.05.tif.grb2.nc idn_cli_chirps-v2.0.2010.06.tif.grb2.nc idn_cli_chirps-v2.0.2010.07.tif.grb2.nc idn_cli_chirps-v2.0.2010.08.tif.grb2.nc idn_cli_chirps-v2.0.2010.09.tif.grb2.nc idn_cli_chirps-v2.0.2010.10.tif.grb2.nc idn_cli_chirps-v2.0.2010.11.tif.grb2.nc idn_cli_chirps-v2.0.2010.12.tif.grb2.nc idn_cli_chirps-v2.0.2011.01.tif.grb2.nc idn_cli_chirps-v2.0.2011.02.tif.grb2.nc idn_cli_chirps-v2.0.2011.03.tif.grb2.nc idn_cli_chirps-v2.0.2011.04.tif.grb2.nc idn_cli_chirps-v2.0.2011.05.tif.grb2.nc idn_cli_chirps-v2.0.2011.06.tif.grb2.nc idn_cli_chirps-v2.0.2011.07.tif.grb2.nc idn_cli_chirps-v2.0.2011.08.tif.grb2.nc idn_cli_chirps-v2.0.2011.09.tif.grb2.nc idn_cli_chirps-v2.0.2011.10.tif.grb2.nc idn_cli_chirps-v2.0.2011.11.tif.grb2.nc idn_cli_chirps-v2.0.2011.12.tif.grb2.nc idn_cli_chirps-v2.0.2012.01.tif.grb2.nc idn_cli_chirps-v2.0.2012.02.tif.grb2.nc idn_cli_chirps-v2.0.2012.03.tif.grb2.nc idn_cli_chirps-v2.0.2012.04.tif.grb2.nc idn_cli_chirps-v2.0.2012.05.tif.grb2.nc idn_cli_chirps-v2.0.2012.06.tif.grb2.nc idn_cli_chirps-v2.0.2012.07.tif.grb2.nc idn_cli_chirps-v2.0.2012.08.tif.grb2.nc idn_cli_chirps-v2.0.2012.09.tif.grb2.nc idn_cli_chirps-v2.0.2012.10.tif.grb2.nc idn_cli_chirps-v2.0.2012.11.tif.grb2.nc idn_cli_chirps-v2.0.2012.12.tif.grb2.nc idn_cli_chirps-v2.0.2013.01.tif.grb2.nc idn_cli_chirps-v2.0.2013.02.tif.grb2.nc idn_cli_chirps-v2.0.2013.03.tif.grb2.nc idn_cli_chirps-v2.0.2013.04.tif.grb2.nc idn_cli_chirps-v2.0.2013.05.tif.grb2.nc idn_cli_chirps-v2.0.2013.06.tif.grb2.nc idn_cli_chirps-v2.0.2013.07.tif.grb2.nc idn_cli_chirps-v2.0.2013.08.tif.grb2.nc idn_cli_chirps-v2.0.2013.09.tif.grb2.nc idn_cli_chirps-v2.0.2013.10.tif.grb2.nc idn_cli_chirps-v2.0.2013.11.tif.grb2.nc idn_cli_chirps-v2.0.2013.12.tif.grb2.nc idn_cli_chirps-v2.0.2014.01.tif.grb2.nc idn_cli_chirps-v2.0.2014.02.tif.grb2.nc idn_cli_chirps-v2.0.2014.03.tif.grb2.nc idn_cli_chirps-v2.0.2014.04.tif.grb2.nc idn_cli_chirps-v2.0.2014.05.tif.grb2.nc idn_cli_chirps-v2.0.2014.06.tif.grb2.nc idn_cli_chirps-v2.0.2014.07.tif.grb2.nc idn_cli_chirps-v2.0.2014.08.tif.grb2.nc idn_cli_chirps-v2.0.2014.09.tif.grb2.nc idn_cli_chirps-v2.0.2014.10.tif.grb2.nc idn_cli_chirps-v2.0.2014.11.tif.grb2.nc idn_cli_chirps-v2.0.2014.12.tif.grb2.nc idn_cli_chirps-v2.0.2015.01.tif.grb2.nc idn_cli_chirps-v2.0.2015.02.tif.grb2.nc idn_cli_chirps-v2.0.2015.03.tif.grb2.nc idn_cli_chirps-v2.0.2015.04.tif.grb2.nc idn_cli_chirps-v2.0.2015.06.tif.grb2.nc idn_cli_chirps-v2.0.2015.07.tif.grb2.nc idn_cli_chirps-v2.0.2015.08.tif.grb2.nc idn_cli_chirps-v2.0.2015.09.tif.grb2.nc idn_cli_chirps-v2.0.2015.10.tif.grb2.nc idn_cli_chirps-v2.0.2015.11.tif.grb2.nc idn_cli_chirps-v2.0.2015.12.tif.grb2.nc idn_cli_chirps-v2.0.2016.01.tif.grb2.nc idn_cli_chirps-v2.0.2016.02.tif.grb2.nc idn_cli_chirps-v2.0.2016.03.tif.grb2.nc idn_cli_chirps-v2.0.2016.04.tif.grb2.nc idn_cli_chirps-v2.0.2016.05.tif.grb2.nc idn_cli_chirps-v2.0.2016.06.tif.grb2.nc idn_cli_chirps-v2.0.2016.07.tif.grb2.nc idn_cli_chirps-v2.0.2016.08.tif.grb2.nc idn_cli_chirps-v2.0.2016.09.tif.grb2.nc idn_cli_chirps-v2.0.2016.10.tif.grb2.nc idn_cli_chirps-v2.0.2016.11.tif.grb2.nc idn_cli_chirps-v2.0.2016.12.tif.grb2.nc idn_cli_chirps-v2.0.2017.01.tif.grb2.nc idn_cli_chirps-v2.0.2017.02.tif.grb2.nc idn_cli_chirps-v2.0.2017.03.tif.grb2.nc idn_cli_chirps-v2.0.2017.04.tif.grb2.nc idn_cli_chirps-v2.0.2017.05.tif.grb2.nc idn_cli_chirps-v2.0.2017.06.tif.grb2.nc idn_cli_chirps-v2.0.2017.07.tif.grb2.nc idn_cli_chirps-v2.0.2017.08.tif.grb2.nc idn_cli_chirps-v2.0.2017.09.tif.grb2.nc idn_cli_chirps-v2.0.2017.10.tif.grb2.nc idn_cli_chirps-v2.0.2017.11.tif.grb2.nc idn_cli_chirps-v2.0.2017.12.tif.grb2.nc idn_cli_chirps-v2.0.2018.01.tif.grb2.nc idn_cli_chirps-v2.0.2018.02.tif.grb2.nc idn_cli_chirps-v2.0.2018.03.tif.grb2.nc idn_cli_chirps-v2.0.2018.04.tif.grb2.nc idn_cli_chirps-v2.0.2018.05.tif.grb2.nc idn_cli_chirps-v2.0.2018.06.tif.grb2.nc idn_cli_chirps-v2.0.2018.07.tif.grb2.nc idn_cli_chirps-v2.0.2018.08.tif.grb2.nc idn_cli_chirps-v2.0.2018.09.tif.grb2.nc idn_cli_chirps-v2.0.2018.10.tif.grb2.nc idn_cli_chirps-v2.0.2018.11.tif.grb2.nc idn_cli_chirps-v2.0.2018.12.tif.grb2.nc idn_cli_chirps-v2.0.2019.01.tif.grb2.nc idn_cli_chirps-v2.0.2019.02.tif.grb2.nc idn_cli_chirps-v2.0.2019.03.tif.grb2.nc idn_cli_chirps-v2.0.2019.05.tif.grb2.nc idn_cli_chirps-v2.0.2019.06.tif.grb2.nc idn_cli_chirps-v2.0.2019.07.tif.grb2.nc idn_cli_chirps-v2.0.2019.08.tif.grb2.nc idn_cli_chirps-v2.0.2019.09.tif.grb2.nc idn_cli_chirps-v2.0.2019.10.tif.grb2.nc idn_cli_chirps-v2.0.2019.11.tif.grb2.nc idn_cli_chirps-v2.0.2019.12.tif.grb2.nc idn_cli_chirps-v2.0.2020.01.tif.grb2.nc idn_cli_chirps-v2.0.2020.02.tif.grb2.nc idn_cli_chirps-v2.0.2020.03.tif.grb2.nc idn_cli_chirps-v2.0.2020.04.tif.grb2.nc merge.nc\n",
"Tue Jul 07 09:38:36 2020: cdo -r -f nc copy idn_cli_chirps-v2.0.1981.01.tif.grb2 ../NetCDF/idn_cli_chirps-v2.0.1981.01.tif.grb2.nc" ;
:CDO = "Climate Data Operators version 1.9.8 (https://mpimet.mpg.de/cdo)" ;
:NCO = "netCDF Operators version 4.9.3 (Homepage = http://nco.sf.net, Code = http://github.com/nco/nco)" ;
}
I would like to change the variable name and edit the content of param255.255.0(time, lat, lon) ; into something like below:
float precipitation(time,lat,lon) ;
precipitation:_FIllValue = -9999.9f ;
precipitation:_CodeMissingValue = "-9999.9f" ;
precipitation:_DimensionNames = "time,lat,lon" ;
precipitation:_Units = "mm/month" ;
precipitation:_missing_values = -9999.9f ;
precipitation:_units = "mm" ;
Is it possible using NCO or CDO? If yes, how to do that?
Is the process I have done above correct and make sense? Any recommendation are welcome.
When opening merge.nc in Panoply, I found all the date is 1970-01-01
How to make the date following the date that could be found in input filename?

If you were willing to read the manual you could easily rename any variable with ncrename and add attributes to your heart's content with ncatted.

I found a way on how to convert a folder of CHIRPS GeoTIFF data to single NetCDF file with time dimension enabled that is CF-Compliant
This is based on Rich Signell's answer on StackExchange: https://gis.stackexchange.com/a/70487 and I modify it so can works using CHIRPS dekad data.
Adjustment is needed if using other timesteps data for CHIRPS. And NCO must be installed before using this script.
#!/usr/bin/env python
import numpy as np
import datetime as dt
import os
import gdal
import netCDF4
import re
ds = gdal.Open('/path/to/dir/chirps-v2.0.1981.01.1.tif') # Data location
a = ds.ReadAsArray()
nlat,nlon = np.shape(a)
b = ds.GetGeoTransform() #bbox, interval
lon = np.arange(nlon)*b[1]+b[0]
lat = np.arange(nlat)*b[5]+b[3]
basedate = dt.datetime(1980,1,1,0,0,0)
# Create NetCDF file
nco = netCDF4.Dataset('chirps_dekads.nc','w',clobber=True) # Output name
# Chunking is optional, but can improve access a lot:
# (see: http://www.unidata.ucar.edu/blogs/developer/entry/chunking_data_choosing_shapes)
chunk_lon=10
chunk_lat=10
chunk_time=12
# Create dimensions, variables and attributes:
nco.createDimension('lon',nlon)
nco.createDimension('lat',nlat)
nco.createDimension('time',None)
timeo = nco.createVariable('time','f4',('time'))
timeo.units = 'days since 1980-1-1 00:00:00'
timeo.standard_name = 'time'
timeo.calendar = 'gregorian'
timeo.axis = 'T'
lono = nco.createVariable('lon','f4',('lon'))
lono.units = 'degrees_east'
lono.standard_name = 'longitude'
lono.long_name = 'longitude'
lono.axis = 'X'
lato = nco.createVariable('lat','f4',('lat'))
lato.units = 'degrees_north'
lato.standard_name = 'latitude'
lato.long_name = 'latitude'
lato.axis = 'Y'
# Create container variable for CRS: lon/lat WGS84 datum
crso = nco.createVariable('crs','i4')
crso.long_name = 'Lon/Lat Coords in WGS84'
crso.grid_mapping_name='latitude_longitude'
crso.longitude_of_prime_meridian = 0.0
crso.semi_major_axis = 6378137.0
crso.inverse_flattening = 298.257223563
# Create float variable for precipitation data, with chunking
pcpo = nco.createVariable('precip', 'f4', ('time', 'lat', 'lon'),
zlib=True,chunksizes=[chunk_time,chunk_lat,chunk_lon],fill_value=-9999.)
pcpo.units = 'mm'
pcpo.standard_name = 'convective precipitation rate'
pcpo.long_name = 'Climate Hazards group InfraRed Precipitation with Stations'
pcpo.time_step = 'dekad'
pcpo.missing_value = -9999.
pcpo.geospatial_lat_min = -50.
pcpo.geospatial_lat_max = 50.
pcpo.geospatial_lon_min = -180.
pcpo.geospatial_lon_max = 180.
pcpo.grid_mapping = 'crs'
pcpo.set_auto_maskandscale(False)
# Additional attributes
nco.Conventions='CF-1.6'
nco.title = "CHIRPS v2.0"
nco.history = "created by Climate Hazards Group. University of California at Santa Barbara"
nco.version = "Version 2.0"
nco.comments = "time variable denotes the first day of the given dekad."
nco.website = "https://www.chc.ucsb.edu/data/chirps"
nco.date_created = "2020-12-10"
nco.creator_name = "Ethan Hunt"
nco.creator_email = "ethan.hunt#mi6.imf"
nco.institution = "Impossible Mission Force"
nco.note = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua"
# Write lon,lat
lono[:]=lon
lato[:]=lat
pat = re.compile('chirps-v2.0.[0-9]{4}\.[0-9]{2}\.[0-9]{1}')
itime=0
# Step through data, writing time and data to NetCDF
for root, dirs, files in os.walk('/path/to/dir/'):
dirs.sort()
files.sort()
for f in files:
if re.match(pat,f):
# read the time values by parsing the filename
year=int(f[12:16])
mon=int(f[17:19])
dekad=int(f[20:21])
date=dt.datetime(year,mon,dekad,0,0,0)
print(date)
dtime=(date-basedate).total_seconds()/86400.
timeo[itime]=dtime
# precipitation
pcp_path = os.path.join(root,f)
print(pcp_path)
pcp=gdal.Open(pcp_path)
a=pcp.ReadAsArray() #data
pcpo[itime,:,:]=a
itime=itime+1
nco.close()

Related

How to set "budget" tag for xgboost hyperband optimization with mlr3tuningspaces?

I am trying to tune xgboost with hyperband and I would like to use the suggested default tuning space from the mlr3tuningspaces package. However, I don't find how to tag a hyperparameter with "budget" while using lts .
Below, I reproduced the mlr3hyperband package example to illustrate my issue:
library(mlr3verse)
library(mlr3hyperband)
library(mlr3tuningspaces)
## this does not work, because I don't know how to tag a hyperparameter
## with "budget" while using the suggested tuning space
search_space = lts("classif.xgboost.default")
search_space$values
## this works because it has a hyperparameter (nrounds) tagged with "bugdget"
search_space = ps(
nrounds = p_int(lower = 1, upper = 16, tags = "budget"),
eta = p_dbl(lower = 0, upper = 1),
booster = p_fct(levels = c("gbtree", "gblinear", "dart"))
)
# hyperparameter tuning on the pima indians diabetes data set
instance = tune(
method = "hyperband",
task = tsk("pima"),
learner = lrn("classif.xgboost", eval_metric = "logloss"),
resampling = rsmp("cv", folds = 3),
measures = msr("classif.ce"),
search_space = search_space,
term_evals = 100
)
# best performing hyperparameter configuration
instance$result
Thanks for pointing this out. I will add the budget tag to the default search space. Until then you can use this code.
library(mlr3hyperband)
library(mlr3tuningspaces)
library(mlr3learners)
# get learner with search space in one go
learner = lts(lrn("classif.xgboost"))
# overwrite nrounds with budget tag
learner$param_set$values$nrounds = to_tune(p_int(1000, 5000, tags = "budget"))
instance = tune(
method = "hyperband",
task = tsk("pima"),
learner = learner,
resampling = rsmp("cv", folds = 3),
measures = msr("classif.ce"),
term_evals = 100
)
Update 28.06.2022
The new API in version 0.3.0 is
learner = lts(lrn("classif.xgboost"), nrounds = to_tune(p_int(1000, 5000, tags = "budget"))

Numbering tickets on a PDF

I have a PDF with the art of a ticket for a fundraising dinner. I am providing a mock-up here so you can reproduce my problem:
mock up ticket
I would like to run the following pseudocode:
for i in 1:200
copy "mock up.pdf" to $i.pdf
inject $i into $i.pdf using font "OpenDyslexic" # place the ticket number in the pdf
end
create "final.pdf"
i = 0
for p in 1:20
add page to "final.pdf"
for column in 1:2
for row in 1:5
i = i + 1
inject $i.pdf in "final.pdf" in row, column of page p
end
end
end
Thank you!
I might have a solution:
#!/bin/env python3
# adapted from https://pymupdf.readthedocs.io/en/latest/faq.html#how-to-insert-text
# and https://pymupdf.readthedocs.io/en/latest/faq.html#how-to-combine-single-pages
import fitz # in fact, pip install pymupdf
#from sys import argv # in a future, I might get all the parameter via command line
width, height = fitz.paper_size("A4")
r = fitz.Rect(0, 0, width, height)
doc = fitz.open("mock up.pdf")
page = doc[0]
print("input file information:")
print(doc.metadata)
print("rotation=",page.rotation)
print("cropbox=",page.cropbox)
print("mediabox=",page.mediabox)
print("rect=",page.rect)
page.set_rotation(0)
artsize=page.rect
(nx, ny) = (200,140) # position of the ticket number inside the pdf
(dx, dy) = page.mediabox_size # the displacement of the ticket inside the output pdf
ntickets=25
nrows=5 # of tickets vertically
ncols=2 # of tickets horizontally
ntickets_per_page = nrows*ncols
outpdf = fitz.open()
nrow = ncol = 0
i = 0
while i < ntickets:
if i % ntickets_per_page == 0:
#print("new page for ticket #",i)
newpage = outpdf.new_page()
nrow, ncol = 0, 0
for ncol in range(1,ncols+1):
for nrow in range(1,nrows+1):
i += 1
if i > ntickets:
break
text = "{:04d}".format(i)
locr = fitz.Rect((ncol-1)*dx,(nrow-1)*dy,ncol*dx,nrow*dy)
#print("location of the ticket:", locr)
newpage.show_pdf_page(locr,doc,0)
p = fitz.Point(nx+(ncol-1)*dx,ny+(nrow-1)*dy)
#print("location of the number for ticket ", i, ": ", p)
rc = newpage.insert_text(p, # bottom left of 1st char
text,
fontname="tibo", # Times, bold
fontsize=12,
rotate=0,
)
i -= 1
print("%i lines printed on %i tickets." % (rc, i))
outpdf.save("tmp{:04d}.pdf".format(i))

Legacy code does not work with new large csv file

I have legacy code writing for pandas.
Now the new data become very large (in CSV format), and it is hard to read_csv with the new files (the file sizes ~ 7,8GB and will be larger in the future).
Could you suggest me the best way to not change the legacy code but still working with large CSV files? I thought to switch to spark but it seems that I will have to change a lot of code.
Many thanks
Have you tried reading the file in chunks? And defining the column dtypes beforehand might help boost the performance
chunksize = 1000000
chunks = pd.read_csv(filepath, dtype=dtypes, chunksize=chunksize)
df = pd.concat((chunk for chunk in chunks), ignore_index=True)
EDIT: Another trick is to reduce the dataframe's memory usage after loading. This is from a Kaggle kernel
def reduce_mem_usage(props):
start_mem_usg = props.memory_usage().sum() / 1024**2
print("Memory usage of properties dataframe is :",start_mem_usg," MB")
NAlist = [] # Keeps track of columns that have missing values filled in.
for col in props.columns:
if props[col].dtype != object: # Exclude strings
# Print current column type
print("******************************")
print("Column: ",col)
print("dtype before: ",props[col].dtype)
# make variables for Int, max and min
IsInt = False
mx = props[col].max()
mn = props[col].min()
# Integer does not support NA, therefore, NA needs to be filled
if not np.isfinite(props[col]).all():
NAlist.append(col)
props[col].fillna(mn-1,inplace=True)
# test if column can be converted to an integer
asint = props[col].fillna(0).astype(np.int64)
result = (props[col] - asint)
result = result.sum()
if result > -0.01 and result < 0.01:
IsInt = True
# Make Integer/unsigned Integer datatypes
if IsInt:
if mn >= 0:
if mx < 255:
props[col] = props[col].astype(np.uint8)
elif mx < 65535:
props[col] = props[col].astype(np.uint16)
elif mx < 4294967295:
props[col] = props[col].astype(np.uint32)
else:
props[col] = props[col].astype(np.uint64)
else:
if mn > np.iinfo(np.int8).min and mx < np.iinfo(np.int8).max:
props[col] = props[col].astype(np.int8)
elif mn > np.iinfo(np.int16).min and mx < np.iinfo(np.int16).max:
props[col] = props[col].astype(np.int16)
elif mn > np.iinfo(np.int32).min and mx < np.iinfo(np.int32).max:
props[col] = props[col].astype(np.int32)
elif mn > np.iinfo(np.int64).min and mx < np.iinfo(np.int64).max:
props[col] = props[col].astype(np.int64)
# Make float datatypes 32 bit
else:
props[col] = props[col].astype(np.float32)
# Print new column type
print("dtype after: ",props[col].dtype)
print("******************************")
# Print final result
print("___MEMORY USAGE AFTER COMPLETION:___")
mem_usg = props.memory_usage().sum() / 1024**2
print("Memory usage is: ",mem_usg," MB")
print("This is ",100*mem_usg/start_mem_usg,"% of the initial size")
return props, NAlist
You can simply apply the above function to your dataframe, and it will help especially in cases where your data has many numeric columns.

Perform Kriging Interpolation using Arcpy

I have list of point feature class. I am trying to write a python script to perform Krigging interpolation. I am getting error massage in this code "Point_Num" is not defined,
Below script i am working
import arcpy
from arcpy import env
from arcpy.sa import *
arcpy.env.overwriteOutput = True
# Check out the ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")
In_Point = r"D:\NPP_Test\MERRA_TEMP_2012C" #(Point feature name:r001_mean, r002_mean.....r012_mean )
Out_Raster = r"D:\NPP_Test\MERRA_TEMP_2012D"
points = arcpy.ListFeatureClasses()
zFields = "GRID_CODE"
#Kriging Veriable
cellSize = 0.05
lagSize = 0.5780481172534
majorRange = 6
partialSill = 3.304292110
nugget = 0.002701348
kRadius = RadiusFixed(20000, 1)
#Mask region of interest
mask = r"D:\Gujarta Shape file\GUJARATSTATE.shp"
for zField in zFields:
Point = Point_Num[:3]
kModelUniversalObj = KrigingModelUniversal("LINEARDRIFT", lagSize, majorRange, partialSill, nugget)
OutKriging = Kriging(inPointFeatures, zField, kModelUniversalObj, cellSize, kRadius)
#IDWMASk = ExtractByMask(outIDW, mask)
KrigMask = ExtractByMask(OutKriging, mask)
#Save outraster as the same name of input
KrigMask.save("r{}.tif".format(Point_Num))

Error when generating pdf using script in R

I'm using R to loop through the columns of a data frame and make a graph of the resulting analysis. I don't get any errors when the script runs, but it generates a pdf that cannot be opened.
If I run the content of the script, it works fine. I wondered if there is a problem with how quickly it is looping through, so I tried to force it to pause. This did not seem to make a difference. I'm interested in any suggestions that people have, and I'm also quite new to R so suggestions as to how I can improve the approach are welcome too. Thanks.
for (i in 2:22) {
# Organise data
pop_den_z = subset(pop_den, pop_den[i] != "0") # Remove zeros
y = pop_den_z[,i] # Get y col
x = pop_den_z[,1] # get x col
y = log(y) # Log transform
# Regression
lm.0 = lm(formula = y ~ x) # make linear model
inter = summary(lm.0)$coefficients[1,1] # Get intercept
slop = summary(lm.0)$coefficients[2,1] # Get slope
# Write to File
a = c(i, inter, slop)
write(a, file = "C:/pop_den_coef.txt", ncolumns = 3, append = TRUE, sep = ",")
## Setup pdf
string = paste("C:/LEED/results/Images/R_graphs/Pop_den", paste(i-2), "City.pdf")
pdf(string, height = 6, width = 9)
p <- qplot(
x, y,
xlab = "Radius [km]",
ylab = "Population Density [log(people/km)]",
xlim = x_range,
main = "Analysis of Cities"
)
# geom_abline(intercept,slope)
p + geom_abline(intercept = inter, slope = slop, colour = "red", size = 1)
Sys.sleep(5)
### close the PDF file
dev.off()
}
The line should be
print(p + geom_abline(intercept = inter, slope = slop, colour = "red", size = 1))
In pdf devices, ggplot (and lattice) only writes to file when explicitly printed.