Skip to content

Commit

Permalink
moved files
Browse files Browse the repository at this point in the history
  • Loading branch information
initze committed Dec 11, 2024
1 parent e6c3998 commit 39fbb2b
Show file tree
Hide file tree
Showing 17 changed files with 1,142 additions and 0 deletions.
Binary file not shown.
Binary file not shown.
96 changes: 96 additions & 0 deletions noaaplotter/scripts/download_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Imports
import argparse

from noaaplotter.utils.download_utils import download_from_noaa


def main():
"""
Main Function
:return:
"""
##### Parse arguments #####
parser = argparse.ArgumentParser(description="Parse arguments.")

parser.add_argument(
"-o",
dest="output_file",
type=str,
required=True,
default="data/data.csv",
help="csv file to save results",
)

parser.add_argument(
"-t", dest="token", type=str, required=False, default="", help="NOAA API token"
)

parser.add_argument(
"-sid",
dest="station_id",
type=str,
required=False,
default="",
help='NOAA Station ID, e.g. "GHCND:USW00026616" for Kotzebue, only if loading through NOAA API',
)

parser.add_argument(
"-loc",
dest="loc_name",
type=str,
required=False,
default="",
help="Location name",
)

parser.add_argument(
"-dt",
dest="datatypes",
type=list,
required=False,
default=["TMIN", "TMAX", "PRCP", "SNOW"],
)

parser.add_argument(
"-start",
dest="start_date",
type=str,
required=True,
help='start date of plot ("yyyy-mm-dd")',
)

parser.add_argument(
"-end",
dest="end_date",
type=str,
required=True,
help='end date of plot ("yyyy-mm-dd")',
)

parser.add_argument(
"-n_jobs",
dest="n_jobs",
type=int,
required=False,
default=1,
help="number of parallel processes",
)

args = parser.parse_args()

download_from_noaa(
output_file=args.output_file,
start_date=args.start_date,
end_date=args.end_date,
datatypes=args.datatypes,
noaa_api_token=args.token,
loc_name=args.loc_name,
station_id=args.station_id,
n_jobs=args.n_jobs,
)


if __name__ == "__main__":
main()
54 changes: 54 additions & 0 deletions noaaplotter/scripts/download_data_ERA5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Imports
import argparse
import os

from src.download_utils import download_era5_from_gee


def main():
"""
Main Function
:return:
"""
##### Parse arguments #####
parser = argparse.ArgumentParser(description='Parse arguments.')

parser.add_argument('-o', dest='output_file', type=str, required=True,
default='data/data.csv',
help='csv file to save results')

parser.add_argument('-lat', dest='lat', type=float, required=True,
help='Latitude of selected location')

parser.add_argument('-lon', dest='lon', type=float, required=True,
help='Longitude of selected location')

parser.add_argument('-loc', dest='loc_name', type=str, required=False,
default='',
help='Location name')

parser.add_argument('-dt', dest='datatypes', type=list, required=False, default=['TMIN', 'TMAX', 'PRCP', 'SNOW'])

parser.add_argument('-start', dest='start_date', type=str, required=True,
help='start date of plot ("yyyy-mm-dd")')

parser.add_argument('-end', dest='end_date', type=str, required=True,
help='end date of plot ("yyyy-mm-dd")')

args = parser.parse_args()

# remove file if exists
if os.path.exists(args.output_file):
os.remove(args.output_file)

download_era5_from_gee(latitude=args.lat,
longitude = args.lon,
end_date= args.end_date,
start_date = args.start_date,
output_file = args.output_file)


if __name__ == "__main__":
main()
100 changes: 100 additions & 0 deletions noaaplotter/scripts/download_data_SST.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Imports
import argparse
import csv
from datetime import datetime
import numpy as np
import os
import pandas as pd
import tqdm
from joblib import delayed, Parallel
from noaaplotter.utils. import dl_noaa_api
import ee
import geemap

def main():
"""
Main Function
:return:
"""
##### Parse arguments #####
parser = argparse.ArgumentParser(description='Parse arguments.')

parser.add_argument('-o', dest='output_file', type=str, required=True,
default='data/data.csv',
help='csv file to save results')

parser.add_argument('-lat', dest='lat', type=float, required=True,
help='Latitude of selected location')

parser.add_argument('-lon', dest='lon', type=float, required=True,
help='Longitude of selected location')

parser.add_argument('-loc', dest='loc_name', type=str, required=False,
default='',
help='Location name')

#parser.add_argument('-dt', dest='datatypes', type=list, required=False, default=['TMIN', 'TMAX', 'PRCP', 'SNOW'])

parser.add_argument('-start', dest='start_date', type=str, required=True,
help='start date of plot ("yyyy-mm-dd")')

parser.add_argument('-end', dest='end_date', type=str, required=True,
help='end date of plot ("yyyy-mm-dd")')

args = parser.parse_args()

# remove file if exists
if os.path.exists(args.output_file):
os.remove(args.output_file)

ee.Initialize()

EE_LAYER = "NOAA/CDR/OISST/V2_1"

location = ee.Geometry.Point([args.lon, args.lat])

# load ImageCollection
col = ee.ImageCollection(EE_LAYER).filterBounds(location).filterDate(args.start_date, args.end_date).select('sst')

