示例#1
0
文件: newaLister.py 项目: bnoon/newa
def run_ddrange(stn, ddtype, accstr, accend):
    try:
        base = newaCommon.Base()
        cabbage = newaDisease.Cabbage()
        smry_dict = {'ddtype': ddtype.replace("dd", "")}
        now = DateTime.now()
        if not accend:
            accend = now
        end_date_dt = accend
        if not accstr:
            accstr = DateTime.DateTime(end_date_dt.year, 1, 1, 0)
        start_date_dt = accstr

        if start_date_dt > end_date_dt:
            return newaCommon_io.errmsg('Start date must be before end data.')

        if end_date_dt.year != now.year:
            smry_dict['this_year'] = False
            end_date_dt = end_date_dt + DateTime.RelativeDate(days=+6)
        else:
            smry_dict['this_year'] = True

        hourly_data, daily_data, download_time, station_name, avail_vars = base.get_hddata2(
            stn, start_date_dt, end_date_dt)
        smry_dict['last_time'] = download_time

        # add forecast data
        if smry_dict['this_year']:
            start_fcst_dt = DateTime.DateTime(
                *download_time) + DateTime.RelativeDate(hours=+1)
            end_fcst_dt = end_date_dt + DateTime.RelativeDate(days=+6)
            hourly_data = newaDisease.add_hrly_fcst(stn, hourly_data,
                                                    start_fcst_dt, end_fcst_dt)
            daily_data = newaDisease.hrly_to_dly(hourly_data)
        else:
            start_fcst_dt = end_date_dt + DateTime.RelativeDate(hours=+1)
            end_fcst_dt = end_date_dt
            end_date_dt = end_date_dt + DateTime.RelativeDate(days=-6)

        if len(daily_data) > 0:
            degday_dict = base.degday_calcs(daily_data, start_date_dt,
                                            end_fcst_dt, ddtype, "accum")

            if len(degday_dict) > 0:
                # get dates for gdd table
                smry_dict = cabbage.setup_dates(smry_dict, end_date_dt)
                # get dd for days of interest (including forecast)
                smry_dict = cabbage.add_ddays(smry_dict, degday_dict,
                                              start_date_dt, end_date_dt)
                return newaLister_io.ddrange_html(station_name, smry_dict,
                                                  degday_dict)
            else:
                return self.nodata(stn, station_name, start_date_dt,
                                   end_date_dt)
        else:
            return self.nodata(stn, station_name, start_date_dt, end_date_dt)
    except:
        print_exception()
    return
示例#2
0
def apple_thin_json(data_dict, biofix_dt, bloom_dt, percentflowerspurs):
	results_list = []
	notes_list = []
	try:
		results_list = []
		notes_list = []
		tkeys = data_dict.keys()
		tkeys.sort()

		if bloom_dt:
			recommendEnd = bloom_dt + DateTime.RelativeDate(days=+35)
		else:
			recommendEnd = None
		if len(tkeys) >= 4:
			list7day = [miss, miss, miss, data_dict[0]['thinIndex'], data_dict[1]['thinIndex'], \
				data_dict[2]['thinIndex'], data_dict[3]['thinIndex']]
		else:
			list7day = []
		for key in tkeys:
			t_dt = biofix_dt + DateTime.RelativeDate(days=+key, hour=0, minute=0, second=0.0)
			fdate = "%d-%02d-%02d" % (t_dt.year,t_dt.month,t_dt.day)
			if data_dict[key]['maxt'] == miss or data_dict[key]['mint'] == miss or data_dict[key]['srad'] == miss:
				data_dict[key]['thinIndex'] = miss
			if key+4 < len(tkeys) and data_dict[key+4]['maxt'] != miss and data_dict[key+4]['mint'] != miss and data_dict[key+4]['srad'] != miss:
				list7day.append(data_dict[key+4]['thinIndex'])
			else:
				list7day.append(miss)
			list7day.pop(0)
			if len(list7day) == 7 and not miss in list7day:
				#avg7day_STRAIGHT = round((sum(list7day)/7.0), 2)
				# changed straight average to 7-day weighted average - kle 2021-3-18
				wtsum = 0
				wtsum += list7day[0] * 0.3
				wtsum += list7day[1] * 0.5
				wtsum += list7day[2]
				wtsum += list7day[3]
				wtsum += list7day[4]
				wtsum += list7day[5] * 0.8
				wtsum += list7day[6] * 0.6
				avg7day = round((wtsum / 5.2), 2)
			else:
				avg7day = "-"
			if bloom_dt and t_dt >= bloom_dt and t_dt <= recommendEnd:
				recommend = get_recommend(avg7day, percentflowerspurs, mround(data_dict[key]['dd4cAccum'],0))
			else:
				recommend = {"efficacy": "NA", "riskColor": 0, "recommend": "-"}
#			results_list.append([fdate, ctof(data_dict[key]['maxt']), ctof(data_dict[key]['mint']),\
#				mround(data_dict[key]['srad'],1), mround(data_dict[key]['thinIndex'],2),avg7day, mround(data_dict[key]['dd4cAccum'],1), recommend])
			day_results = {'date': fdate, 'maxt':ctof(data_dict[key]['maxt']), 'mint': ctof(data_dict[key]['mint']),\
				'srad': mround(data_dict[key]['srad'],1), 'thinIndex': mround(data_dict[key]['thinIndex'],2), \
				'avg7day': avg7day, 'dd4cAccum': mround(data_dict[key]['dd4cAccum'],1)}
			day_results.update(recommend)
			results_list.append(day_results)
		if bloom_dt and (bloom_dt - biofix_dt).days < 21:
			notes_list.append('Difference between Green tip and Bloom is less than 21 days. Results may be unreliable.')
	except:
		print_exception()
	return {"data":results_list, "notes":notes_list}
示例#3
0
def run_apple_thin (stn,accend,greentip,bloom,percentflowerspurs,output):
	try:
		id_parts = stn.split(" ")
		if len(id_parts) == 1:
			if stn[0:3] == '42.' or stn[0:3] == '43.':
				station_type  = 'ucc'
			elif stn[0:1] >= '1' and stn[0:1] <= '9' and stn[1:2] >= '0' and stn[1:2] <= '9':
				station_type = 'njwx'
			elif len(stn) == 4 and stn[0:1].upper() == 'K':
				station_type = 'icao'
			elif len(stn) == 4:
				station_type = 'oardc'
			elif stn[0:3] == 'cu_' or stn[0:3] == 'um_' or stn[0:3] == 'un_' or stn[0:3] == 'uc_':
				station_type = 'cu_log'
			elif stn[0:3] == "ew_":
				stn = stn[3:]
				station_type = 'miwx'
			elif stn[0:5] == "nysm_":
				stn = stn[5:]
				station_type = 'nysm'
			elif len(stn) == 7 and stn[2:3] == "_":
				station_type = 'nwon'
			elif len(stn) == 3 or len(stn) == 6:
				station_type = 'newa'
			else:
				raise StationProblem('Cannot determine station type for %s'%stn)
		else:
			stn = id_parts[0]
			station_type = id_parts[1]
		
		try:
			biofix_dt = greentip + DateTime.RelativeDate(hour=0, minute=0, second=0.0)
		except TypeError:
			return newaTools_io.apple_thin_results(None)
		try:
			bloom_dt = bloom + DateTime.RelativeDate(hour=0, minute=0, second=0.0)
		except TypeError:
			bloom_dt = None
		
		#date range
		accend = accend + DateTime.RelativeDate(hour=0,minute=0,second=0.0)
