#!/usr/local/sci/bin/python2.7 # NOTE - you will need to point this script to where your Python2.7 installation lives #################################################################### # # British Crown Copyright (c) 2018, the Met Office All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # # Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. # # Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # Neither the name of the Met Office nor the names of its contributors # may be used to endorse or promote products derived from this software # without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES# LOSS OF USE, # DATA, OR PROFITS# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ######################################################### #################################################################### # # AUTHOR - Robert Dunn # DATE - 24th September 2018 # #################################################################### # import libraries import urllib2 import os import numpy as np # set fixed variables VERSION = "2.0.2.2017f" REMOTE_LOCATION = "/hadobs/hadisd/v202_2017f/data/" diagnostics = True #********************************************* def http(host, remote_loc, filename, local_loc, diagnostics = False): """ Using urllib2 http access :param str host: HTTP hostename :param str remote_loc: remote directory :param str filename: remote filename :param str local_loc: local directory :param bool diagnostics: extra output """ if diagnostics: print "{}/{}/{}".format(host, remote_loc, filename) req = urllib2.Request("{}/{}/{}".format(host, remote_loc, filename), headers={'User-Agent' : "Magic Browser"}) remote_file = urllib2.urlopen(req) local_file = open(os.path.join(local_loc, filename), "w") local_file.write(remote_file.read()) local_file.close() if diagnostics: print " Downloaded" return # http #*************************************** # read the station listing and extract the IDs station_info = np.genfromtxt("hadisd_station_info_v202.txt", dtype=(str)) station_ids = station_info[:, 0] # spin through the ids, building up the link required for each download for station in station_ids: print station # files are at: # https://www.metoffice.gov.uk/hadobs/hadisd/v202_2017f/data/hadisd.2.0.2.2017f_19310101-20171231_010010-99999.nc.gz # so build up the link http("http://www.metoffice.gov.uk/", REMOTE_LOCATION, "hadisd.{}_19310101-20171231_{}.nc.gz".format(VERSION, station), ".", diagnostics = diagnostics) # escape loop after single download - remove this to run through all stations. break #################################################################### # # END # ####################################################################