Exemple #1
0
def getDailyMax(fil):

    # Get meta info for this station
    fw = FixedWidth(META_CONFIG)
 
    # Get number of observations
    nday = (sum(1 for line in open(fil,'rb')) - 1)/2

    # read in observations
    f = open(fil, 'rb')
    header = f.readline()
    meta = fw._string_to_dict(header)

    fw = FixedWidth(TIDE_CONFIG)

    cols = ['Gauge','Name','Lat','Lon','Method','Datum','Offset','Year','Month','Day','Max_Tide']
    dfOut = pd.DataFrame(columns=cols)

    charOut = np.empty((nday,len(cols)),dtype=object)

    lat,lon = fmtll(meta['lat'],meta['lon'])

    date_last = -9999
    nobs_last = -9999
    i = 0
    for line in f:
        parts = fw._string_to_dict(line)
        lvls = [int(parts[str(ii)]) for ii in range(1, 13)]

        if meta['datum'] == 'X': 
          lvls = [9999 for jj in lvls] # these data are not referenced to a datum

        date = parts['year']+parts['month']+parts['day']
        if date == date_last:
          this_max = np.max(lvls)
          if this_max == 9999:
            this_max = np.nan
    
          if (nobs_last + sum([lvls[ii]!=9999 for ii in range(len(lvls))])) < 12:
            dayMax = -999
          else:
            dayMax = np.max([this_max,max_last])
            if np.isnan(dayMax): dayMax = -999 

          charOut[i,:] = [meta['number']+meta['version'],meta['name'],"{0:.2f}".format(lat),"{0:.2f}".format(lon),meta['method'], \
                          meta['datum'], meta['offset'],parts['year'],parts['month'],parts['day'],str(dayMax)]

          i+=1
        else:
          nobs_last = sum([lvls[ii]!=9999 for ii in range(len(lvls))])
          max_last = np.max(lvls)
          if max_last == 9999:
            max_last = np.nan
          date_last = date
    
    #return dfOut,int(str(date)[0:4]),meta
    return charOut,int(str(date)[0:4]),meta
Exemple #2
0
def getMonMean(fil):

     print "Opening file: %s" %fil
     fw = FixedWidth(META_CONFIG_MON)
 
     # determine how many years we will read
     f = open(fil, 'rb')
     header = f.readline()
     all_years = [int(line[0:12].strip()) for line in f]

     # read in observations
     f = open(fil, 'rb')
     header = f.readline()
     meta = fw._string_to_dict(header)

     nlin = (max(all_years) - min(all_years) + 1)*12
     charOut = np.empty((nlin,7),dtype=object)

     lat,lon = fmtll(meta['lat'],meta['lon'])

     i = 0
     nmiss = 0
     _sum = 0.
     this_mon = 1
     cnt = 0
     for ii,line in enumerate(f):

         line = line.split()
         mon = int(line[1])

         if mon == this_mon:
            year = line[0]
            if line[3] == '9999' or meta['datum'] == 'X':
              nmiss += 1
            else:
              _sum = _sum + float(line[3])
              cnt += 1
         else:

            # The monthly values are calculated from the daily data with a
            # simple average of all the daily values in a month. If seven or
            # fewer values are missing, the monthly value is calculated.  
            if (nmiss <= 7): 
               avg = _sum / float(cnt)
            else:
               avg = -999
            charOut[i,:] = [meta['number']+meta['version'],meta['name'],"{0:.2f}".format(lat),"{0:.2f}".format(lon), \
                            year,str(this_mon).zfill(2),str(int(avg))]
            nmiss = 0
            _sum = 0.
            cnt = 0
            this_mon = mon
            i += 1

     if (nmiss <= 7): 
        avg = _sum / float(cnt)
     else:
        avg = -999
     charOut[i,:] = [meta['number']+meta['version'],meta['name'],"{0:.2f}".format(lat),"{0:.2f}".format(lon), \
                     year,str(this_mon).zfill(2),str(int(avg))]

     return charOut,meta