# Download data
print("Start downloading NOAA CDR OISST v02r01 data.")
print("Download may take a while.\n1yr: ~5 seconds\n10yrs: ~35 seconds\n50yrs: ~8 min")

out_dict = geemap.extract_pixel_values(col, location, getInfo=True)
df_gee = pd.DataFrame(data=[out_dict.keys(), out_dict.values()]).T

# parse dates and values
df_gee['time'] = df_gee[0].apply(lambda x: f'{x[:4]}-{x[4:6]}-{x[6:8]}')
df_gee['feature'] = df_gee[0].apply(lambda x: x[9:])
df_gee['value'] = df_gee[1]

df = df_gee.pivot_table(values='value', columns=['feature'], index='time')#.reset_index(drop=False)

# #### recalculate values
df_new = pd.DataFrame(index=df.index)

temperature_cols = ['sst']
#precipitation_cols = ['total_precipitation']
df_joined = df_new.join(df[temperature_cols]*0.01)#.join(df[precipitation_cols] *1e3).reset_index(drop=False)

# Create Output
df_joined.reset_index(drop=False, inplace=True)
rename_dict = {'time': 'DATE', 'sst': 'TMAX'}
df_renamed = df_joined.rename(columns=rename_dict)
df_renamed['NAME'] = ''
df_renamed['STATION'] = ''
df_renamed['SNWD'] = ''
df_renamed['PRCP'] = ''
df_renamed['TAVG'] = df_renamed['TMAX']
df_renamed['TMIN'] = df_renamed['TMAX']

output_cols = ["STATION","NAME","DATE","PRCP","SNWD","TAVG","TMAX","TMIN"]
df_save = df_renamed[output_cols].astype(str)

df_save.to_csv(args.output_file, index=False)


if __name__ == "__main__":
main()
102 changes: 102 additions & 0 deletions noaaplotter/scripts/plot_daily.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
from noaaplotter.noaaplotter import NOAAPlotter
import argparse

def main():
"""
Main Function
:return:
"""
##### Parse arguments #####
parser = argparse.ArgumentParser(description='Parse arguments.')

parser.add_argument('-infile', dest='infile', type=str, required=True,
default='data/temp.csv',
help='input file with climate data')

parser.add_argument('-t', dest='token', type=str, required=False,
default='',
help='NOAA API token, only if loading through NOAA API')

parser.add_argument('-sid', dest='station_id', type=str, required=False,
default='',
help='NOAA Station ID, e.g. "GHCND:USW00026616" for Kotzebue, only if loading through NOAA API')

parser.add_argument('-start', dest='start_date', type=str, required=True,
help='start date of plot ("yyyy-mm-dd")')

parser.add_argument('-end', dest='end_date', type=str, required=True,
help='end date of plot ("yyyy-mm-dd")')

parser.add_argument('-loc', dest='location', required=False,
type=str, default=None,
help='Location name, must be in data file')

parser.add_argument('-save_plot', dest='save_path', type=str, required=False,
default=None,
help='filepath for plot')

parser.add_argument('-t_range', dest='t_range', type=float, nargs=2, required=False,
default=[None, None],
help='temperature range in plot')

parser.add_argument('-p_range', dest='p_range', type=float, required=False,
default=None,
help='maximum precipitation value in plot')

parser.add_argument('-s_range', dest='s_range', type=float, required=False,
default=None,
help='maximum snow accumulation value in plot')

parser.add_argument('-snow_acc', dest='snow_acc', required=False,
default=False, action='store_true',
help='show snow accumulation, only useful for plotting winter season (e.g. July to June')

parser.add_argument('-filtersize', dest='filtersize', type=int, required=False,
default=7,
help='parameter to smooth climate temperature series by n days for smoother visual appearance. '
'default value: 7')

parser.add_argument('-dpi', dest='dpi', type=float, required=False,
default=100,
help='dpi for plot output')

parser.add_argument('-plot', dest='show_plot', required=False,
default=False, action='store_true',
help='Location name, must be in data file')

parser.add_argument('-figsize', dest='figsize', type=float, nargs=2, required=False,
default=[9, 6],
help='figure size in inches width x height. 15 10 recommended for 1 year, 30 10 for 2 years ...')

parser.add_argument('-title', dest='title', type=str, required=False,
default=None,
help='Plot title')

args = parser.parse_args()

##### Download from NOAA #####

##### Run Plotting function #####
n = NOAAPlotter(args.infile,
location=args.location,
climate_filtersize=args.filtersize)

n.plot_weather_series(start_date=args.start_date,
end_date=args.end_date,
show_snow_accumulation=args.snow_acc,
#kwargs_fig={'dpi':args.dpi, 'figsize':args.figsize},
plot_extrema=True,
show_plot=args.show_plot,
save_path=args.save_path,
plot_tmin=args.t_range[0],
plot_tmax=args.t_range[1],
plot_pmax=args.p_range,
plot_snowmax=args.s_range,
dpi=args.dpi,
figsize=args.figsize,
title=args.title)

if __name__ == "__main__":
main()
Loading

0 comments on commit 39fbb2b

Please sign in to comment.