##		start_date_dt = accend + DateTime.RelativeDate(days=-7) + DateTime.RelativeDate(hour=0,minute=0,second=0.0)
		start_date_dt = biofix_dt
		end_date_dt = accend + DateTime.RelativeDate(days=+6) + DateTime.RelativeDate(hour=23,minute=0,second=0.0)	
		
		# get model results
		data_dict = apple_thinning_model (stn, start_date_dt, end_date_dt, bloom_dt, station_type)
		json_dict = apple_thin_json(data_dict, biofix_dt, bloom_dt,percentflowerspurs)
	except:
		print_exception()
	
	if output == 'json':
		return json.dumps(json_dict)
	else:
		thin_dict = {'selectedDate': accend, 'greentipDate': biofix_dt, 'bloomDate': bloom_dt}
		json_dict.update(thin_dict)
		return newaTools_io.apple_thin_results(json_dict)
示例#4
0
def run_apple_et (stn,accend,greentip,output):
	et_dict = {}
	try:
		#date range
		start_date_dt = accend + DateTime.RelativeDate(days=-7) + DateTime.RelativeDate(hour=0,minute=0,second=0.0)
		end_date_dt = accend + DateTime.RelativeDate(days=+6) + DateTime.RelativeDate(hour=23,minute=0,second=0.0)	

		id_parts = stn.split(" ")
		if len(id_parts) == 1:		
			if stn[0:3] == '42.' or stn[0:3] == '43.':
				station_type  = 'ucc'
			elif stn[0:1] >= '1' and stn[0:1] <= '9' and stn[1:2] >= '0' and stn[1:2] <= '9':
				station_type = 'njwx'
			elif len(stn) == 4 and stn[0:1].upper() == 'K':
				station_type = 'icao'
			elif len(stn) == 4:
				station_type = 'oardc'
			elif stn[0:3] == 'cu_' or stn[0:3] == 'um_':
				station_type = 'cu_log'
			elif stn[0:3] == "ew_":
				stn = stn[3:]
				station_type = 'miwx'
			elif stn[0:5] == "nysm_":
				stn = stn[5:]
				station_type = 'nysm'
			elif len(stn) == 7 and stn[2:3] == "_":
				station_type = 'nwon'
			elif len(stn) == 3 or len(stn) == 6:
				station_type = 'newa'
			else:
				raise StationProblem('Cannot determine station type for %s'%stn)
		else:
			stn = id_parts[0]
			station_type = id_parts[1]
		
		try:
			biofix_dt = greentip + DateTime.RelativeDate(hour=0, minute=0, second=0.0)
		except TypeError:
			return newaTools_io.apple_et_results(None)
		
		# get daily modeled et, solar rad and precipitation
		et_dict['data'] = apple_hourly (stn, start_date_dt, end_date_dt, biofix_dt, station_type)
	except:
		print_exception()
	
	if output == 'json':
		import json
		results_list = []
		etkeys = et_dict['data'].keys()
		etkeys.sort()
		for key in etkeys:
			fdate = "%d-%02d-%02d" % (key[0],key[1],key[2])
			results_list.append([fdate,round(et_dict['data'][key]['et'],2),round(et_dict['data'][key]['prcp'],2)])
		json_dict = json.dumps({"data":results_list})
		return json_dict
	else:
		return newaTools_io.apple_et_results(et_dict)
示例#5
0
def get_precip_forecast(stn, start_date_dt, end_date_dt):
    hourly_fcst = []
    miss = -999
    try:
        stn = stn.upper()
        pdict = hashopen('/ndfd/hourly_forecasts.db', 'r')
        if pdict.has_key(stn):
            stndict = loads(pdict[stn])
            pdict.close()
            firstday_hour = start_date_dt.hour
            lastday_hour = end_date_dt.hour
            start_date_dt = start_date_dt + DateTime.RelativeDate(hour=0)
            end_date_dt = end_date_dt + DateTime.RelativeDate(hour=0)
            theDate_dt = start_date_dt
            while theDate_dt <= end_date_dt:
                theDate = (theDate_dt.year, theDate_dt.month, theDate_dt.day)
                if stndict['qpf'].has_key(theDate):
                    qpf = stndict['qpf'][theDate]
                else:
                    qpf = [miss] * 24
                if stndict['pop12'].has_key(theDate):
                    pop12 = stndict['pop12'][theDate]
                else:
                    pop12 = [miss] * 24
                if theDate_dt == start_date_dt:
                    shour = firstday_hour
                else:
                    shour = 0
                if theDate_dt == end_date_dt:
                    ehour = lastday_hour
                else:
                    ehour = 23
                for hr in range(shour, ehour + 1):
                    theTime = (theDate_dt.year, theDate_dt.month,
                               theDate_dt.day, hr)
                    hourly_fcst.append((theTime, qpf[hr], pop12[hr]))
                    # distribute precipitation over last 6 hours
                    if qpf[hr] != miss:
                        x = len(hourly_fcst) - 1
                        for i in range(x, x - 6, -1):
                            if i >= 0:
                                hourly_fcst[i] = hourly_fcst[i][0:1] + (
                                    qpf[hr] / 6., ) + hourly_fcst[i][2:]
                theDate_dt = theDate_dt + DateTime.RelativeDate(days=+1)
    except:
        print_exception()
    return hourly_fcst


#stn = 'cli'
#start_date_dt = DateTime.DateTime(2009,4,16,8)
#end_date_dt = DateTime.DateTime(2009,4,22,23)
#forecast_dict = get_precip_forecast(stn,start_date_dt,end_date_dt)
#for item in forecast_dict:
#	print item
示例#6
0
def run_apple_thin_specs (stn,accend,output):
	et_dict = {}

	#date range
	start_date_dt = accend + DateTime.RelativeDate(days=-7) + DateTime.RelativeDate(hour=0,minute=0,second=0.0)
	end_date_dt = accend + DateTime.RelativeDate(days=+6) + DateTime.RelativeDate(hour=23,minute=0,second=0.0)	
	
	fcst_stn = copy.deepcopy(stn)
	id_parts = stn.split(" ")
	if len(id_parts) == 1:
		if stn[0:3] == '42.' or stn[0:3] == '43.':
			station_type  = 'ucc'
		elif stn[0:1] >= '1' and stn[0:1] <= '9' and stn[1:2] >= '0' and stn[1:2] <= '9':
			station_type = 'njwx'
		elif len(stn) == 4 and stn[0:1].upper() == 'K':
			station_type = 'icao'
		elif len(stn) == 4:
			station_type = 'oardc'
		elif stn[0:3] == 'cu_' or stn[0:3] == 'um_' or stn[0:3] == 'un_' or stn[0:3] == 'uc_':
			station_type = 'cu_log'
		elif stn[0:3] == "ew_":
			stn = stn[3:]
			station_type = 'miwx'
		elif stn[0:5] == "nysm_":
			stn = stn[5:]
			station_type = 'nysm'
		elif len(stn) == 7 and stn[2:3] == "_":
			station_type = 'nwon'
		elif len(stn) == 3 or len(stn) == 6:
			station_type = 'newa'
		else:
			raise StationProblem('Cannot determine station type for %s'%stn)
	else:
		stn = id_parts[0]
		station_type = id_parts[1]
	
	#need to get greentip and bloom date in DateTime format
	biofix_dd = phen_events_dict['macph_greentip_43']['dd'][2]					#green tip degree day accumulation
	bloom_dd = phen_events_dict['macph_bloom_43']['dd'][2]						#bloom degree day accumulation
	
	hourly_data = {}
	jan1_dt = DateTime.DateTime(end_date_dt.year,1,1,0,0,0)
	fcst_data = get_fcst_data (fcst_stn, 'temp', jan1_dt, end_date_dt)
	hourly_data = get_hourly_data (stn, 'temp', jan1_dt, end_date_dt, hourly_data, fcst_data, station_type)
	biofix_dt, ddmiss = BaseTools().find_biofix (hourly_data, jan1_dt, end_date_dt, 'dd43be', biofix_dd)
	bloom_dt, bloom_ddmiss = BaseTools().find_biofix (hourly_data, jan1_dt, end_date_dt, 'dd43be', bloom_dd)
	if biofix_dt and ddmiss <= 7: 
		et_dict['greentip'] = '%d/%d/%d' % (biofix_dt.month,biofix_dt.day,biofix_dt.year)
	if bloom_dt and bloom_ddmiss <= 7: 
		et_dict['bloom'] = '%d/%d/%d' % (bloom_dt.month,bloom_dt.day,bloom_dt.year)
	ucanid,station_name = get_metadata (stn, station_type)
	et_dict['station_name'] = station_name
	
	return newaTools_io.apple_thin_specs(et_dict)
