#Original Function Location: /themis/lib/dav_lib/public_library/latest/map_projection.dvrc /themis/lib/dav_lib/library/map_projection.dvrc Source Code for Function: "jmars()" in "map_projection.dvrc" (Public)

map_projection_version=1.00

#1.00 initial version. added jmars() function -cse 2-7-2011
#    this allows a user to create a link between jmars and davinci

# jmars                    -created by c.edwards 2-7-2011



define jmars(id,ul,ur,ll,lr,ignore,name,port,host,v) {

    if($ARGC<1) {
        printf("Use this function to output a data file to your JMARS Session\n")
        printf("JMARS will take the input data and project it on to the current map in the Davinci Stamps layer\n\n")
        printf("Required:\n")
        printf("\t$1 = stretched byte data or a file path\n")
        printf("\tul = Latitude//Longitude of the upper left corner of the data\n")
        printf("\tlr = Latitude//Longitude of the lower right corner of the data\n")
        printf("\tNote: if only ul and lr are specified ur and ll values will be filled in automatically\n")
        printf("\tur = Latitude//Longitude of the upper right corner of the data\n")
        printf("\tll = Latitude//Longitude of the lower left corner of the data\n\n")
        printf("OR\n")
        printf("\t$1 = a STRUCT with a stretched byte .data, .ul, .lr, (optional, .ur, .ll) elements in the formats above\n\n")
        printf("OR\n")
        printf("\t$1 = stretched byte data or a file path\n")
        printf("\t$2 = an ISIS2 or ISIS3 structure (from load_pds) with map projection information\n\n")
        printf("Optional:\n")
        printf("\thost = name of the machine the instance of JMARS is running on (Default=\"localhost\")\n")
        printf("\tport = port number specified in the Davinci Stamps Layer (Default=56277)\n")
        printf("\tignore = ignore value for image null space (Default = 0)\n")
        printf("\tname = specifiy a custom dataset name (Default = \"DV Upload \"+ID#\n")
        printf("\tid = specify an id for JMARS to identify this dataset\n")
        printf("\t\t this will auto increment by default, but can be forced to overwrite \n")
        printf("c.edwards 9/8/10\n")
        return(null)
    }

    if(HasValue(ignore)==0) {
        ignore="0"
    } else { 
        igndim=dim(ignore)
        ignore=resize(ignore,igndim[1]*igndim[2]*igndim[3],1,1)
        ignstr=sprintf("%d",ignore[1])
        for(i=2;i<=dim(ignore)[1];i+=1) {
            ignstr=sprintf("%s,%d",ignstr,ignore[i])
        }
        ignore=ignstr
    }

    if(HasValue(v)) printf("Ignore value string is: %s\n",ignore)


    if(HasValue(id)==0) {
        if(fexists($TMPDIR+"/000001_dvjmars.png")) {
            previous=syscall("ls "+$TMPDIR+"/??????_dvjmars.png")
            id=atoi(basename(previous)[:6,length(previous)])+1
        } else {
            id=1
        }
    }

    if(HasValue(v)) printf("id=%i\n",id)

    if(HasValue(name)==0) name=sprintf("DV Upload %.6d",id)
    if(HasValue(port)==0) port=56277
    if(HasValue(host)==0) host="localhost"

    if(HasValue(v)) {
        printf("name=%s\n",name)
        printf("host=%s\n",host)
        printf("port=%i\n",port)
    }

    outfile=sprintf("%s/%.6d_dvjmars.png",$TMPDIR,id)
    urlprefix=sprintf("http://%s:%i/jmarsdvlink?",host,port)

  ver=version()[18:]
  ver="davinci "+ver

    #check for the data with specified ul, lr etc 
    if(type($1)!="STRUCT" && $ARGC==1) {
        if(HasValue(v)) printf("User defined coords case\n")

        if(HasValue(ul)==0) {
            printf("Upper Left Lat//Lon are required\n")
            return(null)
        }
        if(HasValue(lr)==0) {
            printf("Lower Right  Lat//Lon are required\n")
            return(null)
        }
        if(HasValue(ll)==0) {
            printf("Using set Upper Left Longitude and Lower Right Latitude bounds for the Lower Left corner\n")
            ll=lr[1]//ul[2]
        }
        if(HasValue(ur)==0) {
            printf("Using set Upper Left Latitude and Lower Right Longitude bounds for the Upper Right corner\n")
            ur=ul[1]//lr[2]
        }

    }    

    if ($ARGC==2) {
        if(type($2)=="STRUCT" || type($2)=="STRING") {

            if(type($2)=="STRING") {
                if(HasValue(v)) printf("Trying to load pds file\n")
                struct=load_pds($2,data=0)
            } else {
                if(HasValue(v)) printf("We have a pds struct\n")
                struct=$2
            }
            
            if(HasValue(get_struct(struct,"IsisCube"))) {
                #we think we have the isis3 case            
                if(HasValue(v)) printf("ISIS 3 case\n")

                if(HasValue(get_struct(struct.IsisCube,"Mapping"))) {
                    minlat=float(struct.IsisCube.Mapping.MinimumLatitude)
                    maxlat=float(struct.IsisCube.Mapping.MaximumLatitude)
                    westlon=float(struct.IsisCube.Mapping.MinimumLongitude)
                    eastlon=float(struct.IsisCube.Mapping.MaximumLongitude)
                }
                #just because we don't quite trust the isis label to always be min/max
                minlon=min(westlon//eastlon)
                maxlon=max(westlon//eastlon)

                #parse it to the jmars way...            
                ul=maxlat//minlon
                ur=maxlat//maxlon
                ll=minlat//minlon
                lr=minlat//maxlon

            } else if(HasValue(get_struct(struct,"qube"))){
                #we think we ahve the isis2 case
                if(HasValue(v)) printf("ISIS 2 case\n")

                if(HasValue(get_struct(struct.qube,"image_map_projection"))) {
                    minlat=float(struct.qube.image_map_projection.minimum_latitude)
                    maxlat=float(struct.qube.image_map_projection.maximum_latitude)
                    westlon=float(struct.qube.image_map_projection.westernmost_longitude)
                    eastlon=float(struct.qube.image_map_projection.easternmost_longitude)
                }

                #just because we don't quite trust the isis label to always be min/max
                minlon=min(westlon//eastlon)
                maxlon=max(westlon//eastlon)

                #parse it to the jmars way...            
                ul=maxlat//minlon
                ur=maxlat//maxlon
                ll=minlat//minlon
                lr=minlat//maxlon

            } else {
                if(HasValue(get_struct(struct,"ul"))) {    
                    ul=struct.ul
                }
                if(HasValue(get_struct(struct,"lr"))) {    
                    lr=struct.lr
                }
                if(HasValue(get_struct(struct,"ll"))) {    
                    ll=struct.ll
                } else {
                    ll=lr[1]//ul[2]
                }
                if(HasValue(get_struct(struct,"ur"))) {    
                    ur=struct.ur
                } else {
                    ur=ul[1]//lr[2]
                }
            } 
        }
    }

    #check for the structure case where we may have .data, .lr, .ul
    if(type($1)=="STRUCT" && $ARGC==1) {
        if(HasValue(v)) printf("User defined struct case\n")

        struct=$1
        if(HasValue(get_struct($1,"data")) || HasValue(get_struct($1,"image"))) {
            if(HasValue(get_struct($1,"ul")) && HasValue(get_struct($1,"lr"))) {
                ul=get_struct($1,"ul")
                lr=get_struct($1,"lr")
                
                if(HasValue(get_struct($1,"ur"))) {
                    ur=get_struct($1,"ur")
                } else {
                    ur=ul[1]//lr[2]
                }
                if(HasValue(get_struct($1,"ll"))) {
                    ll=get_struct($1,"ll")
                } else {
                    ll=lr[1]//ul[2]
                }
            } else {
                printf("Lat//Lon values are required for each corner\n")
                return(null)
            }

            if(HasValue(get_struct($1,"data"))) {
                data=get_struct($1,"data")
            } else if (HasValue(get_struct($1,"image"))) {
                data=get_struct($1,"image")
            }

            if(type(data)=="byte") {
                if(HasValue(v)) printf("Writing out .data from struct to  file\n")
                if(dim(data)[3]!=1 && dim(data)[3]!=3) {
                    printf("Data must have only 1 or 3 bands\n")
                    return(null)
                } else { 
                    write(data,outfile,png,force=1)
                }
            } else {
                printf("Error only byte data may be used with JMARS at this time\n\n")
                return(null)
            }
        }

    #this is a check to just copy the file to the location (if it is on disk somewhere...)
    #this is probably dangerous since it is most likely not a png file
    } else if(type($1)=="STRING") {
        data=read($1)
        if(dim(data)[3]!=1 && dim(data)[3]!=3) {
            printf("Data must have only 1 or 3 bands\n")
            return(null)
        } else { 
            write(data,outfile,png,force=1)
        }
        
        #this is probably dangerous since it is most likely not a png file
        #copy($1,outfile)

    #this is the case where we have data as a pds structure or with user defined points 
    } else if(type($1)=="byte") {    
        if(HasValue(v)) printf("Writting out unstretched data to file\n")
            if(dim($1)[3]!=1 && dim($1)[3]!=3) {
                printf("Data must have only 1 or 3 bands\n")
                return(null)
            } else { 
                write($1,outfile,png,force=1)
            }
    #this is the case where we have unstretched data as a pds structure or with user defined points
    } else if(type($1)=="float" || type($1)=="short" || type($1)=="int" || type($1)=="double") {
        printf("Error only byte data may be used with JMARS at this time\n\n")
        return(null)
    } else {
        printf("No valid data was found\n")
        return(null)
    }
    
    ulstr=sprintf("&ullat=%f&ullon=%f",ul[1],ul[2])
    urstr=sprintf("&urlat=%f&urlon=%f",ur[1],ur[2])
    llstr=sprintf("&lllat=%f&lllon=%f",ll[1],ll[2])
    lrstr=sprintf("&lrlat=%f&lrlon=%f",lr[1],lr[2])

    url=sprintf("%spath=%s%s%s%s%s&ignore=%s&id=%.6d&name=%s&version=%s",urlprefix,outfile,ulstr,urstr,llstr,lrstr,ignore,id,name,ver)

    verbose=0
    if(HasValue(v))    {
        printf("%s\n",url)
        verbose=3
    }

    success=read_lines(url)
    verbose=3

    if(HasValue(success)) {
        success=atoi(success[,1])
        if(success==1) {
            printf("Your data is now available in: \n\tJMARS Session:\t\"%i\"\n\tData ID:\t\"%.6d\"\n\tData Name:\t\"%s\"\n",success,id,name)
        } else {
            printf("ERROR:Your data is NOT available in JMARS\n")
        }
    } else {
        printf("ERROR:Your data is NOT available in JMARS\n")
    }
    return(success)
}