Skip to content
Snippets Groups Projects

Cron job

Merged Tucker Gary Siegel requested to merge cron-job into master
4 files
+ 185
64
Compare changes
  • Side-by-side
  • Inline
Files
4
+ 0
58
import gdal
import numpy as np
import xarray as xr
import random
def to_freedom_units(data):
return (data * 9/5) + 32
def ReadBilFile(bil):
gdal.GetDriverByName('EHdr').Register()
img = gdal.Open(bil)
band = img.GetRasterBand(1)
data = band.ReadAsArray()
return data
if __name__ == '__main__':
import glob
years = list(range(2020, 2020 + 1))
for y in years:
print (y)
all_ = []
print ("data/PRISM/tmin/PRISM_tmin_stable_4kmD2_%s0101_%s1231/*.bil" % (y, y))
for file in sorted(list(glob.glob("data/PRISM/tmin/PRISM_tmin_stable_4kmD2_%s0101_%s1231_bil/*.bil" % (y, y)))):
# print (file)
a = ReadBilFile(file)
a[a == -9999] = np.nan
a = np.flipud(a)
all_.append(a)
all_ = np.stack(all_)
all_ = to_freedom_units(all_)
tmin = xr.Variable(['date', 'row', 'col'], all_, attrs={"long_name": "min temperature in f"}).chunk({"date": -1}).astype(np.dtype(np.float32))
all_ = []
print ("data/PRISM/tmax/PRISM_tmax_stable_4kmD2_%s0101_%s1231/*.bil" % (y, y))
for file in sorted(list(glob.glob("data/PRISM/tmax/PRISM_tmax_stable_4kmD2_%s0101_%s1231_bil/*.bil" % (y, y)))):
# print (file)
a = ReadBilFile(file)
a[a == -9999] = np.nan
a = np.flipud(a)
all_.append(a)
all_ = np.stack(all_)
all_ = to_freedom_units(all_)
tmax = xr.Variable(['date', 'row', 'col'], all_, attrs={"long_name": "max temperature in f"}).chunk({"date": -1}).astype(np.dtype(np.float32))
data = xr.Dataset(
{
"tmax": tmax,
"tmin": tmin
},
)
print (data)
data.to_netcdf("data/temps_%s.nc" % y)
Loading