示例#7
0
def tp_for_grf(stn, daily_data, smry_dict, start_date_dt, end_date_dt):
    obs_dict = {}
    forecast_data = None
    try:
        mint = []
        maxt = []
        prcpl = []
        obs_days = []
        first = 1
        for dly_dt, tave_hr, tmax, tmin, prcp, lwet, rhum, wspd, srad, st4a, st4x, st4n, dflags in daily_data:
            this_day_dt = DateTime.DateTime(dly_dt[0], dly_dt[1], dly_dt[2])
            if this_day_dt < start_date_dt: continue
            if tmax != miss and tmin != miss:
                if first:
                    first = 0
                mint.append(int(round(tmin, 0)))
                maxt.append(int(round(tmax, 0)))
                prcpl.append(prcp)
                obs_days.append("%d-%d-%d" % (dly_dt[0], dly_dt[1], dly_dt[2]))
        obs_dict['maxt'] = maxt
        obs_dict['mint'] = mint
        obs_dict['prcp'] = prcpl
        obs_dict['obs_days'] = obs_days
        obs_dict['fmaxt'] = []
        obs_dict['fmint'] = []
        obs_dict['fprcp'] = []
        obs_dict['frobs_days'] = []

        # get daily forecast data
        start_fcst_dt = DateTime.DateTime(
            *daily_data[-1][0]) + DateTime.RelativeDate(days=+1)
        end_fcst_dt = end_date_dt + DateTime.RelativeDate(days=+6)
        if end_fcst_dt >= start_fcst_dt:
            fmint = []
            fmaxt = []
            fprcp = []
            fobs_days = []
            forecast_data = get_daily_forecast(stn, start_fcst_dt, end_fcst_dt)
            for dly_dt, tave_hr, tmax, tmin, prcp, lwet, rhum, wspd, srad, st4a, st4x, st4n, dflags in forecast_data:
                if tmax != miss and tmin != miss:
                    if first:
                        first = 0
                    fmint.append(int(round(tmin, 0)))
                    fmaxt.append(int(round(tmax, 0)))
                    fprcp.append(prcp)
                    fobs_days.append("%d-%d-%d" %
                                     (dly_dt[0], dly_dt[1], dly_dt[2]))
            obs_dict['fmaxt'] = fmaxt
            obs_dict['fmint'] = fmint
            obs_dict['fprcp'] = fprcp
            obs_dict['frobs_days'] = fobs_days
    except:
        print_exception()
    return obs_dict, smry_dict, forecast_data
示例#8
0
def deghr_for_grf(hourly_data, start_date, end_date):
    deghr_dict = {}
    try:
        dly_sum = 0.
        dly_msg = 0.
        d4_dh = []
        dly_d4dh_list = []
        date_list = []
        start_date = start_date + DateTime.RelativeDate(
            days=+1)  #start deghr accumulation day after biofix
        start_date = start_date + DateTime.RelativeDate(
            hour=0, minute=0, second=0)
        end_date = end_date + DateTime.RelativeDate(
            hour=23, minute=59, second=59)
        for theTime, temp, prcp, lwet, rhum, wspd, wdir, srad, st4i, eflags in hourly_data:
            temp_eflag, prcp_eflag, lwet_eflag, rhum_eflag, wspd_eflag, wdir_eflag, srad_eflag, st4i_eflag = eflags
            this_date = DateTime.DateTime(*theTime)
            if this_date >= start_date and this_date <= end_date:
                if temp != miss:
                    ddval = newaModel.Apple().get_dhr(temp)
                    dly_sum = dly_sum + ddval
                else:
                    dly_msg = dly_msg + 1

    #			save daily values
                if theTime[3] == 23:
                    if dly_msg >= 2: dly_sum = miss
                    if len(d4_dh) == 4:
                        del d4_dh[0]  #keep total for last 4 days
                    d4_dh.append(dly_sum)
                    if d4_dh.count(miss) == 0 and len(d4_dh) > 0:
                        fdate = "%d-%d-%d" % (theTime[0], theTime[1],
                                              theTime[2])
                        date_list.append(fdate)
                        dly_d4dh_list.append(int(sum(d4_dh)))
                    dly_sum = 0.
                    dly_msg = 0
    #	get last partial day
        if theTime[3] != 23:
            if dly_msg >= 2: dly_sum = miss
            if len(d4_dh) == 4: del d4_dh[0]  #keep total for last 4 days
            d4_dh.append(dly_sum)
            if d4_dh.count(miss) == 0 and len(d4_dh) > 0:
                fdate = "%d-%d-%d" % (theTime[0], theTime[1], theTime[2])
                date_list.append(fdate)
                dly_d4dh_list.append(int(sum(d4_dh)))
        deghr_dict['dates'] = date_list
        deghr_dict['d4dh'] = dly_d4dh_list
    except:
        print 'Error calculating degree hours'
        print_exception()
    return deghr_dict
示例#9
0
    def get_mildew_missing(self):
        last_date = self.dates[-1]
        last_time = apply(DateTime.Date, last_date)
        sTime = last_time + DateTime.RelativeDate(hours=+1)
        eTime = DateTime.DateTime(sTime.year + 1, 1, 1, 0)
        missing_hours = []
        missing_days = []

        while sTime < eTime:
            localTime = getLocalFromEST_time(sTime)
            missing_days.append(localTime.day_of_year)
            missing_hours.append(localTime.hour)
            sTime = sTime + DateTime.RelativeDate(hours=+1)
        return (missing_days, missing_hours)
示例#10
0
def ascospore_for_grf(dd_data, daily_data):
    ascospore_dict = {}
    try:
        daily_prec = []
        # need precip for six days preceding greentip (first day in dd_data)
        first_dt = DateTime.DateTime(dd_data[0][0][0], dd_data[0][0][1],
                                     dd_data[0][0][2])
        minus6_dt = first_dt + DateTime.RelativeDate(days=-6)
        minus6_list = [minus6_dt.year, minus6_dt.month, minus6_dt.day]
        for i in range(len(daily_data)):
            dly_dt, tave_hr, tmax, tmin, prcp, lwet, rhum, wspd, srad, st4a, st4x, st4n, dflags = daily_data[
                i]
            if dly_dt == minus6_list:
                daily_prec.append(daily_data[i][4])
                daily_prec.append(daily_data[i + 1][4])
                daily_prec.append(daily_data[i + 2][4])
                daily_prec.append(daily_data[i + 3][4])
                daily_prec.append(daily_data[i + 4][4])
                daily_prec.append(daily_data[i + 5][4])
        accum_dd = 0.
        date_list = []
        matur_list = []
        error_list = []
        for dly_dt, tmax, tmin, ddval, prec in dd_data:
            fdate = "%d-%d-%d" % (dly_dt[0], dly_dt[1], dly_dt[2])
            if prec != miss:
                daily_prec.append(prec)
            else:
                daily_prec.append(0.)
            if len(daily_prec) > 7:
                del daily_prec[0]
                prec7dy = sum(daily_prec)
            else:
                prec7dy = miss
            if prec7dy > 0.00 or prec7dy == miss:
                if ddval != miss: accum_dd = accum_dd + ddval
            else:
                pass  #don't accumulate anything during dry period
            if ddval != miss:
                comp = math.exp(
                    (math.pi / math.sqrt(3)) * ((-2.49 + (0.01 * accum_dd))))
                maturity = 100. * (comp) / (1. + comp)
                comp2 = math.exp(
                    (math.pi / math.sqrt(3)) * ((-1.676 + (0.01 * accum_dd))))
                error = 100. * (comp2 / (1. + comp2)) - maturity
            else:
                maturity = miss
                error = miss
            if maturity != miss:
                date_list.append(fdate)
                matur_list.append(maturity)
                error_list.append(error)
        ascospore_dict['dates'] = date_list
        ascospore_dict['maturity'] = matur_list
        ascospore_dict['error'] = error_list
    except:
        print_exception()
    return ascospore_dict
示例#11
0
 def get_dates(self, obsTime, eTime):
     sTime = DateTime.DateTime(obsTime.year, obsTime.month, obsTime.day,
                               obsTime.hour)
     dateList = []
     while sTime < eTime:
         this_date = [sTime.year, sTime.month, sTime.day, sTime.hour]
         dateList.append(this_date)
         sTime = sTime + DateTime.RelativeDate(hours=+1)
     return dateList
示例#12
0
文件: newaTools.py 项目: bnoon/newa
def apple_thin_json(thin_dict, biofix_dt, bloom_dt):
	results_list = []
	notes_list = []
	try:
		import json
		results_list = []
		notes_list = []
		tkeys = thin_dict['data'].keys()
		tkeys.sort()
		recommendEnd = bloom_dt + DateTime.RelativeDate(days=+35)
		if len(tkeys) >= 3:
			list4day = [miss, thin_dict['data'][0]['thinIndex'], thin_dict['data'][1]['thinIndex'], thin_dict['data'][2]['thinIndex']]
		else:
			list4day = []
		for key in tkeys:
			t_dt = thin_dict['greentipDate'] + DateTime.RelativeDate(days=+key, hour=0, minute=0, second=0.0)
			fdate = "%d-%02d-%02d" % (t_dt.year,t_dt.month,t_dt.day)
			if thin_dict['data'][key]['maxt'] == miss or thin_dict['data'][key]['mint'] == miss or thin_dict['data'][key]['srad'] == miss:
				thin_dict['data'][key]['dlyCarbonBal'] = miss
				thin_dict['data'][key]['totalDemand'] = miss
				thin_dict['data'][key]['thinIndex'] = miss
			if key+3 < len(tkeys) and thin_dict['data'][key+3]['maxt'] != miss and thin_dict['data'][key+3]['mint'] != miss and thin_dict['data'][key+3]['srad'] != miss:
				list4day.append(thin_dict['data'][key+3]['thinIndex'])
			else:
				list4day.append(miss)
			list4day.pop(0)
			if len(list4day) == 4 and not miss in list4day:
				avg4day = round((sum(list4day)/4.0), 2)
			else:
				avg4day = "-"
			if t_dt >= bloom_dt and t_dt <= recommendEnd:
				recommend = get_recommend(avg4day)
			else:
				recommend = "-"
			results_list.append([fdate, ctof(thin_dict['data'][key]['maxt']), ctof(thin_dict['data'][key]['mint']),\
				mround(thin_dict['data'][key]['srad'],1), mround(thin_dict['data'][key]['dlyCarbonBal'],2),\
				mround(thin_dict['data'][key]['totalDemand'],2), mround(thin_dict['data'][key]['thinIndex'],2),\
				avg4day, recommend])
		if (bloom_dt - biofix_dt).days < 21:
			notes_list.append('Difference between Green tip and Bloom is less than 21 days. Results may be unreliable.')
	except:
		print_exception()
	json_dict = json.dumps({"data":results_list, "notes":notes_list})
	return json_dict
示例#13
0
def get_relative_date(years=None, months=None, days=None):
    """
    Calculates relative past date from current time based on input params.
    :param years: Number of years from current time
    :type: str
    :param months: Number of months from current time
    :type: str
    :param days: Number of days from current time
    :type: str
    :return: mx.DateTime.DateTime object
    """
    exp_date = DateTime.now()
    if years is not None:
        exp_date = exp_date - DateTime.RelativeDate(years=int(years))
    if months is not None:
        exp_date = exp_date - DateTime.RelativeDate(months=int(months))
    if days is not None:
        exp_date = exp_date - DateTime.RelativeDate(days=int(days))
    return exp_date
示例#14
0
    def loop_through_time(self):

        obsTime = DateTime.DateTime(self.sTime.year, self.sTime.month,
                                    self.sTime.day, self.sTime.hour)
        nexTime = obsTime + DateTime.RelativeDate(months=+1)

        while obsTime < self.eTime:

            if nexTime > self.eTime:
                end = (self.eTime.year, self.eTime.month, self.eTime.day,
                       self.eTime.hour)
            else:
                end = (nexTime.year, nexTime.month, nexTime.day, nexTime.hour)

            start = (obsTime.year, obsTime.month, obsTime.day, obsTime.hour)

            try:
                self.get_temperature(start, end)
                self.get_precipitation(start, end)
                self.get_rh(start, end)
                self.get_lwet(start, end)
            except weatherError:
                break
            except:
                print_exception()
                break

            obsTime = DateTime.DateTime(nexTime.year, nexTime.month,
                                        nexTime.day, nexTime.hour)
            nexTime = obsTime + DateTime.RelativeDate(months=+1)

        self.stn.release_tmp()
        if self.tmpVar:
            self.tmpVar.release_tmp()
        self.stn.release_rh()
        if self.rhVar:
            self.rhVar.release_rh()
        self.stn.release_prcp()
        if self.prcpVar:
            self.prcpVar.release_prcp()
        self.stn.release_lwet()
        if self.lwetVar:
            self.lwetVar.release_lwet()
示例#15
0
def getHourlySolar(stn, start_date, end_date, miss, stpr0, wthr0, dwpt0, visi0,
                   ccnd0, chgt0, ceil0, tsky0):
    import string, Data
    from mx import DateTime
    from print_exception import print_exception
    from solar_main_routine import SOLAR_MAIN

    values = []
    vdates = []

    try:
        srdates, srvalues = SOLAR_MAIN(stn, start_date, end_date, stpr0, wthr0,
                                       dwpt0, visi0, ccnd0, chgt0, ceil0,
                                       tsky0)

        start_date_dt = DateTime.DateTime(*start_date)
        end_date_dt = DateTime.DateTime(*end_date)
        sr_start_dt = DateTime.DateTime(*srdates[0])
        sr_end_dt = DateTime.DateTime(*srdates[-1])

        #		pad beginning, if necessary
        theDate = start_date_dt
        while theDate < sr_start_dt:
            vdates.append(theDate.tuple()[:4])
            values.append(miss)
            theDate = theDate + DateTime.RelativeDate(hours=+1)
#		only retain values in requested date range
        for vdy in range(len(srdates)):
            srdate_dt = DateTime.DateTime(*srdates[vdy])
            if srdate_dt >= start_date_dt and srdate_dt < end_date_dt:
                vdates.append(srdates[vdy])
                #				convert from string to value
                values.append(string.atof(srvalues[vdy]))
#		pad end, if necessary
        theDate = sr_end_dt + DateTime.RelativeDate(hours=+1)
        while theDate < end_date_dt:
            vdates.append(theDate.tuple()[:4])
            values.append(miss)
            theDate = theDate + DateTime.RelativeDate(hours=+1)
    except:
        print_exception()

    return (values, vdates)
示例#16
0
	def update_all_days(self) :
		eTime = apply(DateTime.Date,self.eDate)
		#
		# PLEASE NOTE:  
		# For the current program to work, we must start on Jan. 1
		# This has impact on figuring out indexes among other things.
		#               
		sTime = DateTime.DateTime(self.eDate[0],1,1)
		while sTime <= eTime : 
			this_date = "%02d/%02d"%(sTime.month,sTime.day)
			self.all_days[sTime.day_of_year] = this_date
			sTime = sTime + DateTime.RelativeDate(days=+1)
示例#17
0
def getDates():
    dates_dt = []
    for i in range(1, len(sys.argv)):
        cols = sys.argv[i].split(",")
        if len(cols) == 3:
            dates_dt.append(
                DateTime.DateTime(int(cols[0]), int(cols[1]), int(cols[2])))
    if len(dates_dt) == 2:
        #	Two arguments are start and end date
        sdate = dates_dt[0]
        edate = dates_dt[1]
    elif len(dates_dt) == 1:
        #	One argument is end date - do week ending that date
        edate = dates_dt[0]
        sdate = edate + DateTime.RelativeDate(days=-6)
    elif len(dates_dt) == 0:
        #	No arguments default to week ending yesterday
        edate = DateTime.now() + DateTime.RelativeDate(days=-1)
        sdate = edate + DateTime.RelativeDate(days=-6)
    else:
        sys.exit()
    return sdate, edate
示例#18
0
def get_daily_forecast (stn,start_date_dt,end_date_dt):
	daily_fcst = []
	miss = -999
	try:
		stn = stn.upper()
##NEW: next line
		forecast_dict = hashopen('/ndfd/daily_forecasts.db','r')
		if forecast_dict.has_key(stn):
	#		get all daily precip for period
			hourly_fcst = get_precip_forecast(stn,start_date_dt,end_date_dt)
			dly_ppt = {}
			ppt_cnt = 0
			ppt_sum = 0.0
			for dt,qpf,pop in hourly_fcst:
				if qpf != miss:
					ppt_sum = ppt_sum + qpf
					ppt_cnt = ppt_cnt + 1
				if dt[3] == 23:
					if ppt_cnt > 0: dly_ppt[dt[0:3]] = ppt_sum
					ppt_cnt = 0
					ppt_sum = 0.0
	#		get temps and combine with precip, if available
			stndict = loads(forecast_dict[stn])
			theDate_dt = start_date_dt
			while theDate_dt <= end_date_dt:
				int_date = (theDate_dt.year,theDate_dt.month,theDate_dt.day)
				if stndict.has_key(int_date):
					tmax,tmin = stndict[int_date]
					tave = (tmax+tmin)/2.
					eflags = ('', '', '', 'M', 'M', 'M', 'M', 'M')
				else:
					tmax,tmin,tave = miss,miss,miss
					eflags = ('M', 'M', 'M', 'M', 'M', 'M', 'M', 'M')
				if dly_ppt.has_key(int_date):
					rain = dly_ppt[int_date]
					eflags = (eflags[0],eflags[1],eflags[2],"",'M', 'M', 'M', 'M')
				else:
					rain = miss
				daily_fcst.append(([int_date[0],int_date[1],int_date[2]],tave,tmax,tmin,rain,miss,miss,miss,miss,miss,miss,miss,eflags))
				theDate_dt = theDate_dt + DateTime.RelativeDate(days = +1)
		forecast_dict.close()
	except:
		print_exception()
	return daily_fcst

#stn = 'cu_gfr'
#start_date_dt = DateTime.DateTime(2009,5,1)
#end_date_dt = DateTime.DateTime(2009,5,18)
#forecast_dict = get_daily_forecast(stn,start_date_dt,end_date_dt)
#for item in forecast_dict:
#	print item
示例#19
0
	def find_biofix (self, hourly_data, jan1_dt, end_date_dt, smry_type, biofix_dd):
		biofix_date = None
		ddmiss = None
		try:
			ddaccum = 0.
			ddmiss = 0
			dly_max = -999
			dly_min = 999
			dly_miss = 0
			ks = hourly_data.keys()
			ks.sort()
			for key_date in ks:
				theDate = DateTime.DateTime(*key_date)
				hourly_temp = hourly_data[key_date]['temp'][0]
				if hourly_temp != miss:
					if hourly_temp > dly_max:
						dly_max = copy.deepcopy(hourly_temp)
					if hourly_temp < dly_min:
						dly_min = copy.deepcopy(hourly_temp)
				else:
					dly_miss = dly_miss + 1
	
	#			end of day update
				if theDate.hour == 23:
					if dly_miss == 0:
						dly_dd = BaseTools().calc_degday(dly_max, dly_min, smry_type)
					else:
						dly_dd = miss
				
	#				check to see if biofix gdd accum has been reached
					if dly_dd != miss:
						ddaccum = ddaccum + dly_dd
					else:
						ddmiss = ddmiss + 1
					if round(ddaccum,0) >= biofix_dd:
						biofix_date = theDate + DateTime.RelativeDate(hours=0)
						break
					dly_max = -999
					dly_min = 999
					dly_miss = 0
		except:
			print_exception()
		
		return biofix_date, ddmiss
示例#20
0
def tp_for_grf2(daily_data, start_date_dt, start_fcst_dt, useqpf=True):
    obs_dict = {}
    forecast_data = None
    start_fcst_dt = start_fcst_dt + DateTime.RelativeDate(
        hour=0, minute=0, second=0)
    try:
        mint = []
        maxt = []
        prcpl = []
        obs_days = []
        fmint = []
        fmaxt = []
        fprcp = []
        fobs_days = []
        for dly_dt, tave_hr, tmax, tmin, prcp, lwet, rhum, wspd, srad, qpf, st4x, st4n, dflags in daily_data:
            this_day_dt = DateTime.DateTime(dly_dt[0], dly_dt[1], dly_dt[2])
            if this_day_dt < start_date_dt: continue
            if not useqpf: qpf = prcp
            if tmax != miss and tmin != miss:
                if this_day_dt < start_fcst_dt:
                    mint.append(int(round(tmin, 0)))
                    maxt.append(int(round(tmax, 0)))
                    prcpl.append(qpf)
                    obs_days.append("%d-%d-%d" %
                                    (dly_dt[0], dly_dt[1], dly_dt[2]))
                else:
                    fmint.append(int(round(tmin, 0)))
                    fmaxt.append(int(round(tmax, 0)))
                    fprcp.append(qpf)
                    fobs_days.append("%d-%d-%d" %
                                     (dly_dt[0], dly_dt[1], dly_dt[2]))

        obs_dict['maxt'] = maxt
        obs_dict['mint'] = mint
        obs_dict['prcp'] = prcpl
        obs_dict['obs_days'] = obs_days
        obs_dict['fmaxt'] = fmaxt
        obs_dict['fmint'] = fmint
        obs_dict['fprcp'] = fprcp
        obs_dict['frobs_days'] = fobs_days
    except:
        print_exception()
    return obs_dict
示例#21
0
def getMeta(options):
    try:
        data_vals = {}
        metafile = open(
            "%s%s.pkl" % (options["metalocation"], options["area"]), 'rb')
        data_vals["meta"] = pickle.load(metafile)
        metafile.close()
    except IOError:
        print "Unable to open a metadata file; accessing lat/lon from gridData"
        yest = DateTime.now() + DateTime.RelativeDate(days=-1)
        input_dict = {
            "sdate": '%d-%02d-%02d' % (yest.year, yest.month, yest.day),
            "edate": '%d-%02d-%02d' % (yest.year, yest.month, yest.day),
            "grid": 3,
            "elems": 'pcpn',
            "bbox": options["bbox"],
            "meta": 'll'
        }
        data_vals = getData(input_dict)
    return data_vals
示例#22
0
def get_downloadtime(stn, station_type):
    download_time_dt = miss
    try:
        if station_type == 'icao':
            staid = stn.upper()
        else:
            if stn[0:3] == "ew_":
                stn = stn[3:]
        end_date_dt = DateTime.now()
        #		adjust for DST if necessary (don't worry about this for start_date)
        if end_date_dt.dst == 1:
            end_date_dt = end_date_dt + DateTime.RelativeDate(hours=-1)
        start_date_dt = end_date_dt + DateTime.RelativeDate(
            months=-12, hour=0, minute=0, second=0)
        #	 	make first chunk just 2 days, 30 days in successive calls as far back as a year
        start_period_dt = end_date_dt + DateTime.RelativeDate(days=-2)
        end_period_dt = end_date_dt
        while end_period_dt >= start_date_dt:
            results = get_HourlyData(stn, station_type, start_period_dt,
                                     end_period_dt)
            #	 		process data
            if results and len(results["data"]) > 0:
                temp = results["data"]
                for i in range(len(temp) - 1, -1,
                               -1):  # loop back through days
                    hlydate, hlytemps = temp[i]
                    for h in range(
                            len(hlytemps) - 1, -1, -1
                    ):  # loop back through hours (h 23 to 0 is hours 24 to 1)
                        if hlytemps[h] != "M":
                            hyr, hmn, hdy = hlydate.split("-")
                            # add one to hour to go from index (23:0) to actual (24:1)
                            theDate = DateTime.DateTime(
                                int(hyr), int(hmn), int(hdy),
                                h) + DateTime.RelativeDate(hours=+1)
                            download_time_dt = theDate + DateTime.RelativeDate(
                                hours=+theDate.dst)
                            return download_time_dt
#			reset for previous 30-day chunk
            start_period_dt = start_period_dt + DateTime.RelativeDate(days=-30)
            end_period_dt = start_period_dt + DateTime.RelativeDate(days=+30)
    except Exception, e:
        print e
示例#23
0
def run_gridSoilTemps(ll,cover,sdate,edate):
	import numpy, urllib2
	from mx import DateTime
	miss = -999
	datasetBeginDate = DateTime.DateTime(2017,3,1)
	soilTempDirectory = "http://newadata.nrcc.cornell.edu/soilTemps/"
	try:
		if cover not in ['bare','grass']:
			raise gridSoilTempsException("bad cover: %s" % cover)
		#allow location provided as comma-separated string
		if isinstance(ll, basestring) and ll.find(",") > 0:
			ll = map(float, ll.split(","))
		if len(ll) == 2:
			loc = {"lon": ll[0], "lat": ll[1]}
		else:
			raise gridSoilTempsException("bad coordinates %s" % str(ll))
		gridpt = findNearestGrid(loc)
		if not gridpt:
			raise gridSoilTempsException("coordinates outside grid")
		else:
			yy,mm,dd = map(int, sdate.split('-'))
			theDate = DateTime.DateTime(yy,mm,dd)
			if theDate < datasetBeginDate:
				raise gridSoilTempsException('invalid sdate; dataset begins %s-%s-%s' % (datasetBeginDate.year,datasetBeginDate.month,datasetBeginDate.day))
			yy,mm,dd = map(int, edate.split('-'))
			edate_dt = DateTime.DateTime(yy,mm,dd)
			results = {"data": []}
			while theDate <= edate_dt:
				datestr = "%d-%02d-%02d" % (theDate.year,theDate.month,theDate.day)
				soilurl = "%ssoil2in_%s_%s.npy" % (soilTempDirectory, cover, datestr)
				try:
					soilfile = urllib2.urlopen(soilurl)
					soilTemps = numpy.loads(soilfile.read())
					dlyval = int(round(soilTemps[gridpt["lat"],gridpt["lon"]], 0))
				except:
					dlyval = miss
				results["data"].append([datestr, dlyval])
				theDate += DateTime.RelativeDate(days=+1)
	except gridSoilTempsException, e:
		results = {"error": str(e)}
示例#24
0
 def onchange_for_date_end(self, cr, uid, ids, last_medic_exam,
                           medic_exam_delay, medic_exam,
                           medic_exam_delay_alert):
     if not last_medic_exam and medic_exam:
         alert_delay = medic_exam_delay_alert
         medic_exam = mdt.DateTime(int(medic_exam[0:4]),
                                   int(medic_exam[5:7]),
                                   int(medic_exam[8:10]))
         date = str(medic_exam.year) + '-' + str(
             medic_exam.month) + '-' + str(medic_exam.day)
         if alert_delay:
             date_alert = medic_exam - alert_delay * mdt.oneDay
             date_alert = str(date_alert.year) + '-' + str(
                 date_alert.month) + '-' + str(date_alert.day)
         return {
             'value': {
                 'medic_exam': date,
                 'medic_exam_alert': date_alert
             }
         }
     elif not last_medic_exam and not medic_exam:
         return {}
     else:
         alert_delay = medic_exam_delay_alert
         medic_exam = mdt.DateTime(
             int(last_medic_exam[0:4]), int(last_medic_exam[5:7]),
             int(last_medic_exam[8:10]
                 )) + medic_exam_delay * mdt.RelativeDate(years=1)
         date = str(medic_exam.year) + '-' + str(
             medic_exam.month) + '-' + str(medic_exam.day)
         if alert_delay:
             date_alert = medic_exam - alert_delay * mdt.oneDay
             date_alert = str(date_alert.year) + '-' + str(
                 date_alert.month) + '-' + str(date_alert.day)
         return {
             'value': {
                 'medic_exam': date,
                 'medic_exam_alert': date_alert
             }
         }
示例#25
0
def SOLAR_MAIN(ICAO,sd,ed,stpr0,wthr0,dwpt0,visi0,ccnd0,chgt0,ceil0,tsky0):
# 	Define units 1=langleys; 2=MJ/m2; 3=W/m2; 4=BTU/ft2
	units=1

# 	Define Temporal resolution of model output
# 	1=Daily; 2=Hourly
	temp_res=2

# 	Missing Value and initial value for asos
	miss = -999; asos = 0
	
#	Value used when ceiling is "unlimited"
	unlimited = 100000.

#	Codes for haze and fog occurrences	
	haze_list=[81,82]
	fog_list=[70,71,72,73,74,75,77,78]

	######### Obtain the station information ###################################
	sinf = stationInfo()
	asos_date = sinf.getvar(ICAO, 'asos_date')
	lat = sinf.getvar(ICAO, 'lat')
	lon = sinf.getvar(ICAO, 'lon')
	utc_lapse = sinf.getvar(ICAO, 'gmt_offset')
	snow_station = sinf.getvar(ICAO, 'snow_station')
	############################################################################

	######### Find nearest snow station if not identified ######################
	if snow_station == None:
		varmajor = 11; detailed_check = 1
		snow_station = getNearestCoop (lat,lon,varmajor,sd,ed,detailed_check)
		sinf.setvar(ICAO, 'snow_station', snow_station)
	############################################################################

	######### Set utc_lapse to 5 if not available ##############################
	if utc_lapse == -999:
		print 'Error: no gmt offset available; using 5'
		utc_lapse = 5
	############################################################################

	######### Needed since MORECS does full days ###############################
	if ed[3] > 0:
		ed = (DateTime.DateTime(*ed) + DateTime.RelativeDate(days=+1,hour=0)).tuple()[:3]
	############################################################################

	### Initialize hourly data dictionaries
	wx,ceil={},{}
	condition,height={},{}
	pres,dew,vis,tsky,snow={},{},{},{},{}
	for yr in range(sd[0],ed[0]+1):
		wx[yr]={}; ceil[yr]={}
		condition[yr]={}; height[yr]={}
		pres[yr]={}; dew[yr]={}; vis[yr]={}; tsky[yr]={}; snow[yr]={}
		for d in range(1,367):
			wx[yr][d]={}; ceil[yr][d]={}
			condition[yr][d]={}; height[yr][d]={}
			pres[yr][d]={}; dew[yr][d]={}; vis[yr][d]={}; tsky[yr][d]={}
			for h in range(24):
				wx[yr][d][h]=[]
				condition[yr][d][h]=[]; height[yr][d][h]=[]

	### Determine number of 30-day periods ###
	d1=DateTime.Date(sd[0],sd[1],sd[2])
	d2=DateTime.Date(ed[0],ed[1],ed[2])
	num_periods=int(math.floor((d2.absdate-d1.absdate-1)/30.0))+1

	SR_out_dates=[]; SR_out=[]

	### Loop through 30-day periods
	for period in range(num_periods):

#		Create Date List for this period
		date_list=[]
		ds=DateTime.Date(sd[0],sd[1],sd[2])+DateTime.RelativeDate(days=int(30.0*period))
		date_list.append([ds.year,ds.month,ds.day])
		for p_inc in range(1,31):
			dn = ds + DateTime.RelativeDate(days=p_inc)
			if dn <= d2:
				date_list.append([dn.year,dn.month,dn.day])
			else:
				break
		################ For obtaining hourly data via TSVars ###################
		vsd,ved = date_list[0],date_list[-1]
		vsd.append(0)
		ved.append(0)
		pres_list,pres_dates           = getHourlyVars(ICAO, 'stpr', stpr0, vsd, ved)
		wx_list,wx_dates               = getHourlyVars(ICAO, 'wthr', wthr0, vsd, ved)
		dew_list,dew_dates             = getHourlyVars(ICAO, 'dwpt', dwpt0, vsd, ved)
		vis_list,vis_dates             = getHourlyVars(ICAO, 'visi', visi0, vsd, ved)
		tsky_list,tsky_dates           = getHourlyVars(ICAO, 'tsky', tsky0, vsd, ved)
		ceil_list,ceil_dates           = getHourlyVars(ICAO, 'ceil', ceil0, vsd, ved)
		condition_list,condition_dates = getHourlyVars(ICAO, 'ccnd', ccnd0, vsd, ved)
		height_list,height_dates       = getHourlyVars(ICAO, 'chgt', chgt0, vsd, ved)
		#########################################################################

		############### Specify Unlimited Ceiling ('Unl') as 100000 feet ########
		###############    No longer needed, getting ceiling as float    ########
		#for c in range(len(ceil_list)):
		#	if string.find(ceil_list[c],'Unl')!=-1: ceil_list[c]='1000.0'
		
		############### Obtain Snow Cover via TSVars ############################
		# Obtain snow cover data for this station
		var_min_list=[1,4]
		if snow_station != -1:
			snow_list,snow_dates,valid_snow = \
			solar_routines.GET_DAILY_TSVAR(snow_station,date_list[0],date_list[-1],11,var_min_list)
		else:
			valid_snow = 0
		if valid_snow:
			pass
		else:
			snow_list=[]
			snow_dates=date_list
			for i in range(len(snow_dates)):
				snow_list.append(miss)
		#########################################################################

		################ Convert lists to dictionaries ##########################
		for i in range(len(snow_dates)):
			yr=snow_dates[i][0]; month=snow_dates[i][1];
			day=snow_dates[i][2]
			ds = DateTime.Date(yr,month,day)
			j_day = ds.day_of_year
			snow[yr][j_day]=float(snow_list[i])

		for i in range(len(pres_dates)):
			yr=pres_dates[i][0]; month=pres_dates[i][1];
			day=pres_dates[i][2]; hr=pres_dates[i][3]
			ds = DateTime.Date(yr,month,day)
			j_day = ds.day_of_year
			if pres_list[i] != miss:
				pres[yr][j_day][hr]=pres_list[i]*3387.0/1000.0
			else:
				pres[yr][j_day][hr]=pres_list[i]

			dew[yr][j_day][hr]=dew_list[i]
			vis[yr][j_day][hr]=vis_list[i]
			tsky[yr][j_day][hr]=tsky_list[i]
#			ceil[yr][j_day][hr]=float(ceil_list[i])
			ceil[yr][j_day][hr]=ceil_list[i]
			condition[yr][j_day][hr]=condition_list[i].value()
			height[yr][j_day][hr]=height_list[i].value()
			wx[yr][j_day][hr]=wx_list[i].value()

			if len(condition[yr][j_day][hr])==0:
				condition[yr][j_day][hr]=[-999.0,-999.0,-999.0,-999.0]
			for x in range(4):
				try:
					if condition[yr][j_day][hr][x]<0: condition[yr][j_day][hr][x]=-999.0
				except:
					condition[yr][j_day][hr].append(5)

			if len(height[yr][j_day][hr])==0:
				height[yr][j_day][hr]=[-999.0,-999.0,-999.0,-999.0]
			for x in range(4):
				try:
					if height[yr][j_day][hr][x]<0: 
						height[yr][j_day][hr][x]=-999.0
					else: 
						height[yr][j_day][hr][x]=height[yr][j_day][hr][x]/100.0
				except:
					height[yr][j_day][hr].append(-999.0)
		#########################################################################

		## Re-classify model definitions of sky condition 0-9 instead of 1-128 ##
		cover=condition; cover_dates=condition_dates
		coverage = solar_routines.RECLASS_CCOND(condition,cover,condition_dates,tsky)
		#########################################################################

		### Loop through hourly data
		for d in date_list:
			if d==date_list[-1]: continue
			trans,cl_alb={},{}

			# Check if asos is commissioned at this station for this date (d)
			if (d[0]>asos_date[0]) or \
			   (d[0]==asos_date[0] and d[1]>asos_date[1]) or \
			   (d[0]==asos_date[0] and d[1]==asos_date[1] and d[2]>asos_date[2]):
				asos=1
			else:
				asos=0

			# define julian day
			d_cal = DateTime.Date(d[0],d[1],d[2])
			d_cal_next = d_cal+DateTime.RelativeDate(days=1)
			julian_day=d_cal.day_of_year
			julian_day_next=d_cal_next.day_of_year

			if len(height[d[0]][julian_day][0]) == 0:
				for h in range(24): 
					sr_date = [d[0],d[1],d[2],h]
					SR_out_dates.append(sr_date)
					SR_out.append(miss)
				continue

			# assign snow depth, obtained previously from nearby coop station
			try:
				snodpth=snow[d[0]][julian_day]
			except:
				snodpth=miss

			try:
				snodpth_next=snow[d[0]][julian_day_next]
			except:
				snodpth_next=miss

			# assign snow depth by averaging today's and next day's snow depth
			if (snodpth!=miss and snodpth_next!=miss):
				sno=int((snodpth+snodpth_next)/2.0)
			elif (snodpth==miss and snodpth_next!=miss):
				sno=snodpth_next
			elif (snodpth!=miss and snodpth_next==miss):
				sno=snodpth
			else:
				sno=0

			haze_hourly=[]; fog_hourly=[]
			for i in range(24):
				numlyr = 0

				# determine if fog or haze is present
				haze,fog=0,0
				for w in wx[d[0]][julian_day][i]:
					if w in haze_list: haze=1
					if w in fog_list:  fog=1

				haze_hourly.append(haze); fog_hourly.append(fog)

				# If cloud base height is missing, coverage is -BKN, -OVC
				# or -X and ceiling is unlimited, assign a height of 20,000 ft
				for l in range(3):      # loops through lowest 3 cloud layers
					if (height[d[0]][julian_day][i][l]<0.0 and \
					    (coverage[d[0]][julian_day][i][l]==3 or \
					     coverage[d[0]][julian_day][i][l]==4 or \
					     coverage[d[0]][julian_day][i][l]==5 or \
					     coverage[d[0]][julian_day][i][l]==6) and \
					     ceil[d[0]][julian_day][i]==unlimited):
						ceil[d[0]][julian_day][i] = 200.
					if height[d[0]][julian_day][i][l]!=miss: numlyr = numlyr+1

				# If cloud base height is missing, calls the subroutine ceiling to
				# obtain an estimate based on the ceiling height
				for l in range(3):      # loops through lowest 3 cloud layers
					if (height[d[0]][julian_day][i][l]<0.0 and \
					    coverage[d[0]][julian_day][i][l]!=9 and \
					    coverage[d[0]][julian_day][i][l]!=0 and \
					    ceil[d[0]][julian_day][i]!=miss):
						zhight=random.random()
						height_new = solar_routines.CEILING(height[d[0]][julian_day][i],\
								coverage[d[0]][julian_day][i],ceil[d[0]][julian_day][i],zhight)
						height[d[0]][julian_day][i]=height_new
						break

				# Hours with a height given but no coverage will be assigned
				# a coverage of obscured
				if (numlyr==1 and height[d[0]][julian_day][i][0]!=miss and \
				    coverage[d[0]][julian_day][i][0]==9 and vis[d[0]][julian_day][i]<=1):
					coverage[d[0]][julian_day][i][0] = 7

				# call routine to get the cloud transmission values
				trans[i],cl_alb[i] = \
					solar_routines.TRAN(d[0],d[1],asos,haze,fog,\
					vis[d[0]][julian_day][i],height[d[0]][julian_day][i],\
					coverage[d[0]][julian_day][i],ceil[d[0]][julian_day][i],miss)
			
