def getSolarPosition(t, project_data):

	# Get solar position from lat/lng, elevation and datetime
	phi, theta_h, rasc, d, h = sunpos(t, project_data['latitude'], project_data['longitude'], project_data['elevation'])[:5]
	
	# Convert azimuth (N) to azimuth (S)
	phi_south = phi - 180.0;
	
	# Calculate tilt angle from vertical
	eta = project_data['tilt'];
	
	# Calculate surface-solar azimuth angle
	gamma = math.fabs((phi_south - project_data['azimuth']));
	
	if project_data['zenith_filter'] and theta_h > project_data['zenith_limit']:
		theta_h = project_data['zenith_limit']

	# Calculate altitude angle
	beta = 90.0 - theta_h;
	
	# Calculate incident angle to surface
	theta = rad2deg(math.acos((( math.cos(deg2rad(beta)) * math.cos(deg2rad(gamma)) * math.sin(deg2rad(eta)) ) + (math.sin(deg2rad(beta)) * math.cos(deg2rad(eta))))));
	
	# Solar position datum
	sp_datum = {
		'Datetime_UTC': t,
		'Azimuth': phi,
		'Zenith': theta_h,
		'RightAscension': rasc,
		'Declination': d,
		'HourAngle': h,
		'IncidentAngle': theta
	}
	
	return sp_datum
    def rad_clear_instant(self):
        
        self.utc = self.lt - relativedelta(hours=self.ltz)
        self.theta = sunpos(self.utc, self.lat, self.lon, self.z, radians=True)[1]
        if self.theta>=np.pi/2.0:
            rad_total, rad_beam, rad_diff, rad_norm = 0.0, 0.0, 0.0, 0.0
        else:
            self.jd = self.utc.timetuple().tm_yday       
            sun2earth = 1.0-0.01672*np.cos(0.9856*(self.jd-4.0)*np.pi/180.) # relative sun-earth distance [-]
            s  = hybrid.s0*sun2earth**2
            I0 = s*np.cos(self.theta)
            self._air_mass()
            self._corrected_air_mass()
            self._taug()
            self._taur()
            self._tauw()
            self._tauo()
            self._taua()
            self._tau_clear_beam()
            self._tau_clear_diff()
            rad_beam  = I0*self.tau_clear_beam
            rad_diff  = I0*self.tau_clear_diff
            rad_norm  = s *self.tau_clear_beam
            rad_total = rad_beam + rad_diff

        return rad_total, rad_beam, rad_diff, rad_norm
Exemple #3
0
 def coszen(self):
     self.dt = self.datetime
     az, zen, ra, dec, h = sunpos(self.datetime,
                                  self.latitude,
                                  self.longitudeE,
                                  self.surface_elevation_km * 1000.0,
                                  radians=True)
     return s.cos(zen)
Exemple #4
0
def main():
    parser = argparse.ArgumentParser()
    parser.description = "draw analemma, the Sun runs from red point to green point to blue point"
    parser.add_argument("--camera_azimuth", default=-1000, help="azimuth", type=float)
    parser.add_argument("--camera_pitch", default=-1000, help="pitch", type=float)
    parser.add_argument("--camera_roll", default=-1000, help="roll", type=float)
    parser.add_argument("--facing_back", default=True, help="camera facing back or not", type=lambda s: s == "True")
    parser.add_argument("--focal_length", default=24, help="camera focal length in mm", type=float)
    parser.add_argument("--sensor_width", default=36, help="camera sensor width in mm", type=float)
    parser.add_argument("--sensor_height", default=24, help="camera sensor height in mm", type=float)
    parser.add_argument("--pixel_width", default=5760, help="image width in pixel", type=int)
    parser.add_argument("--pixel_height", default=3840, help="image height in pixel", type=int)

    parser.add_argument("--datetime", default=datetime.utcnow(), help="UTC datetime in format %%Y-%%m-%%d H:M:S", type=lambda s: datetime.strptime(s, '%Y-%m-%d %H:%M:%S'))
    parser.add_argument("--npoints_before", default=15, help="number of points before datetime", type=int)
    parser.add_argument("--npoints_after", default=15, help="number of points after datetime", type=int)

    parser.add_argument("--latitude", help="latitude", type=float)
    parser.add_argument("--longitude", help="longitude", type=float)
    parser.add_argument("--elevation", default=0, help="elevation", type=float)

    parser.add_argument("--save", metavar="FILENAME", nargs="?", const="analemma.png", help="save image", type=str)

    args = parser.parse_args()
    if args.latitude == None or args.longitude == None:
        args.latitude, args.longitude = getLatitudeLongitude()
    if args.camera_azimuth == -1000 or args.camera_pitch == -1000 or args.camera_roll == -1000:
        marchDateTime = args.datetime - timedelta(days = (args.datetime.month - 3)*30)
        default_azimuth, default_zenith = sunpos(marchDateTime, latitude=args.latitude, longitude=args.longitude, elevation=args.elevation)[:2]
        # if args.facing_back == True:
        #     args.camera_azimuth = default_azimuth
        #     args.camera_pitch = default_zenith - 180.0
        #     args.camera_roll = 0.0
        # else:
        #     args.camera_azimuth = default_azimuth - 180.0
        #     if args.camera_azimuth < 0.0:
        #         args.camera_azimuth += 360.0
        #     args.camera_pitch = -default_zenith
        #     args.camera_roll = 0.0
        if default_zenith <= 90.0:
            args.camera_azimuth = default_azimuth - 180.0
            if args.camera_azimuth < 0:
                args.camera_azimuth += 360.0
            args.camera_pitch = -default_zenith
            args.camera_roll = 180.0
        else:
            args.camera_azimuth = default_azimuth
            args.camera_pitch = default_zenith - 180.0
            args.camera_roll = 0.0
        if args.facing_back == False:
            args.camera_roll += 180.0
            if args.camera_roll > 180.0:
                args.camera_roll -= 360.0
        print "default camera_azimuth is %f" % args.camera_azimuth
        print "default camera_pitch is %f" % args.camera_pitch
        print "default camera_roll is %f" % args.camera_roll
    xs, ys, colors = getPoints(args)
    plot(args, xs, ys, colors)
Exemple #5
0
def getSolarPositions(args):
    dates = getDateTimes(args.datetime, args.npoints_before, args.npoints_after)
    azimuth_list = []
    zenith_list = []
    for date in dates:
        azimuth,zenith = sunpos(date, latitude=args.latitude, longitude=args.longitude, elevation=args.elevation)[:2]
        azimuth_list.append(azimuth)
        zenith_list.append(zenith)
    return azimuth_list,zenith_list
Exemple #6
0
 def coszen(self):
     """ Return the cosine of the solar zenith."""
     self.dt = self.datetime
     az, zen, ra, dec, h = sunpos(self.datetime,
                                  self.latitude,
                                  self.longitudeE,
                                  self.surface_elevation_km * 1000.0,
                                  radians=True)
     return s.cos(zen)
Exemple #7
0
def calculate_g(direction, tilt, latitude, longitude, time):
    if time is None:
        time = datetime.utcnow()
    azimuth, zenith, _, _, _ = sunpos(time, [latitude], [longitude], 0)[0]

    factor = cos((zenith - tilt) * 0.017453)
    if factor < 0 or zenith > 90:
        factor = 0
    factor *= cos((azimuth - direction) * 0.017453)
    if factor < 0:
        factor = 0
    return 1000 * factor
def init():
    global loopRunning
    loopRunning = True

    while loopRunning:
        time.sleep(5)
        now = datetime.utcnow()
        for thing in myThings():
            lat = thing.paramValue(sunPositionThingLatitudeParamTypeId)
            lon = thing.paramValue(sunPositionThingLongitudeParamTypeId)
            az, zen = sunpos(now, lat, lon, 0)[:2]
            angle = 90 - zen.item()
            logger.log("Updating thing", thing.name, "Angle:", angle)
            thing.setStateValue(sunPositionAngleStateTypeId, angle)
Exemple #9
0
def get_time_and_sza(args, dataframe, longitude, latitude):
	dtime_1970, tz = common.time_common(args.timezone)

	num_rows = dataframe['year'].size
	time, time_bounds, sza = ([0] * num_rows for _ in range(3))

	for idx in range(num_rows):
		keys = ('year', 'month', 'day', 'hour')
		dtime = datetime(*[dataframe[k][idx] for k in keys])
		dtime = tz.localize(dtime.replace(tzinfo=None))

		time[idx] = (dtime - dtime_1970).total_seconds()
		time_bounds[idx] = (time[idx], time[idx] + common.seconds_in_hour)

		sza[idx] = sunpos(dtime, latitude, longitude, 0)[1]

	return time, time_bounds, sza
Exemple #10
0
def solve(args):
    datalist = json.loads(args.data)
    worldPoints = np.empty(shape=[3, 0])
    viewPoints = np.empty(shape=[3, 0])
    for i in range(len(datalist)):
        data = datalist[i]
        xInPixel = data[0]
        yInPixel = data[1]
        date = datetime.strptime(data[2], '%Y-%m-%d %H:%M:%S')
        azimuth, zenith = sunpos(date, latitude=args.latitude, longitude=args.longitude, elevation=args.elevation)[:2]
        worldPoint = worldAzimuthZenith2WorldPoint(azimuth, zenith)
        xInMM, yInMM = pixel2MM(xInPixel, yInPixel, args.pixel_width, args.pixel_height, args.sensor_width, args.sensor_height)
        viewPoint = projection2ViewPoint(xInMM, yInMM, args.focal_length, args.sensor_width, args.sensor_height, args.facing_back)
        worldPoints = np.append(worldPoints, worldPoint, 1)
        viewPoints = np.append(viewPoints, viewPoint, 1)
    cameraAzimuth, cameraPitch, cameraRoll = getOrientation(worldPoints, viewPoints)
    return cameraAzimuth,cameraPitch, cameraRoll
Exemple #11
0
def get_time_and_sza(args, input_file, latitude, longitude):
	dtime_1970, tz = common.time_common(args.timezone)
	header_rows = 8

	with open(input_file) as stream:
		lines = stream.readlines()[header_rows:]

	time, bounds, sza = [], [], []
	for line in lines:
		dtime = line.strip().split(",")[0]
		dtime = datetime.strptime(dtime, '%Y-%m-%dT%H:%M:%SZ')
		dtime = tz.localize(dtime.replace(tzinfo=None))

		seconds = (dtime - dtime_1970).total_seconds()
		time.append(seconds)
		bounds.append((seconds - common.seconds_in_hour, seconds))

		sza.append(sunpos(dtime, latitude, longitude, 0)[1])

	return time, bounds, sza
Exemple #12
0
def get_time_and_sza(args, dataframe, longitude, latitude):
	# Divided by 4 because each hour value is a multiple of 4
	# and then multiplied by 100 to convert decimal to integer
	hour_conversion = 100 / 4
	last_hour = 23
	seconds_in_hour = common.seconds_in_hour
	num_rows = dataframe['year'].size

	month, day, time, time_bounds, sza = ([0] * num_rows for _ in range(5))

	hour = dataframe['julian_decimal_time']
	hour = [round(i - int(i), 3) * hour_conversion for i in hour]
	hour = [int(h) if int(h) <= last_hour else 0 for h in hour]

	dtime_1970, tz = common.time_common(args.timezone)

	for idx in range(num_rows):
		time_year = dataframe['year'][idx]
		time_j = int(dataframe['julian_decimal_time'][idx])
		time_hour = hour[idx]

		temp_dtime = '{} {} {}'.format(time_year, time_j, time_hour)
		temp_dtime = datetime.strptime(temp_dtime, "%Y %j %H")
		temp_dtime = tz.localize(temp_dtime.replace(tzinfo=None))

		if time_j <= 366:
			time[idx] = (temp_dtime - dtime_1970).total_seconds()
		else:
			# Assign time of previous row, if julian_decimal_time > 366
			time[idx] = time[idx - 1]

		time_bounds[idx] = (time[idx] - seconds_in_hour, time[idx])

		sza[idx] = sunpos(temp_dtime, latitude, longitude, 0)[1]

	return hour, month, day, time, time_bounds, sza
