fetch.py 1.43 KB
Newer Older
Isabelle Pison's avatar
Isabelle Pison committed
1
2
import os
import numpy as np
3
4
import pandas as pd
import datetime
Isabelle Pison's avatar
Isabelle Pison committed
5
6
7
from pycif.utils import path


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

def fetch(ref_dir, ref_file, input_dates, target_dir, tracer=None, component=None):

    """
    Reads netcdf files from CAMS for a given species.
    One file per month.

    Args
    -----

    - ref_dir: directory where the original files are found
    - ref_file: (template) name of the original files
    - input_dates: list of two dates: the beginning and end of the simulation
    - target_dir: directory where the links to the orginal files are created

    Returns
    ---------
    - list_dates
    - list_files

    """
Isabelle Pison's avatar
Isabelle Pison committed
29
30
31

    list_files = {}
    list_dates = {}
32
33
34
35
36
37
38
39
40
41
42
    list_period_dates = \
            pd.date_range(input_dates[0], input_dates[1], freq=tracer.file_freq)
    for dd in list_period_dates:
      #to_timedelta does not work with all frequencies!
      datef = datetime.datetime(year = dd.year, month = dd.month + 1, day=1)
      list_hours = pd.date_range(
                   dd, datef, freq="6H")
      list_dates[dd] = [[hh, hh + datetime.timedelta(hours=6)]
                                  for hh in list_hours]
      file = dd.strftime("{}/{}".format(ref_dir, ref_file))
      list_files[dd] = (len(list_hours) * [file])
43

44
45
46
47
      target_file = "{}/{}".format(target_dir, os.path.basename(file))
      path.link(file, target_file)
    
    print('XXXXX pourquoi depasse-t-il sur 6h le mois suivant?XXX')
Isabelle Pison's avatar
Isabelle Pison committed
48
    return list_files, list_dates