#			sys.stdout.write('%s: Trans:  ' % d[0:3])
#			for kle in range(0,24):	
#				sys.stdout.write(' %5.2f' % (trans[kle]))
#			sys.stdout.write('\n')
#			sys.stdout.write('%s Cloud ht:' % d[0:3])
#			for kle in range(0,24):	
#				top = max(height[d[0]][julian_day][kle])
#				sys.stdout.write(' %5d' % (int(top)))
#			sys.stdout.write('\n')

			# call routine to calculate global solar radiation values
			sun_top,sun_clear,sun_cloud,zenith = \
				solar_routines.SOLAR_CALC_H(d[0],d[1],julian_day,\
				pres[d[0]][julian_day],dew[d[0]][julian_day],trans,cl_alb,\
				height[d[0]][julian_day],coverage[d[0]][julian_day],\
				vis[d[0]][julian_day],sno,haze_hourly, fog_hourly,\
				lat,lon,miss,utc_lapse,ICAO,asos)

			for h in range(24):
				sr_date=[d[0],d[1],d[2],h]
				SR_out_dates.append(sr_date)
				if sun_cloud[h] == miss or sun_cloud[h] == -99:
#					print 'Missing solar',ICAO,sr_date,trans[h],height[d[0]][julian_day][h],coverage[d[0]][julian_day][h],vis[d[0]][julian_day][h],pres[d[0]][julian_day][h],dew[d[0]][julian_day][h],sno,fog_hourly[h],haze_hourly[h]
#					print 'Missing solar',ICAO,sr_date,ceil[d[0]][julian_day][h]
					SR_out.append(miss)
				elif units==1:
					SR_out.append(sun_cloud[h]*23.88)
				elif units==2:
					SR_out.append(sun_cloud[h])
				elif units==3:
					SR_out.append(sun_cloud[h]/0.0036)
				elif units==4:
					SR_out.append(sun_cloud[h]*88.054)
				else:
					pass
	return SR_out_dates, SR_out
示例#26
0
def run_apple_scab_plots(stn, end_date_dt, greentip, output):
    try:
        now = DateTime.now()
        smry_dict = {}
        smry_dict['biofix_name'] = 'Greentip'
        daily_data = None
        if not end_date_dt: end_date_dt = now
        start_date_dt = DateTime.DateTime(end_date_dt.year, 3, 15, 0)

        end_date_dt = min(end_date_dt,
                          DateTime.DateTime(end_date_dt.year, 6, 15, 23))

        # greentip can either be passed into this program, read from a file, or estimated from degree day accumulation
        if not greentip:
            greentip = newaModel.Models().get_biofix(
                stn, 'as', end_date_dt.year)  #from file
            if not greentip:
                jan1_dt = DateTime.DateTime(end_date_dt.year, 1, 1)
                daily_data, station_name = newaCommon.Base().get_daily(
                    stn, jan1_dt, end_date_dt)
                biofix_dd = phen_events_dict['macph_greentip_43']['dd'][
                    2]  #by degree day accumulation
                ret_bf_date, ddaccum, ddmiss = newaModel.Models().accum_degday(
                    daily_data, jan1_dt, end_date_dt, 'dd43be', biofix_dd, stn,
                    station_name)
                if ret_bf_date: greentip = ret_bf_date

        smry_dict['biofix'] = "%s-%s-%s" % (greentip.year, greentip.month,
                                            greentip.day)

        # just use observed data (no forecast) in years other than the current
        if end_date_dt.year != now.year:
            this_year = False
            end_date_dt = end_date_dt + DateTime.RelativeDate(days=+6)
        else:
            this_year = True

        # obtain hourly and daily data
        start_date_dt = DateTime.DateTime(end_date_dt.year, 3, 1,
                                          1)  #Leave this March 1
        hourly_data, daily_data, download_time, station_name, avail_vars = newaCommon.Base(
        ).get_hddata2(stn, start_date_dt, end_date_dt)
        smry_dict['station_name'] = station_name

        if this_year:
            # now add the forecast data
            start_fcst_dt = DateTime.DateTime(
                *download_time) + DateTime.RelativeDate(hours=+1)
            end_fcst_dt = end_date_dt + DateTime.RelativeDate(days=+6)
            hourly_data = newaModel.Models().add_hrly_fcst(
                stn, hourly_data, start_fcst_dt, end_fcst_dt, True)
            daily_data = newaModel.Apple().hrly_to_dly(hourly_data)
        else:
            start_fcst_dt = end_date_dt + DateTime.RelativeDate(hours=+1)
            end_fcst_dt = end_date_dt

        # format for plot routine
        if greentip:
            start_date_grf = greentip + DateTime.RelativeDate(days=-6)
        else:
            start_date_grf = start_date_dt
        obs_dict = tp_for_grf2(daily_data, start_date_grf, start_fcst_dt,
                               this_year)

        # calculate base 0C degree days for ascospore maturity
        if greentip:
            dd_data = newaCommon.Base().degday_calcs(daily_data, greentip,
                                                     end_fcst_dt, 'dd0c',
                                                     'prcp')
        else:
            dd_data = []

        if len(dd_data) > 0:
            # calculate ascospore maturity and format for plotting
            ascospore_dict = ascospore_for_grf(dd_data, daily_data)
        else:
            ascospore_dict = {}
            ascospore_dict['dates'] = []
            ascospore_dict['maturity'] = []
            ascospore_dict['error'] = []

        # produce plot
        onLoadFunction = "produce_applescab_graph(%s, %s, %s);" % (
            smry_dict, obs_dict, ascospore_dict)
        return newaGraph_io.apple_disease_plot(onLoadFunction)
    except:
        print_exception()
示例#27
0
 def setup_time(self, eDate):
     (year, month, day) = eDate
     eTime = apply(DateTime.Date, (year, month, day, 23))
     self.eTime = eTime + DateTime.RelativeDate(hours=+1)
     self.sTime = DateTime.DateTime(year, 1, 1, 0)
示例#28
0
def getLocalFromEST(date):
    EST_time = apply(DateTime.Date, date)
    localTime = EST_time + DateTime.RelativeDate(hours=+EST_time.dst)
    day = localTime.day_of_year
    hour = localTime.hour
    return (day, hour)
示例#29
0
def getLocalFromEST_time(EST_time):
    localTime = EST_time + DateTime.RelativeDate(hours=+EST_time.dst)
    return localTime
示例#30
0
	def get_warning_day(self) :
		self.warning_sTime = self.eTime + DateTime.RelativeDate(days=-14)
		self.warning_sDay = self.warning_sTime.day_of_year
		self.warning_eDay = self.eTime.day_of_year