Exemple #13
0
def aaws2nc(args, op_file, station_dict, station_name, convert_temp, convert_press, seconds_in_hour, fillvalue_double):

	header_rows = 8

	column_names = ['timestamp', 'air_temp', 'vtempdiff', 'rh', 'pressure', 'wind_dir', 'wind_spd']

	df = pd.read_csv(args.input_file or args.fl_in, skiprows = header_rows, skip_blank_lines=True, header=None, names = column_names)
	df.index.name = 'time'
	df.loc[:,'air_temp'] += convert_temp
	df.loc[:,'pressure'] *= convert_press
	df =  df.where((pd.notnull(df)), fillvalue_double)

	ds = xr.Dataset.from_dataframe(df)
	ds = ds.drop('time')


	# Intializing variables
	num_rows =  df['timestamp'].size
	time, time_bounds, sza = ([0]*num_rows for x in range(3))
	
	
	print('retrieving latitude and longitude...')
	
	f = open(args.input_file or args.fl_in)
	f.readline()
	for line in f:
		x = str(line[12:].strip('\n'))
		break
	f.close()

	if x == 'AGO-4':
		temp_stn = 'aaws_ago4'
	elif x == 'Alexander Tall Tower!':
		temp_stn = 'aaws_alexander'
	elif x == 'Austin':
		temp_stn = 'aaws_austin'
	elif x == 'Baldrick':
		temp_stn = 'aaws_baldrick'
	elif x == 'Bear Peninsula':
		temp_stn = 'aaws_bearpeninsula'
	elif x == 'Bonaparte Point':
		temp_stn = 'aaws_bonapartepoint'
	elif x == 'Byrd':
		temp_stn = 'aaws_byrd'
	elif x == 'Cape Bird':
		temp_stn = 'aaws_capebird'
	elif x == 'Cape Denison':
		temp_stn = 'aaws_capedenison'
	elif x == 'Cape Hallett':
		temp_stn = 'aaws_capehallett'
	elif x == 'D-10':
		temp_stn = 'aaws_d10'
	elif x == 'D-47':
		temp_stn = 'aaws_d47'
	elif x == 'D-85':
		temp_stn = 'aaws_d85'
	elif x == 'Dismal Island':
		temp_stn = 'aaws_dismalisland'
	elif x == 'Dome C II':
		temp_stn = 'aaws_domecII'
	elif x == 'Dome Fuji':
		temp_stn = 'aaws_domefuji'
	elif x == 'Elaine':
		temp_stn = 'aaws_elaine'
	elif x == 'Elizabeth':
		temp_stn = 'aaws_elizabeth'
	elif x == 'Emilia':
		temp_stn = 'aaws_emilia'
	elif x == 'Emma':
		temp_stn = 'aaws_emma'
	elif x == 'Erin':
		temp_stn = 'aaws_erin'
	elif x == 'Evans Knoll':
		temp_stn = 'aaws_evansknoll'
	elif x == 'Ferrell':
		temp_stn = 'aaws_ferrell'
	elif x == 'Gill':
		temp_stn = 'aaws_gill'
	elif x == 'Harry':
		temp_stn = 'aaws_harry'
	elif x == 'Henry':
		temp_stn = 'aaws_henry'
	elif x == 'Janet':
		temp_stn = 'aaws_janet'
	elif x == 'JASE2007':
		temp_stn = 'aaws_jase2007'
	elif x == 'Kathie':
		temp_stn = 'aaws_kathie'
	elif x == 'Kominko-Slade':
		temp_stn = 'aaws_kominkoslade'
	elif x == 'Laurie II':
		temp_stn = 'aaws_laurieII'
	elif x == 'Lettau':
		temp_stn = 'aaws_lettau'
	elif x == 'Linda':
		temp_stn = 'aaws_linda'
	elif x == 'Lorne':
		temp_stn = 'aaws_lorne'
	elif x == 'Manuela':
		temp_stn = 'aaws_manuela'
	elif x == 'Marble Point':
		temp_stn = 'aaws_marblepoint'
	elif x == 'Marble Point II':
		temp_stn = 'aaws_marblepointII'
	elif x == 'Margaret':
		temp_stn = 'aaws_margaret'
	elif x == 'Marilyn':
		temp_stn = 'aaws_marilyn'
	elif x == 'Minna Bluff':
		temp_stn = 'aaws_minnabluff'
	elif x == 'Mizuho':
		temp_stn = 'aaws_mizuho'
	elif x == 'Mount Siple':
		temp_stn = 'aaws_mountsiple'
	elif x == 'Nico':
		temp_stn = 'aaws_nico'
	elif x == 'PANDA-South':
		temp_stn = 'aaws_pandasouth'
	elif x == 'Pegasus North':
		temp_stn = 'aaws_pegasusnorth'
	elif x == 'Phoenix':
		temp_stn = 'aaws_phoenix'
	elif x == 'Port Martin':
		temp_stn = 'aaws_portmartin'
	elif x == 'Possession Island':
		temp_stn = 'aaws_possessionisland'
	elif x == 'Relay Station':
		temp_stn = 'aaws_relaystation'
	elif x == 'Sabrina':
		temp_stn = 'aaws_sabrina'
	elif x == 'Schwerdtfeger':
		temp_stn = 'aaws_schwerdtfeger'
	elif x == 'Siple Dome':
		temp_stn = 'aaws_sipledome'
	elif x == 'Theresa':
		temp_stn = 'aaws_theresa'
	elif x == 'Thurston Island':
		temp_stn = 'aaws_thurstonisland'
	elif x == 'Vito':
		temp_stn = 'aaws_vito'
	elif x == 'White Island':
		temp_stn = 'aaws_whiteisland'
	elif x == 'Whitlock':
		temp_stn = 'aaws_whitlock'
	elif x == 'Willie Field':
		temp_stn = 'aaws_williefield'
	elif x == 'Windless Bight':
		temp_stn = 'aaws_windlessbight'
	
	latitude = (station_dict.get(temp_stn)[0])
	longitude = (station_dict.get(temp_stn)[1])

	
	print('retrieving station name...')

	if args.station_name:
		print('Default station name overrided by user provided station name')
	else:
		station_name = x


	print('calculating time and sza...')
	
	tz = pytz.timezone(args.timezone)
	dtime_1970 = datetime(1970,1,1)
	dtime_1970 = tz.localize(dtime_1970.replace(tzinfo=None))
	i = 0
	
	with open(args.input_file or args.fl_in, "r") as infile:
		for line in infile.readlines()[header_rows:]:
			temp_dtime = datetime.strptime(line.strip().split(",")[0], '%Y-%m-%dT%H:%M:%SZ')
			temp_dtime = tz.localize(temp_dtime.replace(tzinfo=None))		
			time[i] = (temp_dtime-dtime_1970).total_seconds()
			
			time_bounds[i] = (time[i]-seconds_in_hour, time[i])
			
			sza[i] = sunpos(temp_dtime,latitude,longitude,0)[1]
			
			i += 1

	ds['time'] = (('time'),time)
	ds['time_bounds'] = (('time', 'nbnd'),time_bounds)
	ds['sza'] = (('time'),sza)
	ds['station_name'] = ((),station_name)
	ds['latitude'] = ((),latitude)
	ds['longitude'] = ((),longitude)
	

	ds.attrs = {'source':'surface observation', 'featureType':'timeSeries', 'institution':'UW SSEC', 'reference':'https://amrc.ssec.wisc.edu/', 'Conventions':'CF-1.7', 'data_type':'q1h', 'time_convention':"'time: point' variables match the time coordinate values exactly, whereas 'time: mean' variables are valid for the mean time within the time_bounds variable." + " e.g.: air_temp is continuously measured and then hourly-mean values are stored for each period contained in the time_bounds variable"}

	ds['air_temp'].attrs= {'units':'kelvin', 'long_name':'air temperature', 'standard_name':'air_temperature', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['vtempdiff'].attrs= {'units':'1', 'long_name':'vertical temperature differential', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['rh'].attrs= {'units':'1', 'long_name':'relative humidity', 'standard_name':'relative_humidity', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['pressure'].attrs= {'units':'pascal', 'long_name':'air pressure', 'standard_name':'air_pressure', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['wind_dir'].attrs= {'units':'degree', 'long_name':'wind direction', 'standard_name':'wind_from_direction', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['wind_spd'].attrs= {'units':'meter second-1', 'long_name':'wind speed', 'standard_name':'wind_speed', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['time'].attrs= {'units':'seconds since 1970-01-01 00:00:00', 'long_name':'time of measurement',	'standard_name':'time', 'bounds':'time_bounds', 'calendar':'noleap'}
	ds['sza'].attrs= {'units':'degree', 'long_name':'Solar Zenith Angle', 'standard_name':'solar_zenith_angle', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['station_name'].attrs= {'long_name':'Station Name', 'cf_role':'timeseries_id'}
	ds['latitude'].attrs= {'units':'degrees_north', 'standard_name':'latitude'}
	ds['longitude'].attrs= {'units':'degrees_east', 'standard_name':'longitude'}
	

	encoding = {'air_temp': {'_FillValue': fillvalue_double},
				'vtempdiff': {'_FillValue': fillvalue_double},
				'rh': {'_FillValue': fillvalue_double},
				'pressure': {'_FillValue': fillvalue_double},
				'wind_dir': {'_FillValue': fillvalue_double},
				'wind_spd': {'_FillValue': fillvalue_double},
				'time': {'_FillValue': False},
				'time_bounds': {'_FillValue': False},
				'sza': {'_FillValue': False},
				'latitude': {'_FillValue': False},
				'longitude': {'_FillValue': False}
				}


	write_data(args, ds, op_file, encoding)
Exemple #14
0
def promice2nc(args, op_file, station_dict, station_name, convert_temp,
               convert_press, seconds_in_hour, fillvalue_double):

    header_rows = 1
    convert_current = 1000
    check_na = -999

    column_names = [
        'year', 'month', 'day', 'hour', 'day_of_year', 'day_of_century',
        'air_pressure', 'air_temperature', 'air_temperature_hygroclip',
        'relative_humidity_wrtwater', 'relative_humidity', 'wind_speed',
        'wind_direction', 'shortwave_radiation_down',
        'shortwave_radiation_down_cor', 'shortwave_radiation_up',
        'shortwave_radiation_up_cor', 'albedo_theta',
        'longwave_radiation_down', 'longwave_radiation_up', 'cloudcover',
        'surface_temp', 'height_sensor_boom', 'height_stakes',
        'depth_pressure_transducer', 'depth_pressure_transducer_cor',
        'ice_temp_01', 'ice_temp_02', 'ice_temp_03', 'ice_temp_04',
        'ice_temp_05', 'ice_temp_06', 'ice_temp_07', 'ice_temp_08',
        'tilt_east', 'tilt_north', 'time_GPS', 'latitude_GPS', 'longitude_GPS',
        'elevation', 'hor_dil_prec', 'logger_temp', 'fan_current',
        'battery_voltage'
    ]

    df = pd.read_csv(args.input_file or args.fl_in,
                     delim_whitespace=True,
                     skiprows=header_rows,
                     skip_blank_lines=True,
                     header=None,
                     names=column_names)
    df.index.name = 'time'
    df.replace(check_na, np.nan, inplace=True)
    df.loc[:, [
        'air_temperature', 'air_temperature_hygroclip', 'surface_temp',
        'ice_temp_01', 'ice_temp_02', 'ice_temp_03', 'ice_temp_04',
        'ice_temp_05', 'ice_temp_06', 'ice_temp_07', 'ice_temp_08',
        'logger_temp'
    ]] += convert_temp
    df.loc[:, ['air_pressure']] *= convert_press
    df.loc[:, ['fan_current']] /= convert_current
    df = df.where((pd.notnull(df)), fillvalue_double)

    ds = xr.Dataset.from_dataframe(df)
    ds = ds.drop('time')

    # Intializing variables
    num_rows = df['year'].size
    time, time_bounds, sza, velocity, ice_velocity_GPS_total, ice_velocity_GPS_x, ice_velocity_GPS_y = (
        [0] * num_rows for x in range(7))

    print('retrieving lat and lon...')
    k = os.path.basename(args.input_file or args.fl_in)

    if ('EGP') in k:
        temp_stn = 'promice_egp'
    elif ('KAN_B') or ('Kangerlussuaq-B') in k:
        temp_stn = 'promice_kanb'
    elif ('KAN_L') or ('Kangerlussuaq-L') in k:
        temp_stn = 'promice_kanl'
    elif ('KAN_M') or ('Kangerlussuaq-M') in k:
        temp_stn = 'promice_kanm'
    elif ('KAN_U') or ('Kangerlussuaq-U') in k:
        temp_stn = 'promice_kanu'
    elif ('KPC_L') or ('KronprinsChristianland-L') in k:
        temp_stn = 'promice_kpcl'
    elif ('KPC_U') or ('KronprinsChristianland-U') in k:
        temp_stn = 'promice_kpcu'
    elif ('MIT') in k:
        temp_stn = 'promice_mit'
    elif ('NUK_K') or ('Nuuk-K') in k:
        temp_stn = 'promice_nukk'
    elif ('NUK_L') or ('Nuuk-L') in k:
        temp_stn = 'promice_nukl'
    elif ('NUK_N') or ('Nuuk-N') in k:
        temp_stn = 'promice_nukn'
    elif ('NUK_U') or ('Nuuk-U') in k:
        temp_stn = 'promice_nuku'
    elif ('QAS_A') or ('Qassimiut-A') in k:
        temp_stn = 'promice_qasa'
    elif ('QAS_L') or ('Qassimiut-L') in k:
        temp_stn = 'promice_qasl'
    elif ('QAS_M') or ('Qassimiut-M') in k:
        temp_stn = 'promice_qasm'
    elif ('QAS_U') or ('Qassimiut-U') in k:
        temp_stn = 'promice_qasu'
    elif ('SCO_L') or ('Scoresbysund-L') in k:
        temp_stn = 'promice_scol'
    elif ('SCO_U') or ('Scoresbysund-U') in k:
        temp_stn = 'promice_scou'
    elif ('TAS_A') or ('Tasiilaq-A') in k:
        temp_stn = 'promice_tasa'
    elif ('TAS_L') or ('Tasiilaq-L') in k:
        temp_stn = 'promice_tasl'
    elif ('TAS_U') or ('Tasiilaq-U') in k:
        temp_stn = 'promice_tasu'
    elif ('THU_L') or ('ThuleAirbase-L') in k:
        temp_stn = 'promice_thul'
    elif ('THU_U') or ('ThuleAirbase-U') in k:
        temp_stn = 'promice_thuu'
    elif ('UPE_L') or ('Upernavik-L') in k:
        temp_stn = 'promice_upel'
    elif ('UPE_U') or ('Upernavik-U') in k:
        temp_stn = 'promice_upeu'
    elif ('CEN') in k:
        temp_stn = 'promice_cen'

    latitude = (station_dict.get(temp_stn)[0])
    longitude = (station_dict.get(temp_stn)[1])

    if args.station_name:
        print('Default station name overrided by user provided station name')
    else:
        station_name = station_dict.get(temp_stn)[2]

    print('converting lat_GPS and lon_GPS...')

    def lat_lon_gps(coords):
        deg = np.floor(coords / 100)
        minutes = np.floor(((coords / 100) - deg) * 100)
        seconds = (((coords / 100) - deg) * 100 - minutes) * 100
        return deg + minutes / 60 + seconds / 3600

    # Exclude NAs
    logic1 = df.latitude_GPS != fillvalue_double
    logic2 = df.longitude_GPS != fillvalue_double
    df1 = df[logic1]
    df2 = df[logic2]

    df.latitude_GPS = lat_lon_gps(df1.latitude_GPS)
    df.longitude_GPS = lat_lon_gps(df2.longitude_GPS)

    print('calculating time and sza...')

    tz = pytz.timezone(args.timezone)
    dtime_1970 = datetime(1970, 1, 1)
    dtime_1970 = tz.localize(dtime_1970.replace(tzinfo=None))
    i = 0

    while i < num_rows:

        temp_dtime = datetime(df['year'][i], df['month'][i], df['day'][i],
                              df['hour'][i])
        temp_dtime = tz.localize(temp_dtime.replace(tzinfo=None))
        time[i] = (temp_dtime - dtime_1970).total_seconds()

        time_bounds[i] = (time[i], time[i] + seconds_in_hour)

        sza[i] = sunpos(temp_dtime, latitude, longitude, 0)[1]

        i += 1

    print('calculating ice velocity...')

    def ice_velocity(n, o):
        m, p = 0, 1
        R = 6373.0  #Approx radius of earth
        while p < num_rows:
            if (df['latitude_GPS'][m] == fillvalue_double
                    or df['longitude_GPS'][m] == fillvalue_double
                    or df['latitude_GPS'][n] == fillvalue_double
                    or df['longitude_GPS'][o] == fillvalue_double):
                velocity[m] = fillvalue_double
            else:
                lat1 = radians(df['latitude_GPS'][m])
                lon1 = radians(df['longitude_GPS'][m])
                lat2 = radians(df['latitude_GPS'][n])
                lon2 = radians(df['longitude_GPS'][o])

                dlat = lat2 - lat1
                dlon = lon2 - lon1

                a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2
                c = 2 * atan2(sqrt(a), sqrt(1 - a))

                distance = (
                    R * c) * 1000  #Multiplied by 1000 to convert km to meters

                velocity[m] = round(
                    distance / seconds_in_hour, 4
                )  #Divided by 3600 because time change between 2 records is one hour

            m += 1
            n += 1
            o += 1
            p += 1

        return velocity[:]

    ice_velocity_GPS_total[:] = ice_velocity(1, 1)
    ice_velocity_GPS_x[:] = ice_velocity(0, 1)
    ice_velocity_GPS_y[:] = ice_velocity(1, 0)

    ds['ice_velocity_GPS_total'] = (('time'), ice_velocity_GPS_total)
    ds['ice_velocity_GPS_x'] = (('time'), ice_velocity_GPS_x)
    ds['ice_velocity_GPS_y'] = (('time'), ice_velocity_GPS_y)
    ds['time'] = (('time'), time)
    ds['time_bounds'] = (('time', 'nbnd'), time_bounds)
    ds['sza'] = (('time'), sza)
    ds['station_name'] = ((), station_name)
    ds['latitude'] = ((), latitude)
    ds['longitude'] = ((), longitude)

    ds.attrs = {
        'title':
        'Weather Station Data',
        'source':
        'Surface Observations',
        'featureType':
        'timeSeries',
        'institution':
        'Programme for Monitoring of the Greenland Ice Sheet',
        'reference':
        'http://www.promice.dk/home.html',
        'Conventions':
        'CF-1.7',
        'time_convention':
        "'time: point' variables match the time coordinate values exactly, whereas 'time: mean' variables are valid for the mean time within the time_bounds variable."
        +
        " e.g.: elevation is measured once per hour at the time stored in the 'time' coordinate."
        +
        " On the other hand, air_temperature is continuously measured and then hourly-mean values are stored for each period contained in the time_bounds variable"
    }

    ds['year'].attrs = {'units': '1', 'long_name': 'Year'}
    ds['month'].attrs = {'units': '1', 'long_name': 'Month of Year'}
    ds['day'].attrs = {'units': '1', 'long_name': 'Day of Month'}
    ds['hour'].attrs = {'units': '1', 'long_name': 'Hour of Day(UTC)'}
    ds['day_of_year'].attrs = {'units': '1', 'long_name': 'Day of Year'}
    ds['day_of_century'].attrs = {'units': '1', 'long_name': 'Day of Century'}
    ds['air_pressure'].attrs = {
        'units': 'pascal',
        'long_name': 'Air Pressure',
        'standard_name': 'air_pressure',
        'coordinates': 'longitude latitude',
        'cell_methods': 'time: mean'
    }
    ds['air_temperature'].attrs = {
        'units': 'kelvin',
        'long_name': 'Air Temperature',
        'standard_name': 'air_temperature',
        'coordinates': 'longitude latitude',
        'cell_methods': 'time: mean'
    }
    ds['air_temperature_hygroclip'].attrs = {
        'units': 'kelvin',
        'long_name': 'Air Temperature HygroClip',
        'standard_name': 'air_temperature',
        'coordinates': 'longitude latitude',
        'cell_methods': 'time: mean'
    }
    ds['relative_humidity_wrtwater'].attrs = {
        'units': '1',
        'long_name': 'Relative Humidity wrt Water',
        'standard_name': 'relative_humidity',
        'coordinates': 'longitude latitude',
        'cell_methods': 'time: mean'
    }
    ds['relative_humidity'].attrs = {
        'units': '1',
        'long_name': 'Relative Humidity',
        'standard_name': 'relative_humidity',
        'coordinates': 'longitude latitude',
        'cell_methods': 'time: mean'
    }
    ds['wind_speed'].attrs = {
        'units': 'meter second-1',
        'long_name': 'Wind Speed',
        'standard_name': 'wind_speed',
        'coordinates': 'longitude latitude',
        'cell_methods': 'time: mean'
    }
    ds['wind_direction'].attrs = {
        'units': 'degree',
        'long_name': 'Wind Direction',
        'standard_name': 'wind_from_direction',
        'coordinates': 'longitude latitude',
        'cell_methods': 'time: mean'
    }
    ds['shortwave_radiation_down'].attrs = {
        'units': 'watt meter-2',
        'long_name': 'Shortwave Radiation Down',
        'standard_name': 'downwelling_shortwave_flux_in_air',
        'coordinates': 'longitude latitude',
        'cell_methods': 'time: mean'
    }
    ds['shortwave_radiation_down_cor'].attrs = {
        'units': 'watt meter-2',
        'long_name': 'Shortwave Radiation Down Cor',
        'standard_name': 'downwelling_shortwave_flux_in_air',
        'coordinates': 'longitude latitude',
        'cell_methods': 'time: mean'
    }
    ds['shortwave_radiation_up'].attrs = {
        'units': 'watt meter-2',
        'long_name': 'Shortwave Radiation Up',
        'standard_name': 'upwelling_shortwave_flux_in_air',
        'coordinates': 'longitude latitude',
        'cell_methods': 'time: mean'
    }
    ds['shortwave_radiation_up_cor'].attrs = {
        'units': 'watt meter-2',
        'long_name': 'Shortwave Radiation Up Cor',
        'standard_name': 'upwelling_shortwave_flux_in_air',
        'coordinates': 'longitude latitude',
        'cell_methods': 'time: mean'
    }
    ds['albedo_theta'].attrs = {
        'units': '1',
        'long_name': 'Albedo_theta<70d',
        'standard_name': 'surface_albedo',
        'coordinates': 'longitude latitude',
        'cell_methods': 'time: mean'
    }
    ds['longwave_radiation_down'].attrs = {
        'units': 'watt meter-2',
        'long_name': 'Longwave Radiation Down',
        'standard_name': 'downwelling_longwave_flux_in_air',
        'coordinates': 'longitude latitude',
        'cell_methods': 'time: mean'
    }
    ds['longwave_radiation_up'].attrs = {
        'units': 'watt meter-2',
        'long_name': 'Longwave Radiation Up',
        'standard_name': 'upwelling_longwave_flux_in_air',
        'coordinates': 'longitude latitude',
        'cell_methods': 'time: mean'
    }
    ds['cloudcover'].attrs = {
        'units': '1',
        'long_name': 'Cloud Cover',
        'standard_name': '',
        'coordinates': 'longitude latitude',
        'cell_methods': 'time: mean'
    }
    ds['surface_temp'].attrs = {
        'units': 'kelvin',
        'long_name': 'Surface Temperature',
        'standard_name': 'surface_temperature',
        'coordinates': 'longitude latitude',
        'cell_methods': 'time: mean'
    }
    ds['height_sensor_boom'].attrs = {
        'units': 'meter',
        'long_name': 'Height Sensor Boom',
        'standard_name': '',
        'coordinates': 'longitude latitude',
        'cell_methods': 'time: point'
    }
    ds['height_stakes'].attrs = {
        'units': 'meter',
        'long_name': 'Height Stakes',
        'standard_name': '',
        'coordinates': 'longitude latitude',
        'cell_methods': 'time: point'
    }
    ds['depth_pressure_transducer'].attrs = {
        'units': 'meter',
        'long_name': 'Depth Pressure Transducer',
        'standard_name': '',
        'coordinates': 'longitude latitude',
        'cell_methods': 'time: point'
    }
    ds['depth_pressure_transducer_cor'].attrs = {
        'units': 'meter',
        'long_name': 'Depth Pressure Transducer Cor',
        'standard_name': '',
        'coordinates': 'longitude latitude',
        'cell_methods': 'time: point'
    }
    ds['ice_temp_01'].attrs = {
        'units': 'kelvin',
        'long_name': 'Ice Temperature 1',
        'standard_name': 'land_ice_temperature',
        'coordinates': 'longitude latitude',
        'cell_methods': 'time: mean'
    }
    ds['ice_temp_02'].attrs = {
        'units': 'kelvin',
        'long_name': 'Ice Temperature 2',
        'standard_name': 'land_ice_temperature',
        'coordinates': 'longitude latitude',
        'cell_methods': 'time: mean'
    }
    ds['ice_temp_03'].attrs = {
        'units': 'kelvin',
        'long_name': 'Ice Temperature 3',
        'standard_name': 'land_ice_temperature',
        'coordinates': 'longitude latitude',
        'cell_methods': 'time: mean'
    }
    ds['ice_temp_04'].attrs = {
        'units': 'kelvin',
        'long_name': 'Ice Temperature 4',
        'standard_name': 'land_ice_temperature',
        'coordinates': 'longitude latitude',
        'cell_methods': 'time: mean'
    }
    ds['ice_temp_05'].attrs = {
        'units': 'kelvin',
        'long_name': 'Ice Temperature 5',
        'standard_name': 'land_ice_temperature',
        'coordinates': 'longitude latitude',
        'cell_methods': 'time: mean'
    }
    ds['ice_temp_06'].attrs = {
        'units': 'kelvin',
        'long_name': 'Ice Temperature 6',
        'standard_name': 'land_ice_temperature',
        'coordinates': 'longitude latitude',
        'cell_methods': 'time: mean'
    }
    ds['ice_temp_07'].attrs = {
        'units': 'kelvin',
        'long_name': 'Ice Temperature 7',
        'standard_name': 'land_ice_temperature',
        'coordinates': 'longitude latitude',
        'cell_methods': 'time: mean'
    }
    ds['ice_temp_08'].attrs = {
        'units': 'kelvin',
        'long_name': 'Ice Temperature 8',
        'standard_name': 'land_ice_temperature',
        'coordinates': 'longitude latitude',
        'cell_methods': 'time: mean'
    }
    ds['tilt_east'].attrs = {
        'units': 'degree',
        'long_name': 'Tilt to East',
        'standard_name': '',
        'coordinates': 'longitude latitude',
        'cell_methods': 'time: point'
    }
    ds['tilt_north'].attrs = {
        'units': 'degree',
        'long_name': 'Tilt to North',
        'standard_name': '',
        'coordinates': 'longitude latitude',
        'cell_methods': 'time: point'
    }
    ds['time_GPS'].attrs = {
        'units': 'UTC',
        'long_name': 'Time GPS(hhmmssUTC)',
        'standard_name': 'time'
    }
    ds['latitude_GPS'].attrs = {
        'units': 'degrees_north',
        'long_name': 'Latitude GPS',
        'standard_name': 'latitude'
    }
    ds['longitude_GPS'].attrs = {
        'units': 'degrees_east',
        'long_name': 'Longitude GPS',
        'standard_name': 'longitude'
    }
    ds['elevation'].attrs = {
        'units': 'meter',
        'long_name': 'Elevation GPS',
        'standard_name': '',
        'coordinates': 'longitude latitude',
        'cell_methods': 'time: point'
    }
    ds['hor_dil_prec'].attrs = {
        'units': '1',
        'long_name': 'Horizontal Dilution of Precision GPS',
        'standard_name': '',
        'coordinates': 'longitude latitude',
        'cell_methods': 'time: point'
    }
    ds['logger_temp'].attrs = {
        'units': 'kelvin',
        'long_name': 'Logger Temperature',
        'standard_name': '',
        'coordinates': 'longitude latitude',
        'cell_methods': 'time: point'
    }
    ds['fan_current'].attrs = {
        'units': 'ampere',
        'long_name': 'Fan Current',
        'standard_name': '',
        'coordinates': 'longitude latitude',
        'cell_methods': 'time: point'
    }
    ds['battery_voltage'].attrs = {
        'units': 'volts',
        'long_name': 'Battery Voltage',
        'standard_name': 'battery_voltage',
        'coordinates': 'longitude latitude',
        'cell_methods': 'time: point'
    }
    ds['ice_velocity_GPS_total'].attrs = {
        'units': 'meter second-1',
        'long_name': 'Ice velocity derived from GPS Lat and Long',
        'standard_name': '',
        'coordinates': 'longitude latitude',
        'cell_methods': 'time: mean'
    }
    ds['ice_velocity_GPS_x'].attrs = {
        'units': 'meter second-1',
        'long_name':
        'x-component of Ice velocity derived from GPS Lat and Long',
        'standard_name': 'land_ice_surface_x_velocity',
        'coordinates': 'longitude latitude',
        'cell_methods': 'time: mean'
    }
    ds['ice_velocity_GPS_y'].attrs = {
        'units': 'meter second-1',
        'long_name':
        'y-component of Ice velocity derived from GPS Lat and Long',
        'standard_name': 'land_ice_surface_y_velocity',
        'coordinates': 'longitude latitude',
        'cell_methods': 'time: mean'
    }
    ds['time'].attrs = {
        'units': 'seconds since 1970-01-01 00:00:00',
        'long_name': 'time of measurement',
        'standard_name': 'time',
        'bounds': 'time_bounds',
        'calendar': 'noleap'
    }
    ds['sza'].attrs = {
        'units': 'degree',
        'long_name': 'Solar Zenith Angle',
        'standard_name': 'solar_zenith_angle',
        'coordinates': 'longitude latitude',
        'cell_methods': 'time: mean'
    }
    ds['station_name'].attrs = {
        'long_name': 'Station Name',
        'cf_role': 'timeseries_id'
    }
    ds['latitude'].attrs = {
        'units': 'degrees_north',
        'standard_name': 'latitude'
    }
    ds['longitude'].attrs = {
        'units': 'degrees_east',
        'standard_name': 'longitude'
    }

    encoding = {
        'year': {
            '_FillValue': False
        },
        'month': {
            '_FillValue': False
        },
        'day': {
            '_FillValue': False
        },
        'hour': {
            '_FillValue': False
        },
        'day_of_year': {
            '_FillValue': False
        },
        'day_of_century': {
            '_FillValue': False
        },
        'air_pressure': {
            '_FillValue': fillvalue_double
        },
        'air_temperature': {
            '_FillValue': fillvalue_double
        },
        'air_temperature_hygroclip': {
            '_FillValue': fillvalue_double
        },
        'relative_humidity_wrtwater': {
            '_FillValue': fillvalue_double
        },
        'relative_humidity': {
            '_FillValue': fillvalue_double
        },
        'wind_speed': {
            '_FillValue': fillvalue_double
        },
        'wind_direction': {
            '_FillValue': fillvalue_double
        },
        'shortwave_radiation_down': {
            '_FillValue': fillvalue_double
        },
        'shortwave_radiation_down_cor': {
            '_FillValue': fillvalue_double
        },
        'shortwave_radiation_up': {
            '_FillValue': fillvalue_double
        },
        'shortwave_radiation_up_cor': {
            '_FillValue': fillvalue_double
        },
        'albedo_theta': {
            '_FillValue': fillvalue_double
        },
        'longwave_radiation_down': {
            '_FillValue': fillvalue_double
        },
        'longwave_radiation_up': {
            '_FillValue': fillvalue_double
        },
        'cloudcover': {
            '_FillValue': fillvalue_double
        },
        'surface_temp': {
            '_FillValue': fillvalue_double
        },
        'height_sensor_boom': {
            '_FillValue': fillvalue_double
        },
        'height_stakes': {
            '_FillValue': fillvalue_double
        },
        'depth_pressure_transducer': {
            '_FillValue': fillvalue_double
        },
        'depth_pressure_transducer_cor': {
            '_FillValue': fillvalue_double
        },
        'ice_temp_01': {
            '_FillValue': fillvalue_double
        },
        'ice_temp_02': {
            '_FillValue': fillvalue_double
        },
        'ice_temp_03': {
            '_FillValue': fillvalue_double
        },
        'ice_temp_04': {
            '_FillValue': fillvalue_double
        },
        'ice_temp_05': {
            '_FillValue': fillvalue_double
        },
        'ice_temp_06': {
            '_FillValue': fillvalue_double
        },
        'ice_temp_07': {
            '_FillValue': fillvalue_double
        },
        'ice_temp_08': {
            '_FillValue': fillvalue_double
        },
        'tilt_east': {
            '_FillValue': fillvalue_double
        },
        'tilt_north': {
            '_FillValue': fillvalue_double
        },
        'time_GPS': {
            '_FillValue': fillvalue_double
        },
        'latitude_GPS': {
            '_FillValue': fillvalue_double
        },
        'longitude_GPS': {
            '_FillValue': fillvalue_double
        },
        'elevation': {
            '_FillValue': fillvalue_double
        },
        'hor_dil_prec': {
            '_FillValue': fillvalue_double
        },
        'logger_temp': {
            '_FillValue': fillvalue_double
        },
        'fan_current': {
            '_FillValue': fillvalue_double
        },
        'battery_voltage': {
            '_FillValue': fillvalue_double
        },
        'ice_velocity_GPS_total': {
            '_FillValue': fillvalue_double
        },
        'ice_velocity_GPS_x': {
            '_FillValue': fillvalue_double
        },
        'ice_velocity_GPS_y': {
            '_FillValue': fillvalue_double
        },
        'time': {
            '_FillValue': False
        },
        'time_bounds': {
            '_FillValue': False
        },
        'sza': {
            '_FillValue': False
        },
        'latitude': {
            '_FillValue': False
        },
        'longitude': {
            '_FillValue': False
        }
    }

    write_data(args, ds, op_file, encoding)
Exemple #15
0
tf = TimezoneFinder()

#variable initization
address = '144 East Ave., Ithaca, NY 148530'
print(address)

#Gets latitude and longitude from enteted address
geocode_result = gmaps.geocode(address)
latitude = geocode_result[0]['geometry']['location']['lat']
longitude = geocode_result[0]['geometry']['location']['lng']
print('Latitude: ' + str(latitude))
print('Longitude: ' + str(longitude))

#Gets timezone of location
timezone_str = tf.certain_timezone_at(lng=longitude, lat=latitude)
print(timezone_str)

#Conerts timezone to utc
timezone = pytz.timezone(timezone_str)
utc = datetime.utcnow()
timezone = utc + timezone.utcoffset(utc)
print(timezone)

#uses sunposition's sunpos function to get angles
Azimuth = sunpos(utc, latitude, longitude, 0)[0]
Zenith = sunpos(utc, latitude, longitude, 0)[1]
Elevation = 90 - Zenith

print("Sun Azimuth angle (east to west): " + str(Azimuth) +
      "\nSun Zenith angle: " + str(Zenith) +
      "\nSun Elevation angle from horizon: " + str(Elevation))
Exemple #16
0
def getSolarPosition(date):
    azimuth, zenith = sunpos(date,
                             latitude=latitude,
                             longitude=longitude,
                             elevation=elevation)[:2]
    return azimuth, zenith
Exemple #17
0
def gcnet2nc(args, op_file, station_dict, station_name, convert_temp, convert_press, seconds_in_hour, fillvalue_double):

	header_rows = 54
	check_na = 999.0
	hour_conversion = (100/4)		#Divided by 4 because each hour value is a multiple of 4 and then multiplied by 100 to convert decimal to integer
	last_hour = 23

	column_names = ['station_number', 'year', 'julian_decimal_time', 'sw_down', 'sw_up', 'net_radiation', 'temperature_tc_1', 'temperature_tc_2', 'temperature_cs500_1', 'temperature_cs500_2', 'relative_humidity_1', 'relative_humidity_2', 
	'u1_wind_speed', 'u2_wind_speed', 'u_direction_1', 'u_direction_2', 'atmos_pressure', 'snow_height_1', 'snow_height_2', 't_snow_01', 't_snow_02', 't_snow_03', 't_snow_04', 't_snow_05', 't_snow_06', 't_snow_07', 't_snow_08', 't_snow_09', 't_snow_10', 
	'battery_voltage', 'sw_down_max', 'sw_up_max', 'net_radiation_max', 'max_air_temperature_1', 'max_air_temperature_2', 'min_air_temperature_1', 'min_air_temperature_2', 'max_windspeed_u1', 'max_windspeed_u2', 'stdev_windspeed_u1', 'stdev_windspeed_u2', 
	'ref_temperature', 'windspeed_2m', 'windspeed_10m', 'wind_sensor_height_1', 'wind_sensor_height_2', 'albedo', 'zenith_angle', 'qc1', 'qc9', 'qc17', 'qc25']

	
	df = pd.read_csv(args.input_file or args.fl_in, delim_whitespace=True, skiprows=header_rows, skip_blank_lines=True, header=None, names = column_names)
	df.index.name = 'time'
	df['qc25'] = df['qc25'].astype(str)			# To avoid 999 values marked as N/A
	df.replace(check_na, np.nan, inplace=True)
	df.loc[:,['temperature_tc_1', 'temperature_tc_2', 'temperature_cs500_1', 'temperature_cs500_2', 't_snow_01', 't_snow_02', 't_snow_03', 't_snow_04', 't_snow_05', 't_snow_06', 't_snow_07', 't_snow_08', 't_snow_09', 't_snow_10', 'max_air_temperature_1', 'max_air_temperature_2', 'min_air_temperature_1', 'min_air_temperature_2', 'ref_temperature']] += convert_temp
	df.loc[:,'atmos_pressure'] *= convert_press
	df = df.where((pd.notnull(df)), fillvalue_double)
	df['qc25'] = df['qc25'].astype(int)			#Convert it back to int

	station_number = df['station_number'][0]
	df.drop('station_number', axis=1, inplace=True)

	ds = xr.Dataset.from_dataframe(df)
	ds = ds.drop('time')
	
	
	# Intializing variables
	num_rows =  df['year'].size
	qc_swdn, qc_swup, qc_netradiation, qc_ttc1, qc_ttc2, qc_tcs1, qc_tcs2, qc_rh1, qc_rh2, qc_u1, qc_u2, qc_ud1, qc_ud2, qc_pressure, qc_snowheight1, qc_snowheight2, qc_tsnow1, qc_tsnow2, qc_tsnow3, qc_tsnow4, qc_tsnow5, qc_tsnow6, qc_tsnow7, qc_tsnow8, qc_tsnow9, qc_tsnow10, qc_battery = ([0]*num_rows for x in range(27))

	hour, month, day, time, time_bounds, sza = ([0]*num_rows for x in range(6))

	print('calculating quality control variables...')
	temp1 = [list(map(int, i)) for i in zip(*map(str, df['qc1']))]
	temp9 = [list(map(int, i)) for i in zip(*map(str, df['qc9']))]
	temp17 = [list(map(int, i)) for i in zip(*map(str, df['qc17']))]
	temp25 = [list(map(int, i)) for i in zip(*map(str, df['qc25']))]

	
	qc_swdn[:] = temp1[0]
	qc_swup[:] = temp1[1]
	qc_netradiation[:] = temp1[2]
	qc_ttc1[:] = temp1[3]
	qc_ttc2[:] = temp1[4]
	qc_tcs1[:] = temp1[5]
	qc_tcs2[:] = temp1[6]
	qc_rh1[:] = temp1[7]
	
	qc_rh2[:] = temp9[0]
	qc_u1[:] = temp9[1]
	qc_u2[:] = temp9[2]
	qc_ud1[:] = temp9[3]
	qc_ud2[:] = temp9[4]
	qc_pressure[:] = temp9[5]
	qc_snowheight1[:] = temp9[6]
	qc_snowheight2[:] = temp9[7]

	qc_tsnow1[:] = temp17[0]
	qc_tsnow2[:] = temp17[1]
	qc_tsnow3[:] = temp17[2]
	qc_tsnow4[:] = temp17[3]
	qc_tsnow5[:] = temp17[4]
	qc_tsnow6[:] = temp17[5]
	qc_tsnow7[:] = temp17[6]
	qc_tsnow8[:] = temp17[7]

	qc_tsnow9[:] = temp25[0]
	qc_tsnow10[:] = temp25[1]
	qc_battery[:] = temp25[2]

	ds['qc_swdn'] = (('time',qc_swdn))
	ds['qc_swup'] = (('time',qc_swup))
	ds['qc_netradiation'] = (('time',qc_netradiation))
	ds['qc_ttc1'] = (('time',qc_ttc1))
	ds['qc_ttc2'] = (('time',qc_ttc2))
	ds['qc_tcs1'] = (('time',qc_tcs1))
	ds['qc_tcs2'] = (('time',qc_tcs2))
	ds['qc_rh1'] = (('time',qc_rh1))
	ds['qc_rh2'] = (('time',qc_rh2))
	ds['qc_u1'] = (('time',qc_u1))
	ds['qc_u2'] = (('time',qc_u2))
	ds['qc_ud1'] = (('time',qc_ud1))
	ds['qc_ud2'] = (('time',qc_ud2))
	ds['qc_pressure'] = (('time',qc_pressure))
	ds['qc_snowheight1'] = (('time',qc_snowheight1))
	ds['qc_snowheight2'] = (('time',qc_snowheight2))
	ds['qc_tsnow1'] = (('time',qc_tsnow1))
	ds['qc_tsnow2'] = (('time',qc_tsnow2))
	ds['qc_tsnow3'] = (('time',qc_tsnow3))
	ds['qc_tsnow4'] = (('time',qc_tsnow4))
	ds['qc_tsnow5'] = (('time',qc_tsnow5))
	ds['qc_tsnow6'] = (('time',qc_tsnow6))
	ds['qc_tsnow7'] = (('time',qc_tsnow7))
	ds['qc_tsnow8'] = (('time',qc_tsnow8))
	ds['qc_tsnow9'] = (('time',qc_tsnow9))
	ds['qc_tsnow10'] = (('time',qc_tsnow10))
	ds['qc_battery'] = (('time',qc_battery))

	
	print('retrieving lat and lon...')
	if station_number == 1:
		temp_stn = 'gcnet_swiss'
	elif station_number == 2:
		temp_stn = 'gcnet_crawford'
	elif station_number == 3:
		temp_stn = 'gcnet_nasa-u'
	elif station_number == 4:
		temp_stn = 'gcnet_gits'
	elif station_number == 5:
		temp_stn = 'gcnet_humboldt'
	elif station_number == 6:
		temp_stn = 'gcnet_summit'
	elif station_number == 7:
		temp_stn = 'gcnet_tunu-n'
	elif station_number == 8:
		temp_stn = 'gcnet_dye2'
	elif station_number == 9:
		temp_stn = 'gcnet_jar'
	elif station_number == 10:
		temp_stn = 'gcnet_saddle'
	elif station_number == 11:
		temp_stn = 'gcnet_dome'
	elif station_number == 12:
		temp_stn = 'gcnet_nasa-e'
	elif station_number == 13:
		temp_stn = 'gcnet_cp2'
	elif station_number == 14:
		temp_stn = 'gcnet_ngrip'
	elif station_number == 15:
		temp_stn = 'gcnet_nasa-se'
	elif station_number == 16:
		temp_stn = 'gcnet_kar'
	elif station_number == 17:
		temp_stn = 'gcnet_jar2'
	elif station_number == 18:
		temp_stn = 'gcnet_kulu'
	elif station_number == 19:
		temp_stn = 'gcnet_jar3'
	elif station_number == 20:
		temp_stn = 'gcnet_aurora'
	elif station_number == 21 or 26:
		temp_stn = 'gcnet_petermann-gl'
	elif station_number == 22:
		temp_stn = 'gcnet_peterman-ela'
	elif station_number == 23:
		temp_stn = 'gcnet_neem'
	elif station_number == 30:
		temp_stn = 'gcnet_lar1'
	elif station_number == 31:
		temp_stn = 'gcnet_lar2'
	elif station_number == 32:
		temp_stn = 'gcnet_lar3'
	
	latitude = station_dict.get(temp_stn)[0]
	longitude = station_dict.get(temp_stn)[1]

	if args.station_name:
		print('Default station name overrided by user provided station name')
	else:
		station_name = station_dict.get(temp_stn)[2]

	

	
	print('calculating hour...')
	hour[:] = [int(x) for x in [round((y-int(y)),3)*hour_conversion for y in df['julian_decimal_time']]]
	
	z = 0
	while z < num_rows:
		if hour[z] > last_hour:
			hour[z] = 0
		z += 1

	print("calculating time and sza...")
	
	tz = pytz.timezone(args.timezone)
	dtime_1970 = datetime(1970,1,1)
	dtime_1970 = tz.localize(dtime_1970.replace(tzinfo=None))
	i = 0

	while i < num_rows:
		
		temp_dtime = datetime.strptime("%s %s %s" % (df['year'][i], int(df['julian_decimal_time'][i]), hour[i]), "%Y %j %H")
		temp_dtime = tz.localize(temp_dtime.replace(tzinfo=None))
		time[i] = (temp_dtime-dtime_1970).total_seconds()
		
		time_bounds[i] = (time[i]-seconds_in_hour, time[i])
		
		sza[i] = sunpos(temp_dtime,latitude,longitude,0)[1]
		
		i += 1
	

	if args.analysis:
		print('calculating month and day...')
		def get_month_day(year, day, one_based=False):
			if one_based:  # if Jan 1st is 1 instead of 0
				day -= 1
			dt = datetime(year, 1, 1) + timedelta(days=day)
			return dt.month, dt.day

		j = 0
		while j < num_rows:
			month[j] = get_month_day(int(df['year'][j]), int(df['julian_decimal_time'][j]), True)[0]
			day[j] = get_month_day(int(df['year'][j]), int(df['julian_decimal_time'][j]), True)[1]
			j += 1


		ds['hour'] = (('time'),hour)
		ds['month'] = (('time'),month)
		ds['day'] = (('time'),day)


	ds['time'] = (('time'),time)
	ds['time_bounds'] = (('time', 'nbnd'),time_bounds)
	ds['sza'] = (('time'),sza)
	ds['station_number'] = ((),station_number)
	ds['station_name'] = ((),station_name)
	ds['latitude'] = ((),latitude)
	ds['longitude'] = ((),longitude)

	ds.attrs = {'title':'Surface Radiation Data from Greenland Climate Network', 'source':'Surface Observations', 'featureType':'timeSeries', 'institution':'Cooperative Institute for Research in Enviornmental Sciences', 
	'reference':'http://cires.colorado.edu/science/groups/steffen/gcnet/', 'Conventions':'CF-1.7', 'time_convention':"'time: point' variables match the time coordinate values exactly, whereas 'time: mean' variables are valid for the mean time within the time_bounds variable." + " e.g.: battery_voltage is measured once per hour at the time stored in the 'time' coordinate." + 	" On the other hand, temperature_tc_1 is continuously measured and then hourly-mean values are stored for each period contained in the time_bounds variable"}

	ds['station_number'].attrs= {'units':'1', 'long_name':'Station Number'}
	ds['year'].attrs= {'units':'1', 'long_name':'Year'}
	ds['julian_decimal_time'].attrs= {'units':'decimal time', 'long_name':'Julian Decimal Time', 'note':'For each year, time starts at 1.0000 and ends at 365.9999.'}
	ds['sw_down'].attrs= {'units':'watt meter-2', 'long_name':'Shortwave Flux Down', 'standard_name':'downwelling_shortwave_flux_in_air', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['sw_up'].attrs= {'units':'watt meter-2', 'long_name':'Shortwave Flux Up', 'standard_name':'upwelling_shortwave_flux_in_air', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['net_radiation'].attrs= {'units':'watt meter-2', 'long_name':'Net Radiation', 'standard_name':'surface_net_downward_radiative_flux', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['temperature_tc_1'].attrs= {'units':'kelvin', 'long_name':'TC-1 Air Temperature', 'standard_name':'air_temperature', 'note':'air temperature from TC sensor', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['temperature_tc_2'].attrs= {'units':'kelvin', 'long_name':'TC-2 Air Temperature', 'standard_name':'air_temperature', 'note':'air temperature from TC sensor', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['temperature_cs500_1'].attrs= {'units':'kelvin', 'long_name':'CS500-1 Air Temperature', 'standard_name':'air_temperature', 'note':'air temperature from CS500 sensor', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['temperature_cs500_2'].attrs= {'units':'kelvin', 'long_name':'CS500-2 Air Temperature', 'standard_name':'air_temperature', 'note':'air temperature from CS500 sensor', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['relative_humidity_1'].attrs= {'units':'1', 'long_name':'Relative Humidity 1', 'standard_name':'realtive_humidity', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['relative_humidity_2'].attrs= {'units':'1', 'long_name':'Relative Humidity 2', 'standard_name':'realtive_humidity', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['u1_wind_speed'].attrs= {'units':'meter second-1', 'long_name':'U1 Wind Speed', 'standard_name':'wind_speed', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['u2_wind_speed'].attrs= {'units':'meter second-1', 'long_name':'U2 Wind Speed', 'standard_name':'wind_speed', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['u_direction_1'].attrs= {'units':'degree', 'long_name':'U Direction 1', 'standard_name':'wind_from_direction', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['u_direction_2'].attrs= {'units':'degree', 'long_name':'U Direction 2', 'standard_name':'wind_from_direction', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['atmos_pressure'].attrs= {'units':'pascal', 'long_name':'Atmospheric Pressure', 'standard_name':'surface_air_pressure', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['snow_height_1'].attrs= {'units':'meter', 'long_name':'Snow Height 1', 'standard_name':'snow_height', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['snow_height_2'].attrs= {'units':'meter', 'long_name':'Snow Height 2', 'standard_name':'snow_height', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['t_snow_01'].attrs= {'units':'kelvin', 'long_name':'T Snow 1', 'standard_name':'', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['t_snow_02'].attrs= {'units':'kelvin', 'long_name':'T Snow 2', 'standard_name':'', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['t_snow_03'].attrs= {'units':'kelvin', 'long_name':'T Snow 3', 'standard_name':'', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['t_snow_04'].attrs= {'units':'kelvin', 'long_name':'T Snow 4', 'standard_name':'', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['t_snow_05'].attrs= {'units':'kelvin', 'long_name':'T Snow 5', 'standard_name':'', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['t_snow_06'].attrs= {'units':'kelvin', 'long_name':'T Snow 6', 'standard_name':'', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['t_snow_07'].attrs= {'units':'kelvin', 'long_name':'T Snow 7', 'standard_name':'', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['t_snow_08'].attrs= {'units':'kelvin', 'long_name':'T Snow 8', 'standard_name':'', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['t_snow_09'].attrs= {'units':'kelvin', 'long_name':'T Snow 9', 'standard_name':'', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['t_snow_10'].attrs= {'units':'kelvin', 'long_name':'T Snow 10', 'standard_name':'', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['battery_voltage'].attrs= {'units':'volts', 'long_name':'Battery Voltage', 'standard_name':'battery_voltage', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['sw_down_max'].attrs= {'units':'watt meter-2', 'long_name':'Shortwave Flux Down Max', 'standard_name':'maximum_downwelling_shortwave_flux_in_air', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['sw_up_max'].attrs= {'units':'watt meter-2', 'long_name':'Shortwave Flux Up Max', 'standard_name':'maximum_upwelling_shortwave_flux_in_air', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['net_radiation_max'].attrs= {'units':'watt meter-2', 'long_name':'Net Radiation Max', 'standard_name':'maximum_net_radiation', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['max_air_temperature_1'].attrs= {'units':'kelvin', 'long_name':'Max Air Temperature 1', 'standard_name':'air_temperature', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['max_air_temperature_2'].attrs= {'units':'kelvin', 'long_name':'Max Air Temperature 2', 'standard_name':'air_temperature', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['min_air_temperature_1'].attrs= {'units':'kelvin', 'long_name':'Min Air Temperature 1', 'standard_name':'air_temperature', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['min_air_temperature_2'].attrs= {'units':'kelvin', 'long_name':'Min Air Temperature 2', 'standard_name':'air_temperature', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['max_windspeed_u1'].attrs= {'units':'meter second-1', 'long_name':'Max Windspeed-U1', 'standard_name':'wind_speed', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['max_windspeed_u2'].attrs= {'units':'meter second-1', 'long_name':'Max Windspeed-U2', 'standard_name':'wind_speed', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['stdev_windspeed_u1'].attrs= {'units':'meter second-1', 'long_name':'StdDev Windspeed-U1', 'standard_name':'wind_speed', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['stdev_windspeed_u2'].attrs= {'units':'meter second-1', 'long_name':'StdDev Windspeed-U2', 'standard_name':'wind_speed', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['ref_temperature'].attrs= {'units':'kelvin', 'long_name':'Reference Temperature', 'standard_name':'Need to ask network manager about long name', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['windspeed_2m'].attrs= {'units':'meter second-1', 'long_name':'Windspeed@2m', 'standard_name':'wind_speed', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['windspeed_10m'].attrs= {'units':'meter second-1', 'long_name':'Windspeed@10m', 'standard_name':'wind_speed', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['wind_sensor_height_1'].attrs= {'units':'meter', 'long_name':'Wind Sensor Height 1', 'standard_name':'', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['wind_sensor_height_2'].attrs= {'units':'meter', 'long_name':'Wind Sensor Height 2', 'standard_name':'', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['albedo'].attrs= {'units':'1', 'long_name':'Albedo', 'standard_name':'surface_albedo', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['zenith_angle'].attrs= {'units':'degree', 'long_name':'Zenith Angle', 'standard_name':'solar_zenith_angle', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['qc1'].attrs= {'units':'1', 'long_name':'Quality Control variables 01-08', 'coordinates':'longitude latitude'}
	ds['qc9'].attrs= {'units':'1', 'long_name':'Quality Control variables 09-16', 'coordinates':'longitude latitude'}
	ds['qc17'].attrs= {'units':'1', 'long_name':'Quality Control variables 17-24', 'coordinates':'longitude latitude'}
	ds['qc25'].attrs= {'units':'1', 'long_name':'Quality Control variables 25-27', 'coordinates':'longitude latitude'}
	ds['qc_swdn'].attrs= {'units':'1', 'long_name':'Quality Control flag for Shortwave Flux Down'}
	ds['qc_swup'].attrs= {'units':'1', 'long_name':'Quality Control flag for Shortwave Flux Up'}
	ds['qc_netradiation'].attrs= {'units':'1', 'long_name':'Quality Control flag for Net Radiation'}
	ds['qc_ttc1'].attrs= {'units':'1', 'long_name':'Quality Control flag for TC-1 Air Temperature'}
	ds['qc_ttc2'].attrs= {'units':'1', 'long_name':'Quality Control flag for TC-2 Air Temperature'}
	ds['qc_tcs1'].attrs= {'units':'1', 'long_name':'Quality Control flag for CS500-1 Air Temperature'}
	ds['qc_tcs2'].attrs= {'units':'1', 'long_name':'Quality Control flag for CS500-2 Air Temperature'}
	ds['qc_rh1'].attrs= {'units':'1', 'long_name':'Quality Control flag for Relative Humidity 1'}
	ds['qc_rh2'].attrs= {'units':'1', 'long_name':'Quality Control flag for Relative Humidity 2'}
	ds['qc_u1'].attrs= {'units':'1', 'long_name':'Quality Control flag for U1 Wind Speed'}
	ds['qc_u2'].attrs= {'units':'1', 'long_name':'Quality Control flag for U2 Wind Speed'}
	ds['qc_ud1'].attrs= {'units':'1', 'long_name':'Quality Control flag for U Direction 1'}
	ds['qc_ud2'].attrs= {'units':'1', 'long_name':'Quality Control flag for U Direction 2'}
	ds['qc_pressure'].attrs= {'units':'1', 'long_name':'Quality Control flag for Atmospheric Pressure'}
	ds['qc_snowheight1'].attrs= {'units':'1', 'long_name':'Quality Control flag for Snow Height 1'}
	ds['qc_snowheight2'].attrs= {'units':'1', 'long_name':'Quality Control flag for Snow Height 2'}
	ds['qc_tsnow1'].attrs= {'units':'1', 'long_name':'Quality Control flag for T Snow 1'}
	ds['qc_tsnow2'].attrs= {'units':'1', 'long_name':'Quality Control flag for T Snow 2'}
	ds['qc_tsnow3'].attrs= {'units':'1', 'long_name':'Quality Control flag for T Snow 3'}
	ds['qc_tsnow4'].attrs= {'units':'1', 'long_name':'Quality Control flag for T Snow 4'}
	ds['qc_tsnow5'].attrs= {'units':'1', 'long_name':'Quality Control flag for T Snow 5'}
	ds['qc_tsnow6'].attrs= {'units':'1', 'long_name':'Quality Control flag for T Snow 6'}
	ds['qc_tsnow7'].attrs= {'units':'1', 'long_name':'Quality Control flag for T Snow 7'}
	ds['qc_tsnow8'].attrs= {'units':'1', 'long_name':'Quality Control flag for T Snow 8'}
	ds['qc_tsnow9'].attrs= {'units':'1', 'long_name':'Quality Control flag for T Snow 9'}
	ds['qc_tsnow10'].attrs= {'units':'1', 'long_name':'Quality Control flag for T Snow 10'}
	ds['qc_battery'].attrs= {'units':'1', 'long_name':'Quality Control flag for Battery Voltage'}
	ds['time'].attrs= {'units':'seconds since 1970-01-01 00:00:00', 'long_name':'time of measurement',	'standard_name':'time', 'bounds':'time_bounds', 'calendar':'noleap'}
	ds['sza'].attrs= {'units':'degree', 'long_name':'Solar Zenith Angle', 'standard_name':'solar_zenith_angle', 'coordinates':'longitude latitude', 'cell_methods':'time: mean'}
	ds['station_name'].attrs= {'long_name':'Station Name', 'cf_role':'timeseries_id'}
	ds['latitude'].attrs= {'units':'degrees_north', 'standard_name':'latitude'}
	ds['longitude'].attrs= {'units':'degrees_east', 'standard_name':'longitude'}
	

	encoding = {'julian_decimal_time': {'_FillValue': False},
				'sw_down': {'_FillValue': fillvalue_double},
				'sw_up': {'_FillValue': fillvalue_double},
				'net_radiation': {'_FillValue': fillvalue_double},
				'temperature_tc_1': {'_FillValue': fillvalue_double},
				'temperature_tc_2': {'_FillValue': fillvalue_double},
				'temperature_cs500_1': {'_FillValue': fillvalue_double},
				'temperature_cs500_2': {'_FillValue': fillvalue_double},
				'relative_humidity_1': {'_FillValue': fillvalue_double},
				'relative_humidity_2': {'_FillValue': fillvalue_double},
				'u1_wind_speed': {'_FillValue': fillvalue_double},
				'u2_wind_speed': {'_FillValue': fillvalue_double},
				'u_direction_1': {'_FillValue': fillvalue_double},
				'u_direction_2': {'_FillValue': fillvalue_double},
				'atmos_pressure': {'_FillValue': fillvalue_double},
				'snow_height_1': {'_FillValue': fillvalue_double},
				'snow_height_2': {'_FillValue': fillvalue_double},
				't_snow_01': {'_FillValue': fillvalue_double},
				't_snow_02': {'_FillValue': fillvalue_double},
				't_snow_03': {'_FillValue': fillvalue_double},
				't_snow_04': {'_FillValue': fillvalue_double},
				't_snow_05': {'_FillValue': fillvalue_double},
				't_snow_06': {'_FillValue': fillvalue_double},
				't_snow_07': {'_FillValue': fillvalue_double},
				't_snow_08': {'_FillValue': fillvalue_double},
				't_snow_09': {'_FillValue': fillvalue_double},
				't_snow_10': {'_FillValue': fillvalue_double},
				'battery_voltage': {'_FillValue': fillvalue_double},
				'sw_down_max': {'_FillValue': fillvalue_double},
				'sw_up_max': {'_FillValue': fillvalue_double},
				'net_radiation_max': {'_FillValue': fillvalue_double},
				'max_air_temperature_1': {'_FillValue': fillvalue_double},
				'max_air_temperature_2': {'_FillValue': fillvalue_double},
				'min_air_temperature_1': {'_FillValue': fillvalue_double},
				'min_air_temperature_2': {'_FillValue': fillvalue_double},
				'max_windspeed_u1': {'_FillValue': fillvalue_double},
				'max_windspeed_u2': {'_FillValue': fillvalue_double},
				'stdev_windspeed_u1': {'_FillValue': fillvalue_double},
				'stdev_windspeed_u2': {'_FillValue': fillvalue_double},
				'ref_temperature': {'_FillValue': fillvalue_double},
				'windspeed_2m': {'_FillValue': fillvalue_double},
				'windspeed_10m': {'_FillValue': fillvalue_double},
				'wind_sensor_height_1': {'_FillValue': fillvalue_double},
				'wind_sensor_height_2': {'_FillValue': fillvalue_double},
				'albedo': {'_FillValue': fillvalue_double},
				'zenith_angle': {'_FillValue': fillvalue_double},
				'time': {'_FillValue': False},
				'time_bounds': {'_FillValue': False},
				'sza': {'_FillValue': False},
				'latitude': {'_FillValue': False},
				'longitude': {'_FillValue': False}
				}


	write_data(args, ds, op_file, encoding)
Exemple #18
0
def main():
    indir = "nomiss-merra/"
    outdir = "rrtm-merra/"
    if not os.path.exists(outdir):
        os.makedirs(outdir)

    # constants
    fillvalue_float = 9.96921e+36
    alb = 0.75
    emis = 0.985
    solar_constant = 1367.0

    absorber_vmr = dict()
    absorber_vmr['CO2'] = 355. / 1E6
    absorber_vmr['CH4'] = 1714. / 1E9
    absorber_vmr['N2O'] = 311. / 1E9
    absorber_vmr['O2'] = 0.21
    absorber_vmr['CFC11'] = 0.280 / 1E9
    absorber_vmr['CFC12'] = 0.503 / 1E9
    absorber_vmr['CFC22'] = 0.
    absorber_vmr['CCL4'] = 0.

    # stn names and lat/lon
    lst_stn = pd.read_csv('../resources/stations_radiation.txt')
    stn_names = lst_stn['network_name'].tolist()
    latstn = lst_stn['lat'].tolist()
    lonstn = lst_stn['lon'].tolist()

    nstn = len(stn_names)
    '''stn_names = 'gcnet_summit'
    latstn = 72.57972
    lonstn = -38.50454
    nstn = 1'''

    hours_merra = [1, 4, 7, 10, 13, 16, 19, 22]
    hours_24 = list(range(24))

    cleardays = pd.read_csv('cleardays.csv')

    # main function
    for i in range(nstn):
        stn = stn_names[i]
        lat_deg = latstn[i]
        lon_deg = lonstn[i]
        '''stn = stn_names
        lat_deg = latstn
        lon_deg = lonstn'''

        fout = outdir + stn + '.rrtm.nc'
        sw_dn_complete = []
        sw_up_complete = []
        lw_dn_complete = []
        lw_up_complete = []
        time_op = []

        flname = indir + stn + '.' + 'merra2.inst3.asm.1991-2018' + '.nc'

        clr_dates = cleardays.loc[cleardays['network_name'] == stn, 'date']

        for date in clr_dates:
            sw_dn = []
            sw_up = []
            lw_dn = []
            lw_up = []
            '''flname = indir + stn + '.' + str(date) + '.nc'

            if not os.path.isfile(flname):
                continue'''

            fin = xr.open_dataset(flname)
            fin = fin.sel(time=str(date))

            tmp = fin['t'].values
            ts = fin['ts'].values
            plev = fin['plev'].values
            ps = fin['ps'].values
            h2o_q = fin['q'].values
            o3_mmr = fin['o3'].values
            o3_vmr = climlab.utils.thermo.mmr_to_vmr(o3_mmr, gas='O3')

            aod_count = int(fin['aod_count'].values[0])

            # knob
            aod = np.zeros((6, 1, 42))
            aod[1, 0, -aod_count:] = 0.12 / aod_count
            aod[5, 0, :20] = 0.0077 / 20

            idx = 0

            for hr in hours_merra:
                dtime = datetime.strptime(str(date), "%Y%m%d") + timedelta(
                    hours=hr, minutes=30)

                sza = sunposition.sunpos(dtime, lat_deg, lon_deg, 0)[1]
                cossza = np.cos(np.radians(sza))

                state = make_column(lev=plev[idx],
                                    ps=ps[idx],
                                    tmp=tmp[idx],
                                    ts=ts[idx])
                absorber_vmr['O3'] = o3_vmr[idx]

                rad = climlab.radiation.RRTMG(name='Radiation',
                                              state=state,
                                              specific_humidity=h2o_q[idx],
                                              albedo=alb,
                                              coszen=cossza,
                                              absorber_vmr=absorber_vmr,
                                              emissivity=emis,
                                              S0=solar_constant,
                                              icld=0,
                                              iaer=6,
                                              ecaer_sw=aod)
                rad.compute_diagnostics()

                dout = rad.to_xarray(diagnostics=True)
                sw_dn.append(dout['SW_flux_down_clr'].values[-1])
                sw_up.append(dout['SW_flux_up_clr'].values[-1])
                lw_dn.append(dout['LW_flux_down_clr'].values[-1])
                lw_up.append(dout['LW_flux_up_clr'].values[-1])

                idx += 1

            sw_dn_24 = CubicSpline(hours_merra, sw_dn,
                                   extrapolate=True)(hours_24)
            sw_up_24 = CubicSpline(hours_merra, sw_up,
                                   extrapolate=True)(hours_24)
            lw_dn_24 = CubicSpline(hours_merra, lw_dn,
                                   extrapolate=True)(hours_24)
            lw_up_24 = CubicSpline(hours_merra, lw_up,
                                   extrapolate=True)(hours_24)

            sw_dn_complete.append(sw_dn_24)
            sw_up_complete.append(sw_up_24)
            lw_dn_complete.append(lw_dn_24)
            lw_up_complete.append(lw_up_24)

            for hr in range(24):
                time_op.append(
                    datetime.strptime(str(date), "%Y%m%d") +
                    timedelta(hours=hr, minutes=30))

        # Combine fsds for multiple days into single list
        sw_dn_complete = [
            item for sublist in sw_dn_complete for item in sublist
        ]
        sw_up_complete = [
            item for sublist in sw_up_complete for item in sublist
        ]
        lw_dn_complete = [
            item for sublist in lw_dn_complete for item in sublist
        ]
        lw_up_complete = [
            item for sublist in lw_up_complete for item in sublist
        ]

        # Get seconds since 1970
        time_op = [(i - datetime(1970, 1, 1)).total_seconds() for i in time_op]

        if sw_dn_complete:  # Write data
            ds = xr.Dataset()

            ds['fsds'] = 'time', sw_dn_complete
            ds['fsus'] = 'time', sw_up_complete
            ds['flds'] = 'time', lw_dn_complete
            ds['flus'] = 'time', lw_up_complete
            ds['time'] = 'time', time_op

            ds['fsds'].attrs = {
                "_FillValue":
                fillvalue_float,
                "units":
                'watt meter-2',
                "long_name":
                'RRTM simulated shortwave downwelling radiation at surface'
            }
            ds['fsus'].attrs = {
                "_FillValue":
                fillvalue_float,
                "units":
                'watt meter-2',
                "long_name":
                'RRTM simulated shortwave upwelling radiation at surface'
            }
            ds['flds'].attrs = {
                "_FillValue":
                fillvalue_float,
                "units":
                'watt meter-2',
                "long_name":
                'RRTM simulated longwave downwelling radiation at surface'
            }
            ds['flus'].attrs = {
                "_FillValue":
                fillvalue_float,
                "units":
                'watt meter-2',
                "long_name":
                'RRTM simulated longwave upwelling radiation at surface'
            }
            ds['time'].attrs = {
                "_FillValue": fillvalue_float,
                "units": 'seconds since 1970-01-01 00:00:00',
                "calendar": 'standard'
            }

            ds.to_netcdf(fout)
            print(fout)
def load_picky_solar(mat, t1, t2, configfile):
    from sunposition import sunpos

    jdate = mat["jdate"].squeeze()
    lon = mat["wrflon"]
    lat = mat["wrflat"]
    hgt = mat['hgt']
    swdown = mat['data']
    lon = np.mean(lon)
    lat = np.mean(lat)
    hgt = np.mean(hgt)

    # Only use the data from t1 to t2
    tstart = matplotlib.dates.date2num(t1) + 366
    tend = matplotlib.dates.date2num(t2) + 366
    ind = (jdate >= tstart) * (jdate <= tend)
    jdate = jdate[ind]
    swdown = swdown[ind]
    swdown = np.mean(swdown, axis=1)  #

    datevec = matplotlib.dates.num2date(jdate - 366)
    day_of_year = np.zeros([len(jdate)])
    for i in range(len(jdate)):
        day_of_year[i] = datevec[i].timetuple().tm_yday

    # Find azimuth, zenith, declination, angle
    logger.info(
        'Compute the coordinates of the sun as viewed at the given time and location. This may take some time...'
    )
    coords = np.zeros((len(jdate), 5))
    coords = sunpos(datevec, lat, lon, hgt)
    az = coords[:,
                0]  #coords[...,0] = observed azimuth angle, measured eastward from north
    zen = coords[:,
                 1]  #coords[...,1] = observed zenith angle, measured down from vertical matlab.zen
    delta = coords[:,
                   3]  #coords[...,3] = topocentric declination (delta?) Same as matlab.delta
    omega = coords[:,
                   4] + 360  #coords[...,4] = topocentric hour angle (omega?) Same as matlab.omega -360
    logger.debug('Computiation of coordinates of the sun is finished.')

    def cost(angle):
        return np.cos(np.radians(angle))

    def sint(angle):
        return np.sin(np.radians(angle))

    def tant(angle):
        return np.tan(np.radians(angle))

    #bbeam
    b00 = 1367
    grad = swdown
    eps0 = 1 + 0.033 * cost((360 * day_of_year) / 365)

    # Extraterrestial radiation
    b = b00 * eps0 * cost(zen)
    sun_alt = 90 - zen
    b[sun_alt <= 0] = 0

    # Clearness index
    kt = np.zeros(b.shape)
    kt[b > 0] = grad[b > 0] / b[b > 0]
    kt[kt > 1] = 0

    # Diffuse indices
    fd = 0.868 + 1.335 * (kt) - 5.782 * (kt**2) + 3.721 * (kt**3)
    fd[kt <= 0.13] = 0.952
    fd[kt > 0.8] = 0.141
    drad = grad * fd

    # Beam radiation (Global radiation - diffuse radiation)
    brad = grad - drad  # beam radiation
    bbeam = brad / cost(zen)
    bbeam[bbeam < 0] = 0
    bbeam[bbeam > 2000] = 0

    efficiency = 0.1
    p_inst = 1
    area = p_inst * 0.00875
    derate = 0.77

    #set panel orientation
    panel_matrix_az, panel_matrix_tlt, panel_matrix_weight, azimuth_median, tilt_median = read_solar_panel(
        configfile)

    if not azimuth_median:
        azimuth_median = 0
    if not tilt_median:
        tilt_median = lat

    logger.info(
        'Panel is directed with azimuth at median %d and tilt at median %d' %
        (azimuth_median, tilt_median))

    panel_matrix_az = azimuth_median + panel_matrix_az
    panel_matrix_tlt = tilt_median + panel_matrix_tlt

    prod = np.zeros(len(bbeam))
    # solar tracker is considered
    # TIPS: https://www.e-education.psu.edu/eme810/node/576
    # https://www.e-education.psu.edu/eme810/node/485
    if solar_tracker == False:  # use the standard model if False
        for i in range(3):
            for j in range(3):
                panel_az = panel_matrix_az[i, j]
                panel_slp = panel_matrix_tlt[i, j]
                weight = panel_matrix_weight[i, j]

                #angel between beam and panel
                costh_s = sint(delta) * sint(lat) * cost(panel_slp) - np.sign(
                    lat) * sint(delta) * cost(lat) * sint(panel_slp) * cost(
                        panel_az) + cost(delta) * cost(omega) * cost(
                            lat) * cost(panel_slp) + np.sign(lat) * cost(
                                delta) * cost(omega) * sint(lat) * sint(
                                    panel_slp) * cost(panel_az) + cost(
                                        delta) * sint(omega) * sint(
                                            panel_az) * sint(panel_slp)
                bbeam_panel = bbeam * np.maximum(0, costh_s)

                drad_panel = drad * (1 + cost(panel_slp)) / 2
                rad_panel = bbeam_panel + drad_panel

                prod = prod + weight * rad_panel * efficiency * area * derate

    elif solar_tracker == True:
        costh_s_tracker = 1  # strålen treffer 90 grader på panelet hele tiden, ergo er cos av denne vinkelen lik 1
        bbeam_panel = bbeam * costh_s_tracker  # bbeam sørger for at dette blir null når solen er nede
        drad_panel = drad * (1 + cost(zen)) / 2  # setter zentih angle som tilt
        rad_panel = bbeam_panel + drad_panel
        prod = rad_panel * efficiency * area * derate

    return jdate, swdown, prod, lat, lon
Exemple #20
0
    for row in reader:
        date_time = row[0].split(' UTC')[0]
        date = date_time.split(' ')[0]
        time = date_time.split(' ')[1]

        year = int(date.split('-')[0])
        month = int(date.split('-')[1])
        day = int(date.split('-')[2])

        hour = int(time.split(':')[0])
        minute = int(time.split(':')[1])
        second = int(time.split(':')[2])

        dt = datetime(year, month, day, hour, minute, second, 0)

        azimuth, zenith = sunpos(dt,lat,lon,ele)[:2]
        elevation = 90 - zenith

        azimuth = round(azimuth, 3)
        elevation = round(elevation, 3)
        temperature = float(row[2])
        humidity = float(row[3])
        vPanel = float(row[4])
        vPyra = float(row[5])

        date = date.replace('-','')

        if date not in data.keys():
            data[date] = ()

        if 7 < hour < 15:
Exemple #21
0
def ReadDecomp(F1, F2, F3, F4, SAT_ID, SEN_ID):
    if (SAT_ID == "C03" and SEN_ID == "MX"):
        t = "2019-11-06 10:22:56"
        lat = 21.45
        lon = 78.12
        cloud = 0
        total = 0
        ##################################### Deep Learning Code #########################################

        model = Sequential()
        model.add(Dense(16, activation='relu', input_shape=(4, )))
        model.add(BatchNormalization())
        model.add(Dense(32, activation='relu'))
        model.add(BatchNormalization())
        model.add(Dense(64, activation='relu'))
        model.add(BatchNormalization())
        model.add(Dense(32, activation='relu'))
        model.add(BatchNormalization())
        model.add(Dense(16, activation='relu'))
        model.add(BatchNormalization())
        model.add(Dense(2, activation='softmax'))
        model.load_weights(
            "/maintenance/rkrg_dnd/DLProjects/RealTimeCloudC3/Model_Final.h5")

        ##################################################################################################
        files = [F1, F2, F3, F4]
        AUX_LEN = 200
        CCD_COUNT = 16080
        NUMBER_OF_BITS = 11
        BYTE_COUNT = 2
        LINE_BYTES = CCD_COUNT * BYTE_COUNT
        while not (os.path.exists(F1) and os.path.exists(F2)
                   and os.path.exists(F3) and os.path.exists(F4)):
            time.sleep(1)
            print('waiting for the pass')
        start_point = 0
        file1_size = 0

        while True:
            f_sizes = []
            for i in range(1, 5):
                f2 = os.stat(files[i - 1])
                file2_size = f2.st_size
                comp = file2_size - file1_size
                f_sizes.append(comp)
            f_sizes = np.array(f_sizes)
            min_lines = np.min((f_sizes // (LINE_BYTES + AUX_LEN)))
            #print(file1_size, file2_size, comp

            if min_lines > 0:
                status = 0
                print('Reading newly received data')
                # FILE_SIZE= os.path.getsize(FILE_NAME)
                # print(FILE_SIZE)
                # if(not NUM_LINES.is_integer()):
                #     print(' THE DATA MIGHT BE CORRUPTED OR IS INCOMPLETE, IGNORING THE INCOMPLETE LINE AND READING COMPLETE LINES')

                #- READING BINARY ---
                # 0 (or os.SEEK_SET): means your reference point is the beginning of the file
                # 1 (or os.SEEK_CUR): means your reference point is the current file position
                # 2 (or os.SEEK_END): means your reference point is the end of the file
                bands = []
                for i in range(1, 5):
                    file = open(files[i - 1], "rb")
                    data = []
                    file.seek(start_point)
                    end_point = start_point + min_lines * (LINE_BYTES +
                                                           AUX_LEN)
                    for line in range(min_lines):

                        # - EACH LINE CONSISTS OF BYTE_COUNT*CCD_COUNT BYTES
                        # - DETERMINE THE NUMBER OF LINES = FILE_SIZE(bytes)/12000
                        # - IF NOT EXACTLY DIVISIBLE TAKE FLOOR. ( RETURN A WARNING IF POSSIBLE )
                        file.seek(AUX_LEN, 1)
                        x = np.fromfile(file, np.uint16, CCD_COUNT)
                        #print(x)
                        data.append(x)
                    # break
                    #print(np.shape(np.array(data)), min_line
                    bands.append(np.array(data))
                    #print(line)
                start_point = end_point
                file1_size = file1_size + min_lines * (LINE_BYTES + AUX_LEN)
                print(np.shape(np.array(bands)), min_lines)
                az, zen = sunpos(t, lat, lon, 0)[:2]
                print('Generating TOA Images')
                bands = GenerateTOAImages(np.array(bands), 4, 1, t, 90 - zen,
                                          C3_ExoAtmospheric_Irradiance)
                print(np.shape(bands))
                print(
                    'TOA Images are generated and are being classified to check for cloud pixels'
                )
                bands = np.array(bands)
                X_test = np.reshape(bands, (4, -1))
                X_test = np.swapaxes(X_test, 0, 1)
                print(np.shape(X_test))
                labels_test = model.predict_classes(X_test,
                                                    batch_size=10000000,
                                                    verbose=1)
                total = total + len(labels_test)
                labels_test = labels_test.reshape(
                    np.shape(bands)[1],
                    np.shape(bands)[2])
                print('Classification Done using Deep Neural Network')
                cloud = cloud + np.count_nonzero(labels_test == 0)

                #return(labels_test, min_lines)
            else:
                if status == 0:
                    t1 = datetime.datetime.now()
                    status = 1
                else:
                    diff = datetime.datetime.now() - t1
                    if diff.seconds > 15:

                        print('Cloud Percentage: ' +
                              str((cloud / total) * 100))
                        return 0

                pass
    else:
        return None
Exemple #22
0
def main():
    indir = "nomiss-airx3std/"
    outdir = "rrtm-airx3std/"
    if not os.path.exists(outdir):
        os.makedirs(outdir)

    # constants
    fillvalue_float = 9.96921e+36
    alb = 0.75
    emis = 0.985
    solar_constant = 1367.0

    absorber_vmr = dict()
    absorber_vmr['CO2'] = 355. / 1E6
    absorber_vmr['CH4'] = 1714. / 1E9
    absorber_vmr['N2O'] = 311. / 1E9
    absorber_vmr['O2'] = 0.21
    absorber_vmr['CFC11'] = 0.280 / 1E9
    absorber_vmr['CFC12'] = 0.503 / 1E9
    absorber_vmr['CFC22'] = 0.
    absorber_vmr['CCL4'] = 0.

    # stn names and lat/lon
    lst_stn = pd.read_csv('../resources/stations_radiation.txt')
    stn_names = lst_stn['network_name'].tolist()
    latstn = lst_stn['lat'].tolist()
    lonstn = lst_stn['lon'].tolist()

    nstn = len(stn_names)
    '''stn_names = 'imau09'
    latstn = -75.0
    lonstn = 0.0
    nstn = 1'''

    cleardays = pd.read_csv('cleardays.csv')

    # main function
    for i in range(nstn):
        stn = stn_names[i]
        lat_deg = latstn[i]
        lon_deg = lonstn[i]
        '''stn = stn_names
        lat_deg = latstn
        lon_deg = lonstn'''

        fout = outdir + stn + '.rrtm.nc'
        sw_dn_complete = []
        sw_up_complete = []
        lw_dn_complete = []
        lw_up_complete = []
        time_op = []

        clr_dates = cleardays.loc[cleardays['network_name'] == stn, 'date']

        for date in clr_dates:
            sw_dn = []
            sw_up = []
            lw_dn = []
            lw_up = []

            sw_dn_final = [None] * 24
            sw_up_final = [None] * 24
            lw_dn_final = [None] * 24
            lw_up_final = [None] * 24
            for sfx in ['A', 'D']:
                flname = indir + stn + '/' + stn + '.' + str(
                    date) + '.' + sfx + '.nc'

                if not os.path.isfile(flname):  # Check if nomiss file exists
                    continue

                fin = xr.open_dataset(flname)

                # Get values from nomiss netCDF file
                tmp = fin['t'].values
                ts = fin['ts'].values
                plev = fin['plev'].values
                ps = fin['ps'].values

                state = make_column(lev=plev, ps=ps, tmp=tmp, ts=ts)

                o3 = fin['o3'].values
                absorber_vmr['O3'] = o3

                h2o_q = fin['q'].values

                aod_count = fin['aod_count'].values

                # knob
                aod = np.zeros((6, 1, 24))
                aod[1, 0, -aod_count:] = 0.12 / aod_count
                aod[5, 0, :15] = 0.0077 / 15

                # Calculate shortwave radiation down for each hour
                for hr in range(24):
                    dtime = datetime.strptime(flname.split('.')[1],
                                              "%Y%m%d") + timedelta(hours=hr,
                                                                    minutes=30)

                    sza = sunposition.sunpos(dtime, lat_deg, lon_deg, 0)[1]
                    cossza = np.cos(np.radians(sza))

                    rad = climlab.radiation.RRTMG(name='Radiation',
                                                  state=state,
                                                  specific_humidity=h2o_q,
                                                  albedo=alb,
                                                  coszen=cossza,
                                                  absorber_vmr=absorber_vmr,
                                                  emissivity=emis,
                                                  S0=solar_constant,
                                                  icld=0,
                                                  iaer=6,
                                                  ecaer_sw=aod)
                    rad.compute_diagnostics()

                    dout = rad.to_xarray(diagnostics=True)
                    sw_dn.append(dout['SW_flux_down_clr'].values[-1])
                    sw_up.append(dout['SW_flux_up_clr'].values[-1])
                    lw_dn.append(dout['LW_flux_down_clr'].values[-1])
                    lw_up.append(dout['LW_flux_up_clr'].values[-1])

            if sw_dn:
                if len(sw_dn) == 24:
                    sw_dn_final = sw_dn  # If only either 'A' or 'D' file present
                    sw_up_final = sw_up  # If only either 'A' or 'D' file present
                    lw_dn_final = lw_dn  # If only either 'A' or 'D' file present
                    lw_up_final = lw_up  # If only either 'A' or 'D' file present
                else:
                    count = 0
                    while count < 24:
                        sw_dn_final[count] = (sw_dn[count] + sw_dn[
                            count + 24]) / 2.0  # Average of both 'A' and 'D'
                        sw_up_final[count] = (sw_up[count] + sw_up[
                            count + 24]) / 2.0  # Average of both 'A' and 'D'
                        lw_dn_final[count] = (lw_dn[count] + lw_dn[
                            count + 24]) / 2.0  # Average of both 'A' and 'D'
                        lw_up_final[count] = (lw_up[count] + lw_up[
                            count + 24]) / 2.0  # Average of both 'A' and 'D'
                        count += 1

                sw_dn_complete.append(
                    sw_dn_final
                )  # Combine sw_dn_final for multiple days in a single variable
                sw_up_complete.append(
                    sw_up_final
                )  # Combine sw_up_final for multiple days in a single variable
                lw_dn_complete.append(
                    lw_dn_final
                )  # Combine lw_dn_final for multiple days in a single variable
                lw_up_complete.append(
                    lw_up_final
                )  # Combine lw_up_final for multiple days in a single variable

                for hr in range(
                        24):  # Time variable to be written in netCDF file
                    time_op.append(
                        datetime.strptime(str(date), "%Y%m%d") +
                        timedelta(hours=hr, minutes=30))

        if sw_dn_complete:  # Write data

            # Make single list from list of lists
            sw_dn_complete = [
                item for sublist in sw_dn_complete for item in sublist
            ]
            sw_up_complete = [
                item for sublist in sw_up_complete for item in sublist
            ]
            lw_dn_complete = [
                item for sublist in lw_dn_complete for item in sublist
            ]
            lw_up_complete = [
                item for sublist in lw_up_complete for item in sublist
            ]

            # Get seconds since 1970
            time_op = [(i - datetime(1970, 1, 1)).total_seconds()
                       for i in time_op]

            ds = xr.Dataset()

            ds['fsds'] = 'time', sw_dn_complete
            ds['fsus'] = 'time', sw_up_complete
            ds['flds'] = 'time', lw_dn_complete
            ds['flus'] = 'time', lw_up_complete
            ds['time'] = 'time', time_op

            ds['fsds'].attrs = {
                "_FillValue":
                fillvalue_float,
                "units":
                'watt meter-2',
                "long_name":
                'RRTM simulated shortwave downwelling radiation at surface'
            }
            ds['fsus'].attrs = {
                "_FillValue":
                fillvalue_float,
                "units":
                'watt meter-2',
                "long_name":
                'RRTM simulated shortwave upwelling radiation at surface'
            }
            ds['flds'].attrs = {
                "_FillValue":
                fillvalue_float,
                "units":
                'watt meter-2',
                "long_name":
                'RRTM simulated longwave downwelling radiation at surface'
            }
            ds['flus'].attrs = {
                "_FillValue":
                fillvalue_float,
                "units":
                'watt meter-2',
                "long_name":
                'RRTM simulated longwave upwelling radiation at surface'
            }
            ds['time'].attrs = {
                "_FillValue": fillvalue_float,
                "units": 'seconds since 1970-01-01 00:00:00',
                "calendar": 'standard'
            }

            ds.to_netcdf(fout)
            print(fout)