-2

I have a problem with the choice of calculation years.

python flux2nc.py ../data/output/fluxes/ ../data/output/
    IMPORTANT: ../data/output/fluxes/ SHOULD CONTAIN ONLY FLUXES FILES!!!
    Choose output parameter
    1 - Precipitation
    2 - Evapotranspiration
    3 - Runoff
    4 - Base flow
    5 - Snow Water Equivalent
    6 - Soil moisture
    Choose output (1 a 6)>3
    Enter start year:2011
    End year:2012
    Traceback (most recent call last):
      File "flux2nc.py", line 240, in <module>
        main()
      File "flux2nc.py", line 234, in main
        flux2nc(sys.argv[1],sys.argv[2])
      File "flux2nc.py", line 120, in flux2nc
        inidate = dt.date(start_year,1,1)
    TypeError: an integer is required (got type str)

I know that this problem is already posed, but I can't find the exact solution given my limited knowledge on python, and the script is pretty much complicated.

here is the part of the source code, related to my question.

# import dependencies
import sys
import os, string
# handle dates...
import datetime as dt
# NetCDF and Numeric
from netCDF4 import *
from numpy import *

    # if the date information is not set get it from user
    if start_year == None:
        # for what date?
        start_year = input("Enter start year:")
    if end_year == None:
        end_year = input("End year:")

    # set date information in datetime object
    inidate = dt.date(start_year,1,1)
    enddate = dt.date(end_year,12,31)

    # calculate number of days in time series
    days = enddate.toordinal() - inidate.toordinal()+1
Bouram
  • 33
  • 1
  • 1
  • 3
  • Hi there! Since you seem new here, please go over [how to ask](https://stackoverflow.com/help/how-to-ask) and [on-topic](https://stackoverflow.com/help/on-topic). This will help others to help you. If you have questions, provide your code as a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). Happy coding! – TwistedSim Apr 19 '18 at 20:12
  • Notice the error says, "an integer is required (got type str)". When you get user input via `input()` you get a string. `dt.date` requires integers (see [documentation](https://docs.python.org/3/library/datetime.html#datetime.date) so you must convert the input. This question may be helpful: [How can I read inputs as integers](https://stackoverflow.com/q/20449427/945456). – Jeff B Apr 19 '18 at 20:34

1 Answers1

2

Your error is probably that you forgot to cast the input(...) to int:

start_year = input('Enter start year')
start_year = int(start_year)

you should do the same for end_year and output.

Note: It's really hard to try to help you without the source code. I need to infer a lot of things to help you diagnosis this error.

TwistedSim
  • 1,960
  • 9
  • 23