KBDI_from_NetCDF_BoM_ftp-checkpoint



In [4]:
import xarray as xr
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from ftplib import FTP

KBDI_from_BOM_file = './IDV71147_VIC_KBDI_SFC.nc'
KBDI_from_BOM_ftp = 'ftp://ftp.bom.gov.au/register/bom023/rKCm6dbQ/adfd/IDV71147_VIC_KBDI_SFC.nc.gz'

ftp = FTP('ftp.bom.gov.au')
ftp.login()
ftp.cwd('register/bom023/rKCm6dbQ/adfd')
ftp.retrlines('LIST')
ftp.retrbinary('RETR IDV71147_VIC_KBDI_SFC.nc.gz', open('IDV71147_VIC_KBDI_SFC.nc.gz', 'wb').write)
ftp.quit()


-rw-r--r--   18 1504     1502      7660335 Aug 16 20:13 IDV71000_VIC_T_SFC.grb
-rw-r--r--   18 1504     1502      6965466 Aug 16 20:13 IDV71000_VIC_T_SFC.nc.gz
-rw-r--r--   18 1504     1502      7261832 Aug 16 20:13 IDV71001_VIC_Td_SFC.grb
-rw-r--r--   18 1504     1502      6863905 Aug 16 20:13 IDV71001_VIC_Td_SFC.nc.gz
-rw-r--r--   18 1504     1502       452912 Aug 16 20:13 IDV71002_VIC_MaxT_SFC.grb
-rw-r--r--   18 1504     1502       269719 Aug 16 20:13 IDV71002_VIC_MaxT_SFC.nc.gz
-rw-r--r--   18 1504     1502       507772 Aug 16 20:13 IDV71003_VIC_MinT_SFC.grb
-rw-r--r--   18 1504     1502       305155 Aug 16 20:13 IDV71003_VIC_MinT_SFC.nc.gz
-rw-r--r--    9 1504     1502       449213 Aug 16 20:13 IDV71004_VIC_Precip_SFC.grb
-rw-r--r--    9 1504     1502       851034 Aug 16 20:13 IDV71004_VIC_Precip_SFC.nc.gz
-rw-r--r--    6 1504     1502       206356 Aug 16 20:13 IDV71005_VIC_DailyPrecip_SFC.grb
-rw-r--r--    6 1504     1502      1249390 Aug 16 20:13 IDV71005_VIC_DailyPrecip_SFC.nc.gz
-rw-r--r--   18 1504     1502     11777824 Aug 16 20:13 IDV71006_VIC_Wind_Mag_SFC.grb
-rw-r--r--   18 1504     1502      9254018 Aug 16 20:13 IDV71006_VIC_Wind_Mag_SFC.nc.gz
-rw-r--r--   18 1504     1502       861640 Aug 16 20:12 IDV71014_VIC_DailyPrecip25Pct_SFC.nc.gz
-rw-r--r--   18 1504     1502       474422 Aug 16 20:13 IDV71015_VIC_DailyPrecip50Pct_SFC.nc.gz
-rw-r--r--    6 1504     1502       293023 Aug 16 20:13 IDV71016_VIC_DailyPrecip75Pct_SFC.nc.gz
-rw-r--r--   11 1504     1502      1541813 Aug 16 20:12 IDV71017_VIC_Sky_SFC.grb
-rw-r--r--   11 1504     1502       702363 Aug 16 20:12 IDV71017_VIC_Sky_SFC.nc.gz
-rw-r--r--   18 1504     1502      6450044 Aug 16 20:13 IDV71018_VIC_RH_SFC.grb
-rw-r--r--   18 1504     1502      9248936 Aug 16 20:13 IDV71018_VIC_RH_SFC.nc.gz
-rw-r--r--   18 1504     1502      4314218 Aug 16 20:13 IDV71071_VIC_WindMagKmh_SFC.nc.gz
-rw-r--r--   18 1504     1502      7481382 Aug 16 20:13 IDV71089_VIC_Wind_Dir_SFC.grb
-rw-r--r--   18 1504     1502     15341764 Aug 16 20:13 IDV71089_VIC_Wind_Dir_SFC.nc.gz
-rw-r--r--   18 1504     1502       190604 Aug 16 20:12 IDV71090_VIC_DailyPoP_SFC.grb
-rw-r--r--   18 1504     1502        96064 Aug 16 20:12 IDV71090_VIC_DailyPoP_SFC.nc.gz
-rw-r--r--   18 1504     1502        52677 Aug 16 20:13 IDV71094_VIC_WxThunderstorms_SFC.nc.gz
-rw-r--r--   18 1504     1502      4939864 Aug 16 20:13 IDV71109_VIC_MixHgt_SFC.nc.gz
-rw-r--r--    5 1504     1502        94852 Aug 16 20:12 IDV71122_VIC_GFDI_SFC.nc.gz
-rw-r--r--   18 1504     1502       934828 Aug 16 20:13 IDV71127_VIC_DF_SFC.nc.gz
-rw-r--r--    5 1504     1502       108298 Aug 16 20:12 IDV71139_VIC_Curing_SFC.nc.gz
-rw-r--r--    5 1504     1502       421088 Aug 16 20:13 IDV71147_VIC_KBDI_SFC.nc.gz
Out[4]:
'221 Goodbye.'

In [5]:
import gzip
with gzip.open('IDV71147_VIC_KBDI_SFC.nc.gz') as f:
    content = f.read()

In [6]:
# Read the todays KBDI for Victoria
fh = xr.open_dataset(content)

In [7]:
# Today only
kbdi = fh['KBDI_SFC'][:][:][1]

df = kbdi.to_dataframe()

In [8]:
# Remove coords that are NaN
df = df[np.isfinite(df['KBDI_SFC'])]

In [9]:
# Export CSV file
df.to_csv('./kbdi.csv')

In [10]:
fig = plt.figure(figsize=(12,6))
plt.title(str(kbdi.attrs['long_name']))
kbdi.plot()
plt.show()



In [ ]: