-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhotoPeriod_netcdf.py
More file actions
137 lines (110 loc) · 4.73 KB
/
PhotoPeriod_netcdf.py
File metadata and controls
137 lines (110 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import numpy as np
from numpy import *
import netCDF4, os, re, datetime, multiprocessing, time, timeit
from multiprocessing import Lock
from astral import LocationInfo
from astral.sun import sun
from sympy import *
from datetime import date
#Return date object
def getDate(day, year):
datereturn = datetime.datetime(year, 1, 1) + datetime.timedelta(day)
datereturn = datereturn.timetuple()
return datereturn
def doCalc(year):
path = '\\\\10.0.0.2\\work\\gis\\projects\\WSI\\data\\'#'D:\\GIS\\projects\\LCC_WSI_Climate\\python code\\wsi\\'
os.chdir(path)
#Variables within netCDF that are needed for calculations
air = 'air.2m.'
lat = 'lat.nc'
lon = 'lon.nc'
#/Variables
#Setting up code variables
goodmonth = (1,2,3,9,10,11,12)
#Loop that processes every year within yearlist
yearstr = str(year)
#Imports the air netcdf file
f = netCDF4.MFDataset(air + yearstr + '.nc')
atemp = f.variables['air']
print('Current year: ' + yearstr)
ntimes, ny, nx = atemp.shape
photo_val = np.zeros((ntimes, ny, nx), dtype=float)
#Lat and lon
latin = netCDF4.Dataset(lat)
latintemp = latin.variables['lat']
lonin = netCDF4.Dataset(lon)
lonintemp = lonin.variables['lon']
#Looping through time variable
for i in range(ntimes): #ntimes
datereturn = getDate(i, year)
day = datereturn[2]
month = datereturn[1]
if month not in goodmonth:
continue
if yearstr == '2019' and month not in (1,2,3,4):
continue
print
print('Processing ' + str(month) + '/' + str(day) + '/' + yearstr)
#Process only the area of interest
for b in range(50, 180): #ny 50, 180
for c in range(150, 280): #nx 150, 280
#need photo period
d = date(year, month, day)
city = LocationInfo('Atlanta')
s = sun(city.observer, date=d)
photo = s['sunset'] - s['sunrise']
photosec = photo.total_seconds()
photomin = photosec / 60
photo_val[i,b,c] = photomin
# create NetCDF file
print('Creating variables for ', yearstr)
model_val_year = ('\\\\10.0.0.2\\work\\gis\\projects\\WSI\\data\\Photo_period_' + yearstr + '.nc')
nco = netCDF4.Dataset(model_val_year,'w',clobber=True)
nco.createDimension('time', None)
nco.createDimension('x',nx)
nco.createDimension('y',ny)
timeo = nco.createVariable('time', 'f8', ('time'))
lono = nco.createVariable('lon','f4',('y','x'))
lato = nco.createVariable('lat','f4',('y','x'))
xo = nco.createVariable('x','f4',('x'))
yo = nco.createVariable('y','f4',('y'))
lco = nco.createVariable('Lambert_Conformal','i4')
photo_val_v = nco.createVariable('photo_period', 'f4', ('time', 'y', 'x'))
photo_val_v.units='minutes'
photo_val_v.long_name='Photo period in minutes'
photo_val_v.grid_mapping = 'Lambert_Conformal'
# copy all the variable attributes from original file
for var in ['time', 'lon','lat','x','y', 'Lambert_Conformal']:
for att in f.variables[var].ncattrs():
setattr(nco.variables[var],att,getattr(f.variables[var],att))
# copy variable data for lon,lat,x and y
timeo[:]=f.variables['time'][:]
lono[:]=f.variables['lon'][:]
lato[:]=f.variables['lat'][:]
xo[:]=f.variables['x'][:]
yo[:]=f.variables['y'][:]
# write the model_val data
photo_val_v[:,:,:] = photo_val
# copy Global attributes from original file
for att in f.ncattrs():
setattr(nco,att,getattr(f,att))
nco.Conventions='CF-1.6'
nco.close()
####
# MAIN
####
if __name__ == '__main__':
print('Starting script')
#Start timing entire process
startfirst = timeit.default_timer()
#Call doCalc
yearlist = list(range(1979, 2022))
pool = multiprocessing.Pool(4)
e = pool.map(doCalc, yearlist)
pool.close()
#print("Joining pool")
pool.join()
print(e)
stoplast = timeit.default_timer()
print(stoplast - startfirst)
print("Complete")